Java interview questions for fresher/intermediate — Part 1
When we hear the word interview, most of us especially freshers or candidates with 1–2 years of experience feel nervous or get stressed overthinking the interview. Here are some of the basic and almost asked by every interviewer just like me.
Java Interview questions- Basics
- Explain OOPS in java:- Go through the basic definition and explain using a real-world example.
- Object: Entity having state and behavior and is defined as an instance of a class.
- Class: Collections of objects and defined as a blueprint from which objects can be created.
- Abstraction: Hiding internal details and showing only minimum required details(abstract class and interface).
- Polymorphism: Ability of an object to take on different forms( method overloading and overriding).
- Encapsulation: Wrapping code and data together in a single unit( class);
- Inheritance: When an object inherits all the properties and behavior of the parent object.
2. What is JVM?
JVM stands for java virtual machine, which provides the runtime environment in which java bytecode can be executed.
3. What are static methods and static variables?
Static methods or variables are available for all objects of the class and can be accessed using the class name. Used when we have a common task/value to be shared among objects of the class.
4. Difference between abstract class and interface.
Abstract class and interface both provide abstraction.
Interfaces: can only have abstract methods, only static and final variables.
Abstract class: can have abstract and non-abstract methods, final ,non-final,static,non-static variables. The child class is used to instantiate the abstract class.
5. Why string is immutable or final in java?
The string is made final to not allow others to extend it in order to achieve security, synchronization, concurrency, caching, and class loading. Objects references are changed not the object itself.
6. What is a static block?
Static blocks are used to initialize the static data member and are executed before the main method at the time of class loading.
7. What is a final class?
when a class is declared as final, it cannot be inherited by subclasses.
8. What is generic in java?
Generics provide compile-time type safety that allows programmers to catch invalid types at compile time and allows them to store only a single type of object.