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
Which one is faster, static method or instance method?
  1. static methods are faster than instance methods, because they don't use virtual pointer concept internally.
  2. instance methods are faster than static methods, as they don't use virtual pointer concept internally.
  3. both instance methods and static methods are equal in terms of speed and efficiency. It is all your requirement which one to use.
  4. all are true
Show Answer
Share Facebook gmail Twitter
2
How many public classes are allowed in a java file?
  1. you can have more than one public outer class in a java file
  2. you can have more than one class in a given java file, where only one public class in it.
  3. if you want to have more than one public class, then you have to define them in their own file.
  4. you can't write more than one class in a file
Show Answer
Share Facebook gmail Twitter
3
Which of the below is not a proper naming convention in java?
  1. class name should start with Capital letters
  2. method name should start with small letter.
  3. if a method name is having more than one word then from second word onwards it should start with capital letters
  4. method & class name should start with capital letter. and every word also should start with capital letters.
Show Answer
Share Facebook gmail Twitter
4
how many times a static variable will be initialized? 
Assuming that we are initializing that variable out side the constructor.
  1. every time you create an object for that class, that static variable will be created and initialized
  2. static variable will be created & initialized only once that is when you create first object for that class.
  3. static variable will be created and initialized only once that is at program loading time.
  4. none
Show Answer
Share Facebook gmail Twitter
5
Example program for Java initialization blocks : 
what will be the output of below java program?


import java.util.*;
import java.lang.*;

class MainClass
{ 
    {
        System.out.println("normal initialization block..");
    }
    static{
        System.out.println("static initialization block..");
    }
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
    }
}
  1. it prints in below order
        normal initialization block..
        static initialization block..
        Hello, World!
  2. it prints in below order
        static initialization block..
        Hello, World!
  3. it prints in below order
       Hello, World!
        static initialization block..
  4. it prints in below order
       Hello, World!
        static initialization block..
         normal initialization block..
Show Answer
Share Facebook gmail Twitter
6
Example program for Java initialization blocks : 
what will be the output of below java program?


import java.util.*;
import java.lang.*;

class MainClass
{ 
    {
        System.out.println("normal initialization block..");
    }
    static{
        System.out.println("static initialization block..");
    }
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        MainClass r = new  MainClass();
        MainClass s = new MainClass();
        
    }
}
  1. it prints in below order
        normal initialization block..
        static initialization block..
        Hello, World!
  2. it prints in below order
        static initialization block..
        Hello, World!
        normal initialization block..
  3. it prints in below order
        static initialization block..
        Hello, World!
        normal initialization block..
        normal initialization block..
  4. it prints in below order
        static initialization block..
        normal initialization block..
        Hello, World!
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
14
Sample program for instance members and static members:
what is the output of below java program

import java.util.*;
import java.lang.*;

class MainClass
{  
    public static void main(String args[])
    {
        int i=10;
        int j=10;
        Value v = new Value();
        v.disp();
    }
}

class Value{

    int i = 10;
    static int si = 20;
    public void disp(){
        System.out.println(i);
        System.out.println(si);
    }

    public static void sfun(){
        System.out.println(i);
        System.out.println(si);
    }

    public static void test(){
        sfun();
        disp();
    }
}
  1. 20-10-2013
  2. it gives compile time error because from sfun() which is a static function, we are trying to access non-static variables that is i. which is not possible. This is the only error in this program.
  3. This will give compile time error because in test() function which is a static method, you are trying to access non-static method disp() which is wrong. Along with option 2 this is also one more compile time error. so in this program there are 2 errors are there.
  4. This will give compile time error because in test() function which is a static method, you are trying to access non-static method disp() and static method sfun() which is wrong. Along with option 2 this is also one more compile time error. so in this program there are 2 errors are there.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
18
java local variables:
what is the output of below java program

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {
        Rect r = new Rect(3,4);
        r.findArea();
    }
}

class Rect{
    int len, bred;
    public Rect(int l, int b){
        len = l;
        bred = b;
    }
    public void findArea(){
        System.out.println("area of rect is.."+ (l*b));
    }
}
  1. area of rect is.. 12
  2. area of rect is..0
  3. it gives comile time error because you are trying to access local variables of one function in other function. l and b are local variables of constructor but you are trying to use them in findArea function which is wrong. so it gives compile time error.
  4. it gives run time exception because you are trying to access local variables of one function in other function. l and b are local variables of constructor but you are trying to use them in findArea function which is wrong. so it gives run time error.
Show Answer
Share Facebook gmail Twitter
19
If a function is not accessing any instance variable in it, then should it be defined as static or non static method?
  1. There is no need to make a function static un necessarily.
  2. you have to make the function as static since you are not accessing any instance variable from that function. else it gives compile time error.
  3. you have to make the function as static since you are not accessing any instance variable from that function. Static functions are faster than instance functions.
  4. keep it as instance function even if you are not accessing any instance variable because instance methods are faster than static methods.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :