Java Programming Interview Questions

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

public class Question05 {
  public static void main(String[] args) {
    Question05Sub myref = new Question05Sub();
    try{
      myref.test();
    }catch(IOException ioe){}
  }
  void test() throws IOException{
    System.out.println("In Question05");
    throw new IOException();
  }
}

class Question05Sub extends Question05 {
  void test() {
    System.out.println("In Question05Sub");
  }
}

Answers
a) In Question05
b) In Question05Sub
c) Compilation error
d) In Question05
   In Question05Sub

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

public class ThreadClass {
  public static void main(String[] args) {
    new ThreadClass().doSomething();
  }

  public void doSomething(){
    int i=5;
    Thread t = new Thread(new Runnable(){
      public void run(){
        for(int j=0;j<=i;j++){
          System.out.print(" "+j);
        }
      }
    });
    t.start();
  }
}


Answers
a) Print 0,1,2,3,4
b) Print 1,2,3,4
c) Compilation error
d) throws RunTimeException

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

public class Parent {
  static {
    System.out.println("Inside Parent static");
  }
  {
    System.out.println("Inside Parent init");
  }
  public Parent(){
    System.out.println("Parent Const");
  }
  public static void main(String args[]){
    new MyChild();
  }
}

class MyChild extends Parent{
  
  static {
    System.out.println("Inside Child static");
  }
  {
    System.out.println("Inside Child init");
  }
  public MyChild(){
    System.out.println("Child Const");
  } 
}

Answers
a) 
Inside Parent static
Inside Child static
Inside Parent init
Parent Const
Inside Child init
Child Const

b) 
Inside Parent static
Inside Child static
Inside Parent init
Inside Child init
Parent Const
Child Const

c) 
Inside Parent static
Inside Child static
Inside Parent init
Inside Child init
Child Const
Parent Const

d)
Inside Parent init
Inside Child init
Inside Parent static
Inside Child static
Child Const
Parent Const

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

public class Test {
  public static void main(String args[]){
      int i = 132;
    short s = 15;
    byte b = (byte)i;
    int x = b + s;
    System.out.println(x);
  }
}

Answers
a) 147
b) -109
c) Compilation error
d) Rutime error

Q5)What is o/p of following program?

public class Test {
  public static void main(String args[]){
    int i = 132;
    List list = new ArrayList<String>();
    list.add(new Object());
    list.add("Hi");
    list.add(i);
    System.out.println(list.get(1));
  }
}

Answers
a) Hi
b)Compilation Error
c) Runtime Error
d) 132

Q6)Which of the o/p following program?

public class StringTest {
  public static void main(String[] args) {
    String String = "String"; //line 1
    int temp = 2;     //line 2
    Object:for(int main=0;main<(temp*String.length());main+=temp){ //line 3
      System.out.print(String.charAt(main)+" "); //line 4
      if(main>temp)   //line 5
        break Object;   //line 6
    }
  }
}

Answers
a) Prints S r n
b) Prints t i g
c) Compilation Error
d) Prints t g

Q7) Select methods that correctly overload the following method

byte bMethod(short i) throws Exception {...}

Answers
a) int bMethod(int i) throws IOException{...} 
b) protected int bMethod(short s) throws FileNotFoundException{...} 
c) private String aMethod(byte b,short s) throws Exception{...} 
d) char bMethod(String s) throws RuntimeException{...} 
e) int bMethod(short sh){...}

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); 
   } 
} 

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