Java Interview Questions - Principles
Here are the most popular Java interview questions about the key principles of the language.See all questions
Explain why Java is Pass-by-Value and not Pass-by-Reference. Give an example.
Pass-by-value is when the reference to an object is copied to a new variable.
Pass-by-reference is when a reference to an object is passed directly to the other method.
When a method is called in Java, new Stack space is allocated for the method and all variables in the arguments are copied to the new Stack block.
In the first print...
Is it recommended to use the final keyword in method parameters?
Yes, Java is a Pass-by-Value language, so all the variables are local to the current method. Changing the value of the method parameter will not result in any changes in the calling method. To avoid mistakes and make the code more readable, marking the method parameters final is the best approach. This also makes it mandatory for the developer to e...
What is the difference between:
final is a keyword which can be used on a variable, class, or method.
finally is a code block that is guaranteed to run after a throws block, even if the exception is not thrown.
finalize is a method
What happens if the main method is not static?
If a program has a main method without the static keyword, it will compile, but will throw a Runtime Exception with
Explain the uses of
this is a self-reference to the current instance of the class. It can be used to invoke the constructor, to reference a class-level variable or method, or to refer to or pass the object instance.
this is used for calling the constructor within another constructor, it is called constructor chaining. It must be the first line of the constru
Can you create an object of a class within the same class? Can this lead to an error?
Yes, you can instantiate a class within itself. Example:
The line commented as
Error line is an example of self-object instantiation leading to an error. In this case, the JVM will allocate stack space on each constructor call, and the stack will soon run out of space leading to a
StackOverflowError, a common recursion problem.
Describe a potential way of creating a memory leak in Java. Give an example.
Because Java has automated memory management, it precludes code errors relating to allocation of memory for objects and reclaiming memory from objects. This prevents user errors arising due to incorrect code for memory management, but it does not avoid design flaws that can result in memory leaks.
Example of Design flaw:
We are required to implem...
Explain instruction reordering. In what context can instruction reordering create problems? How can it be fixed?
Instruction reordering is when the execution order of the code is changed by the JVM at compile time or run time. This is done to optimize the code. Instruction reordering guarantees program order execution. This means that two statements which are related will have a happens-before relation, but two unrelated statements can be executed out-of-orde...