0

programming languages Online Quiz - 109

Description: programming languages Online Quiz - 109
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

Which interface does TreeMap class implement ?

  1. Set

  2. SortedSet

  3. List

  4. Tree

  5. SortedMap


Correct Option: E

AI Explanation

To answer this question, we need to understand the TreeMap class in Java.

The TreeMap class in Java implements the SortedMap interface.

The SortedMap interface is a sub-interface of the Map interface and extends it. It represents a map that is sorted according to the natural ordering of its keys or a custom Comparator.

Therefore, the correct answer is E) SortedMap.

Which statement is true for the following code? public class Rpcraven { public static void main(String argv[]){ Pmcraven pm1 = new Pmcraven("one"); pm1.run(); Pmcraven pm2 = new Pmcraven("two"); pm2.run(); } } class Pmcraven extends Thread { private String sTname=""; Pmcraven(String s) { sTname = s; } public void run(){ for(int i =0; i < 2 ; i++){ try { sleep(1000); }catch(InterruptedException e){ } yield(); System.out.println(sTname); } } }

  1. Compile time error, class Rpcraven does not import java.lang.Thread

  2. Output of One One Two Two

  3. Output of One Two One Two

  4. Output of One Two Two One


Correct Option: B

What will happen when you attempt to compile and run the following code? public class Bground extends Thread{ public static void main(String argv[]){ Bground b = new Bground(); b.run(); } public void start(){ for (int i = 0; i <10; i++){ System.out.println("Value of i = " + i); } } }

  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.


Correct Option: D

What will be the output of the following program? package scjp.threadTest; public class Ques01 { public static void main(String[] args) { HelloThread hello = new HelloThread(); hello.start(); } } class MyThread implements Runnable{ @Override public void run() { System.out.println("My thread"); } } class HelloThread extends Thread{ public void start(){ System.out.println("Hello thread"); } public void run(){ Thread thread = new Thread(new MyThread()); thread.start(); } }

  1. The program will output 'Hello Thread' and 'My Thread'.

  2. The program will output 'Hello Thread'.

  3. The program will output 'My Thread' and 'Hello Thread'.

  4. The order of the outputs is not will vary upon execution.


Correct Option: B

What will be output for the following program? public class BoxingTest { public static void main(String[] args) { byte b = 10; method(b); } static void method(int i){ System.out.println("Primitive Type call"); } static void method(Integer i){ System.out.println("Wrapper Type Call"); } }

  1. Wrapper Type Call

  2. Primitive Type Call

  3. Compiler Error

  4. Compiles fine, throws runtime exception


Correct Option: B

How does the set collection deal with duplicate elements?

  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.


Correct Option: B

What will be the outcome of line no. 9 in the following program ? 1. public class SearchArray { 2. public static void main(String[] args) { 3. String[] strArr = {"one","two","three","four","five","six","seven"}; 4. boolean[] boolArr = {true,false,false,true,false}; 5. int[] intArr = {22,11,66,99}; 6. Arrays.sort(intArr); 7. for(int n=0; n

  1. -3

  2. 3

  3. -4

  4. 4


Correct Option: C
  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.


Correct Option: C
Explanation:

The correct answer is C. Compilation fails.

The code will fail to compile at line 18 because the for loop cannot iterate over an iterator. The reverse() method returns an iterator, but the for loop expects an object.

To fix the code, you can either change the for loop to a while loop, or you can return a list from the reverse() method.

public static List reverse(List list) {
  Collections.reverse(list);
  return list;
}

With this change, the code will compile and run successfully, and the output will be 3, 2, 1.

Here is an explanation of each option:

  • Option A: This option is incorrect because the for loop cannot iterate over an iterator. The reverse() method returns an iterator, but the for loop expects an object.
  • Option B: This option is incorrect because the code will not run. The for loop will fail to compile at line 18.
  • Option C: This option is correct because the code will fail to compile at line 18.
  • Option D: This option is incorrect because the code will not run. The for loop will fail to compile at line 18.
  • Option E: This option is incorrect because no exception will be thrown at runtime. The code will simply fail to compile.

Given: 11. public static Collection get() { 12. Collection sorted = new LinkedList(); 13. sorted.add(’B”); sorted.add(”C”); sorted.add(”A”); 14. return sorted; 15. } 16. public static void main(String[] args) { 17. for (Object obj: get()) { 18. System.out.print(obj + “, “); 19. } 20. } What is the result?

  1. A, B, C,

  2. B, C, A,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.


Correct Option: B
Explanation:

To solve this question, the user must understand the syntax and behavior of the given Java code. In this code, a method named get() is defined that returns a Collection of type LinkedList containing three strings: "B", "C", and "A". The main method then iterates through each element in the Collection returned by get() and prints its value followed by a comma and a space.

Going through each option:

A. A, B, C,: This option is incorrect because the order of the strings in the Collection returned by get() is "B", "C", "A". Therefore, the output of the program will be "B, C, A,".

B. B, C, A,: This option is correct. As explained above, the Collection returned by get() contains the strings "B", "C", and "A", in that order. The main method iterates through each element in the Collection and prints its value, resulting in the output "B, C, A,".

C. Compilation fails.: This option is incorrect because there are no syntax errors or other issues in the code that would cause it to fail to compile.

D. The code runs with no output.: This option is incorrect because the main method prints output to the console, as described above.

E. An exception is thrown at runtime.: This option is incorrect because there are no errors or exceptional conditions in the code that would cause it to throw an exception.

Therefore, the correct answer is:

The Answer is: B. B, C, A,

Is this code legal ? class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} class A{ void thrower() throws ExceptionA{ throw new ExceptionA(); } } public class B extends A{ void thrower() throws ExceptionB{ throw new ExceptionB(); } }

  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2


Correct Option: A

What will be output for the following program? public class BoxingTest { public static void main(String[] args) { byte b = 10; method(b); } static void method(int i){ System.out.println("Primitive Type call"); } static void method(Integer i){ System.out.println("Wrapper Type Call"); } }

  1. Wrapper Type Call

  2. Primitive Type Call

  3. Compiler Error

  4. Compiles fine, throws runtime exception


Correct Option: B

How does the set collection deal with duplicate elements?

  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.


Correct Option: B

What will be the outcome of line no. 9 in the following program ? 1. public class SearchArray { 2. public static void main(String[] args) { 3. String[] strArr = {"one","two","three","four","five","six","seven"}; 4. boolean[] boolArr = {true,false,false,true,false}; 5. int[] intArr = {22,11,66,99}; 6. Arrays.sort(intArr); 7. for(int n=0; n

  1. -3

  2. 3

  3. -4

  4. 4


Correct Option: C

What will happen when you attempt to compile and run the following code? public class Bground extends Thread{ public static void main(String argv[]){ Bground b = new Bground(); b.run(); } public void start(){ for (int i = 0; i <10; i++){ System.out.println("Value of i = " + i); } } }

  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.


Correct Option: D

AI Explanation

To answer this question, let's go through each option:

Option A) A compile time error indicating that no run method is defined for the Thread class. This option is incorrect. The code defines a run method in the Bground class, which extends the Thread class. Therefore, there is a run method defined for the Thread class.

Option B) A run time error indicating that no run method is defined for the Thread class. This option is also incorrect. As mentioned earlier, the code defines a run method in the Bground class, which extends the Thread class. Therefore, there is a run method defined for the Thread class.

Option C) Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect. Although the code defines a start method in the Bground class, it doesn't actually start a new thread by calling the start method. Instead, it directly calls the run method on the Bground object. This means that the code will execute the for loop and print the values 0 to 9, but it will do so in the main thread, not in a separate thread.

Option D) Clean compile but no output at runtime. This option is correct. The code will compile without any errors, but it will not produce any output at runtime. This is because the start method, which would have started a new thread and executed the code in the run method concurrently, is not called. Instead, the code directly calls the run method, which executes the for loop in the main thread. Since the code doesn't use the start method, there is no separate thread to execute the for loop and print the values.

Therefore, the correct answer is option D) Clean compile but no output at runtime.

What will be the output of the following program? package scjp.threadTest; public class Ques01 { public static void main(String[] args) { HelloThread hello = new HelloThread(); hello.start(); } } class MyThread implements Runnable{ @Override public void run() { System.out.println("My thread"); } } class HelloThread extends Thread{ public void start(){ System.out.println("Hello thread"); } public void run(){ Thread thread = new Thread(new MyThread()); thread.start(); } }

  1. The program will output 'Hello Thread' and 'My Thread'.

  2. The program will output 'Hello Thread'.

  3. The program will output 'My Thread' and 'Hello Thread'.

  4. The order of the outputs is not will vary upon execution.


Correct Option: B

Program output : package com.collection.examples; import java.util.HashSet; public class HashSetExample { public static void main(String[] args) { HashSet s = new HashSet();//1 for(short i = 0; i<100;i++){ //2 s.add(i); //3 s.remove(i-1); //4 } System.out.println(s.size()); //5 } }

  1. NumberFormat Exception

  2. 100

  3. Compilation fails at line 4

  4. 99


Correct Option: B

Which of the following options is true for the following program: try { if (choice) { while (true) } else { system .exit(1): } }finally { codetocleanup(); }

  1. Runtime Exception

  2. finally block will be executed

  3. finally block will not be executed

  4. Compilation error in else block


Correct Option: C

Is this code legal ? class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} public class Test{ void thrower() throws ExceptionB{ throw new ExceptionB(); } public static void main(String[] args){ Test t = new Test(); try{t.thrower();} catch(ExceptionA e) {} catch(ExceptionB e) {} } }

  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2


Correct Option: B

Is this code legal ? class ExceptionA extends Exception {} class ExceptionB extends ExceptionA {} class A{ void thrower() throws ExceptionA{ throw new ExceptionA(); } } public class B extends A{ void thrower() throws ExceptionB{ throw new ExceptionB(); } }

  1. yes

  2. no

  3. DO NOT SELECT 1

  4. DO NOT SELECT 2


Correct Option: A
- Hide questions