Interview Questions on Java

Interview Questions on Java

Welcome Learners!. In this post Interview Questions on Java you will be having few questions and answers related to java. These questions will definitely help you in your interviews. Please do read carefully and prepare well. You can also refer to my posts Java – A robust Language, Factorial of a number, Infosys Interview Experience – PP role.

Interview Questions on Java

Questions 1-5

Q1. What are the core OOP’s concepts?
Ans: As we all know Abstraction, Encapsulation, Inheritance and Polymorphism are the core OOP’s concepts.

Q2. What is an Object?
Ans: Object is an instance or implementation of a class. It has state, behavior and identity. It is also called as an instance of a
class.

Q3. What is meant by Encapsulation?
Ans: Encapsulation is the technique of making the fields in a class private and providing access to the fields
via public methods.

Q4. What is the difference between an object and object reference?
Ans: An object is an instance of a class. Object reference is a pointer to the object. There can be many
references to the same object.

Q5. What will trim() method of String class do?
Ans: trim() eliminate spaces from both the ends of a string.

Questions 6-10

Q6. What is the difference between String, Stringbuffer and StringBuilder?
Ans: String is immutable whereas StringBuffer and StringBuilder can change their values. The only difference between StringBuffer and StringBuilder is StringBuilder is not synchronize whereas StringBuffer is synchronize. So when you want your application to run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

Q7. What are the default values of primitive data types?
Ans: For boolean data type, it is false. And for byte, short, int and long, it is 0. For float and double, it is 0.0. For
char, the default value is ‘u000’.

Q8. What is the difference between static and non-static variables?
Ans: A static variable associates with the class as a whole rather than with specific instances of a class.
Non-static variables take on unique values with each object instance

Q9. Explain Inheritance?
Ans: Inheritance is a concept which enables you to reuse properties and behavior of a class in another class. The
class which is being reused or inherited is called the super class or the parent class. The class which is acquiring the properties is called the subclass or the child class of the inherited class.

Q10. Why you declare the main method static?
Ans: main method is the entry point for a Java application. And JVM calls main method even before the instantiation of the class,hence it is declared as static. You can call static methods even before the creation of objects.