Java Programming – Advanced MCQ Test
40 Higher-Level Multiple Choice Questions
Class: 10th Standard | Marks: 40 | Time: 50 Minutes
Name: ___________________________ Roll No: ________ Date: ____________
Topics Covered: Switch-Case | If-Else-If | Number Based | Looping | Java Statements | Dry Run | Debugging
| Analytical
Instructions: Choose the BEST answer for each question. Each question carries 1 mark. No negative marking.
🔷 Switch-Case
Q1. What will be the output of the following code?
int x = 3;
switch(x) {
case 1:
case 2:
case 3: [Link]("Match"); break;
case 4: [Link]("Four"); break;
default: [Link]("None");
}
A) None
B) Match
C) Four
D) Match Four
Answer: _____
Q2. In Java, the switch statement can work with which of the following data types?
A) int and float
B) int and double
C) int, char, and String
D) All data types
Answer: _____
Q3. What is the output of the following code?
int a = 5;
switch(a % 2) {
case 0: [Link]("Even"); break;
case 1: [Link]("Odd"); break;
default: [Link]("Unknown");
}
A) Even
B) Unknown
C) Odd
D) Compile Error
Answer: _____
Q4. What happens when a switch expression does not match any case and there is NO default?
A) Compile error occurs
B) Runtime error occurs
C) Nothing happens, program continues
D) First case executes
Answer: _____
Q5. What is the output?
char grade = 'B';
switch(grade) {
case 'A': [Link]("Excellent"); break;
case 'B':
case 'C': [Link]("Good"); break;
default: [Link]("Average");
}
A) Excellent
B) Average
C) Good
D) Good Average
Answer: _____
🔷 If-Else-If
Q6. What will be the output?
int x = 10;
if(x > 10) {
[Link]("A");
} else if(x == 10) {
[Link]("B");
} else if(x >= 10) {
[Link]("C");
} else {
[Link]("D");
}
A) A
B) B
C) C
D) B C
Answer: _____
Q7. What is the output of this nested if statement?
int a = 8, b = 5;
if(a > 5) {
if(b > 3) {
[Link]("Both");
} else {
[Link]("Only A");
}
} else {
[Link]("Neither");
}
A) Only A
B) Neither
C) Both
D) Compile Error
Answer: _____
Q8. What will be the output?
int marks = 75;
String grade;
if(marks >= 90) grade = "A";
else if(marks >= 75) grade = "B";
else if(marks >= 60) grade = "C";
else grade = "F";
[Link](grade);
A) A
B) B
C) C
D) F
Answer: _____
Q9. Which of the following is TRUE about if-else-if chains?
A) All conditions are always checked
B) Once a true condition is found, rest are skipped
C) The default block always runs
D) It can only have 3 conditions maximum
Answer: _____
Q10. What is the output?
int p = 5, q = 5;
if(p == q) {
[Link]("Equal");
} else if(p > q) {
[Link]("P greater");
} else {
[Link]("Q greater");
}
A) Equal
B) P greater
C) Q greater
D) Compile Error
Answer: _____
🔷 Number Based
Q11. What will be the output of this number-based program?
int n = 153;
int sum = 0, temp = n;
while(temp > 0) {
int d = temp % 10;
sum += d * d * d;
temp /= 10;
}
[Link](n == sum);
A) false
B) 153
C) true
D) Compile Error
Answer: _____
Q12. What does the following code compute?
int n = 12;
int result = 0;
while(n > 0) {
result = result * 10 + n % 10;
n /= 10;
}
[Link](result);
A) 12
B) 6
C) 21
D) 1
Answer: _____
Q13. How many times does the inner loop body execute in total?
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= i; j++) {
[Link]("* ");
}
}
A) 3
B) 6
C) 9
D) 4
Answer: _____
Q14. What is the output of this program?
int n = 1;
while(n <= 100) {
if(n % 7 == 0 && n % 3 == 0) {
[Link](n);
break;
}
n++;
}
A) 7
B) 21
C) 42
D) 3
Answer: _____
Q15. What will the following code print?
int x = 256;
int count = 0;
while(x > 1) {
x = x / 2;
count++;
}
[Link](count);
A) 7
B) 9
C) 8
D) 128
Answer: _____
🔷 Looping
Q16. What is the output of this loop?
int x = 1;
while(x < 100) {
x *= 3;
}
[Link](x);
A) 99
B) 81
C) 243
D) 100
Answer: _____
Q17. What is the output?
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(i == j) [Link](i + " ");
}
}
A) 0 1 2
B) 0 0 1 1 2 2
C) 1 2 3
D) 0 1 2 0 1 2
Answer: _____
Q18. How many times does this loop execute?
int i = 1;
while(i != 50) {
i += 7;
}
A) 7 times
B) 50 times
C) Infinite loop
D) 8 times
Answer: _____
Q19. What is the output of the following?
int sum = 0;
for(int i = 1; i <= 10; i += 2) {
sum += i;
}
[Link](sum);
A) 25
B) 30
C) 55
D) 20
Answer: _____
Q20. What will be printed?
for(int i = 5; i >= 1; i--) {
if(i % 2 == 0) continue;
[Link](i + " ");
}
A) 5 3 1
B) 4 2
C) 5 4 3 2 1
D) 1 3 5
Answer: _____
Q21. What is the output?
int i = 0, j = 10;
while(i < j) {
i++;
j--;
}
[Link](i + " " + j);
A) 5 5
B) 6 4
C) 4 6
D) 10 0
Answer: _____
Q22. What is the output of the following nested loop?
for(int i=1; i<=3; i++) {
for(int j=3; j>=i; j--) {
[Link]("* ");
}
[Link]();
}
A) 3 rows with 3,2,1 stars
B) 3 rows with 1,2,3 stars
C) 9 stars in one line
D) 3 rows with 3 stars each
Answer: _____
🔷 Java Statements
Q23. What is the output of this code involving break in nested loops?
outer:
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++) {
if(j == 2) break outer;
[Link](i + "" + j + " ");
}
}
A) 11 12 21 22 31 32
B) 11
C) 11 21 31
D) Compile Error
Answer: _____
Q24. What is the difference between break and return in a loop?
A) They do the same thing
B) break exits only the loop; return exits the entire method
C) return exits only the loop; break exits the method
D) break is used in switch only
Answer: _____
Q25. What will be the output?
int x = 10;
if(x > 5) {
[Link]("A");
if(x > 8) {
[Link]("B");
}
[Link]("C");
}
A) A C
B) A B
C) A B C
D) B C
Answer: _____
Q26. What is the output?
for(int i=1; i<=5; i++) {
if(i%2 == 0) {
[Link]("E ");
continue;
}
[Link]("O ");
}
A) O E O E O
B) E E
C) O O O
D) O E O E O E
Answer: _____
Q27. What does the following program do?
int n = 12345;
while(n > 0) {
[Link](n % 10 + " ");
n /= 10;
}
A) Prints digits of n in original order
B) Prints digits of n in reverse order
C) Prints the sum of digits
D) Infinite loop
Answer: _____
🔷 Dry Run Output
Q28. What is the final value of 'result' after this code runs?
int result = 1;
for(int i = 1; i <= 5; i++) {
result *= i;
}
[Link](result);
A) 15
B) 25
C) 120
D) 60
Answer: _____
Q29. Trace the code and find the output:
int a = 2, b = 3;
for(int i = 0; i < 4; i++) {
int temp = a + b;
a = b;
b = temp;
}
[Link](a + " " + b);
A) 5 8
B) 8 13
C) 13 21
D) 3 5
Answer: _____
Q30. What is printed?
int x = 5;
int y = x++;
[Link](x + " " + y);
A) 5 5
B) 6 5
C) 5 6
D) 6 6
Answer: _____
Q31. What does this code print?
int i = 0;
while(true) {
i++;
if(i == 5) break;
}
[Link](i);
A) 0
B) 4
C) 5
D) Infinite loop
Answer: _____
Q32. What is the output?
int n = 7;
int r = 0;
for(int i = 1; i <= n; i++) {
if(n % i == 0) r++;
}
[Link](r == 2 ? "Prime" : "Not Prime");
A) Not Prime
B) 2
C) Prime
D) 7
Answer: _____
Q33. What is the output of this code?
int sum = 0;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
sum++;
}
}
[Link](sum);
A) 10
B) 15
C) 25
D) 5
Answer: _____
🔷 Debugging
Q34. What is the bug in the following code that should print numbers 1 to 5?
for(int i = 1; i <= 5; i++) {
[Link](i);
if(i == 3) i = 6;
}
A) No bug, prints 1 2 3 4 5
B) Bug: prints 1 2 3 then stops (i set to 6, then i++ makes i=7 > 5)
C) Infinite loop
D) Compile error
Answer: _____
Q35. Find the error: This code should print the sum of 1 to 10 but gives wrong answer.
int sum = 0;
for(int i = 0; i <= 10; i++) {
sum = sum + i;
}
[Link](sum);
A) No error – it correctly prints 55
B) Loop should start at i=1 not i=0
C) Should be i < 10 not i <= 10
D) sum should start at 1
Answer: _____
Q36. What is wrong with this infinite loop code?
int i = 1;
while(i <= 10) {
[Link](i);
}
A) Condition is wrong
B) Missing i++ inside the loop causing infinite loop
C) while loop cannot print
D) Nothing is wrong
Answer: _____
Q37. What logical error exists in this prime checking code?
int n = 9, count = 0;
for(int i = 1; i < n; i++) {
if(n % i == 0) count++;
}
if(count == 1) [Link]("Prime");
else [Link]("Not Prime");
A) Loop should go up to n/2
B) Should check count == 2 not count == 1 (since i starts at 1, not 2)
C) Should use count == 0
D) No error
Answer: _____
🔷 Analytical
Q38. Which code correctly checks if a number is between 10 and 50 (inclusive)?
A) if(n > 10 && n < 50)
B) if(n >= 10 || n <= 50)
C) if(n >= 10 && n <= 50)
D) if(n >= 10 && n >= 50)
Answer: _____
Q39. What is the equivalent for loop for the following while loop?
int i = 10;
while(i > 0) {
[Link](i + " ");
i -= 2;
}
A) for(int i=10; i>0; i++)
B) for(int i=10; i>0; i-=2)
C) for(int i=0; i<10; i+=2)
D) for(int i=10; i>=0; i--)
Answer: _____
Q40. What will this tricky code print?
int x = 5;
[Link](x > 3 ? (x > 7 ? "High" : "Mid") : "Low");
A) High
B) Mid
C) Low
D) Compile Error
Answer: _____
Total: 40 Marks | Each question = 1 Mark | All The Best!