Tricky Java Interview Questions: References
Tricky Java Interview Questions: References
You have 2 free stories left this month. Sign up and get an extra one for free.
In this article, you can learn the answers to some of the tricky questions
asked in the Java interviews.
skilled.dev
. . .
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.
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.
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.
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.
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.
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 .
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.
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
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
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.
References
Top 10 Tricky Java interview questions and Answers
. . .
skilled.dev
1.2K claps
WRIT T EN BY
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