Core Java Interview Questions and Answers

Ques 6. How do you know if an explicit object casting is needed?

Ans. If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:

Object a; Customer b; b = (Customer) a;

When you assign a subclass to a variable having a superclass type, the casting is performed automatically.

Is it helpful?
YesNoAdd CommentView Comments

Ques 7. What are the differences between the methods sleep() and wait()?

Ans.

The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call.

The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Is it helpful?
YesNoAdd CommentView Comments

Ques 8. Can you write a Java class that could be used both as an applet as well as an application?

Ques 9. What is the difference between constructors and other methods in core java?

Ans.

Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.

Constructor needs to have the same name as that of the class whereas functions need not be the same.

There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value.

There is no return statement in the body of the constructor.

The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

Is it helpful?
YesNoAdd CommentView Comments

Ques 10. Can you call one constructor from another if a class has multiple constructors?

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class type is permitted.

To better understand what this refers to, consider the following version of Box( ):