10 basic Java interview questions with their answers
1. What is Java?
Answer:
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is platform-independent due to its "Write Once, Run Anywhere" (WORA) capability achieved through the Java Virtual Machine (JVM).
2. What are the key features of Java?
Answer:
- Object-Oriented: Everything in Java is treated as an object.
- Platform-Independent: Java code runs on any platform that supports JVM.
- Robust: Provides strong memory management and exception handling.
- Multithreaded: Supports concurrent execution of two or more threads.
- Secure: Includes security features like cryptography and runtime checks.
3. What is the difference between JDK, JRE, and JVM?
Answer:
- JDK (Java Development Kit): Includes tools for developing Java applications, like compilers and libraries.
- JRE (Java Runtime Environment): Provides libraries and JVM to run Java applications.
- JVM (Java Virtual Machine): Executes Java bytecode and ensures platform independence.
4. What is the difference between ==
and .equals()
in Java?
Answer:
==
: Compares memory references (address) of two objects..equals()
: Compares the content of two objects (e.g., strings).
5. What are the data types in Java?
Answer: Java has two types of data types:
- Primitive:
int
,float
,double
,boolean
,char
,byte
,short
,long
. - Non-Primitive: Arrays, Strings, Classes, Interfaces, etc.
6. What is the difference between Array
and ArrayList
?
Answer:
- Array: Fixed size, can store elements of the same type.
- ArrayList: Dynamic size, part of Java's Collection framework, can store objects.
7. What is an object in Java?
Answer: An object is an instance of a class that has states (fields/attributes) and behaviors (methods). For example, a car object has attributes like color and speed, and methods like drive and brake.
8. What is a constructor in Java?
Answer:
A constructor is a special method used to initialize objects. It has the same name as the class and no return type.
Example:
9. What is the difference between abstract
class and interface
?
Answer:
- Abstract Class: Can have both abstract and concrete methods. Supports inheritance (one class).
- Interface: All methods are abstract by default (before Java 8). A class can implement multiple interfaces.
10. What is the purpose of the static
keyword in Java?
Answer:
The static
keyword is used to define members (variables or methods) that belong to the class rather than a specific instance.
Example:
These are fundamental questions often asked in beginner-level Java interviews.
Comments
Post a Comment