Java Programming Interview Questions

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

public class Widening1 {
  public void f1(Object o1) {
    System.out.println("Inside f1 with object as argument");
  }
  public void f1(String s) {
    System.out.println("Inside f1 with String as argument");
  }
  public static void main(String[] args) {
    new Widening1().f1(null);
  }
}

Answers
a) Inside f1 with String as argument
b) Inside f1 with object as argument
c) Compilation error
d) Runtime error

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

public class Widening1 {}
  public void f1(Object o1) {
   System.out.println("Inside f1 with object as argument");
  }
  public void f1(String s) {
    System.out.println("Inside f1 with String as argument");
  }
  public void f1(String s){
    System.out.println("Inside f1 with Integer as argument");
  }
  public static void main(String[] args) {
    new Widening1().f1(null);
  }
}

Answers
a) Inside f1 with String as argument
b) Inside f1 with object as argument
c) Compilation error
d) Inside f1 with object as argument

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

public class MyClass{
  public static void main(String[] args) {
    int[] dest = new int[]{0,1,2,3,4,5};
    System.out.println(dest[0]+ dest[5]+dest[2]);
  }
}

Answers
a) 052
b) Compilation Error
c) 7
d)152

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

public class Overloading {
   public void f1(Integer i){
     System.out.println("inside 1");
   }
   public void f1(int i){
     System.out.println("inside 2");
   }
   public static void main(String args[]){
     new Overloading().f1(8);
   }
}

Answers
a) inside 1
b) inside 2
c) Compilation error
d) Rutime error

Q5)Wil this code compile fine?

public class FinalHashMap {
     1   private static final Map map = new HashMap();
     2   public static void main(String[] args) {
     3   map.put("param1","value1");
     4   map.put("param2","value2");
     5   map=new HashMap();
  }
}

Answers
a) Yes
b)No,Compilation Error at line 3
c)No, Runtime Error
d)No,Compilation Error at line 5

Q6)Which of the o/p following program?

public class FinalHashMap {
     1   private final Map map = new HashMap();
     2   public static void main(String[] args) {
     3   map.put("param1","value1");
     4   map.put("param2","value2");
     5   map=new HashMap();
  }
}

Answers
a) Yes
b) No,Compilation Error at line 3
c) No, Runtime Error
d) No,Compilation Error at line 5

Q7) What is o/p of following program

public class Parent {

 public static void staticMethod(){
   System.out.println("Inside Parent static");
 }
 public static void main(String[] args) {
   Parent p = new Child();
   p.staticMethod();
 }
}
class Child extends Parent {
   public static void staticMethod(){
     System.out.println("Inside Child static");
   }
}

Answers 
 a) Inside Parent static
 b) Inside Child static
 c) Compilte time error.

Q8) Is it compulsory to implement print() method in abstract class?/

public interface Interface1 {
   void print(String str);
}
public abstract class AbstractClass implements Interface1 {}