Java Code Questions
Java Code Questions
```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'.
```java
```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.
```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.
```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.
```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)'.
```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.
```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.
```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.
```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)'.
```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.
```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.
```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)'.
```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.
```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.
```java
```java
public class Q18 {
public static void main() {
System.out.println("Hello");
}
}
```
A. Yes, and it prints Hello
```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.
```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.
```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.
```java
public class Q22 {
public static void main(String[] args) {
int c = 1;
while (c < 3) {
```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
```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.
```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.
```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.
```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.
```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)'.
```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.
```java
```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.
```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.
```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)'.
```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.
```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.
```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.
```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.