Java certification Quiz
Java Quiz - 4

« 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 age=25;
  Test() {
  }
  public static void main(String[] args) {
     System.out.println("Age = "+ ++age );
  }
}
a) Gives output : Age = 26
b) Will not compile.
c) Gives output : Age = 25
d) None of above
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Will not compile.
Cannot access instance variable age from static method main.
Correct!
Cannot access instance variable age from static method main.

2. For the following code :

public class Test { 
  static public void main(String args[]) {
    int x = 10, y;
    if(x  <  10)   y = 1;
    if(x >= 10)   y = 2;
   System.out.println("y is " + (y--));
  }
}
a) Will not compile
b) Gives output : y is 1
c) Gives output : y is 2
d) Gives output : y is 0

Wrong! Try again.
Wrong Again! Try one more chance.

Wrong!
The correct answer is a) Will not compile : local variable y has to be initialised before using it.
Correct!
Will not compile : local variable y has to be initialised before using it.

3. Which two are valid constructors for Thread?

 1. Thread(Runnable r, String name) 
 2. Thread() 
 3. Thread(int priority) 
 4. Thread(Runnable r, ThreadGroup g) 
 5. Thread(Runnable r, int priority)
a) 3 and 5
b) 2 and 3
c) 2 and 4
d) 1 and 2

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) 1 and 2 are valid constructors.
Correct!
1 and 2 are valid constructors

4. The following code :

  public class Test implements IntA{ 
     public void print(){
          System.out.println("Print done");
     }
     public static void main(String args[]) {
         IntA tst = new Test();
         tst.print();
     }
  }
  public interface IntA {
     void print();
  }
a) print() in Test cannot be public.
b) Gives output : Print done.
c) Cannot access print() in static method.
d) IntA cannot refer to object of Test.

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Gives output : Print done.
Correct! Gives output : Print done.

5. For following:

public class Test extends A{ 
  void n() throws Exception {
  }
  public static void main(String args[]) {
     Test tst = new Test();
     System.out.println("Error thrown");
  }
}
public class A {
   void n() throws Exception {
       throw new NullPointerException("error message");
   } 
}
a) Will not compile.
b) Gives output : error message
c) Gives output : Error thrown
d) None of above

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Gives output : Error thrown .
Correct!
Gives output : Error thrown.

6. What is output?

int b = 3;
if ( !(b > 3)) {
    System.out.print("square");
}{
    System.out.print("circle");
}
System.out.println("...");
a) Compile error
b) square...
c) circle...
d) squarecircle...

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) squarecircle...There is no else statement
Correct!
squarecircle... There is no else statement

7. Which of following are true of a default constructor?

 A. The default constructor initializes method variables.
 B. The compiler always creates a default 
constructor for every class.  C. The default constructor may invoke the parameterized
constructor of the superclass.  D. The default constructor initializes the instance
variables declared in the class.  E. When a class has only constructors with parameters,
the compiler does not create a default constructor.
a) D and E
b) B and D
c) B and E
d) C and D

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) D and E
Correct! D and E