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

Show Answer
Share Facebook gmail Twitter
2
What is the purpose of constructors in java ? creating object or initializing instance variables?
  1. Constructor is used to create object
  2. Constructor is used to initialize the instance variables of an object.
  3. It is advisable to use constructor to initialize static variables also.
  4. Constructor will be called before creating the object.
Show Answer
Share Facebook gmail Twitter
3
Does java has parameterized constructors? Is it possible to overload constructors?
  1. yes constructors can have parameters, but it is not possible to overload constructors.
  2. yes constructors can have parameters, it is also possible to overload constructors.
  3. no constructors can have parameters, it is also not possible to overload constructors.
  4. no constructors can have parameters, it is possible to overload constructors.
Show Answer
Share Facebook gmail Twitter
4
Can we have return type for a constructor in java? Can we have private constructors in java? 
  1. Constructor should not have return type. Yes, we can have private constructors in java.
  2. constructor can be private. Constructor will have return type.
  3. constructor should be always public. Constructor should not have return type.
  4. constructor can be both private or public. Some times constructors have return type.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
6
Which of the below statements are true with respect to this() function call in a constructor?
  1. this() function call in a constructor should be always the first line, else it gives compile time error.
  2. I can place this() function call any where in the constructor, to call other constructors
  3. I can call more than one constructor from a given constructor directly.
  4. I can call constructors from non-constructor functions also, like any other instance methods.
Show Answer
Share Facebook gmail Twitter
7
How to share common code between all the constructors? What is the best to avoid duplicated code in constructors?
  1. Write that code in all the constructors.
  2. write that logic outside the constructors, it will be copied automatically to all constructors.
  3. use initialization block, it will be copied automatically to all constructors.
  4. write a function that has the common logic, and call that function from all the constructors.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
9
when does a static initialization block will be executed, in java?
  1. every time you create an object for that class, that static initialization block will be executed
  2. static initialization block will be executed only once. That is when you create first object for that class.
  3. static initialization block will be executed only once. That is at program loading time.
  4. none
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
13
Parameterized constructors in Java:
what is the output of below Java program?
class Base{
        int i;
        public Base(int z){
                i = z;
        }
        public void disp(){
                System.out.println(i);
        }
}
public class ConstructorDemo {
        public static void main(String[] args) {
                Base b = new Base();
                b.disp();
        }
}
  1. it prints 0
  2. it prints garbage value
  3. it gives compile time error, as there is no constructor which takes 0 parameters.
  4. it won't give any error, because by default java gives a default constructor for every class. so it prints 0.
Show Answer
Share Facebook gmail Twitter
14
which is true about constructor's return type in Java?
  1. constructor should not have any return type, else it is a compile time error.
  2. constructor should not have any return type, else it will be treated like any other normal function but wont give any compile time error.
  3. constructors can have return type.
  4. all are true
Show Answer
Share Facebook gmail Twitter
15
What is constructor chaining?
  1. In Java you can call one constructor of a class from another constructor of same class. This is called as constructor chaining in Java. 
  2. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. This is called as constructor chaining in java.
  3. calling multiple constructors in a single statement while creating an object, is called as constructor chaining.
  4. none
Show Answer
Share Facebook gmail Twitter
16
Is it possible to have constructor in abstract class?
  1. Though, you cannot create objects for the abstract class still there is a possibility that abstract classes may have constructor,the reason is derived classes may call the base or abstract class constructor.
  2. it is not possible to have constructors in an abstract class.
  3. it is possible to have constructors in abstract class, buecause to create object for that class it requires constructor.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
17
What is a constructor?
  1. constructor is a special function which will be having same name as the class name, with out having any return type. Constructors re mostly public, though we can have private constructors also. If a constructor is having return type, then it will be treated like a normal method.
  2. Constructors are used to initialize instance variables while creating an object for that class. Constructors will be called immediately after creating an object for that class to initialize instance variables.
  3. both the statements are correct.
  4. none
Show Answer
Share Facebook gmail Twitter
18
What is constructor overriding?
  1. Constructors cannot be overridden in Java.
  2. Since constructors are special functions which can't be inherited to derived classes, so it is not possible for derived classes to override a constructor of base class.
  3. But derived classes can call base class constructors by using super keyword.
  4. all are correct.
Show Answer
Share Facebook gmail Twitter
Read more papers :