Interview Questions

Java Keywords
Key Concepts
OOPS in java
Java Collections#1
Java Collections #2
Exceptions #1
Exceptions #2
Threads#1
Threads#2
InnerClass
Serialization
Immutable Class
Cloning in Java
Garbage Collection
JSP #1
JSP #2
Programming Q#1
Programming Q#2
Programming Q#3


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

a) In Question05
b) In Question05Sub
c) Compilation error
d) In Question05
  In Question05Sub
Answer
Answer is c. If the parent method throws a Checked Exception then the child class overriding it can throw same exception or unchecked exception or any subclass of the Exception thrown (in this case subclass of IOException)
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();
   }
 }

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

Answer
Answer is c, the local variable i cannot be accessed directly inside inner class. To make it accessible it should be declared as final.
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");
   }

 }

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

Answer
Answer is a.
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);
  }
 }

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

Answer
Answer is b.
Q5)What is o/p of following program?

 public class Test {

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

a) Hi
b)Compilation Error
c) Runtime Error
d) 132
Answer
Answer is a.
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       System.out.print(String.charAt(main)+" "); //line 4
      if(main>temp) //line 5
       break Object; //line 6
      }
    }
 }


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

Answer
Answer is a.
Q7) Select methods that correctly overload the following method
byte bMethod(short i) throws Exception {...}

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){...}

Answer
Answer is a,c,d.
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'
Answer
Answer is d.final variable need to be declared always even the default value should be mentioned explicitly.