Java certification Quiz
Java Quiz - 23

« 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 output ?
 public class Test {
     public static void main(String... args) {
        int x =5;
        x *= 3 + 7;
       System.out.println(x);
   }
 }
a) 50
b) 22
c) 10
d) Compile Error
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong!  The correct answer is a) 50. Expression 3+7 is resolved first and then assigned to x after multiplying with original value
Correct! 50. Expression 3+7 is resolved first and then assigned to x after multiplying with original value
2. Which of the following is true?
 A. A class that is abstract may not be instantiated.
 B. The final keyword indicates that the body of a method is to be found elsewhere. 
 C. A static variable indicates there is only one copy of that variable
 D. A method defined as private indicates that it is accessible to all other classes in the same package
a) A & B
b) A & D
c) A & C
d) C & D


Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) A & C
Correct! c) A & C
3. Which of these statements are true?
 A. For each try block there must be at least one catch block defined.
 B. A try block may be followed by any number of finally blocks.
 C. A try block must be followed by at least one finally or catch block.
 D. If both catch and finally blocks are defined, catch block must precede the finally block.
a) C & D
b) A & B
c) B & C
d) A & D

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) C & D
Correct! C & D
4. What is the output? 
public class Test {
   int squares = 21;
   public static void main(String[] args) {
      new Test().go();
   }
   void go() {
     incr(++squares);
     incr(++squares);
    System.out.println(squares);
  }
  void incr(int squares) { squares += 10; }
}
a) 43
b) 42
c) 32
d) 23


Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) 23. incr() method assigns value to local variable
Correct! 23. incr() method assigns value to local variable
5. Will the following code compile?
abstract interface IService {
   String company="ABC Ltd";
   void service();
   int getId();
}
a) YES
b) No. Interface cannot be declared abstract
c) No. service() should be declared public
d) No. company should be declared public, static, final

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) YES
Correct! YES
6. What is the output?
public class TestParent { 
    static {
      System.out.print(" parent-static");
    }
}
public class Test extends TestParent{ 
    static {
      System.out.print(" child-static");
    }
    {
      System.out.print(" child-instance");
    }
    public static void main(String args[]) { 
      System.out.print(" main");
      Test t = new Test();
    }
}
a) parent-static child-static main child-instance
b) parent-static child-static child-instance main
c) child-static main child-instance
d) child-static parent-static main child-instance

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) parent-static child-static main child-instance
Correct! parent-static child-static main child-instance
7.  What is the output?
public static void main(String[] args) {
    Set set = new TreeSet();
    set.add("7");
    set.add("9");
    set.add(9);
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      System.out.print(iter.next() + " ");      
    }
  }
a) 7 9
b) Throws ClassCastException
c) 7 9 9
d) Compile Error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is b) Throws ClassCastException
TreeSet, being sorted, tries to compare previous String with Integer newly added.
Correct!   Throws ClassCastException
TreeSet, being sorted, tries to compare previous String with Integer newly added.