Top-10-Tricky-Java-Programming-Interview-Questions-You-Must-Know

If you are preparing for a Java job interview, you must be familiar with these tricky programming questions. Every interview usually comes with tricky questions that trick candidates into thinking either too much or not at all.

With over 45 billion active virtual machines globally, Java is one of the most popular programming languages in the world. It is the number one development language for microservices and is the most-used language for DevOps, AI, VR, Big Data, Continuous Integration, Analytics, Mobile, Chatbots, and much more.

With over 9 million active Java developers globally, the demand for talent in this domain is at an all-time high. As per Mercer, one of the global leaders in talent assessment, the role of a Java developer is one of the most in-demand roles in 2022, thanks to the ever-increasing adoption of Java in multiple domains, devices, and organisations.

However, clearing an interview for this job role isn’t all that easy, given that most interview panellists tend to throw tricky questions at the candidates to find the best talent for the job. Let’s see some of those.

Java Programming – Tricky Interview Questions

Java-Programming-Tricky-Interview-Questions

Let us show you the ten most tricky Java programming questions you can expect in an interview, with solutions.

1. What will the following Java program print?

public class Test { public static void main(String[] args) {

System.out.println(Math.min(Double.MIN_VALUE, 0.0d)); } }

Answer:

This question is tricky because, unlike the Integer class, where the MIN VALUE is negative, the Double class’s MAX VALUE and MIN VALUE are both positive integers. Double.MIN VALUE is 2^(-1074), a double constant with the least magnitude of all double values.

Also Read:  Why Do Leading IT Companies Have Entrance Tests?

Because of Double, this program will print 0.0 instead of the obvious answer. The value of MIN VALUE is greater than zero.

If you’d like to understand the difference between a compiler and an interpreter in Java, Click here.

2. What will happen if you put the return statement or System.exit () on the ‘try‘ or ‘catch‘ block? Will the ‘finally’ block execute?

Answer:

Many experienced Java programmers think the ‘finally’ block will always execute. However, that reasoning is challenged here by putting a return statement in the ‘try’ or ‘catch’ block or calling System.exit() from the ‘try’ or ‘catch’ block.

The solution for this mind-bender is that the ‘finally’ block will execute successfully, even if you put a return statement in the ‘try’ or ‘catch’ block. However, the ‘finally’ block will fail to execute if you call System.exit() from the ‘try’ or ‘catch’ block.

3. Can you override a private or static method in Java?

Answer:

Method overriding is always an excellent topic to confuse developers. You cannot override a static method in Java. It will hide the superclass method if you create an identical method with the same return type and method arguments as the ones in the child class. This is also known as method hiding.

Likewise, overriding a private method in the subclass is impossible as it is not accessible there. As a workaround, you can create a new private method with the same name in the child class.

If you’re new to software development and overwhelmed with new terms, here are the 19 most important terms you should know!

Also Read:  Interview Prep: Slay That Interview With Our FAQs on DSA

4. What will the expression 1.0 / 0.0 return? Will it throw an exception or any compile-time errors?

Answer:

It will compile successfully and not throw ArithmeticException. It will instead return Double.Infinity.

5. Does Java support multiple inheritances?

Answer:

This is the most challenging problem in Java. Interviewers frequently argue that since C++ can allow multiple direct inheritances, why can’t Java? 

Because Java supports multiple inheritances of Type by enabling one interface to extend other interfaces, the answer is considerably more complicated than it appears. Java does not allow multiple inheritances of implementation.

6. What will happen if we put a key object in a HashMap already there?

Answer:

HashMap does not allow duplicate keys, so putting the same key again will overwrite the original mapping. The same key will generate the same hashcode and end up at the same bucket position. Each bucket contains a linked list of maps.

This is an entry object that has both a Key and a Value. Java will compare each entry’s Key object to this new key using the equals() method, and if the comparison returns true, the value object in that entry will be replaced by the new value.

7. What does the following Java program print?

public class Test { public static void main(String[] args) throws Exception { char[] chars = new char[] {‘\u0097’}; String str = new String(chars); byte[] bytes = str.getBytes(); System.out.println(Arrays.toString(bytes)); } }

Answer:

This question is particularly tricky because this program’s output depends on the operating system and locale. On a Windows XP with the US locale, the above program prints 63, but if you run this program on Linux or Solaris, you will get different values.

Also Read:  Can Online Coding Bootcamp Give Your Salary a Boost?

8. If a method throws NullPointerException in the superclass, can we override it with a method that throws RuntimeException?

Answer:

Yes, you can throw a superclass of RuntimeException in overridden method, but you can not do that if it’s a checked Exception.

9. What is the difference between CyclicBarrier and CountDownLatch in Java?

Answer:

You can reuse CyclicBarrier even if the Barrier is broken, but you can not reuse CountDownLatch in Java.

10. Can you access a non-static variable in the static context?

Answer:

No, you cannot access a non-static variable from the static context in Java, as it will give a compile-time error.

With these answers in your arsenal, you are well-equipped to handle some of the trickiest questions any Java interviewer can throw at you. If you are a new learner in the field of programming or want to upskill and take your career to the next level, CodeQuotient offers their flagship SuperCoders Program – a full-stack development internship. It focuses on in-depth, project-based practical learning to unlock the supreme programmer inside you!


Get UGC Approved, BCA Degree
And Earn While You Do That

Join CodeQuotient's Under-Graduate Program In Software Engineering

Get Paid Internship with Our Hiring Partners to Sponsor Your Fees

100% Placement Assistance


Leave a Reply

Your email address will not be published. Required fields are marked *

Archives

CodeQuotient

©️ 2024. All Rights Reserved.