Sign in Get started
WRIT E FOR US CODING INT ERVIEW COURS E →
You have 2 free stories left this month. Sign up and get an extra one for free.
Tricky Java Interview Questions
Do you know the answers?
Manusha Chethiyawardhana Follow
Jun 4 · 4 min read
Photo by bruce mars on Unsplash
According to the StackOverflow developer survey results for 2019, Java
ranks fourth in the most popular programming languages. And
according to Indeed, Java is the second most demanding programming
language in the United States. Because of this popularity, you can
understand that Java has a very competitive job market. Interviewers
often ask tricky questions from candidates to test their knowledge of
Java. So you’re going to have to be ready to answer those questions and
ensure your job position.
In this article, you can learn the answers to some of the tricky questions
asked in the Java interviews.
Coding Interview Questions | Skilled.dev
A full platform where I teach you everything you need to land your next job
and the techniques to…
skilled.dev
. . .
Q1 — What is the output of the given Java code?
public class Test {
public static void main(String[] args) {
method(null);
}
public static void method(Object o) {
System.out.println("Object method");
}
public static void method(String s) {
System.out.println("String method");
}
Answer
It will print “String method”. First of all, null is not an object in Java. But
we know that we can assign null to any object reference type in Java.
Java String is also an object of the class java.lang.String . Here, the Java
compiler chooses to call the overloaded method with the most specific
parameters. Which would be String because the String class is more
specific than the Object class.
Q2 — What will be the output of the given Java code?
public class Test{
public static void main(String[] args){
Integer num1 = 100;
Integer num2 = 100;
if(num1==num2){
System.out.println("num1 == num2");
}
else{
System.out.println("num1 != num2");
}
}
}
Answer
It will print “num1 == num2”. Whenever two different object references
are compared using “==,” the value is always “false.” But here, because
of the Integer caching, num1 and num2 are autoboxed. Thus num1==num2
returns “true”. Integer caching happens only for values between -128
and 127.
Q3 — How does Garbage Collection prevent a Java application
from going out of memory?
Answer
Java Garbage Collector does not prevent a Java application from going
out of memory. It simply cleans the unused memory when an object is
out of scope and no longer needed. As a result, garbage collection is not
guaranteed to prevent a Java app from going out of memory.
Q4 — Is Java “pass-by-reference” or “pass-by-value”?
Answer
Java is always “pass-by-value”. However, when we pass the value of an
object, we pass the reference to it because the variables store the object
reference, not the object itself. But this isn’t “pass-by-reference.” This
could be confusing for beginners.
Q5 — How many String objects are created by the below code?
public class Test{
public static void main(String[] args){
String s = new String("Hello World");
}
}
Answer
Two String objects are created. When the new operator is used to create
a String object, if the object does not exist in the Java String Pool, it will
first be created in it, and then in the heap memory as well. You can
simply learn all about Java Strings in my article below.
String , StringBuilder, and StringBu er Do You Know the
Di erence?
A complete guide for Strings in Java
medium.com
Q6 — What is the output of the below Java code?
public class Test{
public static void main(String[] arr){
System.out.println(0.1*3 == 0.3);
System.out.println(0.1*2 == 0.2);
}
}
Answer
The first print statement prints “false” and the second prints “true”. This
happens simply because of the rounding error in floating-point numbers.
Only numbers that are powers of 2 can be represented precisely by a
simple binary representation. Numbers that do not correspond to a
power of 2 must be rounded to fit into a limited number of bits. Here,
because Java uses double to represent decimal values, only 64 bits are
available to represent the number. Therefore, 0.1*3 would not be equal
to 0.3 .
Q7 — Is it possible to override or overload a static method in Java?
Answer
It’s possible to overload static Java methods, but it’s not possible to
override them. You can write another static method with the same
signature in the subclass, but it’s not going to override the superclass
method. It’s called method hiding in Java.
Q8 — What’s the most reliable way to test whether two double
values are equal?
Answer
The most reliable and accurate way to determine whether two double
values are equal to each other is to use Double.compare() and test it
against 0.
Double.compare(d1, d2) == 0
Q9 — Will the finally block be executed if the try or catch block
executes a return statement?
Answer
Yes, the finally block will still be executed even if a return statement was
executed in a try or catch block. This is a very popular and tricky Java
question. The only way we can stop finally block from being executed is Top highlight
to use the System.exit() method.
Q10 — What happens when we run the below Java code?
public class Test{
public static void main(String[] args){
System.out.println("main method");
}
public static void main(String args){
System.out.println("Overloaded main method");
}
}
Answer
It prints “main method”. There will be no error or exception because the
main method can be overloaded in Java. It has to be called from within
the main method to be executed just like any other method.
. . .
That’s all for this article. I hope you learned something new.
Thank you for reading and happy coding!
References
Top 10 Tricky Java interview questions and Answers
Oracle Java Documentation
. . .
Coding Interview Questions | Skilled.dev
A full platform where I teach you everything you need to land your next job
and the techniques to…
skilled.dev
Sign up for "Top Stories" from Level Up Coding
A monthly summary of the best stories shared in Level Up Coding
Create a free Medium account to get "Top Stories" in
Get this newsletter your inbox.
Programming Software Development Technology Java Software Engineering
1.2K claps
WRIT T EN BY
Manusha Chethiyawardhana Follow
faculty and Taekwondo player
Level Up Coding Follow
Coding tutorials and news. The developer homepage
gitconnected.com
See responses (2)
More From Medium
Linux user tries My Top 20 VS Code 5 T hings T hat Are Hard 5 Lessons I’ve Learned
Windows, in 2020 Extensions To Grasp When You on How to Structure
Dominik Tarnowski in Level Up Neo Hao Jun in Level Up Start Programming Code
Coding Coding Daan in Level Up Coding Daan in Level Up Coding
4 JavaScript Tricks You SOLID Principles — 3 Habits T hat Will Help Pandas DataFrame
Should Know Simpli ed with You Become a Top (Python): 10 useful
Anupam Chugh in Level Up Illustrations Developer tricks
Coding Animesh Gaitonde in Level Up Manish Jain in Level Up Coding Maurizio Sluijmers in Level Up
Coding Coding
Discover Medium Make Medium yours Become a member
Welcome to a place where words matter. On Medium, smart Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
voices and original ideas take center stage - with no ads in best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
sight. Watch
About Help Legal