Tricky Java Interview Questions

Tricky Java Interview Questions

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.

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. This 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.” Which 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.

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 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.

Credit : https://levelup.gitconnected.com/

Harsh Soni

Building @CrysPay on Aptos | ?? @RouterProtocol | Degree-ing ?? | Flutter + Web3?? | Pythoneer | DappDev | Sanskrit ????????| Contributor @PSF |

2 年

Reach++

回复
Ahmad Fawad Butt

Software Engineer | Software Business Analyst | Jr. Product Owner | Computer Systems Analyst | Product Manager Aspirant | SFC | Scrum Master | Power BI | SAAS

2 年

Wow

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了