Java Interview Questions and Answers for Freshers
The Java questions asked most in campus drives and technical rounds at service and product companies, each with the answer and a short explanation. Includes 'what does this code print?' questions.
Q1. Which of these is NOT a primitive type in Java?
- A. int
- B. String
- C. boolean
- D. char
Show answerHide answer
Answer: B. String
String is a class (an object type); the eight primitives are byte, short, int, long, float, double, char and boolean.Q2. What is the default value of an int field in a Java class?
- A. 0
- B. Garbage value
- C. null
- D. undefined
Show answerHide answer
Answer: A. 0
Fields get default values: 0 for numbers, false for boolean, null for objects. Local variables have no default and must be initialised.Q3. Which keyword is used to inherit a class in Java?
- A. super
- B. inherits
- C. extends
- D. implements
Show answerHide answer
Answer: C. extends
class B extends A. A class implements interfaces.Q4. How many classes can a Java class directly extend?
- A. None
- B. Two
- C. Any number
- D. One
Show answerHide answer
Answer: D. One
Java has single inheritance for classes; use interfaces for multiple types.Q5. What does JVM stand for?
- A. Joint Virtual Memory
- B. Java Verified Module
- C. Java Variable Method
- D. Java Virtual Machine
Show answerHide answer
Answer: D. Java Virtual Machine
The JVM runs compiled bytecode, which is why Java is 'write once, run anywhere'.Q6. Which method must every Java application have to start running?
- A. public void start()
- B. public static void main(String[] args)
- C. public static run()
- D. static int main()
Show answerHide answer
Answer: B. public static void main(String[] args)
The JVM looks for public static void main(String[] args).Q7. What is the difference between == and equals() for Strings?
- A. == compares references, equals() compares contents
- B. equals() only works for numbers
- C. They are the same
- D. == compares contents, equals() compares references
Show answerHide answer
Answer: A. == compares references, equals() compares contents
Two different String objects with the same text are equal() but not ==.Q8. Which collection does NOT allow duplicate elements?
- A. ArrayList
- B. HashSet
- C. Vector
- D. LinkedList
Show answerHide answer
Answer: B. HashSet
A Set keeps unique elements.Q9. What happens when you access index 5 of an array of length 5?
- A. It returns 0
- B. It returns null
- C. ArrayIndexOutOfBoundsException
- D. Compile error
Show answerHide answer
Answer: C. ArrayIndexOutOfBoundsException
Valid indexes are 0 to 4; Java checks bounds at run time.Q10. Which keyword prevents a method from being overridden?
- A. static
- B. const
- C. private
- D. final
Show answerHide answer
Answer: D. final
A final method can't be overridden; a final class can't be extended.Q11. What is method overloading?
- A. A child class redefining a parent method
- B. Calling a method too many times
- C. A method that returns many values
- D. Same method name with different parameters in one class
Show answerHide answer
Answer: D. Same method name with different parameters in one class
Overloading is resolved at compile time; overriding happens in subclasses and is resolved at run time.Q12. Which of these is a checked exception?
- A. IOException
- B. NullPointerException
- C. ArithmeticException
- D. ArrayIndexOutOfBoundsException
Show answerHide answer
Answer: A. IOException
Checked exceptions must be caught or declared with throws; the others are RuntimeExceptions.Q13. Can an interface have a method with a body in Java 8 and later?
- A. Only private methods
- B. Yes, default and static methods
- C. Only if the interface is abstract
- D. No, never
Show answerHide answer
Answer: B. Yes, default and static methods
Java 8 added default and static methods to interfaces.Q14. Why are Java Strings immutable?
- A. Because they are primitives
- B. For security, thread safety and the string pool
- C. They are not immutable
- D. To save typing
Show answerHide answer
Answer: B. For security, thread safety and the string pool
Immutable strings can be safely shared and cached in the string pool.Q15. Which class should you use to build a long string inside a loop?
- A. StringBuilder
- B. Character
- C. String
- D. Scanner
Show answerHide answer
Answer: A. StringBuilder
StringBuilder appends without creating a new object each time.Q16. What does the static keyword mean for a field?
- A. The field is stored on the stack
- B. The field is private
- C. One copy is shared by all objects of the class
- D. The value can never change
Show answerHide answer
Answer: C. One copy is shared by all objects of the class
Static members belong to the class, not to individual objects.Q17. Which access modifier makes a member visible only inside its own class?
- A. protected
- B. private
- C. default (no modifier)
- D. public
Show answerHide answer
Answer: B. private
private is the most restrictive; protected adds subclasses and the package.Q18. What is the time complexity of get(key) in a HashMap on average?
- A. O(n log n)
- B. O(1)
- C. O(log n)
- D. O(n)
Show answerHide answer
Answer: B. O(1)
Hashing finds the bucket directly; worst case with many collisions is slower.Q19. What is garbage collection in Java?
- A. Removing duplicate elements from a list
- B. Automatic freeing of objects that are no longer referenced
- C. Clearing the console
- D. Deleting unused source files
Show answerHide answer
Answer: B. Automatic freeing of objects that are no longer referenced
The JVM reclaims memory automatically; there is no delete in Java.Q20. Which keyword refers to the current object inside a method?
- A. super
- B. current
- C. self
- D. this
Show answerHide answer
Answer: D. this
this.name = name; assigns the parameter to the field.Q21. What is the parent class of every class in Java?
- A. Main
- B. Class
- C. Base
- D. Object
Show answerHide answer
Answer: D. Object
Every class extends java.lang.Object directly or indirectly.Q22. Which of these can NOT be instantiated with new?
- A. A class with a private field
- B. A class with static methods
- C. A final class
- D. An abstract class
Show answerHide answer
Answer: D. An abstract class
Abstract classes exist to be extended; create objects of their concrete subclasses.Q23. What does this code print?
int a = 5, b = 2; System.out.println(a / b);
- A. 2
- B. 3
- C. 2.0
- D. 2.5
Show answerHide answer
Answer: A. 2
Both operands are int, so / does integer division and drops the .5.Q24. What does this code print?
System.out.println("" + 10 + 20);- A. Error
- B. 10 20
- C. 1020
- D. 30
Show answerHide answer
Answer: C. 1020
The String comes first, so + joins text: "" + 10 + 20 = "1020".Q25. What does this code print?
System.out.println(10 + 20 + "Java");
- A. 1020Java
- B. Java30
- C. Java1020
- D. 30Java
Show answerHide answer
Answer: D. 30Java
Evaluated left to right: 10 + 20 = 30 first, then joined with "Java".Q26. What does this code print?
String a = "hi"; String b = new String("hi"); System.out.println((a == b) + " " + a.equals(b));- A. true false
- B. false true
- C. false false
- D. true true
Show answerHide answer
Answer: B. false true
new String creates a different object, so == is false; equals() compares the text.Q27. What does this code print?
int x = 5; int y = x++; System.out.println(y + " " + x);
- A. 5 5
- B. 6 6
- C. 6 5
- D. 5 6
Show answerHide answer
Answer: D. 5 6
x++ gives the old value (5) and then increments x to 6.Q28. What does this code print?
System.out.println("abc".charAt(1));- A. a
- B. ab
- C. b
- D. Error
Show answerHide answer
Answer: C. b
charAt counts from 0: index 1 is 'b'.Q29. What does this code print?
int[] a = {1, 2, 3}; System.out.println(a.length);- A. Error
- B. 2
- C. 3
- D. 4
Show answerHide answer
Answer: C. 3
length is a field for arrays: {1, 2, 3} has 3 elements.Q30. What does this code print?
int n = 2; switch (n) { case 1: System.out.print("One "); case 2: System.out.print("Two "); case 3: System.out.print("Three"); break; default: System.out.print("Default"); }- A. Two Three
- B. Two Three Default
- C. Default
- D. Two
Show answerHide answer
Answer: A. Two Three
There is no break after case 2, so execution falls through into case 3, which does break.Q31. What does this code print?
java.util.List<Integer> list = new java.util.ArrayList<>(java.util.List.of(1, 2, 3)); list.remove(1); System.out.println(list);
- A. [1, 3]
- B. [1, 2]
- C. [3]
- D. [2, 3]
Show answerHide answer
Answer: A. [1, 3]
remove(1) with an int removes the element at index 1 (the value 2).Q32. What does this code print?
class Parent { String name() { return "Parent"; } } class Child extends Parent { String name() { return "Child"; } } public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.name()); } }- A. Child
- B. Parent
- C. Error
- D. ParentChild
Show answerHide answer
Answer: A. Child
The object is a Child, so the overridden method runs (runtime polymorphism).Q33. What does this code print?
public class Main { static String f() { try { return "done"; } finally { System.out.print("finally "); } } public static void main(String[] args) { System.out.println(f()); } }- A. finally
- B. Error
- C. done
- D. finally done
Show answerHide answer
Answer: D. finally done
finally runs before the method returns, even when try has a return.Q34. What does this code print?
class Counter { static int count = 0; Counter() { count++; } } public class Main { public static void main(String[] args) { new Counter(); new Counter(); System.out.println(Counter.count); } }- A. 1
- B. 0
- C. 3
- D. 2
Show answerHide answer
Answer: D. 2
static count is shared by every Counter object, and two were created.Q35. What does this code print?
public class Main { static void change(int v) { v = 20; } public static void main(String[] args) { int x = 10; change(x); System.out.println(x); } }- A. 10
- B. 0
- C. Error
- D. 20
Show answerHide answer
Answer: A. 10
Java passes a copy of the int value; changing it inside the method doesn't affect x.Q36. What does this code print?
public class Main { static void change(int[] a) { a[0] = 99; } public static void main(String[] args) { int[] arr = {1, 2}; change(arr); System.out.println(arr[0]); } }- A. 0
- B. 99
- C. 1
- D. Error
Show answerHide answer
Answer: B. 99
The array reference is copied, but it points to the same array, so the change is visible.Q37. What does this code print?
String s = "java"; String t = s.toUpperCase(); System.out.println(t + " " + s);
- A. JAVA JAVA
- B. JAVA java
- C. java java
- D. Java java
Show answerHide answer
Answer: B. JAVA java
toUpperCase returns a new String; s itself never changes.Q38. What does this code print?
for (int i = 0; i < 3; i++) System.out.print(i + (i < 2 ? " " : ""));
- A. 1 2
- B. 0 1 2 3
- C. 0 1 2
- D. 1 2 3
Show answerHide answer
Answer: C. 0 1 2
The loop prints while i < 3: 0, 1, 2.Q39. What does this code print?
System.out.println(5.0 / 0);
- A. NaN
- B. ArithmeticException
- C. 0
- D. Infinity
Show answerHide answer
Answer: D. Infinity
Dividing a double by zero gives Infinity; only integer division by zero throws.Q40. What does this code print?
java.util.Map<String, Integer> m = new java.util.TreeMap<>(); m.put("b", 2); m.put("a", 1); System.out.println(m);- A. {b=2, a=1}
- B. [a, b]
- C. Error
- D. {a=1, b=2}
Show answerHide answer
Answer: D. {a=1, b=2}
A TreeMap keeps its keys sorted.
Practised these? Now prove it.
Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.
Start a mock test