About NewTechnoBuzz
Advertise
Contact Us

Monday, July 7, 2014

Tricky Java interview questions and Answers Part 1

Tricky Java interview questions are those question which has some surprising element and you require something special to know. Most of the tricky Java questions comes from method overloading and overriding, Serialization, Collections, Checked and Unchecked Exception and subtle Java programming details like Integer overflow, Datatype conversion, autoboxing and unboxing etc. Most important thing to answer tricky Java question is attitude and analytical thinking , which helps even if you don't know the answer. Anyway in this Java article we will see some tricky Java questions and requires more than average knowledge of Java programming language. As per my experience there are always one or two tricky or tough Java interview question on any core Java or J2EE interviews. Sometimes, interviewer wants to check the in-depth knowledge and instead of asking direct questions, he/she gives some scenario based to check what approach the candidate takes to solve the question.

Here is my list of few tricky Java interview questions, Though I have prepared and shared lot of difficult core java interview question and answers. You need some subtle details of Java programming language to answer these questions.

Can you override private or static method in Java?

Method overriding is a good topic to ask tricky 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. See Can you override private method in Java or more details.

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

This tricky question 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 allow duplicate keys. See How 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. See Rules of method overriding in Java for more details.

What is difference between StringBuffer and StringBuilder in Java ?

Classic Java questions which some people think tricky and some consider very easy. StringBuilder in Java was introduced in JDK 1.5 and 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 its considered bad practice to use StringBuffer any more, because in almost 99% scenario, you perform string concatenation on same thread. See StringBuilder 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 non-static variable from static context in Java. If you try, it will give compile time error. This is actually a common problem beginners in Java face, when they try to access instance variable inside main method. Because main is static in Java, and instance variables are non-static, you can not access instance variable inside main. Read why you can not access non-static variable from static method to learn more about this tricky Java questions.

What is the difference between creating String as new() and literal?

When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.
String str = new String("Test");
does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.

What is immutable object? Can you write immutable object?

Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object. For example is String is immutable in Java. Mostly Immutable are also final in Java, in order to prevent sub class from overriding methods in Java which can compromise Immutability. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.

What is difference between Executor.submit() and Executer.execute() method ?

There is a difference when looking at exception handling. If your tasks throws an exception and if it was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked exception or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will re-throw this exception, wrapped in an ExecutionException.


0 comments