Java Multiple choice Interview Questions

Q1)What is the o/p of following program?

public class Test{
 static {
  i=5;
 }
 static int i;
 
 public static void main(String[] args) {
  System.out.println("i value is "+i);
 }
}

Answers
a) 0
b) 5
c) Compilation error
d) 1

Q2) What is the o/p of following program?

public class Test{
  static int i=5;
  public static void main(String[] args) {
    Test test = null;
    System.out.println("i value is "+test.i);
  }
}


Answers
a) 0
b) 5
c) Compilation error
d) NullPointerException

Q3)What is the o/p of following program?

class Father {
protected Father() {
  System.out.println("Created a Father");
 }
}
public class Child extends Father {
  private Child() {
    System.out.println("Inside child");
  }
  public static void main(String args[]) {
    new Child();
  }
}

Answers
a) Inside Child
b) Created a Father
c) Compilation error
d) Created a Father
   Inside Child

Q4)What is the o/p of following program?

class Father {
 protected Father(String str) {
  System.out.println("Created a Father " +str);
 }
}
public class Child extends Father {
 private Child() {
  System.out.println("Inside child");
 }
 public static void main(String args[]) {
  super("Hi");
  new Child();
 }

a) Inside Child
b) Created a Father
c) Compilation error
d) Created a Father Hi
    Inside Child

Q5)What is a WeakHashMap?

a) A hashtable-based Map implementation with weak keys
b) A list with weak references to objects
c) A hasttable map with duplictated keys
d) A general purpose hashtable-based implementation to better store.

Q6)Which of the following is/are true?

a) if("String  ".trim() == "String") 
b) if("   String   ".trim() == "String") 
c) if("String".trim() == "String") 
d) if("Str ing ".trim() == "String")

Q7) Which of the following lines allow main method to be executed by ClassLoader?


1: public static String main(String args[]); 
2: protected static void main(String args[]); 
3: final public static void main(String args[]); 
4: public static void main(); 
5: private static void main(String args[]); 
6: public void main(String args[]); 

Q8) What is o/p of following program?

public class FinalVar { 
  private int final i; 
  public static void main(String args[]){ 
    System.out.println(new FinalVar().i); 
  } 
} 

a) 1  
b) 0  
c) RunTimeException occurs  
d) Compile time error 'i should be initialized'