Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interview questions and answers

1
Given the code. What is the result? 

class A { 
 public void b() throws Exception { 
  throw new Exception(); 
 } 
} 
public class B extends A { 
 public void b() { 
  System.out.print("OK"); 
 } 
 public static void main(String args[]) {  
  A h = new B(); 
  h.b(); 
 }  
} 
  1. An exception is thrown at runtime
  2. Compilation fails because the overridden method b() does not throw Exception
  3. The code runs and displays OK
  4. Compilation error occurs  while calling h.b()
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
3
What happens on compilation and execution of code? 

public class Test {   
    public static void main(String[] args)     { 
        try {  
            
        }  
        finally { 
          System.out.println( "Finally" );  
        }  
    }  
} 
  1. Finally is printed
  2. Compilation fails because try block must contain some statements 
  3. Compilation fails because try block must have atleast one catch block
  4. code executes without any output
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
6
Predict the output of the following assuming the code is inside main method:

try{
int a[] =new int[6];
for(int i = 0; i<7; i++){
a[i]=(i+1)*10;
} 
}catch(Exception e)
{
out.println("Exception:"+e);
}
  1. Compilation Error
  2. Code will compile successfully and will not display any output
  3. ArrayIndexOutOfBoundsException is thrown and caught
  4. NumberFormatException is thrown and caught
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
8
package com.techpalle.skillgun;

public class Test{ 
 static void m(int i) throws NullPointerException{
  i=17/i;
 }
 public static void main(String s[]) {
  m(0);
 }
}

What happens on compilation and execution of the above code?
  1. code throws ArithmeticException
    
  2. code throws NullPointerException
  3. code does not compile because main does not handle exception
  4. code does not compile because m must throw ArithmeticException instead of NullPointerException
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
12
fun1() calls fun2(), where fun2() raises a checked exception. How does fun2() indicate fun1() that it has to handle that exception?
  1.  throw new Exception()
  2.  throw Exception
  3.  throws Exception 
  4. Don't need to specify anything
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
14
How to define a method that throws FileNotFoundException and returns an int?
  1. int m() throws FileNotFoundException{}
  2. int m() throw FileNotFoundException{}
  3. int m() throw new FileNotFoundException{}
  4. int m() {} throws FileNotFoundException
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :