Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tricky Java Interview Questions and Answers

by Bhavya Sri, on Mar 15, 2018 11:02:52 AM

Tricky Java Interview Questions and Answers

Q1. Java Tricky Programming Question 2
What will below statements print?

Ans:
long
longWithL = 1000*60*60*24*365L;

long longWithoutL = 1000*60*60*24*365;
System.out.println(longWithL);
System.out.println(longWithoutL);

Q2. Can you override private or static method in Java ?

Ans: “Another popular Java tricky question… You can not override private or static method in Java. If you create a similar method with same return type and same method arguments, that’s called method hiding.”

Q3.  What does the following Java program print?

Ans: 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, where MIN_VALUE is negative, both the MAX_VALUE and MIN_VALUE of the Double class are positive numbers. The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0. I have asked this question to Java developer having experience up to 3 to 5 years and surprisingly almost 70% candidate got it wrong.

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

Ans: This is a very popular tricky Java question and it's tricky because many programmers think that no matter what, but the finally block will always execute. This question challenge that concept by putting a return statement in the try or catch block or calling System.exit() from try or catch block. Answer of this tricky question in Java is that finally block will execute even if you put a return statement in the try block or catch block but finally block won't run if you call System.exit() from try or catch block. 

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

Ans: Another popular Java tricky question, As I said method overriding is a good topic to ask trick questions in Java. Anyway, you can not override a private or static method in Java, if you create a similar method with same return type and same method arguments in child class then it will hide the superclass method, this is known as method hiding.

Similarly, you cannot override a private method in sub class because it's not accessible there, what you do is create another private method with the same name in the child class. See Can you override a private method in Java or more details.

Q6Brain-Teasing Java Interview Questions

Ans: We have boiled down the huge range of typical and most tricky Java interview questions from these guides, and on Quora, to a list of 20, some of them answered, to help you prepare for your important job interview.

The consensus among Java experts online is, concentrate on the fundamentals, don’t wing it, and do your homework. Then no Java interview questions fired at you on the day will seem at all remotely tricky. Good luck, Java AGENTs

Q7. What does the following Java program print?

public class Test {
public static void main(String[] args) {
System.out.println(Math.min(Double.MIN_VALUE, 0.0d));
}
}

Ans: This question is tricky because unlike the Integer, where MIN_VALUE is negative, both the MAX_VALUE and MIN_VALUE of the Double class are positive numbers. The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0. I have asked this question to Java developer having experience up to 3 to 5 years and surprisingly almost 70% candidate got it wrong.

Q8. where an id is an integer number.

Ans: Well, three is nothing wrong in this Java question until you guarantee that id is always positive. This Java question becomes tricky when you can't guarantee that id is positive or negative. the tricky part is, If id becomes negative than subtraction may overflow and produce an incorrect result. See How to override compareTo method in Java for the complete answer of this Java tricky question for an experienced programmer.

Q9. How do you ensure that N thread can access N resources without deadlock?

Ans: If you are not well versed in writing multi-threading code then this is a real tricky question for you. This Java question can be tricky even for the experienced and senior programmer, who are not really exposed to deadlock and race conditions. The key point here is ordering, if you acquire resources in a particular order and release resources in the reverse order you can prevent deadlock. See how to avoid deadlock in Java for a sample code example.

Q10. What is difference between CyclicBarrier and CountDownLatch in Java

Ans: Relatively newer Java tricky question, only been introduced from Java 5. The main difference between both of them is that you can reuse CyclicBarrier even if Barrier is broken, but you can not reuse CountDownLatch in Java. See CyclicBarrier vs CountDownLatch in Java for more differences.

[teaserbox type="4" img="2802" title="Interested in Learning Python" subtitle="Join Us Now" link_url="https://www.mytectra.com/python-training-in-bangalore.html" target="blank"]

Q11. What is the difference between StringBuffer and StringBuilder in Java?

Ans: Classic Java questions which some people think tricky and some consider very easy. StringBuilder in Java was introduced in JDK 1.5 and the only difference between both of them is that StringBuffer methods e.g. length(), capacity() or append() are synchronized while corresponding methods in StringBuilder are not synchronized.

Because of this fundamental difference, concatenation of String using StringBuilder is faster than StringBuffer. Actually, it's considered the bad practice to use StringBuffer anymore, because, in almost 99% scenario, you perform string concatenation on the same thread. See StringBuilder vs StringBuffer for more differences.

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

Ans: Another tricky Java question from Java fundamentals. No, you can not access a non-static variable from the static context in Java. If you try, it will give compile time error. This is actually a common problem beginner in Java face when they try to access instance variable inside the main method. Because main is static in Java, and instance variables are non-static, you can not access instance variable inside main. See, why you can not access a non-static variable from static method to learn more about this tricky Java questions.

Q13. Does Java support multiple inheritances?

Ans: This is the trickiest question in Java if C++ can support direct multiple inheritances than why not Java is the argument Interviewer often give. Answer of this question is much more subtle then it looks like, because Java does support multiple inheritances of Type by allowing an interface to extend other interfaces, what Java doesn't support is multiple inheritances of implementation. This distinction also gets blur because of default method of Java 8, which now provides Java, multiple inheritances of behavior as well. See why multiple inheritances are not supported in Java to answer this tricky Java question.

Q14. What will happen if we put a key object in a HashMap which is already there?

Ans: This tricky Java question is part of another frequently asked question, How HashMap works in Java. HashMap is also a popular topic to create confusing and tricky question in Java. Answer of this question is if you put the same key again then it will replace the old mapping because HashMap doesn't allow duplicate keys. The Same key will result in the same hashcode and will end up at the same position in the bucket.

Each bucket contains a linked list of Map.Entry object, which contains both Key and Value. Now Java will take the Key object from each entry and compare with this new key using equals() method, if that return true then value object in that entry will be replaced by new value. See How HashMap works in Java for more tricky Java questions from HashMap.

Q15.  What does the following Java program print?

Ans: 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, where MIN_VALUE is negative, both the MAX_VALUE and MIN_VALUE of the Double class are positive numbers. The Double.MIN_VALUE is 2^(-1074), a double constant whose magnitude is the least among all double values. So unlike the obvious answer, this program will print 0.0 because Double.MIN_VALUE is greater than 0. I have asked this question to Java developer having experience up to 3 to 5 years and surprisingly almost 70% candidate got it wrong.

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

Ans: This is a very popular tricky Java question and it's tricky because many programmers think that no matter what, but the finally block will always execute. This question challenge that concept by putting a return statement in the try or catch block or calling System.exit() from try or catch block. Answer of this tricky question in Java is that finally block will execute even if you put a return statement in the try block or catch block but finally block won't run if you call System.exit() from try or catch block.

[teaserbox type="4" img="2803" title="Interested in Learning Cloud Computing AWS Architect" subtitle="Explore!" link_url="https://www.mytectra.com/aws-training-in-bangalore.html" target="blank"]

Related Interview Questions...

Core Java Interview Questions And Answers

Core Java Programming Interview Questions and Answers

Multithreading in Java Interview Questions and Answers

Javascript Interview Questions

Java/J2EE Apps Integration Questions and Answers.

Java Collections Interview Question and Answers

Interview Questions For Selenium with Java

Java Web Services Interview Questions and Answers

Java Interview Questions and Answers

Topics:Tricky Java Interview Questions and AnswersInformation Technologies (IT)

Comments

Subscribe

Top Courses in Python

Top Courses in Python

We help you to choose the right Python career Path at myTectra. Here are the top courses in Python one can select. Learn More →

aathirai cut mango pickle

More...