Java Basics Interview Questions
40 essential Core Java interview questions covering fundamentals, JVM, data types, strings, and more.
40 essential Core Java interview questions covering fundamentals, JVM, data types, strings, and more. This interview-focused guide covers essential java basics interview questions concepts for technical interviews.
Java Basics Interview Questions
Master the fundamental building blocks of Java. These 40 questions cover everything from JVM architecture to string handling—the core concepts every Java developer must know.
1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now Oracle). It follows the principle of "Write Once, Run Anywhere" (WORA) because compiled Java bytecode runs on any system with a JVM.
[!NOTE] Key Features: Simple, Object-Oriented, Platform Independent, Secured, Robust, Architecture Neutral, Portable, High Performance, Multithreaded, Distributed.
2. What is JVM, JRE, and JDK?
- JVM (Java Virtual Machine): Executes Java bytecode. Provides runtime environment. Platform dependent.
- JRE (Java Runtime Environment): JVM + Core Libraries + Other components to run Java applications. Does NOT include compiler.
- JDK (Java Development Kit): JRE + Development tools (compiler
javac, debugger, etc.). Required for developing Java applications.
[!TIP] Interview Tip: Remember it as JDK ⊃ JRE ⊃ JVM. JDK contains JRE, JRE contains JVM.
3. Explain the Java compilation and execution process.
- Source Code: Write
.javafile - Compilation:
javaccompiles to.classfile (bytecode) - Class Loader: JVM loads
.classfile into memory - Bytecode Verification: Verifies code safety
- Execution: JVM interprets/runs bytecode (with JIT compilation for hot code)
4. Why is Java called platform-independent?
Java code compiles to bytecode (.class files), not machine-specific code. The JVM on each platform interprets this same bytecode, making Java platform-independent at the source/bytecode level. (The JVM itself is platform-dependent.)
5. What is the difference between JDK, JRE, and JVM?
| Feature | JDK | JRE | JVM | |---------|-----|-----|-----| | Full Form | Java Development Kit | Java Runtime Environment | Java Virtual Machine | | Purpose | Develop + Run | Run only | Executes bytecode | | Includes Compiler | Yes | No | No | | Platform Dependent | Yes | Yes | Yes |
6. What are the primitive data types in Java?
Java has 8 primitive data types:
- byte (1 byte, -128 to 127)
- short (2 bytes, -32,768 to 32,767)
- int (4 bytes, -2^31 to 2^31-1)
- long (8 bytes, -2^63 to 2^63-1)
- float (4 bytes, single-precision floating point)
- double (8 bytes, double-precision floating point)
- char (2 bytes, 0 to 65,535, Unicode)
- boolean (JVM-dependent, true/false)
[!IMPORTANT] All primitive types have fixed sizes regardless of the platform. This is a key reason Java is portable.
7. What is autoboxing and unboxing?
- Autoboxing: Automatic conversion of primitive type to its wrapper class object.
int→Integer - Unboxing: Automatic conversion of wrapper class object to primitive type.
Integer→int
Integer num = 100; // Autoboxing: int → Integer
int value = num; // Unboxing: Integer → int
8. What are wrapper classes?
Wrapper classes wrap primitive types into objects. Each primitive has a corresponding wrapper: Integer, Double, Boolean, Character, Byte, Short, Long, Float. They're needed for Collections (which only store objects) and provide utility methods.
9. What is a variable in Java?
A variable is a named memory location that stores a value. Java has three types:
- Local variables: Declared inside methods
- Instance variables: Declared in class but outside methods (non-static)
- Static variables: Declared with
statickeyword, shared across all instances
10. What are Java naming conventions?
- Classes/Interfaces: PascalCase (
MyClass,Runnable) - Methods/Variables: camelCase (
myMethod,firstName) - Constants: UPPER_SNAKE_CASE (
MAX_VALUE,PI) - Packages: lowercase (
com.example.myapp)
11. What is the difference between == and .equals()?
==compares references (memory addresses) for objects; compares values for primitives..equals()compares content/value of objects (when properly overridden).
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
12. What is a String in Java?
A String is a sequence of characters. In Java, String is an immutable object of the java.lang.String class. Once created, a String's value cannot be changed.
13. Why is String immutable in Java?
- Security: Used in class loading, network connections, file paths
- String Pool: Enables memory optimization through reuse
- Thread Safety: Immutable objects are inherently thread-safe
- HashMap Key: Consistent
hashCode()since value never changes
[!TIP] When you "modify" a String, Java actually creates a new String object. Use
StringBuilderfor frequent modifications.
14. What is the String Pool?
The String Pool (String Constant Pool) is a special memory area in the heap where String literals are stored. When you create a String literal, JVM checks the pool first—if it exists, the reference is reused rather than creating a new object.
String s1 = "Hello"; // Stored in String Pool
String s2 = "Hello"; // Same reference from pool (s1 == s2 is true)
String s3 = new String("Hello"); // New object in heap (not from pool)
15. What is the difference between String, StringBuilder, and StringBuffer?
| Feature | String | StringBuilder | StringBuffer | |---------|--------|---------------|--------------| | Mutability | Immutable | Mutable | Mutable | | Thread Safety | Immutable (safe) | Not thread-safe | Thread-safe (synchronized) | | Performance | Slow for concat | Fastest | Slower than StringBuilder | | Introduced | Java 1.0 | Java 5 | Java 1.0 |
16. How do you compare two Strings in Java?
Use equals() for content comparison, equalsIgnoreCase() for case-insensitive comparison, compareTo() for lexicographic ordering. Never use == for String content comparison.
17. What is the main method signature?
public static void main(String[] args)
- public: Accessible by JVM
- static: No object needed to call it
- void: Returns nothing
- String[] args: Command-line arguments
18. Can we have multiple main methods?
Yes, through method overloading. But JVM only calls public static void main(String[] args). Other main methods must be called explicitly.
19. What is System.out.println()?
- System: A final class in
java.lang - out: A static
PrintStreamfield of System (standard output) - println(): Method of PrintStream that prints with a newline
20. What are access modifiers in Java?
- private: Accessible only within the same class
- default (no modifier): Accessible within the same package
- protected: Accessible within same package + subclasses
- public: Accessible from anywhere
| Modifier | Class | Package | Subclass | World | |----------|-------|---------|----------|-------| | private | Yes | No | No | No | | default | Yes | Yes | No | No | | protected | Yes | Yes | Yes | No | | public | Yes | Yes | Yes | Yes |
21. What is a constructor?
A constructor is a special method used to initialize objects. It has the same name as the class, no return type, and is called automatically when an object is created using new. If no constructor is written, Java provides a default no-arg constructor.
22. What is constructor overloading?
Having multiple constructors in the same class with different parameter lists. Compiler differentiates them by number, type, and order of parameters.
class Student {
Student() { } // No-arg constructor
Student(String name) { } // One-arg constructor
Student(String name, int age) { } // Two-arg constructor
}
23. What is this keyword?
this refers to the current instance of the class. Used to:
- Refer to instance variables (when shadowed by parameters)
- Call another constructor (
this()) - Pass current object as parameter
- Return current object from method
24. What is super keyword?
super refers to the parent class instance. Used to:
- Access parent class methods/variables
- Call parent class constructor (
super()) - Must be the first statement in a constructor
25. What is the difference between this and super?
| this | super |
|--------|---------|
| Refers to current object | Refers to parent class object |
| Used to call current class constructor | Used to call parent class constructor |
| this() calls same-class constructor | super() calls parent constructor |
26. What is a static variable?
A static variable belongs to the class rather than any instance. It's shared across all objects, initialized once when the class loads, and accessed using the class name.
class Counter {
static int count = 0;
Counter() { count++; }
}
27. What is a static method?
A static method belongs to the class, not instances. It can only access static members directly, can't use this/super, and is called using ClassName.methodName().
28. What is a static block?
A static block runs once when the class is first loaded. Used to initialize static variables.
static {
System.out.println("Static block executed once");
}
29. Can we override static methods?
No. Static methods are bound at compile time (static binding). If a subclass defines a static method with the same signature, it's called method hiding, not overriding.
30. What is the final keyword?
final prevents modification:
- final variable: Cannot be reassigned (becomes constant)
- final method: Cannot be overridden
- final class: Cannot be subclassed
[!IMPORTANT] A final reference variable can't point to a different object, but the object's internal state CAN change (unless the object itself is immutable).
31. What is the difference between final, finally, and finalize()?
- final: Keyword for constants, non-overridable methods, non-inheritable classes
- finally: Block that always executes after try-catch (cleanup)
- finalize(): Method called by garbage collector before object destruction (deprecated in Java 9+)
32. What are arrays in Java?
An array is a container object holding a fixed number of values of a single type. Array size is fixed after creation. Arrays are objects in Java.
int[] numbers = new int[5]; // Declaration + initialization
int[] values = {1, 2, 3, 4, 5}; // Declaration + initialization with values
33. What is type casting?
Type casting converts one data type to another:
- Widening (Implicit): Smaller to larger type, automatic (
int→long) - Narrowing (Explicit): Larger to smaller type, requires cast (
double→int)
int num = 100;
long bigNum = num; // Widening (automatic)
int smallNum = (int) 9.99; // Narrowing (explicit) — result: 9
34. What is the instanceof operator?
instanceof checks if an object is an instance of a specific class/interface. Returns true/false. Commonly used before type casting.
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
}
35. What is a package in Java?
A package groups related classes and interfaces. Prevents naming conflicts, controls access, and organizes code. Packages are stored in directories matching their names.
package com.example.myapp;
import java.util.ArrayList;
36. What is the difference between import and package?
- package: Declares which package the current class belongs to (first line of file)
- import: Brings classes from other packages into scope for use
37. What is the import static statement?
import static imports static members (methods/variables) of a class, allowing them to be used without class qualification.
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
// Now use: double area = PI * r * r;
38. What is method overloading?
Method overloading is defining multiple methods with the same name but different parameters (number, type, or order). Return type alone is NOT sufficient for overloading. This is compile-time polymorphism.
39. Can the main method be overloaded?
Yes, you can have multiple main methods with different parameters. But JVM only calls main(String[] args).
40. What happens if the main method is not static?
The program will compile but throw a runtime error: Main method is not static. JVM needs to call main without creating an object, which is only possible for static methods.
AI Mentor
Confused about "Core Java basics interview questions covering JVM, data types, strings, access modifiers, and OOP fundamentals"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Which component actually executes Java bytecode?