Friday, 8 December 2017

Top 10 Most Frequently Asked Interview Core Java Interview Questions And Answers

Java Interview Question and answer, top 10 Java Interview Question and answer,Most Frequently Asked Interview Core Java Interview Questions And Answers,What is immutable object? Can you write immutable object?,What is the difference between creating String as new() and literal?,How do you handle error condition while writing stored procedure or accessing stored procedure from java?,What is the difference between an Inner Class and a Sub-Class?,What are the various access specifiers for Java classes?,What’s the purpose of Static methods and static variables?, What is data encapsulation and what’s its significance?,


Q1. What is immutable object? Can you write immutable object?
Ans:  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.

Q2. What is the difference between creating String as new() and literal?
Ans:  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 s = 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.


Q3. How do you handle error condition  while writing stored procedure or accessing stored procedure from java?

Ans:  This is one of the tough Java interview question and its open for all, my friend didn't know the answer so he didn't mind telling me. my take is that stored procedure should return error code if some operation fails but if stored procedure itself fail than catching SQLException is only choice.

Q4. What is the difference between an Inner Class and a Sub-Class?
Ans:  An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.
Q5. What are the various access specifiers for Java classes?
Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:
1. Public : Class,Method,Field is accessible from anywhere.
2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.
3. Default: Method,Field,class can be accessed only from the same package and not from outside of it’s native package.
4. Private: Method,Field can be accessed from the same class to which they belong.
Q6. What’s the purpose of Static methods and static variables?
Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.
Q7. What is data encapsulation and what’s its significance?
Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.
Q8. When do you override hashcode and equals() ?
Ans : Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.

Q9. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?
Ans : Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object)
Q10. Does not overriding hashcode() method has any performance implication ?
Ans : This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.



DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

1 comments:

  1. It is very good blog and useful for students and developer , Thanks for sharing

    Java Online Training Bangalore

    ReplyDelete