JAVA MCQs
1. Which of these is not a primitive data type in Java?
A) int
B) float
C) String
D) boolean
✅ Answer: C) String
2. Which data type is used to store a single character?
A) char
B) byte
C) string
D) character
✅ Answer: A) char
3. What is the default value of a byte variable?
A) 0
B) null
C) false
D) undefined
✅ Answer: A) 0
4. What is the size of a float in Java?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes
✅ Answer: B) 4 bytes
5. Which data type can store the largest integer value?
A) int
B) long
C) short
D) byte
✅ Answer: B) long
6. Which of the following is true about variables in Java?
A) Variables must be declared before use
B) Variables can change type at runtime
C) Variables need not be initialized before use
D) Variables cannot be reassigned
✅ Answer: A) Variables must be declared before use
7. Which keyword is used to define a constant in Java?
A) static
B) const
C) final
D) immutable
✅ Answer: C) final
8. What is the size of a double in Java?
A) 4 bytes
B) 8 bytes
C) 16 bytes
D) Platform dependent
✅ Answer: B) 8 bytes
9. Which data type would you use to store true/false values?
A) bit
B) boolean
C) int
D) char
✅ Answer: B) boolean
10. Which of these is a valid declaration?
A) int 1value;
B) int _value;
C) int $value;
D) Both B and C
✅ Answer: D) Both B and C
11. What is the range of a byte in Java?
A) –128 to 127
B) –255 to 255
C) –32768 to 32767
D) 0 to 255
✅ Answer: A) –128 to 127
12. Which of these can store Unicode characters?
A) byte
B) char
C) int
D) String
✅ Answer: B) char
13. Which of the following is a floating-point literal?
A) 5
B) 5.0f
C) 5L
D) ‘5’
✅ Answer: B) 5.0f
14. What is the default value of a char?
A) ‘0’
B) ‘\u0000’
C) null
D) space
✅ Answer: B) ‘\u0000’
15. Which keyword converts a primitive type to a string automatically?
A) cast
B) valueOf()
C) toString()
D) None (done by compiler)
✅ Answer: D) None (done automatically)
16. Which wrapper class corresponds to int?
A) Int
B) integer
C) Integer
D) Number
✅ Answer: C) Integer
17. Which statement declares a variable correctly?
A) int num = 10;
B) num int = 10;
C) 10 = int num;
D) declare int num;
✅ Answer: A) int num = 10;
18. Which conversion happens automatically in Java?
A) Narrowing
B) Widening
C) Explicit
D) Downcasting
✅ Answer: B) Widening
19. What happens if you assign a double to an int variable?
A) Automatic conversion
B) Compile-time error
C) Value rounded
D) Runtime exception
✅ Answer: B) Compile-time error
20. Which of the following can hold a decimal value?
A) byte
B) int
C) float
D) short
✅ Answer: C) float
21. Which of these literals is invalid?
A) 0x1A
B) 075
C) 0b1010
D) 12.3dF
✅ Answer: D) 12.3dF
22. What is the result of float f = 10/4;?
A) 2.5
B) 2.0
C) 2.0f
D) Error
✅ Answer: B) 2.0
23. What will float f = 10/4f; output when printed?
A) 2.0
B) 2.5
C) 2
D) Error
✅ Answer: B) 2.5
24. What does casting do?
A) Changes variable name
B) Converts data type
C) Copies variable
D) Removes memory
✅ Answer: B) Converts data type
25. Which operator is used for type casting?
A) (type)
B) cast()
C) as
D) convert()
✅ Answer: A) (type)
26. What is the output of System.out.println(10 + 20.0);?
A) 30
B) 30.0
C) Error
D) 1020.0
✅ Answer: B) 30.0
27. Which keyword prevents modification of a variable once assigned?
A) const
B) final
C) static
D) immutable
✅ Answer: B) final
28. What is the size of a short variable?
A) 8 bits
B) 16 bits
C) 32 bits
D) 64 bits
✅ Answer: B) 16 bits
29. Which of these is true about type casting?
A) Automatic from float to int
B) Automatic from int to float
C) Cannot convert char to int
D) Always loses data
✅ Answer: B) Automatic from int to float
30. Which of the following is a valid variable name?
A) 2value
B) $value
C) value#
D) @value
✅ Answer: B) $value
31. Which wrapper class corresponds to boolean?
A) Boolean
B) Bool
C) booleanWrapper
D) Logical
✅ Answer: A) Boolean
32. Which of these is used for memory-efficient storage of true/false?
A) int
B) char
C) boolean
D) bit
✅ Answer: C) boolean
33. Which statement about literals is true?
A) Literals must be variables
B) Literals are constant values
C) Literals change at runtime
D) Literals are only numeric
✅ Answer: B) Literals are constant values
34. What is the range of an int?
A) –128 to 127
B) –32,768 to 32,767
C) –2³¹ to 2³¹ – 1
D) 0 to 4,294,967,295
✅ Answer: C) –2³¹ to 2³¹ – 1
35. Which conversion must be done explicitly?
A) int → float
B) float → int
C) char → int
D) int → long
✅ Answer: B) float → int
36. Which data type has the smallest range?
A) byte
B) short
C) int
D) long
✅ Answer: A) byte
37. Which statement declares multiple variables?
A) int a, b, c;
B) int a b c;
C) declare int a, b;
D) int a; b; c;
✅ Answer: A) int a, b, c;
38. What is autoboxing?
A) Automatic conversion of primitives to wrapper objects
B) Converting class to object
C) Boxing algorithm
D) None
✅ Answer: A) Automatic conversion of primitives to wrapper objects
39. What is unboxing?
A) Converting wrapper to primitive
B) Converting primitive to wrapper
C) Casting float to int
D) None
✅ Answer: A) Converting wrapper to primitive
40. Which is valid code?
A) long x = 100L;
B) long x = 100l;
C) Both A and B
D) None
✅ Answer: C) Both A and B
41. What is the result of System.out.println('A' + 1);?
A) A1
B) 66
C) B
D) Error
✅ Answer: B) 66
42. Which variable is shared among all instances of a class?
A) final
B) static
C) instance
D) local
✅ Answer: B) static
43. Which of these is not a numeric type?
A) float
B) double
C) char
D) boolean
✅ Answer: D) boolean
44. Which statement is true about null?
A) null is a keyword
B) null can be assigned to any reference type
C) null can be assigned to primitive types
D) null equals “0”
✅ Answer: B) null can be assigned to any reference type
45. What happens when you assign null to a primitive variable?
A) It becomes 0
B) It becomes false
C) Compile-time error
D) Runtime exception
✅ Answer: C) Compile-time error
46. Which of these can be declared but not initialized immediately?
A) local variable
B) static variable
C) instance variable
D) both B and C
✅ Answer: D) both B and C
47. Which keyword is used to create an object?
A) new
B) create
C) construct
D) make
✅ Answer: A) new
48. What is the output of System.out.println(true + false);?
A) 1
B) truefalse
C) Error
D) 0
✅ Answer: C) Error
49. Which of the following can be used for large-precision decimal numbers?
A) float
B) double
C) BigDecimal
D) long
✅ Answer: C) BigDecimal
50. What is the wrapper class for char?
A) Character
B) Char
C) character
D) Ch
✅ Answer: A) Character
51. Which of the following is the correct way to declare multiple variables of the same type?
A) int a = 1, b = 2, c = 3;
B) int a; int b; int c;
C) int a b c;
D) Both A and B
✅ Answer: D) Both A and B
52. Which of the following statements about final variables is true?
A) They can be reassigned
B) Must be initialized once
C) Can change in loop
D) Cannot be primitive
✅ Answer: B) Must be initialized once
53. Which of the following types is 64-bit?
A) int
B) long
C) float
D) double
✅ Answer: B) long
54. Which literal type is default for floating-point numbers in Java?
A) float
B) double
C) long
D) int
✅ Answer: B) double
55. Which of the following declarations is invalid?
A) byte b = 127;
B) byte b = 128;
C) short s = 32767;
D) int i = 2147483647;
✅ Answer: B) byte b = 128;
56. Which of the following is true about wrapper classes?
A) They convert primitives to objects
B) They cannot store null
C) They are mutable
D) They replace primitive types
✅ Answer: A) They convert primitives to objects
57. Which method converts a string to an integer?
A) Integer.parseInt(String)
B) Integer.toInt(String)
C) parseInt(String)
D) Integer.valueOf(String)
✅ Answer: A) Integer.parseInt(String)
58. Which is correct for declaring a character literal?
A) char c = “A”;
B) char c = ‘A’;
C) char c = A;
D) char c = \u0041;
✅ Answer: B) char c = ‘A’;
59. Which of the following represents Unicode in Java?
A) ‘\u0041’
B) ‘\u41’
C) ‘\u004G’
D) ‘\u041’
✅ Answer: A) ‘\u0041’
60. Which of the following represents an octal literal in Java?
A) 010
B) 0x10
C) 10
D) 0b10
✅ Answer: A) 010
61. Which operator is used for left shift?
A) <<
B) >>
C) >>>
D) <<<<
✅ Answer: A) <<
62. Which operator is used for right shift with sign?
A) <<
B) >>
C) >>>
D) &
✅ Answer: B) >>
63. Which operator is used for unsigned right shift?
A) <<
B) >>
C) >>>
D) &
✅ Answer: C) >>>
64. What is the result of int x = 7 & 3;?
A) 3
B) 7
C) 1
D) 0
✅ Answer: C) 3
(7 → 0111, 3 → 0011, 0111 & 0011 = 0011 = 3)
65. What is the result of int x = 7 | 3;?
A) 3
B) 7
C) 15
D) 0
✅ Answer: B) 7
66. What is the result of int x = 7 ^ 3;?
A) 4
B) 3
C) 7
D) 0
✅ Answer: A) 4
67. Which operator is used for logical NOT?
A) !
B) ~
C) ^
D) &
✅ Answer: A) !
68. Which statement is correct about instanceof operator?
A) Checks object type at runtime
B) Converts object type
C) Compares primitive values
D) Throws exception
✅ Answer: A) Checks object type at runtime
69. Which of the following is a ternary operator?
A) ?:
B) ::
C) +
D) &&
✅ Answer: A) ?:
70. What is the output of System.out.println(5 + 2 * 3);?
A) 21
B) 11
C) 15
D) 17
✅ Answer: B) 11
71. Which operator is right-associative?
A) =
B) +
C) *
D) %
✅ Answer: A) =
72. Which operator cannot be applied to boolean?
A) &
B) ||
C) %
D) ^
✅ Answer: C) %
73. Which of the following is valid type casting?
A) int i = (int) 5.5;
B) float f = (float) 5;
C) double d = 10;
D) All of the above
✅ Answer: D) All of the above
74. Which of the following is widening conversion?
A) int → long
B) double → int
C) long → byte
D) float → char
✅ Answer: A) int → long
75. Which is narrowing conversion?
A) int → double
B) double → int
C) int → long
D) char → int
✅ Answer: B) double → int
76. Which wrapper class is for char?
A) Character
B) Char
C) char
D) CharacterWrapper
✅ Answer: A) Character
77. Which wrapper class is for boolean?
A) Boolean
B) Bool
C) booleanWrapper
D) Logical
✅ Answer: A) Boolean
78. Which method converts integer to string?
A) Integer.toString(int)
B) Integer.parseInt(int)
C) String.parseInt(int)
D) String.toInteger(int)
✅ Answer: A) Integer.toString(int)
79. What is the default value of float?
A) 0.0
B) 0.0f
C) 0
D) null
✅ Answer: B) 0.0f
80. What is the default value of double?
A) 0.0
B) 0.0d
C) 0
D) null
✅ Answer: A) 0.0
81. What is the default value of long?
A) 0
B) 0L
C) null
D) 0.0
✅ Answer: A) 0
82. Which of the following is a valid variable name?
A) _var1
B) $var
C) var_1
D) All of the above
✅ Answer: D) All of the above
83. Which of these is invalid variable name?
A) 1var
B) var1
C) _1var
D) $var
✅ Answer: A) 1var
84. Which of the following is mutable?
A) String
B) StringBuffer
C) Wrapper classes
D) All primitive types
✅ Answer: B) StringBuffer
85. Which is immutable?
A) String
B) StringBuilder
C) StringBuffer
D) All primitive types
✅ Answer: A) String
86. Which operator is used for addition assignment?
A) +=
B) ++
C) =
D) +
✅ Answer: A) +=
87. Which operator is used for subtraction assignment?
A) -=
B) —
C) =
D) –
✅ Answer: A) -=
88. Which operator is used for multiplication assignment?
A) *=
B) x=
C) =
D) *
✅ Answer: A) *=
89. Which operator is used for division assignment?
A) /=
B) /
C) =
D) %
✅ Answer: A) /=
90. Which operator is used for remainder assignment?
A) %=
B) /
C) *
D) +
✅ Answer: A) %=
91. What is the output of int x=10; System.out.println(x++);?
A) 10
B) 11
C) 9
D) Error
✅ Answer: A) 10
92. What is the output of int x=10; System.out.println(++x);?
A) 10
B) 11
C) 9
D) Error
✅ Answer: B) 11
93. What is the output of int x=5; int y=3; System.out.println(x>y);?
A) true
B) false
C) 1
D) 0
✅ Answer: A) true
94. What is the output of int x=5; int y=3; System.out.println(x<y);?
A) true
B) false
C) 1
D) 0
✅ Answer: B) false
95. What is the output of int x=5; int y=5; System.out.println(x==y);?
A) true
B) false
C) 1
D) 0
✅ Answer: A) true
96. Which of these is an invalid literal?
A) 0b1010
B) 012
C) 0x1A
D) 08
✅ Answer: D) 08 (invalid octal)
97. Which of these is a valid floating literal?
A) 3.14f
B) 3.14d
C) 3.14
D) All of the above
✅ Answer: D) All of the above
98. Which of these can store a single Unicode character?
A) char
B) String
C) byte
D) int
✅ Answer: A) char
99. Which of these cannot be used as identifier?
A) class
B) _class
C) $class
D) class1
✅ Answer: A) class
100. Which statement about primitive types is true?
A) They are objects
B) They have wrapper classes
C) They are mutable
D) Cannot store numeric values
✅ Answer: B) They have wrapper classes
101. Which of the following is the modulus operator in Java?
A) %
B) /
C) *
D) &
✅ Answer: A) %
102. What is the result of 5 + 3 * 2?
A) 16
B) 11
C) 10
D) 13
✅ Answer: B) 11
103. Which operator has the highest precedence in Java?
A) +
B) *
C) ++ (increment)
D) =
✅ Answer: C) ++ (increment)
104. Which of the following is a relational operator?
A) +
B) <
C) &
D) ||
✅ Answer: B) <
105. What is the result of 10 / 4 in Java?
A) 2.5
B) 2
C) 2.0
D) 0
✅ Answer: B) 2
106. Which operator is used for bitwise AND?
A) &&
B) &
C) |
D) ~
✅ Answer: B) &
107. Which operator is used to compare object references, not values?
A) ==
B) equals()
C) !=
D) =
✅ Answer: A) ==
108. What is the output of int a = 5; a++; System.out.println(a);?
A) 5
B) 6
C) 4
D) Error
✅ Answer: B) 6
109. What is the difference between ++i and i++?
A) No difference
B) ++i increments before use, i++ increments after use
C) i++ increments before use, ++i increments after use
D) ++i is illegal
✅ Answer: B) ++i increments before use, i++ increments after use
110. Which operator is used to check if two values are not equal?
A) !=
B) <>
C) !
D) =!
✅ Answer: A) !=
111. What is the output of System.out.println(5 & 3);?
A) 1
B) 7
C) 0
D) 2
✅ Answer: D) 1
112. Which operator is used for logical OR?
A) |
B) ||
C) &
D) &&
✅ Answer: B) ||
113. Which operator is used for conditional (ternary) operation?
A) ? :
B) ::
C) &&
D) %
✅ Answer: A) ? :
114. What is the output of System.out.println(true && false);?
A) true
B) false
C) 1
D) 0
✅ Answer: B) false
115. What is the output of System.out.println(true | false);?
A) true
B) false
C) 1
D) 0
✅ Answer: A) true
116. Which operator can be used for both arithmetic and string concatenation?
A) +
B) *
C) /
D) %
✅ Answer: A) +
117. What is the output of int x = 10; int y = 3; System.out.println(x % y);?
A) 1
B) 0
C) 3
D) 10
✅ Answer: A) 1
118. Which operator performs integer division?
A) /
B) %
C) *
D) //
✅ Answer: A) /
119. What is the result of ~5?
A) -6
B) 6
C) -5
D) 5
✅ Answer: A) -6
120. Which operator shifts bits to the left?
A) <<
B) >>
C) >>>
D) >>>>
✅ Answer: A) <<
121. Which operator shifts bits to the right with sign extension?
A) <<
B) >>
C) >>>
D) &
✅ Answer: B) >>
122. Which operator shifts bits to the right without sign extension?
A) >>
B) >>>
C) <<
D) &
✅ Answer: B) >>>
123. Which operator in Java is used for assignment?
A) =
B) ==
C) :=
D) =>
✅ Answer: A) =
124. What is the output of int a = 5; int b = 2; System.out.println(a / b);?
A) 2.5
B) 2
C) 3
D) Error
✅ Answer: B) 2
125. What is the output of System.out.println(2 + 3 * 4);?
A) 20
B) 14
C) 24
D) 9
✅ Answer: B) 14
126. Which operator is used for bitwise XOR?
A) ^
B) &
C) |
D) %
✅ Answer: A) ^
127. Which operator performs logical NOT?
A) !
B) ~
C) ^
D) &&
✅ Answer: A) !
128. What is the output of System.out.println(10 > 5 ? "Yes" : "No");?
A) Yes
B) No
C) true
D) false
✅ Answer: A) Yes
129. Which of these is a unary operator?
A) +
B) –
C) ++
D) All of the above
✅ Answer: D) All of the above
130. Which of these operators is used to check instance type?
A) instanceof
B) typeof
C) isA
D) type
✅ Answer: A) instanceof
131. Which operator returns the remainder of a division?
A) /
B) %
C) &
D) ||
✅ Answer: B) %
132. Which operator is used for compound assignment?
A) +=
B) ++
C) -=
D) Both A and C
✅ Answer: D) Both A and C
133. Which operator has lowest precedence?
A) *
B) +
C) =
D) ++
✅ Answer: C) =
134. Which operator combines assignment and arithmetic?
A) +
B) +=
C) =
D) ++
✅ Answer: B) +=
135. What is the output of System.out.println(3 + 4 * 2 / (1 - 5) % 2);?
A) 1
B) 3
C) 2
D) 0
✅ Answer: D) 0
136. What is the result of true ^ false?
A) true
B) false
C) Error
D) 1
✅ Answer: A) true
137. Which operator is used for logical AND?
A) &
B) &&
C) |
D) ||
✅ Answer: B) &&
138. Which operator is used for post-increment?
A) ++i
B) i++
C) i–
D) –i
✅ Answer: B) i++
139. Which operator is used for pre-increment?
A) ++i
B) i++
C) i–
D) –i
✅ Answer: A) ++i
140. Which operator is used for inequality comparison?
A) !=
B) =!
C) <>
D) ==
✅ Answer: A) !=
141. Which operator in Java can concatenate strings?
A) &
B) +
C) *
D) %
✅ Answer: B) +
142. Which operator cannot be applied to boolean values?
A) &&
B) ||
C) %
D) !
✅ Answer: C) %
143. Which operator is used to access members of an object?
A) . (dot)
B) ->
C) ::
D) @
✅ Answer: A) . (dot)
144. What is the output of System.out.println(5 << 1);?
A) 5
B) 10
C) 2
D) 1
✅ Answer: B) 10
145. What is the output of System.out.println(10 >> 1);?
A) 5
B) 10
C) 2
D) 20
✅ Answer: A) 5
146. Which operator is used to cast types explicitly?
A) (type)
B) cast()
C) convert()
D) as
✅ Answer: A) (type)
147. Which operator checks if two references point to the same object?
A) equals()
B) ==
C) !=
D) instanceof
✅ Answer: B) ==
148. Which operator is used for unsigned right shift?
A) >>
B) >>>
C) <<
D) &
✅ Answer: B) >>>
149. What is the output of System.out.println(1 + 2 + "3");?
A) 6
B) 33
C) 33.0
D) 123
✅ Answer: D) 33
(Check carefully: 1+2=3, then concatenated with “3” → “33”)
150. Which of these operators is right-associative?
A) =
B) +
C) *
D) %
✅ Answer: A) =
✅ Set 3 Complete (Operators & Expressions 101–150)
If you want, I can immediately continue with Set 4: Control Statements (151–200) next.
Do you want me to do that now?
You said:
ChatGPT said:
Perfect! ✅ Let’s continue with Set 4: Control Statements (151–200) covering if, switch, loops, break/continue — all unique MCQs.
🧮 Set 4: Control Statements (50 MCQs, #151–200)
151. Which keyword is used to exit a loop immediately?
A) break
B) continue
C) exit
D) stop
✅ Answer: A) break
152. Which keyword skips the current iteration of a loop?
A) break
B) continue
C) skip
D) pass
✅ Answer: B) continue
153. Which of the following is a valid if statement?
A) if x > 5 { }
B) if (x > 5) { }
C) if x > 5 then { }
D) if (x > 5) then { }
✅ Answer: B) if (x > 5) { }
154. Which loop is guaranteed to execute at least once?
A) for
B) while
C) do-while
D) foreach
✅ Answer: C) do-while
155. Which statement is correct about the switch in Java?
A) It works with int, char, String, enum types
B) It works with double and float
C) It works only with integers
D) It works with boolean values
✅ Answer: A) It works with int, char, String, enum types
156. What is the output of:
int x = 10;
if (x > 5) System.out.println("Yes"); else System.out.println("No");
A) Yes
B) No
C) Error
D) Nothing
✅ Answer: A) Yes
157. Which statement is used to exit a switch case?
A) exit
B) break
C) continue
D) stop
✅ Answer: B) break
158. Which loop is used when the number of iterations is known?
A) while
B) do-while
C) for
D) if
✅ Answer: C) for
159. Which of the following is an infinite loop?
A) for(;;) { }
B) while(true) { }
C) do { } while(true);
D) All of the above
✅ Answer: D) All of the above
160. Which statement about break is true?
A) It terminates the current loop or switch
B) It skips current iteration
C) It ends program
D) It pauses loop
✅ Answer: A) It terminates the current loop or switch
161. Which statement about continue is true?
A) Skips the current iteration and continues the loop
B) Terminates the loop
C) Exits the program
D) Does nothing
✅ Answer: A) Skips the current iteration and continues the loop
162. What is the output?
int x = 0;
while(x < 5) {
x++;
if(x == 3) break;
}
System.out.println(x);
A) 2
B) 3
C) 5
D) 0
✅ Answer: B) 3
163. What is the output?
for(int i=0;i<5;i++){
if(i==2) continue;
System.out.print(i);
}
A) 01234
B) 0134
C) 1234
D) 124
✅ Answer: B) 0134
164. Which of these is valid switch syntax?
A) switch(x) { case 1: break; }
B) switch x { case 1: }
C) switch(x) case 1: break;
D) switch(x) { case 1 break; }
✅ Answer: A) switch(x) { case 1: break; }
165. Which of the following is correct for nested if?
A) if(a>5) if(b>3) System.out.println(“Yes”);
B) if(a>5) if b>3 System.out.println(“Yes”);
C) if(a>5) then if(b>3) System.out.println(“Yes”);
D) if a>5 if(b>3) print “Yes”;
✅ Answer: A) if(a>5) if(b>3) System.out.println(“Yes”);
166. Which loop checks condition before executing loop body?
A) for
B) while
C) do-while
D) foreach
✅ Answer: B) while
167. Which loop checks condition after executing loop body?
A) for
B) while
C) do-while
D) foreach
✅ Answer: C) do-while
168. What is the output?
int i=1;
while(i<=3){
System.out.print(i);
i++;
}
A) 123
B) 12
C) 1
D) 0
✅ Answer: A) 123
169. Which is not a valid loop control statement?
A) break
B) continue
C) stop
D) return
✅ Answer: C) stop
170. Which is true about switch-case?
A) Cases can contain multiple statements
B) Cases must end with break (except last)
C) Default is optional
D) All of the above
✅ Answer: D) All of the above
171. What is the output?
int x=2;
switch(x){
case 1: System.out.println("1");
case 2: System.out.println("2");
case 3: System.out.println("3");
}
A) 2
B) 23
C) 123
D) Error
✅ Answer: B) 23 (fall-through without break)
172. Which of these is correct for infinite for-loop?
A) for(;;) {}
B) for(;true;) {}
C) for(int i=0;i>=0;i++) {}
D) All of the above
✅ Answer: D) All of the above
173. Which statement terminates current iteration and continues next?
A) break
B) continue
C) return
D) exit
✅ Answer: B) continue
174. Which loop is preferred when number of iterations is unknown?
A) for
B) while
C) do-while
D) foreach
✅ Answer: B) while
175. Which statement is true about do-while loop?
A) Executes body at least once
B) Executes body only if condition true
C) Executes infinite times
D) None of the above
✅ Answer: A) Executes body at least once
176. Which is correct nested loop syntax?
A) for(int i=0;i<3;i++){ for(int j=0;j<2;j++){ } }
B) for i in 0..3 do for j in 0..2 do {}
C) for(i=0;i<3;i++) for j=0;j<2;j++{}
D) None
✅ Answer: A) for(int i=0;i<3;i++){ for(int j=0;j<2;j++){ } }
177. Which is correct way to exit a method immediately?
A) break
B) continue
C) return
D) stop
✅ Answer: C) return
178. Which statement about loops is true?
A) Loops can be nested
B) Loops can be infinite
C) Loops can be controlled with break/continue
D) All of the above
✅ Answer: D) All of the above
179. What is the output?
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
System.out.print(i+j);
}
}
A) 001112
B) 011123
C) 012012
D) 001112
✅ Answer: D) 001112
180. Which is true about switch?
A) Can switch on String (Java 7+)
B) Can switch on double
C) Can switch on boolean
D) Cannot switch on char
✅ Answer: A) Can switch on String (Java 7+)
181. Which is valid while loop?
A) while(x<5) { }
B) while x<5 { }
C) while(x<5)
D) while x<5
✅ Answer: A) while(x<5) { }
182. Which is true about break inside nested loops?
A) Break exits only innermost loop
B) Break exits all loops
C) Break skips current iteration
D) Break does nothing
✅ Answer: A) Break exits only innermost loop
183. Which loop always checks condition first?
A) while
B) do-while
C) for
D) None
✅ Answer: A) while
184. Which statement about continue is false?
A) Skips remaining code of current iteration
B) Works with for, while, do-while loops
C) Terminates loop
D) Moves to next iteration
✅ Answer: C) Terminates loop
185. Which loop is suitable for iterating arrays?
A) for
B) while
C) do-while
D) All of the above
✅ Answer: D) All of the above
186. Which keyword can be used in labeled break?
A) break label;
B) continue label;
C) exit label;
D) stop label;
✅ Answer: A) break label;
187. Which keyword can be used in labeled continue?
A) break label;
B) continue label;
C) stop label;
D) exit label;
✅ Answer: B) continue label;
188. Which is the default case in switch?
A) Must be last
B) Optional
C) Cannot contain statements
D) Must contain break
✅ Answer: B) Optional
189. Which is invalid loop?
A) for(int i=0;;i++)
B) while(true)
C) do{}while(false)
D) for(int i=0;i<5;i++;)
✅ Answer: D) for(int i=0;i<5;i++;)
190. What is the output?
int x=1;
do {
System.out.print(x);
x++;
} while(x<4);
A) 123
B) 12
C) 1
D) 0
✅ Answer: A) 123
191. Which is correct for enhanced for-loop (foreach)?
A) for(int x: arr) { }
B) for(x in arr) { }
C) foreach(int x: arr) { }
D) for x: arr { }
✅ Answer: A) for(int x: arr) { }
192. Which statement about nested loops is true?
A) Inner loop completes for every outer loop iteration
B) Outer loop completes for every inner loop iteration
C) Nested loops are not allowed
D) Break in inner loop breaks outer loop automatically
✅ Answer: A) Inner loop completes for every outer loop iteration
193. Which statement about while loop is false?
A) Condition is checked before executing body
B) Can be infinite
C) Must execute at least once
D) Can use break/continue
✅ Answer: C) Must execute at least once
194. Which loop is guaranteed to execute at least once?
A) while
B) do-while
C) for
D) None
✅ Answer: B) do-while
195. Which statement is correct about switch fall-through?
A) Case executes until break is found
B) Case executes only one statement
C) Case cannot contain multiple statements
D) Case executes all cases automatically
✅ Answer: A) Case executes until break is found
196. Which statement is true for labeled break?
A) Exits specified loop
B) Skips iteration
C) Exits method
D) Pauses loop
✅ Answer: A) Exits specified loop
197. Which statement is true for labeled continue?
A) Skips to next iteration of specified loop
B) Terminates loop
C) Exits method
D) Pauses loop
✅ Answer: A) Skips to next iteration of specified loop
198. Which statement about loops is false?
A) Loops can be nested
B) Loops can have empty bodies
C) Loops cannot be infinite
D) Loops can use break/continue
✅ Answer: C) Loops cannot be infinite
199. Which loop uses an iterator internally?
A) Enhanced for-loop
B) Standard for
C) while
D) do-while
✅ Answer: A) Enhanced for-loop
200. What is the output?
int x=0;
for(; x<3;){
System.out.print(x);
x++;
}
A) 012
B) 123
C) 01
D) 0
✅ Answer: A) 012
(#201–250)
201. Which keyword is used to define a method in Java?
A) function
B) method
C) void / return type
D) define
✅ Answer: C) void / return type
202. Which of these is the correct method declaration?
A) void myMethod() { }
B) myMethod void() { }
C) function myMethod() { }
D) void: myMethod() { }
✅ Answer: A) void myMethod() { }
203. What is method overloading?
A) Defining multiple methods with same name but different parameters
B) Defining multiple classes with same method
C) Changing method body at runtime
D) None of the above
✅ Answer: A) Defining multiple methods with same name but different parameters
204. What is method overriding?
A) Same method in subclass with same signature as superclass
B) Multiple methods with same name in same class
C) Changing method return type only
D) Overloading a method
✅ Answer: A) Same method in subclass with same signature as superclass
205. What is the default return type if a method does not return any value?
A) int
B) void
C) null
D) None
✅ Answer: B) void
206. Which of these is true about static methods?
A) Can be called without creating an object
B) Cannot access instance variables directly
C) Can access static variables directly
D) All of the above
✅ Answer: D) All of the above
207. Which of these can access instance variables?
A) static method
B) instance method
C) main method
D) None
✅ Answer: B) instance method
208. Which of the following is correct to call a static method?
A) ClassName.methodName();
B) object.methodName();
C) methodName();
D) All of the above
✅ Answer: D) All of the above
209. Which of these is true about recursion?
A) Method calls itself
B) Must have a base condition
C) Can be direct or indirect
D) All of the above
✅ Answer: D) All of the above
210. Which statement is true about a method’s return type?
A) Must match method declaration
B) Can differ from method declaration
C) Void methods must return a value
D) None
✅ Answer: A) Must match method declaration
211. What is recursion?
A) A loop inside a method
B) A method calling itself
C) Overloading method
D) Overriding method
✅ Answer: B) A method calling itself
212. Which method is automatically called when an object is created?
A) finalize()
B) main()
C) constructor
D) init()
✅ Answer: C) constructor
213. Which keyword calls the superclass constructor?
A) super()
B) this()
C) parent()
D) base()
✅ Answer: A) super()
214. Which keyword refers to the current object?
A) current
B) this
C) self
D) object
✅ Answer: B) this
215. Which method signature is invalid for overloading?
A) void add(int a, int b)
B) void add(double a, double b)
C) int add(int x, int y)
D) void add(int a, int b)
✅ Answer: D) void add(int a, int b) (duplicate exact signature)
216. Which method is called by JVM first in a program?
A) static method
B) main()
C) constructor
D) finalize()
✅ Answer: B) main()
217. Which method cannot be overridden?
A) final method
B) static method
C) private method
D) All of the above
✅ Answer: D) All of the above
218. Which is the correct recursive method to calculate factorial?
A) int fact(int n) { if(n==0) return 1; return nfact(n-1); }
B) int fact(int n) { return nfact(n-1); }
C) int fact(int n) { if(n==1) return 0; return nfact(n-1); }
D) int fact(int n) { nfact(n-1); }
✅ Answer: A) int fact(int n) { if(n==0) return 1; return n*fact(n-1); }
219. Which method is called when an object is destroyed?
A) finalize()
B) destructor()
C) free()
D) delete()
✅ Answer: A) finalize()
220. What is tail recursion?
A) Recursive call is last statement of method
B) Recursive call is first statement
C) Multiple recursive calls
D) Recursion with loops
✅ Answer: A) Recursive call is last statement of method
221. Which is a non-recursive method to compute factorial?
A) Using loop
B) Using recursion
C) Using switch-case
D) None
✅ Answer: A) Using loop
222. Which of the following is true about recursion depth?
A) Must eventually reach base case
B) Can recurse infinitely without error
C) Is automatically optimized in Java
D) None
✅ Answer: A) Must eventually reach base case
223. Which method type cannot access instance variables directly?
A) static
B) instance
C) constructor
D) final
✅ Answer: A) static
224. Which is correct way to call instance method?
A) object.methodName();
B) ClassName.methodName();
C) methodName();
D) None
✅ Answer: A) object.methodName();
225. What happens if recursion does not have a base condition?
A) Compile-time error
B) Runtime error (StackOverflowError)
C) Executes once
D) JVM ignores
✅ Answer: B) Runtime error (StackOverflowError)
226. What is overloading determined by?
A) Method name only
B) Method name and parameter list
C) Return type
D) Access modifier
✅ Answer: B) Method name and parameter list
227. Can private methods be overloaded?
A) Yes
B) No
C) Only in subclass
D) Only if static
✅ Answer: A) Yes
228. Can private methods be overridden?
A) Yes
B) No
C) Only if final
D) Only if static
✅ Answer: B) No
229. Which of these is correct way to define recursive Fibonacci?
A) int fib(int n){ if(n<=1) return n; return fib(n-1)+fib(n-2); }
B) int fib(int n){ return fib(n-1)+fib(n-2); }
C) int fib(int n){ if(n==0) return 0; else return fib(n-1); }
D) int fib(int n){ return n+fib(n-1); }
✅ Answer: A) int fib(int n){ if(n<=1) return n; return fib(n-1)+fib(n-2); }
230. Which statement about methods is false?
A) Can have return type
B) Can have parameters
C) Must always return a value
D) Can be static or instance
✅ Answer: C) Must always return a value
231. Which keyword prevents method overriding?
A) final
B) static
C) private
D) abstract
✅ Answer: A) final
232. Which method cannot be static?
A) Instance methods
B) main()
C) finalize()
D) All methods can be static
✅ Answer: A) Instance methods
233. Which method allows multiple constructors in a class?
A) Constructor overloading
B) Method overloading
C) Constructor overriding
D) Method overriding
✅ Answer: A) Constructor overloading
234. Which is correct way to call a constructor from another constructor in same class?
A) this()
B) super()
C) parent()
D) base()
✅ Answer: A) this()
235. Which of the following is a valid method call?
A) obj.display();
B) display(); (from static context)
C) ClassName.display(); (static method)
D) Both A and C
✅ Answer: D) Both A and C
236. Which of these methods is abstract?
A) Declared without body
B) Declared with body
C) static method
D) final method
✅ Answer: A) Declared without body
237. Which of these can be recursive?
A) Static method
B) Instance method
C) Private method
D) All of the above
✅ Answer: D) All of the above
238. Which of the following is true about recursion?
A) Must reach base case
B) Stack is used
C) Can lead to StackOverflowError
D) All of the above
✅ Answer: D) All of the above
239. Can constructors be recursive?
A) Yes
B) No
C) Only if static
D) Only if final
✅ Answer: B) No
240. Which method is called before garbage collection?
A) finalize()
B) destructor()
C) free()
D) clean()
✅ Answer: A) finalize()
241. What is default access modifier for methods?
A) public
B) private
C) protected
D) package-private (default)
✅ Answer: D) package-private (default)
242. Can abstract methods be static?
A) No
B) Yes
C) Only in interfaces
D) Only in abstract classes
✅ Answer: A) No
243. Can abstract methods have body?
A) No
B) Yes
C) Only if static
D) Only if final
✅ Answer: A) No
244. Which of these statements about this is true?
A) Refers to current object
B) Can call current class constructor
C) Cannot be used in static method
D) All of the above
✅ Answer: D) All of the above
245. Which statement about super is true?
A) Refers to superclass
B) Can call superclass constructor
C) Can access superclass methods
D) All of the above
✅ Answer: D) All of the above
246. Which statement about method return type is correct?
A) Must match return statement
B) Can differ from return statement
C) Void methods can return a value
D) None
✅ Answer: A) Must match return statement
247. Which of the following can a static method access directly?
A) Static variables
B) Instance variables
C) Instance methods
D) None
✅ Answer: A) Static variables
248. Can final methods be overloaded?
A) Yes
B) No
C) Only in subclass
D) Only if static
✅ Answer: A) Yes
249. Can final methods be overridden?
A) No
B) Yes
C) Only in subclass
D) Only if static
✅ Answer: A) No
250. Which of these is true about recursion?
A) Direct or indirect
B) Must have base case
C) Can be optimized by JVM tail recursion (partially)
D) All of the above
✅ Answer: D) All of the above
251. Which of the following declares an array of integers?
A) int arr[5];
B) int[] arr = new int[5];
C) int arr();
D) array<int> arr = new int[5];
✅ Answer: B) int[] arr = new int[5];
252. What is the default value of an int array element?
A) 0
B) null
C) 0.0
D) Undefined
✅ Answer: A) 0
253. What is the default value of a boolean array element?
A) true
B) false
C) 0
D) null
✅ Answer: B) false
254. Which of these is valid array initialization?
A) int[] arr = {1, 2, 3};
B) int arr[] = new int[]{1, 2, 3};
C) int[] arr = new int[3]{1,2,3};
D) Both A and B
✅ Answer: D) Both A and B
255. How do you access the length of an array?
A) arr.length()
B) arr.size()
C) arr.length
D) arr.count()
✅ Answer: C) arr.length
256. Which of these is a correct way to declare a 2D array?
A) int[][] arr = new int[2][3];
B) int arr[][] = new int[2][3];
C) Both A and B
D) int arr[2][3];
✅ Answer: C) Both A and B
257. Which of the following is immutable in Java?
A) String
B) StringBuilder
C) StringBuffer
D) char[]
✅ Answer: A) String
258. Which class is mutable and thread-safe?
A) String
B) StringBuilder
C) StringBuffer
D) char[]
✅ Answer: C) StringBuffer
259. Which class is mutable but not thread-safe?
A) String
B) StringBuilder
C) StringBuffer
D) StringTokenizer
✅ Answer: B) StringBuilder
260. Which method returns the length of a string?
A) str.length()
B) str.size()
C) str.length
D) str.count()
✅ Answer: A) str.length()
261. Which method compares two strings ignoring case?
A) equals()
B) equalsIgnoreCase()
C) compareTo()
D) compareToIgnoreCase()
✅ Answer: B) equalsIgnoreCase()
262. Which method checks if a string starts with a specific prefix?
A) startsWith()
B) endsWith()
C) beginsWith()
D) prefix()
✅ Answer: A) startsWith()
263. Which method checks if a string ends with a specific suffix?
A) startsWith()
B) endsWith()
C) suffix()
D) finishWith()
✅ Answer: B) endsWith()
264. Which of the following is true about StringBuilder?
A) Mutable
B) Methods like append() modify original object
C) Faster than String concatenation in loop
D) All of the above
✅ Answer: D) All of the above
265. Which of the following is not a wrapper class?
A) Integer
B) Float
C) String
D) Character
✅ Answer: C) String
266. Which of the following is true about inheritance?
A) Supports code reusability
B) Allows method overriding
C) Can have single or multiple levels
D) All of the above
✅ Answer: D) All of the above
267. Which keyword is used to inherit a class?
A) implements
B) extends
C) inherits
D) super
✅ Answer: B) extends
268. Which keyword is used to implement an interface?
A) extends
B) implements
C) interface
D) inherit
✅ Answer: B) implements
269. Which of these is true about abstract classes?
A) Cannot be instantiated
B) Can have abstract methods
C) Can have concrete methods
D) All of the above
✅ Answer: D) All of the above
270. Which of these is true about interfaces?
A) Cannot contain instance variables
B) Methods are abstract by default
C) Supports multiple inheritance
D) All of the above
✅ Answer: D) All of the above
271. Which of these is correct about polymorphism?
A) Same method name, different behavior
B) Compile-time polymorphism: method overloading
C) Runtime polymorphism: method overriding
D) All of the above
✅ Answer: D) All of the above
272. Which access modifier allows subclass in different package to access members?
A) private
B) protected
C) public
D) default
✅ Answer: B) protected
273. Which exception is thrown for division by zero in integers?
A) ArithmeticException
B) NullPointerException
C) ArrayIndexOutOfBoundsException
D) NumberFormatException
✅ Answer: A) ArithmeticException
274. Which exception is thrown when array index is invalid?
A) ArrayIndexOutOfBoundsException
B) IndexOutOfBoundsException
C) NullPointerException
D) IllegalArgumentException
✅ Answer: A) ArrayIndexOutOfBoundsException
275. Which exception occurs when null reference is accessed?
A) NullPointerException
B) ArithmeticException
C) ClassCastException
D) ArrayIndexOutOfBoundsException
✅ Answer: A) NullPointerException
276. Which is a checked exception?
A) IOException
B) ArithmeticException
C) NullPointerException
D) ArrayIndexOutOfBoundsException
✅ Answer: A) IOException
277. Which is an unchecked exception?
A) IOException
B) ArithmeticException
C) FileNotFoundException
D) SQLException
✅ Answer: B) ArithmeticException
278. Which block is used to handle exceptions?
A) try
B) catch
C) finally
D) All of the above
✅ Answer: D) All of the above
279. Which block executes regardless of exception occurrence?
A) try
B) catch
C) finally
D) None
✅ Answer: C) finally
280. Which statement is true about throw?
A) Used to explicitly throw exception
B) Used to catch exception
C) Used to define exception
D) None
✅ Answer: A) Used to explicitly throw exception
281. Which statement is true about throws?
A) Declares exceptions a method can throw
B) Catches exceptions
C) Used to define custom exception
D) None
✅ Answer: A) Declares exceptions a method can throw
282. Which of these is true about encapsulation?
A) Hiding internal data
B) Accessed via getters/setters
C) Provides data security
D) All of the above
✅ Answer: D) All of the above
283. Which of these is true about constructor overloading?
A) Multiple constructors with different parameters
B) Constructors can be static
C) Must have same parameter list
D) None
✅ Answer: A) Multiple constructors with different parameters
284. Which keyword prevents inheritance?
A) final
B) abstract
C) private
D) static
✅ Answer: A) final
285. Which of these statements about instanceof is true?
A) Checks object type at runtime
B) Used for type casting
C) Converts object type
D) Throws exception
✅ Answer: A) Checks object type at runtime
286. Which of the following is true about multiple inheritance in Java?
A) Classes can extend multiple classes
B) Interfaces support multiple inheritance
C) Neither classes nor interfaces support multiple inheritance
D) Both classes and interfaces support multiple inheritance
✅ Answer: B) Interfaces support multiple inheritance
287. Which is correct way to handle multiple exceptions?
A) Multiple catch blocks
B) Single catch block with pipe (|)
C) Both A and B
D) None
✅ Answer: C) Both A and B
288. Which method in Object class is used for cloning?
A) clone()
B) copy()
C) duplicate()
D) replicate()
✅ Answer: A) clone()
289. Which method checks object equality based on content?
A) equals()
B) ==
C) compareTo()
D) hashCode()
✅ Answer: A) equals()
290. Which method returns hash code of an object?
A) hashCode()
B) equals()
C) compareTo()
D) identityHashCode()
✅ Answer: A) hashCode()
291. Which of these is true about abstract classes vs interfaces?
A) Abstract class can have instance variables
B) Interface cannot have instance variables
C) Abstract class can have constructors
D) All of the above
✅ Answer: D) All of the above
292. Which is true about final class?
A) Cannot be inherited
B) Can have methods
C) Can have variables
D) All of the above
✅ Answer: D) All of the above
293. Which statement about static block is true?
A) Executes once when class loads
B) Executes before main()
C) Used for initialization
D) All of the above
✅ Answer: D) All of the above
294. Which method is used to convert string to integer safely?
A) Integer.parseInt()
B) Integer.valueOf()
C) Both A and B
D) None
✅ Answer: C) Both A and B
295. Which keyword is used to define a custom exception?
A) class MyException extends Exception { }
B) class MyException implements Exception { }
C) exception MyException { }
D) throws MyException
✅ Answer: A) class MyException extends Exception { }
296. Which exception occurs during type casting of incompatible objects?
A) ClassCastException
B) IOException
C) ArithmeticException
D) NullPointerException
✅ Answer: A) ClassCastException
297. Which of the following converts string to char array?
A) str.toCharArray()
B) str.getChars()
C) str.chars()
D) All of the above
✅ Answer: D) All of the above
298. Which of the following copies one array to another?
A) System.arraycopy()
B) Arrays.copyOf()
C) Both A and B
D) clone()
✅ Answer: C) Both A and B
299. Which of these is correct to reverse a string?
A) new StringBuilder(str).reverse().toString()
B) str.reverse()
C) str.revert()
D) String.reverse(str)
✅ Answer: A) new StringBuilder(str).reverse().toString()
300. Which statement is true about exception hierarchy?
A) Throwable is superclass of all exceptions
B) Error and Exception are subclasses of Throwable
C) RuntimeException is unchecked
D) All of the above
✅ Answer: D) All of the above