1 10-java-interview-questions-&-answers by best onlinetrainers

12
Java Interview Q & A By www.bestonlinetrainers.com

Upload: bestonlinetrainers

Post on 10-May-2015

322 views

Category:

Education


0 download

DESCRIPTION

Crack your Java interview by knowing all the questions that your interviewer may ask. Brush up your knowledge with this superb set of Java interview questions & answers prepared by BestOnlineTrainers

TRANSCRIPT

Page 1: 1 10-java-interview-questions-&-answers by best onlinetrainers

Java Interview Q & ABy www.bestonlinetrainers.com

Page 2: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q1: To implement key Object in HashMap which two methods are used?

Answer:

In HashMap, to use any object as Key it must implement equals and HashCode method in Java.

Learn Java. Ask for a free demo www.bestonlinetrainers.com

Page 3: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q2:What is an immutable object? Mention an immutable object?

Answer:

Objects that cannot be modified once created are immutable objects in JAVA. A new object can be resulted due to any modification in an immutable object. For example, in JAVA, String is immutable. In order to prevent sub class from overriding methods in Java which can compromise Immutability, immutable object are mostly final in JAVA. This same functionality can be achieved by making member as non final but private and not modifying them except in constructor.

Learn Java. Ask for a free demo www.bestonlinetrainers.com

Page 4: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q3:WState the difference between creating String as new() and literal?

Answer:

When string is created with new() Operator, it’s created in a heap and not added into string pool whereas using literal String are created in String pool itself which exists in PermGen area of heap, Example: String s = new String("Test"); It does not put the object in String pool , but needs to be called String.intern() method which is used to put them into String pool explicitly. It’s only then that’s it create String object as String literal. For e.g. String s = "Test" Java automatically put that into String pool

Page 5: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q4:What is difference between StringBuffer and StringBuilder in Java?

Answer:

Classic Java questions can seem to be tricky and very easy. In Java 5, StringBuilder is introduced and the only difference between both of them is that StringBuffer methods are synchronized while StringBuilder is non- synchronized.

Page 6: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q5: Overriding HashCode() method has any difference in performance implication?

Answer:

Yes .A poor HashCode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.

Page 7: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q6: While writing stored procedure or accessing stored procedure from java how is the error condition handled? Answer:

This is one of the tough questions normally asked in Java interview. The stored procedure should return error code if some operation fails but if stored procedure itself fails than catching SQLException is the only choice left .

Page 8: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q7: At the compile time are the imports checked for validity? That is, will the code containing an import such as java.lang.ABCD compile up? Answer:

Yes. The imports are checked for the semantic validity at the compile time. The above line of import will not compile if it has the code, an error is shown saying, cannot resolve symbol.

Symbol: class ABCD

Location: package io

Import java.io.ABCD;

Page 9: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q8: State difference between Executor.submit() and Executer.execute() method? Answer:

When looking at exception handling, there is a difference. If the tasks throws an exception and if it was submitted with the execute then this exception will go to the uncaught exception handler (that is when one explicitly is not provided, the default one will just print the stack trace to System.err). If the task is submitted with any thrown exception or a checked exception or not, is then part of the task's return status. And 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.

Page 10: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q9: What is the difference between factory and abstract factory pattern?

Answer:

One more level of abstraction is provided by Abstract Factory .Different factories from each Abstract Factory responsible for the creation of different hierarchies of objects based on type of factory are considered. For example; Abstract Factory extended by AutomobileFactory, UserFactory, RoleFactoryetc.For every object created in that genre, each individual factory would be responsible.

Page 11: 1 10-java-interview-questions-&-answers by best onlinetrainers

Q10: Define Singleton? Which is better: to make whole method synchronized or only critical section synchronized?

Answer:

Singleton in JAVA is a class with just one instance in the whole Java application. One such example is java.lang.Runtime is a Singleton class .With the introduction Enum by Java 5, creating a Singleton is easy rather than being tricky as earlier.

Page 12: 1 10-java-interview-questions-&-answers by best onlinetrainers

Want to learn Java?Visit : http://www.bestonlinetrainers.com/programming/java-training.html

Email : [email protected]

Phone : USA: +(1) 6783896789

UK: +(44)- 2032393637

India: +(91) 9246449191

Ask for a free demo TODAY : http://www.bestonlinetrainers.com/demo/index.html