Java MCQs - Set 1 with Detailed Answers
1. 1. What is the result of the expression: 5 + 2 * 3 - 1 << 2?
Operator precedence: * > + > <<, so 2*3=6, 5+6=11, 11-1=10, then 10 << 2 = 40.
Answer: 40 (not listed; possibly a typo)
________________________________________
2. 2. Which design pattern ensures a class has only one instance?
Singleton ensures only one instance exists.
Answer: B) Singleton
________________________________________
3. 3. Code:
int x = 5;
int y = ++x * 2 + x--;
++x = 6, so 6*2 = 12, x-- = 6 (x becomes 5 after), 12+6 = 18.
Answer: C) 18
________________________________________
4. 4. String str1 = "Java"; String str2 = new String("Java");
System.out.println(str1 == str2);
str1 is interned, str2 is a new object, == compares reference.
Answer: B) false
________________________________________
5. 5. Which keyword is used to restrict a class from being subclassed?
final prevents inheritance.
Answer: B) final
________________________________________
6. 6. System.out.println(10 + 20 + "30" + 40 + 50);
10+20=30, then "30"→"3030", then +40 → "303040", +50 → "30304050".
Answer: A) 30304050
________________________________________
7. 7. int a = 5; int b = a++ + ++a;
a++=5 (a becomes 6), ++a=7 → 5+7=12.
Answer: B) 12
________________________________________
8. 8. What does volatile keyword ensure in Java?
Ensures visibility of changes across threads.
Answer: B) Visibility of changes across threads
________________________________________
9. 9. String s1 = "abc"; String s2 = "a" + "bc"; System.out.println(s1 == s2);
Both are compile-time constants, hence same reference.
Answer: A) true
________________________________________
10. 10. Which stream is used for reading primitive data types in Java?
DataInputStream reads primitives.
Answer: A) DataInputStream
________________________________________
11. 11. Which exception is thrown when accessing a null object?
Accessing a null reference throws NullPointerException.
Answer: C) NullPointerException
________________________________________
12. 12. int[] arr = new int[5]; System.out.println(arr[0]);
Default int array values are 0.
Answer: A) 0
________________________________________
13. 13. Time complexity of HashMap.get() in average case?
HashMap average case is O(1).
Answer: A) O(1)
________________________________________
14. 14. What happens if class overrides equals() but not hashCode()?
Breaks hashCode-equals contract; hash collections fail.
Answer: B) Hash-based collections may not work properly
________________________________________
15. 15. Annotation to override method?
@Override indicates method overrides superclass.
Answer: A) @Override
________________________________________
16. 16. String s = "abc"; s.concat("def"); System.out.println(s);
concat() returns new string, doesn't change original.
Answer: B) abc
________________________________________
17. 17. Integer i1 = 100; Integer i2 = 100; System.out.println(i1 == i2);
Values between -128 to 127 are cached, references same.
Answer: A) true
________________________________________
18. 18. Abstract class features?
Can have both abstract and non-abstract methods.
Answer: D) Can have both abstract and non-abstract methods
________________________________________
19. 19. What causes deadlock?
Nested synchronized blocks can cause deadlock.
Answer: D) Nested synchronized blocks
________________________________________
20. 20. String a = "hello"; String b = "he" + "llo"; System.out.println(a == b);
Both are compile-time constants, same reference.
Answer: A) true
________________________________________
21. 21. Concept of same method name, different params?
Overloading allows method name reuse with diff params.
Answer: A) Overloading
________________________________________
22. 22. Interface for comparison?
Comparator allows external comparison logic.
Answer: A) Comparator
________________________________________
23. 23. int a = 10; int b = 20; System.out.println(a+++b);
Parsed as (a++) + b → 10 + 20 = 30.
Answer: A) 30
________________________________________
24. 24. What happens when main() is private?
Compiles, but JVM can’t access → Runtime error.
Answer: C) Runtime error
________________________________________
25. 25. Cleanup before GC?
finalize() is called before object destruction.
Answer: B) finalize()
________________________________________
26. 26. Best collection for LIFO?
Stack follows LIFO.
Answer: B) Stack
________________________________________
27. 27. Keyword for exception handling?
try block starts exception handling.
Answer: D) try
________________________________________
28. 28. Immutable class?
String is immutable.
Answer: A) String
________________________________________
29. 29. Implements Map interface?
HashMap is a Map.
Answer: A) HashMap
________________________________________
30. 30. Access specifier for same package?
Default (no modifier) means package-private.
Answer: D) default
________________________________________