Java certification Quiz
Java Quiz - 18

« 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 »

1. For the following:
  1.  public class Test {
  2.    public static void main(String args[]) {
  3.        Test t = new Test();
  4.        System.out.println(t.sum ( new Integer[] {1,2,3,4,5,6,7,8,9} ));
  5.        System.out.println(t.sum ( 1,1,1,1,1));
  6.    }  
  7.    public int sum(Integer... arr) {
  8.       int mysum = 0;
  9.       for ( int i : arr )
 10.          mysum += i;
 11.       return mysum;
 12.    }
 13. } 
a) Compile error at line 4.
b) Compile error at line 5.
c) Gives output: 45 and 5
d) Compile error at line 7.
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!  The correct answer is c) Gives output: 45 and 5
sum (Integer .. arr) is a generic method
Correct! Gives output: 45 and 5
sum (Integer .. arr) is a generic method
2. What is the output?
public class Test implements Runnable  { 
    int x, y; 
    public void run() { 
        for(int i = 0; i < 100; i++) 
            synchronized(this) { 
               x = 12; 
               y = 12; 
            } 
        System.out.print(x + " " + y + " "); 
    } 
    public static void main(String args[]) { 
           Test run = new Test(); 
           Thread t1 = new Thread(run); 
           Thread t2 = new Thread(run); 
           t1.start(); 
           t2.start(); 
   }
}
a) Compile error
b) Prints 200 times 12
c) Gives output: 12 12 12 12
d) Dead Lock

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: 12 12 12 12.
x and y are printed outside for loop
Correct! c) Gives output: 12 12 12 12
x and y are printed outside for loop

3. Which of these are NOT TRUE?
A. A singleton cannot be cloned
B. Dependant objects in a class are cloned in deep cloning
C. An object can be cloned only if it implements Cloneable interface
D. An object can be cloned only if it overrides clone() method
a) A, D
b) B, D
c) A
b) None of them

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) A
Singleton can be cloned
Correct! A
Singleton can be cloned

4. What is the output?

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class Test {
 public static void main(String[] args) throws Exception {
  Method meth = Test.class.getMethod("main", String[].class);
  for (Parameter p : meth.getParameters()) {
   System.out.println(p);
  }
 }
}
a) Compile error
b) Exception thrown
c) args
d) java.lang.String[] arg0

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) java.lang.String[] arg0
Correct! java.lang.String[] arg0
5. For the following :
public interface TestInterface { 
  public static final String name="Bangalore";
}
public class Test implements TestInterface { 
  public static final String name="London";
  public static void main(String args[]) { 
       System.out.println(name);
  }
}

a) class Test will not compile
b) interface TestInterface will not compile
c) Gives output : Bangalore
d) Gives output : London

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Gives output : London
Correct!
Gives output : London
6.  For the following :
public class Test extends TestParent { 
    public static String getName() {
              return "London";
    }
    public static void main(String args[]) { 
             System.out.println(getName());
    }
}
public class TestParent { 
     public static String getName() {
              return "Bangalore";
     } 
}
a) Compile error. Cannot override static method
b) Compile error. Cannot call getName() from main method
c) London
d) Bangalore

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) London
Correct! London
7. public class NumberObjectDemo {
      public static void main(String[] args) {
        try {
            NumberObject number1 = new NumberObject();
        } 
        catch (Throwable t) {
            t.printStackTrace();
        }
        NumberObject number2 = new NumberObject();
     }
  }
  public class NumberObject {
    static int number = 1 / 0;
    public NumberObject () {
         System.out.println("number undefined");
    }
  }
a) Throws ExceptionInInitializerError and NoClassDefFoundError
b) Compile Error
c) Throws ExceptionInInitializerError and ClassNotFoundException
d) Gives Output : number undefined
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Throws ExceptionInInitializerError and NoClassDefFoundError
ExceptionInInitializerError : indicates a failure in the static initialization block, as 1/0 is mentioned in static block.
NoClassDefFoundError : indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it as it has failed to load
Correct!   Throws ExceptionInInitializerError and NoClassDefFoundError
ExceptionInInitializerError : indicates a failure in the static initialization block, as 1/0 is mentioned in static block.
NoClassDefFoundError : indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it as it has failed to load