You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: java-multiple-choice-questions-answers.md
+16-20Lines changed: 16 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -209,10 +209,9 @@ C. int num1, num2 = 0;
209
209
D. int num1 = 0, num2 = 0;
210
210
```
211
211
A. double num1, int num2 = 0;
212
-
213
-
Explanation: A. Option A does not compile because Java does not allow declaring different types as
214
-
part of the same declaration.
215
212
```
213
+
**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration.
214
+
216
215
#### Q. What is the output of the following?
217
216
public static void main(String... args) {
218
217
String chair, table = "metal";
@@ -225,9 +224,9 @@ C. nullmetal
225
224
D. The code does not compile
226
225
```
227
226
D. The code does not compile
228
-
229
-
Explanation: The local variable chair may not have been initialized
230
227
```
228
+
**Explanation**: The local variable chair may not have been initialized
229
+
231
230
#### Q. Which is correct about an instance variable of type String?
232
231
A. It defaults to an empty string.
233
232
B. It defaults to null.
@@ -262,11 +261,9 @@ C. Two
262
261
D. Three
263
262
```
264
263
C. Two
265
-
266
-
Explanation: Objects have instance methods while primitives do not. Since int is a primitive, you
267
-
cannot call instance methods on it. Integer and String are both objects and have
268
-
instance methods
269
264
```
265
+
**Explanation**: Objects have instance methods while primitives do not. Since int is a primitive, you cannot call instance methods on it. Integer and String are both objects and have instance methods.
266
+
270
267
#### Q. Which of the following does not compile?
271
268
A. int num = 999;
272
269
B. int num = 9_9_9;
@@ -288,9 +285,9 @@ C. p3
288
285
D. p4
289
286
```
290
287
D. P4
291
-
292
-
Explanation: Type mismatch: cannot convert from double to float
293
288
```
289
+
**Explanation**: Type mismatch: cannot convert from double to float
290
+
294
291
#### Q. How many instance initializers are in this code?
295
292
```java
296
293
publicclassBowling {
@@ -322,10 +319,9 @@ C. It will be called exactly once.
322
319
D. It may be called one or more times.
323
320
```
324
321
A. It may be called zero or one times.
325
-
326
-
The finalize() method may not be called, such as if your program crashes.
327
-
However, it is guaranteed to be called no more than once.
328
322
```
323
+
**Explanation**: The `finalize()` method may not be called, such as if your program crashes. However, it is guaranteed to be called no more than once.
324
+
329
325
#### Q. Which of the following is true about primitives?
330
326
A. You can call methods on a primitive.
331
327
B. You can convert a primitive to a wrapper class object simply by assigning it.
@@ -356,19 +352,19 @@ C. char and int
356
352
D. None of the above
357
353
```
358
354
C. char and int
359
-
360
-
Explanation: The wrapper class for int is Integer and the wrapper class for char is Character. All other primitives have the same name. For example, the wrapper class for boolean is Boolean.
361
355
```
356
+
**Explanation**: The wrapper class for int is Integer and the wrapper class for char is Character. All other primitives have the same name. For example, the wrapper class for boolean is Boolean.
357
+
362
358
#### Q. How do you force garbage collection to occur at a certain point?
363
359
A. Call System.forceGc()
364
360
B. Call System.gc()
365
361
C. Call System.requireGc()
366
362
D. None of the above
367
363
```
368
364
D. None of the above
369
-
370
-
Explanation: While you can suggest to the JVM that it might want to run a garbage collection cycle, the JVM is free to ignore your suggestion.
371
365
```
366
+
**Explanation**: While you can suggest to the JVM that it might want to run a garbage collection cycle, the JVM is free to ignore your suggestion.
367
+
372
368
#### Q. How many of the String objects are eligible for garbage collection right before the end of the main method?
373
369
```java
374
370
publicstaticvoid main(String[] fruits) {
@@ -386,9 +382,9 @@ C. Two
386
382
D. Three
387
383
```
388
384
C. Two
389
-
390
-
Explanation: All three references point to the String apple. This makes the other two String objects eligible for garbage collection.
391
385
```
386
+
**Explanation**: All three references point to the String apple. This makes the other two String objects eligible for garbage collection.
0 commit comments