Java certification Quiz
Java Quiz - 6

« 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 output?

public class Test {
 public static void main(String... args) {
  new Test().doIt();
 }
 public void doIt() {
  int n = 5;
  doIt2();
  System.out.println(n);
 }
 public void doIt2() {
  int i,m=7;
  System.out.print("m");
  for (i = 0; i < 2; i++) {
    System.out.print("["+i+","+m+"]");
  }
 }
}
a) m[0,7][1,7]5
b) m[0,7][1,7][2,6]5
c) m5
d) Compile error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) m[0,7][1,7]5
Correct! m[0,7][1,7]5

2. What is output?

class Test {
 void main() {
  System.out.println("one");
 }
 static public void main(String args) {
  System.out.println("two");
 }
 public static void main(String... args) {
  System.out.println("three");
 }
 void main(Object[] args) {
  System.out.println("four");
 }
}
a) three
b) one two three four
c) three four
d) Compile error

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

3. What is ouput?

class Test {
 int m(double d) {
 System.out.println("one");
 return 0;
}
String m(double d) {
 System.out.println("two");
 return null;
}
double m(double d) {
 System.out.println("three");
 return 0.0;
}
public static void main(String[] args) {
 new Test().m(4.0);
}
}
a) three
b) two
c) one
d) Compile error

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

4. Assume the following code has assertion enabled Run as : java -ea Test

public class Test{ 
   public void methodA(int i) {
         assert i >= 0 : methodB();
         System.out.println(i);
   }
   public void methodB() {
         System.out.println("The value must not be negative");
   } 
   public static void main(String args[]) {
         Test test = new Test();
         test.methodA(-10); 
  }
}
a) Gives output : -10
b) Will not compile.
c) Results in assertion error : The value must not be negative
d) Throws Assertion Error.
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Will not compile : Assertion statement second part has to be an expression. Here methodB() is void.
Correct!
Assertion statement second part has to be an expression. Here methodB() is void.

5. What is the output ?

public class Sup  
{    int x;
    Sup() {  x = 1; }
    Sup(String str) {
        x = 2;
    }
}
public class Test extends Sup   {
    Test() {  x = 3;   }
    Test(String str) {
        x = 4;
    }
    public static void main(String args[]) {
        Sup s = new Test("abc");
        System.out.println(s.x);
   }
}
a) 1
b) 2
c) 3
d) 4
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!The correct answer is d) 4
Only default constructor of super is called
Correct! 4.
Only default constructor of super is called

6. What is output?

import com.abc.B;
public class Test {
    public static void main(String[] args) {
         B b = new B();
         b.print();
    }
}
package com.abc;
public class A {
    protected int id = 5;
}
package com.abc;
public class B {
    public void print() {
        A a = new A();
        System.out.println("Id : "+ a.id);
   }
}
a) Id : 5
b) Compile error. Protected member id in A cannot be accessed by B
c) Will compile, but gives exception when run
d) Compile error. Cannot create object of A in B print() method

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) Id : 5
Correct!
Id : 5

7. What is the output?

 public class Test {
     public static void main (String [] args) {
      StringBuffer a = new StringBuffer ("A");
      StringBuffer b = new StringBuffer ("B");
      change(a,b);
      System.out.println(a+","+b);
     }
    static void change(StringBuffer x, StringBuffer y) {
        y.append(x);
        y = x;
    }
 }
a) Compile error
b) A,B
c) A,A
d) A,BA

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) A,BA
Correct! A,BA