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
What are the class naming conventions in Java?
  1. 1.Class name should start start with a  capital letter.
    2.A class name should not consist of a keyword.
    3.A class name must be unique.
    4.A class name should not contain any spaces.
  2. 1. Class name should start start with a  small letter.
    2. A class name should not consist of a keyword.
    3. A class name must be unique.
    4. A class name should not contain any spaces.
  3. 1.Class name should start start with a  small letter.
    2.A class name should not consist of a keyword.
    3.A class name must be unique.
    4.A class name can contain any spaces.
  4. 1. Class name should start start with a  capital letter.
    2. A class name should not consist of a keyword.
    3. A class name must be unique.
    4. A class name can  contain any spaces.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
3
What is static class?
  1. You cannot declare a top-level class as a static class. Java will throw a compilation error. Only inner classes can be declared as static. This is also called as static nested class. 
  2. A static nested class is just like a top-level class. Just for packaging purpose we can keep all similar static classes in an outer class.
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
5
How to swap two strings in Java?
  1. public class SwapString
    {
    public static void main(String[] args)
    {
    String s1 = "abc";
    String s2 = "xyz";
    String temp = s1;
    s1 = s2;
    s2 = temp;
    System.out.println("After swap :"+s1+" "+s2);
    }
    }
  2. public class SwapString
    {
    public static void main(String[] args)
    {
    String s1 = "abc";
    String s2 = "xyz";
    String temp = s1.concat(s2);
    s1 = temp.substring(s1.length());
    s2 = temp.substring(0,s1.length());
    System.out.println("After swap :"+s1+" "+s2);
    }
    }
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
6
How to swap two integers in Java?
  1. public class SwapInt
    {
    public static void main(String[] args)
    {
    int i = 10;
    int j = 20;
    int temp = i;
    i = j;
    j = temp;
    System.out.println("After swap :"+i+" "+j);
    }
    }
  2. public class SwapInt
    {
    public static void main(String[] args)
    {
             int i = 10;
             int j = 20;
             i^=j;
             j^=i;
             i^=j;
             System.out.println("After swap :"+i+" "+j);
    }
    }
  3. public class SwapInt
    {
    public static void main(String[] args)
    {
             int i = 10;
             int j = 20;
             i = i+j;
             j = i-j;
             i = i-j;
             System.out.println("After swap :"+i+" "+j);
    }
    }
  4. all are correct.
Show Answer
Share Facebook gmail Twitter
7
How to call a method in Java?
  1. class CallMethod
    {
    public void method1()
    {
    System.out.println("method1");
    method2(); //calling method2()
    }
    public void method2()
    {
    System.out.println("method2");
    }
    }
  2. class CallMethod
    {
    public void method1()
    {
    System.out.println("method1");
    this.method2(); //calling method2()
    }
    public void method2()
    {
    System.out.println("method2");
    }
    }
  3. both are correct.
  4. none
Show Answer
Share Facebook gmail Twitter
8
How to call a method from other class?
  1. public class MainClass
     {
        public static void main(String[ ] args)
       {
         display(); // calling another class     
                              display method.
       }
    }
    class OtherClass
    {
    
    void display()
    {
    System.out.println("display method called");
    }
    
  2. public class MainClass
     {
        public static void main(String[ ] args)
       {
         OtherClass.display(); // calling another class     
                              display method.
       }
    }
    class OtherClass
    {
    
    void display()
    {
    System.out.println("display method called");
    }
    
  3. public class MainClass
     {
        public static void main(String[ ] args)
       {
         OtherClass o =new OtherClass();
         o.display; // calling another class     
                              display method.
       }
    }
    class OtherClass
    {
    
    void display()
    {
    System.out.println("display method called");
    }
    
  4. public class MainClass
     {
        public static void main(String[ ] args)
       {
         OtherClass o =new OtherClass();
         o.display(); // calling another class     
                              display method.
       }
    }
    class OtherClass
    {
    
    void display()
    {
    System.out.println("display method called");
    }
    
Show Answer
Share Facebook gmail Twitter
9
public static void test(int x) 
{ 
    int odd = 1; 
    if(odd)
    {
        System.out.println("odd"); 
    } 
    else 
    {
        System.out.println("even"); 
    } 
}

Assuming that the main() method is defined in the same class as test() and it calls test(), which statement is/are true?
  1. The test() method fails to compile
  2. The test() method prints "odd"
  3. The test() method prints "even"
  4. "odd" will be printed for odd values of x, and "even" for even values of x
Show Answer
Share Facebook gmail Twitter
10
What will happen on compilation and execution of the following code ?

public class Test {  
 public static void main(String[] args){ 
  final int x=10;  
  int y=20;  
  switch (y){  
  default:  
  case x+10:  
  case x-1:  
  case x+1:  
  }  
}
  1. Code does not compile because of inappropriate use of case statement
  2. Code does not compile because of an attempt to change the constant case value.
  3. Code does not compile because default should be placed only at the end of all case statements.
  4. Code compiles and executes cleanly.
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
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 :