Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Core Java Interview Questions and Answers

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

Yes. Use this( ) syntax.
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( ):
Box(double w, double h, double d) { 
	this.width = w;
	this.height = h;
	this.depth = d;
}
The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking object.

Is it helpful? Add Comment View Comments
 

Ques 62. What can go wrong if you replace && with & in the following code:

String a=null;
if (a!=null && a.length()>10) {
	...
}
A single ampersand here would lead to a NullPointerException.

Is it helpful? Add Comment View Comments
 

Ques 63. What's the difference between a queue and a stack?

Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

You can think of a queue like a line at the bank. The first person to get there will get to the teller first. If a bunch of people come while all the tellers are busy, they stand in line in the order in which they arrived. That is to say, new people (items) are added to the end of the line and the first person in line is the only one who is called to a teller. In real life this is known as "first come, first served." In programming terms it's known as first-in-first-out, or FIFO.

You can think of a stack like a deck of cards. You can put down cards into a pile on your table one at a time, but if you want to draw cards, you can only draw them from the top of the deck one at a time. Unlike a queue, the first card to be put down is the last card to be used. This is known as first-in-last-out, or FILO (also called LIFO for last-in-first-out).

A queue is a first-in-first-out data structure. When you add an element to the queue you are adding it to the end, and when you remove an element you are removing it from the beginning.

A stack is a first-in-last-out data structure. When you add an element to a stack you are adding it to the end, and when you remove an element you are removing it from the end.

Is it helpful? Add Comment View Comments
 

Ques 64. What does the "final" keyword mean in front of a variable? A method? A class?

FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived
A final variable cannot be reassigned,
but it is not constant. For instance,
final StringBuffer x = new StringBuffer()
x.append("hello");
is valid. X cannot have a new value in it,but nothing stops operations on the object
that it refers, including destructive operations. Also, a final method cannot be overridden
or hidden by new access specifications.This means that the compiler can choose
to in-line the invocation of such a method.(I don't know if any compiler actually does
this, but it's true in theory.) The best example of a final class is
String, which defines a class that cannot be derived.

Is it helpful? Add Comment View Comments
 

Ques 65. Is the ternary operator written x : y ? z or x ? y : z ?

It is written x ? y : z.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2021 WithoutBook