Skip to content

Commit 5496ae6

Browse files
committed
Update java-multiple-choice-questions-answers.md
1 parent a640c6c commit 5496ae6

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

java-multiple-choice-questions-answers.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
## Java Multiple Choice Questions
1+
# Java Multiple Choice Questions
22

33

4-
#### Q. Which of the follwing below live on the heap in java?
4+
## Q. Which of the follwing below live on the heap in java?
55
* Class
66
* Instance variable
77
* Method
88
* Object
99
```
1010
Object
1111
```
12-
#### Q. Which of the following interface is used to declare core methods in java?
12+
## Q. Which of the following interface is used to declare core methods in java?
1313
* Set
1414
* EventListner
1515
* Collection
1616
* Comparator
1717
```
1818
Collection
1919
```
20-
#### Q. Which of these interface handle sequences?
20+
## Q. Which of these interface handle sequences?
2121
* Set
2222
* List
2323
* Comparator
2424
* Collection
2525
```
2626
List
2727
```
28-
#### Q. Which of this interface must contain a unique element?
28+
## Q. Which of this interface must contain a unique element?
2929
* Set
3030
* List
3131
* Array
3232
* Collection
3333
```
3434
Set
3535
```
36-
#### Q. Which of the following declarations does not compile?
36+
## Q. Which of the following declarations does not compile?
3737
A. double num1, int num2 = 0;
3838
B. int num1, num2;
3939
C. int num1, num2 = 0;
4040
D. int num1 = 0, num2 = 0;
4141
```
4242
A. double num1, int num2 = 0;
4343
```
44-
#### Q. What is the output of following program?
44+
## Q. What is the output of following program?
4545
```java
4646
public class Test {
4747

@@ -64,7 +64,7 @@ Output
6464
<b><a href="#">↥ back to top</a></b>
6565
</div>
6666

67-
#### Q. What is the output of following program?
67+
## Q. What is the output of following program?
6868
```java
6969
import java.util.ArrayList;
7070
public class Test {
@@ -83,7 +83,7 @@ Output
8383
```
8484
[1, 1, 1]
8585
```
86-
#### Q. What is the output of following program?
86+
## Q. What is the output of following program?
8787
```java
8888
public class Test {
8989

@@ -101,15 +101,15 @@ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
101101

102102
at Test.main(Test.java:5)
103103
```
104-
#### Q. Which statement about a valid .java file is true?
104+
## Q. Which statement about a valid .java file is true?
105105
**A.** It can only contain one class declaration.
106106
**B.** It can contain one pulic class declaration and one public interface definition.
107107
**C.** It must define at least one public class.
108108
**D.** It may define at most one public class.
109109
```
110110
Answer: D
111111
```
112-
#### Q. What is the output of following program?
112+
## Q. What is the output of following program?
113113
```java
114114
public class Test {
115115

@@ -134,7 +134,7 @@ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
134134
<b><a href="#">↥ back to top</a></b>
135135
</div>
136136

137-
#### Q. What is the output of following program?
137+
## Q. What is the output of following program?
138138
```java
139139
public class Test{
140140

@@ -157,7 +157,7 @@ Output
157157
```
158158
[2 5]
159159
```
160-
#### Q. What is the output of following program?
160+
## Q. What is the output of following program?
161161
```java
162162
public class Test
163163
{
@@ -183,7 +183,7 @@ Exception in thread "main" java.lang.Error: Unresolved compilation problem:
183183
<b><a href="#">↥ back to top</a></b>
184184
</div>
185185

186-
#### Q. What is the output of following program?
186+
## Q. What is the output of following program?
187187
```java
188188
public class Test
189189
{
@@ -214,7 +214,7 @@ Output
214214
a : 2
215215
J : 2
216216
```
217-
#### Q. Which of the following declarations does not compile?
217+
## Q. Which of the following declarations does not compile?
218218
A. double num1, int num2 = 0;
219219
B. int num1, num2;
220220
C. int num1, num2 = 0;
@@ -224,7 +224,7 @@ A. double num1, int num2 = 0;
224224
```
225225
**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration.
226226

227-
#### Q. What is the output of the following?
227+
## Q. What is the output of the following?
228228
```java
229229
public static void main(String... args) {
230230
String chair, table = "metal";
@@ -245,15 +245,15 @@ D. The code does not compile
245245
<b><a href="#">↥ back to top</a></b>
246246
</div>
247247

248-
#### Q. Which is correct about an instance variable of type String?
248+
## Q. Which is correct about an instance variable of type String?
249249
A. It defaults to an empty string.
250250
B. It defaults to null.
251251
C. It does not have a default value.
252252
D. It will not compile without initializing on the declaration line
253253
```
254254
B. It defaults to null.
255255
```
256-
#### Q. How many of the following methods compile?
256+
## Q. How many of the following methods compile?
257257
```java
258258
public class Test
259259
{
@@ -282,15 +282,15 @@ C. Two
282282
```
283283
**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.
284284

285-
#### Q. Which of the following does not compile?
285+
## Q. Which of the following does not compile?
286286
A. int num = 999;
287287
B. int num = 9_9_9;
288288
C. int num = _9_99;
289289
D. None of the above; they all compile.
290290
```
291291
C. int num = _9_99;
292292
```
293-
#### Q. Which is the first line to trigger a compiler error?
293+
## Q. Which is the first line to trigger a compiler error?
294294
```java
295295
double d1 = 5f; // p1
296296
double d2 = 5.0; // p2
@@ -309,7 +309,7 @@ D. P4
309309
<b><a href="#">↥ back to top</a></b>
310310
</div>
311311

312-
#### Q. How many instance initializers are in this code?
312+
## Q. How many instance initializers are in this code?
313313
```java
314314
public class Bowling {
315315
{
@@ -333,7 +333,7 @@ D. Three
333333
```
334334
C. Two
335335
```
336-
#### Q. What is true of the finalize() method?
336+
## Q. What is true of the finalize() method?
337337
A. It may be called zero or one times.
338338
B. It may be called zero or more times.
339339
C. It will be called exactly once.
@@ -343,15 +343,15 @@ A. It may be called zero or one times.
343343
```
344344
**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.
345345

346-
#### Q. Which of the following is true about primitives?
346+
## Q. Which of the following is true about primitives?
347347
A. You can call methods on a primitive.
348348
B. You can convert a primitive to a wrapper class object simply by assigning it.
349349
C. You can convert a wrapper class object to a primitive by calling valueOf().
350350
D. You can store a primitive directly into an ArrayList.
351351
```
352352
B. You can convert a primitive to a wrapper class object simply by assigning it.
353353
```
354-
#### Q. What is the output of the following?
354+
## Q. What is the output of the following?
355355
```java
356356
Integer integer = new Integer(4);
357357
System.out.print(integer.byteValue());
@@ -370,7 +370,7 @@ C. The code does not compile.
370370
<b><a href="#">↥ back to top</a></b>
371371
</div>
372372

373-
#### Q. Which two primitives have wrapper classes that are not merely the name of the primitive with an uppercase letter?
373+
## Q. Which two primitives have wrapper classes that are not merely the name of the primitive with an uppercase letter?
374374
A. byte and char
375375
B. byte and int
376376
C. char and int
@@ -380,7 +380,7 @@ C. char and int
380380
```
381381
**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.
382382

383-
#### Q. How do you force garbage collection to occur at a certain point?
383+
## Q. How do you force garbage collection to occur at a certain point?
384384
A. Call System.forceGc()
385385
B. Call System.gc()
386386
C. Call System.requireGc()
@@ -390,7 +390,7 @@ D. None of the above
390390
```
391391
**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.
392392

393-
#### Q. How many of the String objects are eligible for garbage collection right before the end of the main method?
393+
## Q. How many of the String objects are eligible for garbage collection right before the end of the main method?
394394
```java
395395
public static void main(String[] fruits) {
396396
String fruit1 = new String("apple");
@@ -410,7 +410,7 @@ C. Two
410410
```
411411
**Explanation**: All three references point to the String apple. This makes the other two String objects eligible for garbage collection.
412412

413-
#### Q. Which of the following does not compile?
413+
## Q. Which of the following does not compile?
414414
A. double num = 2.718;
415415
B. double num = 2._718;
416416
C. double num = 2.7_1_8;
@@ -423,7 +423,7 @@ B. double num = 2._718;
423423
<b><a href="#">↥ back to top</a></b>
424424
</div>
425425

426-
#### Q. Which of the following is the output of this code, assuming it runs to completion?
426+
## Q. Which of the following is the output of this code, assuming it runs to completion?
427427
```java
428428
public class Toy {
429429
public void play() {
@@ -450,7 +450,7 @@ B. play-play-
450450
```
451451
**Explanation**: If there was a finalize() method, this would be a different story. However, the method here is finalizer. Tricky! That’s just a normal method that doesn’t get called automatically. Therefore clean is never output.
452452

453-
#### Q. What is the value of tip after executing the following code snippet?
453+
## Q. What is the value of tip after executing the following code snippet?
454454
```java
455455
int meal = 5;
456456
int tip = 2;
@@ -465,7 +465,7 @@ A. 1
465465
```
466466
**Explanation**: In ternary expressions, only one of the two right-most expressions are evaluated. Since `meal>6` is false, `––tip` is evaluated and `++tip` is skipped. The result is that `tip` is changed from 2 to 1.
467467

468-
#### Q. What is the output of the following application?
468+
## Q. What is the output of the following application?
469469
```java
470470
String john = "john";
471471
String jon = new String(john);

0 commit comments

Comments
 (0)