Java certification Quiz
Java Quiz - 8

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

interface Benefits {
    void calculateSalary();
}
public class Employee {
  public Benefits getBenefitsInstance() {
         return (
              new Benefits() {
                  public void calculateSalary() {
                       printSalary(20000);
                  }
              }
        );
 }
 private void printSalary(int sal) {
       System.out.println("Salary : "+sal);
 }
 public static void main(String[] args) {
       Employee emp = new Employee();
       Benefits b = emp.getBenefitsInstance();
       b.calculateSalary();
 }
}
a) Compile error
b) Salary : 20000
c) Runtime exception
d) No output
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is b) Salary : 20000. Inner class can access members of outer class
Correct!
Inner class can access members of outer class
2. What is the output?
interface Benefits {
    void calculateSalary();
}
public class Employee {
   public Benefits getBenefitsInstance(String name) {
           return (
                 new Benefits() {
                     public void calculateSalary() {
                         name = "Mr. "+name;
                         System.out.print(name);
                         printSalary(20000);
                     }
                 }
           );
   }
    private void printSalary(int sal) {
           System.out.println(" salary:"+sal);
    }
    public static void main(String[] args) {
           Employee emp = new Employee();
           Benefits b = emp.getBenefitsInstance("John");
           b.calculateSalary();
    }
}
a) salary:20000
b) Mr. John salary:20000
c) Compile Error
d) Runtime Exception

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Compile Error. Local variable 'name' is accessed within inner class is effectively final and cannot be modified
Correct!
Local variable 'name' is accessed within inner class is effectively final and cannot be modified

3. The following code: 
public class Test implements A, B {
    public static void main(String args[]) {
        A a = new Test();
        a.m1();
        B b = (Test)a;
        b.m1();
    }  
   public void m1() {
        System.out.print("Test ");
   }
}
interface A {
   void m1();
}
interface B {
   void m1();
}
a) Runtime Error
b) Compile Error
c) Gives output: Test Test
d) None of above

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) Gives output: Test Test
Correct!
Gives output: Test Test
4. What is the output of following bitwise operations?
 int m = +12;
 m<<2; 
 int n = -12;
 n>>2;
a) 10 and -10
b) 24 and -10
c) 48 and -3
d) 20 and -6

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is c) 48 and -3. The binary value 001100 when left shifted 2 times, becomes 110000 which is 48
Correct!
48 and -3. The binary value 001100 when left shifted 2 times, becomes 110000 which is 48
5. For the code below :
class StringCheck {
   static String str = null;
   public static void main(String[] args) {
	String s;
	if(str=="abc")
	s = str+10;
	System.out.println(str);
	System.out.print(s);
   }
}
a) null and null are printed as output..
b) runtime error.
c) Compile error, cannot access str from main.
d) Compile error, s not initialised.

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is d) Compile error, s not initialised. . Local variables have to be initialised
Correct!
Compile error, s not initialised. Local variables have to be initialised

6. For the following program:  
public class Test {
    private void display(int count) {
        switch(count) {
           case 1:
               System.out.print("1 ");
           case 2:
               System.out.print("2 ");
           case 3:
               System.out.print("3 ");
           default:
                System.out.println(count);
        }
   }
   public static void main(String[] args) {
        Test t = new Test();
        t.display(2);
   }
}
a) 2 3 2
b) 2
c) Compile error
d) 2 2

Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!
The correct answer is a) 2 3 2. As break is not used, 3 and default are also printed
Correct!
As break is not used, 3 and default are also printed
7. For the following
Integer in = new Integer (42);
Long lg = new Long (42);
Double db = new Double (42.0);
Which of the following give output true?
A. in ==42
B. in == db
C. db.equals(in)
D. in.equals(db)
E. in.equals(42)
a) D and E
b) A and B
c) C and D
d) A and E

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