Java certification Quiz
Java Quiz - 2

« 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. What is the ouput?

import java.util.*;
public class Test { 
   static int n=1;
   static String s="2";
   static {
      System.out.print(n + " ");
   }
   public Test(){
      n = 3;
   }
   public void printS() {
       System.out.print(s + " ");
   }
    public static void main(String args[]) {
       System.out.print("0 ");
       Test t = new Test();
       t.printS();
    }
}
a) 0 1 2
b) 3 0 2
c) 0 3 2
d) 1 0 2

Wrong!Try again..
Wrong Again! Try one more chance..
Wrong! The correct answer is d) 1 0 2
static blocks are executed first.
Correct!
static blocks are executed first.

2. Which are correct?

a) int sum(int first, int second) { first + second; }
b) int sum(int first, int second) { return first + second; }
c) int sum(int first, second) { return first + second; }
d) sum(int first, int second) { return first + second; }

Wrong! Try again..
Wrong Again! Try one more chance..
Wrong! The correct answer is b) int sum(int first, int second) { return first + second; }
Correct! int sum(int first, int second) { return first + second; }

3. What is the output?

public class Test extends Thread
   {
     public static void main(String[] args) {
      Thread t = new Thread(new Test());
       t.start();
       t.start();
      System.out.println("main");
    }
    public void run() {
      System.out.println("thread ");
    }
  } 
a) throws IllegalMonitorStateException at runtime
b) throws IllegalThreadStateException at runtime
c) Compile Error
d) output: thread thread main

Wrong! Try again.
Wrong Again! Try one more chance..
Wrong!
The correct answer is b) throws IllegalThreadStateException at runtime. start() can be called only once
Correct!
throws IllegalThreadStateException at runtime. start() can be called only once

4. What is the output?

import java.util.*;
public class Test { 
    public static void main(String args[]) {
       Set<String> s = new HashSet<>();
       System.out.print(s.add("A"));
       System.out.print(s.add("F"));
       System.out.print(s.add("A"));
       System.out.print(s.add("B"));
   }
} 
a) truetruetruetrue
b) truetruefalsefalse
c) truefalsetruefalse
d) truetruefalsetrue
Wrong! Try again.
Wrong Again! Try one more chance..
Wrong! The correct answer is d) truetruefalsetrue
Set does not allow multiples. 'A' cannot be added second time
Correct!
truetruefalsetrue. Set does not allow multiples. 'A' cannot be added second time

5. When using an ArrayList as implementation of List, what happens if
adding an element exceeds the ArrayList's capacity?

a) Throws ArrayIndexOutOfBoundsException
b) Error cannot be handled in code
c) ArrayList expands automatically to fit the addition
d) JVM terminates the application
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) ArrayList expands automatically to fit the addition
Correct!
ArrayList expands automatically to fit the addition

6. Which of the options can be used as return type of method getVal() at XXX

public class Test {
      public static XXX getVal(byte x, double y) {
          return (short) x/y*2;
      }
      public static void main (String [] args) {
         Test t = new Test();
         System.out.println(getVal((byte)120, 6.0));
      }
    } 
A. Double
B. Float
C. Byte
D. Long
E. Number
F. Integer
a) A and C
b) A and B
c) C and F
d) A and E
Wrong! Try again.
Wrong Again! Try one more chance..
Wrong!
The correct answer is d) A and E. Others have loss of precision / type mis-match
Correct! Others have loss of precision / type mis-match

7. Which statement at XXX will give output of 10

import java.io.IOException;
public class Test implements Runnable{
    public int data;
    public void run() {
       try {
          Thread.sleep(2000);
          data = 10;
       } 
       catch(InterruptedException e) { }
    }
    public static void main (String [] args) {
       try {
          Test a = new Test();
          Thread t = new Thread(a);
          t.start();
          XXX
          System.out.println(a.data);
       }
       catch(Exception e) { }
    }}
a) t.wait();
b) t.yield();
c) t.notify();
d) t.join();
Wrong! Try again.
Wrong Again! Try one more chance..
Wrong!
The correct answer is d) t.join(); main thread waits till thread t is dead
Correct! t.join(); main thread waits till thread t is dead