0% found this document useful (0 votes)
2 views

Java Code Questions

The document contains a series of multiple-choice questions related to Java programming, including code snippets and expected outputs. Each question provides options for answers along with explanations for the correct choice. The questions cover various topics such as conditional statements, loops, and the main method signature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Code Questions

The document contains a series of multiple-choice questions related to Java programming, including code snippets and expected outputs. Each question provides options for answers along with explanations for the correct choice. The questions cover various topics such as conditional statements, loops, and the main method signature.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Java Multiple Choice Questions

Q1. What will be the output of the following Java code?

```java
public class Test {
public static void main(String[] args) {
int a = 5;
int b = 10;
if (a < b) {
System.out.println("a is less than b");
} else {
System.out.println("a is not less than b");
}
}
}
```
A. a is not less than b
B. a is less than b
C. Compilation error
D. Runtime exception
Answer: a is less than b
Explanation: Since 5 < 10, the if condition is true and it prints 'a is less than b'.

Q2. Will the following Java code compile successfully?

```java

public class Demo {


public static void main(String[] args) {
int num = 10;
switch(num) {
case 10:
System.out.println("Ten");
case 20:
System.out.println("Twenty");
}
}
}
```
A. Yes, and it prints 'Ten'
B. Yes, and it prints 'Ten' and 'Twenty'
C. No, compilation error due to missing break
D. No, compilation error due to missing default
Answer: Yes, and it prints 'Ten' and 'Twenty'
Explanation: The switch statement doesn't require 'break'; it will fall through if break is omitted.

Q3. What will be the output of the following Java code?

```java
public class Q0 {
public static void main(String[] args) {
int a = 1;
int b = 2;
if (a > b) {

System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 1 < 2, so 'a is smaller' is printed.

Q4. What is the output of the following Java code?

```java
public class Q1 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}

```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q5. What will be printed by the following code?

```java
public class Q2 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q6. Will this Java program compile?

```java
public class Q3 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q7. What will this code output?

```java
public class Q4 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:

System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}
```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q8. What will be the output of the following Java code?

```java
public class Q5 {
public static void main(String[] args) {
int a = 6;
int b = 7;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {

System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 6 < 7, so 'a is smaller' is printed.

Q9. What is the output of the following Java code?

```java
public class Q6 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0

D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q10. What will be printed by the following code?

```java
public class Q7 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.
Q11. Will this Java program compile?

```java

public class Q8 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q12. What will this code output?

```java
public class Q9 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:

System.out.println("Default");
}
}
}
```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.
Q13. What will be the output of the following Java code?

```java
public class Q10 {
public static void main(String[] args) {
int a = 11;
int b = 12;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}

}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 11 < 12, so 'a is smaller' is printed.

Q14. What is the output of the following Java code?

```java
public class Q11 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.
Q15. What will be printed by the following code?

```java
public class Q12 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q16. Will this Java program compile?

```java
public class Q13 {
public static void main() {
System.out.println("Hello");
}

}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q17. What will this code output?

```java
public class Q14 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}

```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q18. What will be the output of the following Java code?

```java
public class Q15 {
public static void main(String[] args) {
int a = 16;
int b = 17;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller

D. Compilation error
Answer: a is smaller
Explanation: 16 < 17, so 'a is smaller' is printed.
Q19. What is the output of the following Java code?

```java
public class Q16 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q20. What will be printed by the following code?

```java

public class Q17 {


public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q21. Will this Java program compile?

```java
public class Q18 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello

B. Yes, but prints nothing


C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q22. What will this code output?

```java
public class Q19 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}
```
A. One
B. Two

C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q23. What will be the output of the following Java code?

```java
public class Q20 {
public static void main(String[] args) {
int a = 21;
int b = 22;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 21 < 22, so 'a is smaller' is printed.

Q24. What is the output of the following Java code?

```java
public class Q21 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q25. What will be printed by the following code?

```java
public class Q22 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {

System.out.print(c + " ");


c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q26. Will this Java program compile?

```java
public class Q23 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid

Explanation: main method must be 'public static void main(String[] args)'.

Q27. What will this code output?

```java
public class Q24 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}
```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q28. What will be the output of the following Java code?

```java
public class Q25 {
public static void main(String[] args) {
int a = 26;
int b = 27;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 26 < 27, so 'a is smaller' is printed.

Q29. What is the output of the following Java code?

```java
public class Q26 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q30. What will be printed by the following code?

```java
public class Q27 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}

}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q31. Will this Java program compile?

```java
public class Q28 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q32. What will this code output?

```java
public class Q29 {
public static void main(String[] args) {
int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}
```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q33. What will be the output of the following Java code?

```java

public class Q30 {


public static void main(String[] args) {
int a = 31;
int b = 32;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 31 < 32, so 'a is smaller' is printed.

Q34. What is the output of the following Java code?

```java
public class Q31 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {

sum += i;
}
System.out.println(sum);
}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q35. What will be printed by the following code?

```java
public class Q32 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2

B. 1 2 3
C. 1 2 3
D. 0 1 2
Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

Q36. Will this Java program compile?

```java
public class Q33 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
B. Yes, but prints nothing
C. No, main method signature is invalid
D. No, println is used incorrectly
Answer: No, main method signature is invalid
Explanation: main method must be 'public static void main(String[] args)'.

Q37. What will this code output?

```java
public class Q34 {
public static void main(String[] args) {

int val = 2;
switch(val) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("Default");
}
}
}
```
A. One
B. Two
C. Default
D. Compilation error
Answer: Two
Explanation: Case 2 matches, 'Two' is printed and break prevents fall-through.

Q38. What will be the output of the following Java code?

```java
public class Q35 {
public static void main(String[] args) {
int a = 36;

int b = 37;
if (a > b) {
System.out.println("a is greater");
} else if (a == b) {
System.out.println("a is equal");
} else {
System.out.println("a is smaller");
}
}
}
A. a is greater
B. a is equal
C. a is smaller
D. Compilation error
Answer: a is smaller
Explanation: 36 < 37, so 'a is smaller' is printed.

Q39. What is the output of the following Java code?

```java
public class Q36 {
public static void main(String[] args) {
int sum = 0;
for(int i = 0; i < 3; i++) {
sum += i;
}
System.out.println(sum);

}
}
```
A. 3
B. 6
C. 0
D. Compilation error
Answer: 3
Explanation: i takes values 0, 1, 2 => sum = 0+1+2 = 3.

Q40. What will be printed by the following code?

```java
public class Q37 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
System.out.print(c + " ");
c++;
}
}
}
```
A. 1 2
B. 1 2 3
C. 1 2 3
D. 0 1 2

Answer: 1 2
Explanation: Loop prints c while c < 3, so prints 1 and 2.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy