0

programming languages Online Quiz - 107

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

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

Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add(” 1”); list.add(”2”); list.add(”3”); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + “,”); 20. } ‘What is the result?

  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

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

AI Explanation

To determine the result of the given code, let's go through each line and understand what is happening:

Line 11: The get() method is declared with a return type of Collection. Line 12: A new LinkedList object called sorted is created. Line 13: The elements "B", "C", and "A" are added to the sorted collection. Line 14: The sorted collection is returned. Line 16: The main method is declared. Line 17: A for-each loop is used to iterate over the elements of the collection returned by the get() method. Line 18: The current element is printed. Line 19: A comma and a space are printed after each element.

Based on the code, the elements in the sorted collection are added in the order "B", "C", and "A".

Therefore, when the for-each loop in line 17 iterates over the collection, the elements will be printed in the order they were added: "B", "C", and "A".

So, the result of the code will be:

B, C, A,

Therefore, the correct answer is B) B, C, A.

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
  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
Explanation:

To understand what will happen when you attempt to compile and run the given code, the user needs to know the concept of multithreading in Java.

The code defines a class Bground that extends the Thread class and overrides its start() method. In the main method, an instance of Bground is created and its run() method is called.

Now, let's go through each option and explain why it is right or wrong:

A. A compile time error indicating that no run method is defined for the Thread class. This option is incorrect because the Thread class already has a run() method defined, and the Bground class inherits this method from Thread. Therefore, there will be no compile time error.

B. A run time error indicating that no run method is defined for the Thread class. This option is incorrect because the Bground class has a run() method defined that overrides the run() method of the Thread class. Therefore, there will be no run time error.

C. Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect because the run() method of the Bground class is not called with the start() method. Instead, the run() method is called directly in the main method. Therefore, only the start() method of the Bground class is executed, which does not print any output.

D. Clean compile but no output at runtime. This option is correct. The code will compile without any errors and there will be no output at runtime because only the start() method of the Bground class is executed, which does not print any output.

Therefore, the correct answer is: 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
  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
Explanation:

To answer this question, the user needs to understand the basic functionality of the Set collection in Java.

Option A is incorrect. When attempting to add an element to a Set that already exists in the Set, an exception is not thrown. Instead, the Set simply ignores the duplicate element and does not add it to the collection.

Option B is correct. The add method of a Set returns a boolean value of false if the element being added already exists in the Set. This is because Sets do not allow duplicate elements.

Option C is incorrect. Sets are designed to contain only unique elements. If an element already exists in the Set, it will not be added again. Therefore, a Set cannot contain elements that return duplicate values from a call to the equals method.

Option D is incorrect. Duplicate values will not cause an error at compile time. Instead, the Set will simply ignore the duplicate value when the code is executed.

Therefore, the correct answer is:

The Answer is: B. The add method returns false if you attempt to add an element with a duplicate value.

A monitor called nik has 3 threads, having same priority, in its waiting pool; How can we notify one of the threads, named 'thread1' to move from Waiting state to Ready State?

  1. Execute notify(thread1); from within synchronized code of mon.

  2. Execute mon.notify(thread1); from synchronized code of any object.

  3. Execute thread1.notify(); from synchronized code of any object.

  4. Execute thread1.notify(); from any code(synchronized or not) of any object.

  5. You cannot specify which thread will get notified.


Correct Option: E

Which of the below is not an implicit object of JSP ?

  1. request

  2. session

  3. context

  4. page


Correct Option: C

What are OOPs concepts ?

  1. Inheritance

  2. Exception

  3. deadlock

  4. Thread

  5. Networking


Correct Option: A

Which of the below is not an implicit object of JSP ?

  1. request

  2. session

  3. context

  4. page


Correct Option: C

Which of the below is not an implicit object of JSP ?

  1. request

  2. session

  3. context

  4. page


Correct Option: C

What are OOPs concepts ?

  1. Inheritance

  2. Exception

  3. deadlock

  4. Thread

  5. Networking


Correct Option: A

GenericServlet is a __________

  1. Interface

  2. Class

  3. Abstract Class

  4. Filter

  5. None


Correct Option: C
- Hide questions