Java course menu
Beginner Java Interview Questions and Answers
Common Java interview questions for freshers with clear answers and runnable examples: JDK vs JRE vs JVM, primitive vs reference types, == vs equals, String immutability, String vs StringBuilder, static, final, overloading vs overriding, and short coding questions.
On this page
- Concepts
- 1. What is the difference between the JDK, JRE and JVM?
- 2. What are primitive types and reference types?
- 3. What is the difference between == and equals()?
- 4. Why are Strings immutable in Java?
- 5. What is the difference between String, StringBuilder and StringBuffer?
- 6. What does static mean?
- 7. What does final mean on a variable, a method and a class?
- 8. What is the difference between method overloading and overriding?
- 9. Is Java pass by value or pass by reference?
- 10. What are the default values of fields?
- Short coding questions
- 11. Reverse a string.
- 12. Check whether a number is prime.
- 13. Find the largest element in an array.
- 14. Count the vowels in a string.
- Tips for beginner interviews
These questions come up in nearly every entry-level Java interview. Try answering each one out loud first, then open the solution. A clear explanation with a tiny example impresses more than a memorised definition.
Concepts#
1. What is the difference between the JDK, JRE and JVM?#
Show solution
- JVM (Java Virtual Machine): runs compiled Java bytecode, and JIT-compiles hot code to native machine code.
- JRE (Java Runtime Environment): the JVM plus the standard class libraries needed to run Java programs.
- JDK (Java Development Kit): the JRE plus development tools such as the
javaccompiler,javadocandjar. You need the JDK to write Java programs.
Source (.java) is compiled by javac to bytecode (.class), which any JVM can run: "write once, run anywhere".
2. What are primitive types and reference types?#
Show solution
Java has 8 primitive types (byte, short, int, long, float, double, char, boolean), whose variables hold the value directly and cannot be null. Everything else (String, arrays, every class) is a reference type: the variable holds a reference to an object on the heap, can be null, and has methods.
Each primitive has a wrapper class (int → Integer) used where objects are required, such as in collections.
3. What is the difference between == and equals()?#
Show solution
== compares values for primitives, but references (identity) for objects. equals() compares contents, as defined by the class.
public class Main {
public static void main(String[] args) {
String a = new String("java");
String b = new String("java");
System.out.println(a == b);
System.out.println(a.equals(b));
}
}false
trueAlways compare strings (and other objects) with equals.
4. Why are Strings immutable in Java?#
Show solution
A String's contents can never change; methods like toUpperCase() return a new string. Benefits:
- String pool: identical literals can share one object safely, saving memory.
- Security and reliability: values used as file names, URLs or map keys cannot be changed by other code.
- Thread safety: immutable objects can be shared between threads without locks.
- Cached hash codes: strings make fast, reliable
HashMapkeys.
5. What is the difference between String, StringBuilder and StringBuffer?#
Show solution
Use StringBuilder to build strings in loops; StringBuffer is a legacy synchronized version.
6. What does static mean?#
Show solution
A static member belongs to the class rather than to any object. A static field has one shared copy; a static method can be called on the class name (Math.max(2, 3)) without creating an object, but it cannot use instance fields or this. main is static so the JVM can call it before any object exists.
7. What does final mean on a variable, a method and a class?#
Show solution
finalvariable: can be assigned only once (for a reference, the variable cannot point elsewhere, but the object can still change).finalmethod: cannot be overridden by subclasses.finalclass: cannot be extended (for exampleString).
8. What is the difference between method overloading and overriding?#
Show solution
- Overloading: same method name, different parameter lists, usually in the same class; chosen at compile time from the argument types.
- Overriding: a subclass provides its own implementation of an inherited method with the same signature; chosen at run time from the actual object (dynamic dispatch). Mark it with
@Override.
9. Is Java pass by value or pass by reference?#
Show solution
Always pass by value. For primitives, the value is copied. For objects, the reference is copied, so a method can change the object's contents, but cannot make the caller's variable point to a different object.
import java.util.ArrayList;
import java.util.List;
public class Main {
static void modify(List<String> list) {
list.add("added"); // visible to the caller
list = new ArrayList<>(); // only changes the local copy of the reference
list.add("lost");
}
public static void main(String[] args) {
List<String> items = new ArrayList<>();
modify(items);
System.out.println(items);
}
}[added]10. What are the default values of fields?#
Show solution
Fields (class and instance variables) get default values: 0 for numeric types, 0.0 for double/float, false for boolean, '\u0000' for char, and null for references. Local variables get no default; the compiler rejects reading one before it is assigned.
Short coding questions#
11. Reverse a string.#
Show solution
public class Main {
public static void main(String[] args) {
String s = "interview";
String r1 = new StringBuilder(s).reverse().toString();
char[] chars = s.toCharArray();
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
char tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
String r2 = new String(chars);
System.out.println(r1 + " " + r2);
}
}weivretni weivretni12. Check whether a number is prime.#
Show solution
public class Main {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int d = 2; (long) d * d <= n; d++) {
if (n % d == 0) return false;
}
return true;
}
public static void main(String[] args) {
for (int n : new int[] {1, 2, 17, 21, 97}) {
System.out.println(n + (isPrime(n) ? " prime" : " not prime"));
}
}
}1 not prime
2 prime
17 prime
21 not prime
97 primeChecking divisors only up to √n makes it O(√n). The long cast prevents d * d from overflowing for large n.
13. Find the largest element in an array.#
Show hint
Track the best value so far, or use a stream.
Show solution
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] v = {12, 45, 7, 89, 23};
int largest = v[0];
for (int x : v) {
if (x > largest) largest = x;
}
System.out.println(largest + " " + Arrays.stream(v).max().getAsInt());
}
}89 89Mention the empty-array case: v[0] throws, and max() returns an empty OptionalInt.
14. Count the vowels in a string.#
Show solution
public class Main {
public static void main(String[] args) {
String text = "Java Interview Questions";
int count = 0;
for (char c : text.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) >= 0) count++;
}
System.out.println(count);
}
}10Tips for beginner interviews#
- Explain why, not only what: immutability, pass by value and
equalsvs==are about how Java treats objects. - State the time complexity of your code without being asked.
- Mention edge cases:
null, empty input, overflow. - Next, try the Intermediate questions.
Finished this lesson?