Top 10 tricky Java interview questions and answers
来源:互联网 发布:淘宝如何收藏店铺 编辑:程序博客网 时间:2024/05/19 04:55
Top 10 tricky Java interview questions and answers
Tricky Java interview questions are those question which has some surprise element on it and if you answer tricky Java question with common sense, you are likely to fail because they are tricky, they require something special to know. Most of thetricky Java questions comes from method overloading and overriding, Checked and Unchecked Exception and subtle Java programming details likeInteger overflow. Most important thing to answer tricky Java question is attitude andanalytical thinking , which helps even if you don't know the answer. Anyway in this Java article we will see 10 Java questions which is real tricky and requires more than average knowledge of Java programming language to answer. As per my experience there are always one or two tricky or tough Java interview question on any core Java orJ2EE interviews, so its good to prepare tricky questions from Java in advance.
Tricky Java interview questions are those question which has some surprise element on it and if you answer tricky Java question with common sense, you are likely to fail because they are tricky, they require something special to know. Most of thetricky Java questions comes from method overloading and overriding, Checked and Unchecked Exception and subtle Java programming details likeInteger overflow. Most important thing to answer tricky Java question is attitude andanalytical thinking , which helps even if you don't know the answer. Anyway in this Java article we will see 10 Java questions which is real tricky and requires more than average knowledge of Java programming language to answer. As per my experience there are always one or two tricky or tough Java interview question on any core Java orJ2EE interviews, so its good to prepare tricky questions from Java in advance.
10 Tricky Java interview question - Answered
Here is my list of 10 tricky Java interview questions, Though I have prepared and shared lot ofdifficult core java interview question and answers, But I have chosen them as Top 10 tricky questions because you can not guess answers of this tricky java questions easily, you need some subtle details of Java programming language to answer these questions.
What will happen if you call return statement or System.exit on try or catch block ? will finally block execute?
This is a very popular tricky Java question and its tricky because many programmer think that finally block always executed. This question challenge that concept by putting return statement in 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 return statement in try block or catch block but finally block won't run if you call System.exit form try or catch.
Can you override private or static method in Java ?
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 private or static method in Java, if you create similar method with same return type and same method arguments that's called method hiding. SeeCan you override private method in Java ormore details.
Does Java support multiple inheritance ?
This is the trickiest question in Java, if C++ can support direct multiple inheritance than why not Java is the argument Interviewer often give. SeeWhy multiple inheritance is not supported in Java to answer this tricky Java question.
What will happen if we put a key object in a HashMap which is already there ?
This tricky Java questions is part of How HashMap works in Java, which is also a popular topic to create confusing and tricky question in Java. well if you put the same key again than it will replace the old mapping because HashMap doesn't allowduplicate keys. SeeHow HashMap works in Java for more tricky Java questions from HashMap.
If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?
One more tricky Java questions from overloading and overriding concept. Answer is you can very well throw super class of RuntimeException in overridden method but you can not do same if its checked Exception. SeeRules of method overriding in Java for more details.
What is the issue with following implementation of compareTo() method in Java
public int compareTo(Object o){
Employee emp = (Employee) emp;
return this.id - o.id;
}
where id is an integer number ?
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 not guaranteed id is positive or negative. If id is negative thansubtraction may overflow and produce incorrect result. See How to override compareTo method in Java for complete answer of this Java tricky question for experienced programmer.
How do you ensure that N thread can access N resources without deadlock
If you are not well versed in writing multi-threading code then this is real tricky question for you. This Java question can be tricky even for experienced and senior programmer, who are not really exposed todeadlock and race conditions. Key point here is order, if you acquire resources in a particular order and release resources in reverse order you can prevent deadlock. Seehow to avoid deadlock in Java for a sample code example.
What is difference between CyclicBarrier and CountDownLatch in Java
Relatively newer Java tricky question, only been introduced form Java 5. Main difference between both of them is that you can reuseCyclicBarrier even if Barrier is broken but you can not reuseCountDownLatch in Java. See CyclicBarrier vs CountDownLatch in Java for more differences.
What is difference between StringBuffer and StringBuilder in Java ?
Classic Java questions which some people thing tricky and some consider very easy. StringBuilder in Java is introduced in Java 5 and only difference between both of them is that Stringbuffer methods aresynchronized while StringBuilder is non synchronized. SeeStringBuilder vs StringBuffer for more differences.
Can you access non static variable in static context?
Another tricky Java question from Java fundamentals. No you can not access static variable in non static context in Java. Readwhy you can not access non-static variable from static method tolearn more about this tricky Java questions.
This was my list of 10 most common tricky question in Java . It's not a bad idea to prepare tricky Java question before appearing for any core Java or J2EE interview. One or two open ended or tricky question is quite common in Java interviews.
- Top 10 tricky Java interview questions and answers
- Top 10 Java Serialization Interview Questions and Answers
- Top 10 Java Serialization Interview Questions and Answers
- Top 10 Collection Interview Questions Answers in Java
- Top 10 tough core Java interview questions answers programming
- 10 Hibernate Interview Questions and Answers for Java J2EE Programmers
- Core Java Interview Questions and Answers
- Java Exception Interview Questions and Answers
- Java Thread Interview Questions And Answers
- 115 Java Interview Questions and Answers
- 25 Java Array Interview Questions And Answers
- .NET Interview Questions And Answers
- JSP Interview Questions and Answers
- mysql interview questions and answers
- Java Multi-Threading and Concurrency Interview Questions with Answers
- 30 Java Exception Handling Interview Questions And Answers
- Top 10 Java Serialization Interview questions
- Top 10 Java Serialization Interview questions
- Java在线打开Word
- cocos2dx目录结构解析
- linear programming , quadratic programming, nonlinear programming and integer programming
- app后端设计(2)--xmpp的使用(2014.01.14更新)
- Storm DRPC ERROR: java.lang.RuntimeException: No DRPC servers configured for topology
- Top 10 tricky Java interview questions and answers
- HDU 3308 LCIS
- ios平移动画
- ecshop模板修改后还原的原因
- 随机生成手机短信验证码
- Spring MVC 的请求参数获取
- C#抽象类和抽象方法
- makefile详解
- HDU 1213 How Many Tables 并查集 水~