Java certification Quiz
Java Quiz - 3

« 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 code :

public class Test {
  int d;
  public static void main(String[] args) {
    Test t = null;
    // A
    System.out.print("Ok");
  }
}
Which expression at A will throw a NullPointerException ? 
a) if(t==null || t.d>31).
b) if(t==null && t.d>31)
c) if(t!=null && t.d>31)
d) None of the above

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) if(t==null && t.d>31).
In case of short circuit operators && and || if the first expression alone can determine a condition, then the second is not checked. Here, in cases a and c the second expression is not verified
Correct!
In case of short circuit operators && and || if the first expression alone can determine a condition, then the second is not checked. Here, in cases a and c the second expression is not verified

2. What is the output of the following :

public class Test {
   public static void main(String[] args) { 
     System.out.println(Math.max(5.0, 8));
   }
} 
a) 8
b) 8.0
c) error, no method max(double, int) in Math class
d) None of the above

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) 8.0
Correct!
8.0

3. What is the output of the following :

public class Orange extends Fruit {
  public static void main(String[] args) { 
      Orange t = new Orange();
      if(t instanceof Fruit) System.out.println("Fruit");
      if(t instanceof Apple) System.out.println("Apple");
      if(t instanceof Orange) System.out.println("Orange");
  }
}
class Fruit {
}
class Apple extends Fruit {
} 
a) Compile error
b) Orange
c) Fruit, Apple,Orange
d) Fruit, Orange.

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Compile Error
Correct! a) Compile Error

4. The following code :

public class Test {
  public static void main(String[] args) {
    if(5 & 7 > 0 && 5 | 7 < 0)
      System.out.println("true");
  }
}
a) prints output true
b) no output
c) does not compile.
d) Run time exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) does not compile.
< and > have higher precedence to & and | so 5 & 7 and 5 | 7 should be enclosed in parenthesis.
Correct!
< and > have higher precedence to & and | so 5 & 7 and 5 | 7 should be enclosed in parenthesis.

5. What is the output of the following :

import java.io.*;
class Test {
  public static void main(String[] args) throws InterruptedException {
     try {
        Thread.sleep(1000);
        System.out.println("try");
     }
     catch(IOException e) {
        System.out.println("catch");
     }
     finally {
        System.out.println("finally");
     }
  }
}
a) try, finally
b) Compile error.
c) try
d) catch, finally

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Compile error .
IOException is never thrown in try block. It is a checked exception
Correct!
Compile error. IOException is never thrown in try block. It is a checked exception

6. The following code

class Test {
  Test() throws Exception {
  }
  public static void main (String[] args) throws Exception {
    Test t = new Test();
  }
}
class Child extends Test {
}
a) class Child will not compile.
b) both will not compile.
c) compiles and runs fine
d) class Test will not compile.

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) class Child will not compile.
Exception should be declared to be thrown in the default Constructor in inherited class
Correct!
Exception should be declared to be thrown in the default Constructor in inherited class

7. For the following code :

import java.io.IOException;
public class Test implements Runnable{
    private int m, n;
    public synchronized void run() {
      try {
        for(int i =0; i<10; i++) {
          m++; n++;
          Thread.sleep(100);
          System.out.println(m+", "+n);
        }
      } 
      catch(InterruptedException e) {
      }
    }
    public static void main (String [] args) {
        try {
           Test a = new Test();
           new Thread(a).start();
           new Thread(a).start();
         }
         catch(Exception e) {
         }
    }
}
a) The program prints pairs of equal values twice (for example, “1, 1” followed by “1, 1”, followed by “2, 2” and so on)
b) Code will not compile.
c) The program prints pairs of equal values (for example, “1, 1” followed by “2, 2” and so on)
d) The program prints pairs of equal or unequal values (for example, “1, 1” or “2, 1” and so on)

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) The program prints pairs of equal values (for example, “1, 1” followed by “2, 2” and so on)
Correct!
The program prints pairs of equal values (for example, “1, 1” followed by “2, 2” and so on)