Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Switch Practice

Multi-way branching with switch statements on ints, chars and strings.

1
Day of the Week
Easy
Read a number N (1-7) and print the day of the week: 1=Monday ... 7=Sunday. Use a switch statement.
A single integer N.
The day name: Monday, Tuesday, ..., Sunday.
Sample Input 1
1
Sample Output 1
Monday
Sample Input 2
5
Sample Output 2
Friday
Sample Input 3
2
Sample Output 3
Tuesday
Sample Input 4
7
Sample Output 4
Sunday
Sample Input 5
3
Sample Output 5
Wednesday
1
switch (n) { case 1: ... }
2
Month Name
Easy
Read a number N (1-12) and print the month name. Use a switch statement.
A single integer N.
The month name: January, February, ..., December.
Sample Input 1
1
Sample Output 1
January
Sample Input 2
12
Sample Output 2
December
Sample Input 3
2
Sample Output 3
February
Sample Input 4
6
Sample Output 4
June
Sample Input 5
9
Sample Output 5
September
1
switch (n) { case 1: ... }
3
Vowel or Consonant (Switch)
Easy
Read a single lowercase letter and print "Vowel" if it is a, e, i, o or u, otherwise "Consonant". Use a switch statement.
A single lowercase letter.
"Vowel" or "Consonant".
Sample Input 1
a
Sample Output 1
Vowel
Sample Input 2
b
Sample Output 2
Consonant
Sample Input 3
e
Sample Output 3
Vowel
Sample Input 4
z
Sample Output 4
Consonant
Sample Input 5
i
Sample Output 5
Vowel
a-z
switch (ch) { case 'a': case 'e': ... }
1
Grade with Switch
Medium
Read a grade letter (A-F) and print the description using a switch statement: A="Excellent", B="Good", C="Average", D="Below Average", F="Fail", anything else="Invalid".
A single uppercase letter.
The description word.
Sample Input 1
A
Sample Output 1
Excellent
Sample Input 2
F
Sample Output 2
Fail
Sample Input 3
B
Sample Output 3
Good
Sample Input 4
C
Sample Output 4
Average
Sample Input 5
X
Sample Output 5
Invalid
A-F or other single letter
switch (grade) { case ... }
2
Simple Calculator (Switch)
Medium
Read an operator (+, -, *, /) and two integers A and B. Print the result of A op B using a switch statement. Division is integer division; B is never 0.
Three tokens on one line: operator char, A, B.
A single integer: the result.
Sample Input 1
+ 5 3
Sample Output 1
8
Sample Input 2
* 4 6
Sample Output 2
24
Sample Input 3
- 10 4
Sample Output 3
6
Sample Input 4
/ 17 5
Sample Output 4
3
Sample Input 5
+ -5 3
Sample Output 5
-2
operator in +,-,*,/; -10^6
switch (op) { case '+': ... }
3
Number of Days in Month
Medium
Read a month number N (1-12) and print the number of days in it. February always has 28 days here (ignore leap years). Use a switch statement.
A single integer N.
A single integer: days in month.
Sample Input 1
1
Sample Output 1
31
Sample Input 2
2
Sample Output 2
28
Sample Input 3
4
Sample Output 3
30
Sample Input 4
12
Sample Output 4
31
Sample Input 5
11
Sample Output 5
30
1
Months 1,3,5,7,8,10,12 → 31; 4,6,9,11 → 30; 2 → 28.
4
Traffic Light Action
Medium
Read a traffic light color (Red, Yellow, Green) and print the action: Red="Stop", Yellow="Get Ready", Green="Go", anything else="Invalid". Use a switch statement.
A single word (case-sensitive).
"Stop", "Get Ready", "Go" or "Invalid".
Sample Input 1
Red
Sample Output 1
Stop
Sample Input 2
Green
Sample Output 2
Go
Sample Input 3
Yellow
Sample Output 3
Get Ready
Sample Input 4
Blue
Sample Output 4
Invalid
Sample Input 5
green
Sample Output 5
Invalid
Red, Yellow or Green
switch (color) { case "Red": ... } (Java 7+ string switch).
1
Digit to Word (0-9)
Hard
Read a digit N (0-9) and print its English word ("Zero", "One", ..., "Nine") using a switch statement.
A single integer N.
The English word for N.
Sample Input 1
0
Sample Output 1
Zero
Sample Input 2
5
Sample Output 2
Five
Sample Input 3
9
Sample Output 3
Nine
Sample Input 4
2
Sample Output 4
Two
Sample Input 5
7
Sample Output 5
Seven
0
switch (n) { case 0: ... }
2
Quadrant of a Point
Hard
Read two integers X and Y and print the quadrant: "Q1" (+,+), "Q2" (-,+), "Q3" (-,-), "Q4" (+,-), "Origin" (0,0), "X-axis" or "Y-axis". Use a switch or nested logic.
A single line with two integers X and Y.
The quadrant/axis name.
Sample Input 1
3 4
Sample Output 1
Q1
Sample Input 2
-3 4
Sample Output 2
Q2
Sample Input 3
-3 -4
Sample Output 3
Q3
Sample Input 4
3 -4
Sample Output 4
Q4
Sample Input 5
0 0
Sample Output 5
Origin
-10^6
Check zero cases first, then signs.
3
Season Finder
Hard
Read a month number (1-12) and print the season: 12,1,2="Winter", 3,4,5="Spring", 6,7,8="Summer", 9,10,11="Autumn". Use a switch statement.
A single integer N.
"Winter", "Spring", "Summer" or "Autumn".
Sample Input 1
1
Sample Output 1
Winter
Sample Input 2
6
Sample Output 2
Summer
Sample Input 3
4
Sample Output 3
Spring
Sample Input 4
10
Sample Output 4
Autumn
Sample Input 5
12
Sample Output 5
Winter
1
switch (n) { case 12: case 1: case 2: ... }
Competitive MCQs — Java Switch
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
Which types can a switch statement use?
Correct!
Wrong — correct answer is .
switch supports int-like types, char, String, and enums — not double/float.
Q2
What is the output of this code?
java
1
2
3
4
5
6
int day = 3;
switch (day) {
    case 1: System.out.print("Mon"); break;
    case 3: System.out.print("Wed"); break;
    default: System.out.print("Other");
}
Correct!
Wrong — correct answer is .
day == 3 matches case 3 → "Wed", then break exits.
Q3
What is the output of this code?
int day = 3;
switch (day) {
case 1: System.out.print("Mon"); break;
case 3: System.out.print("Wed"); break;
default: System.out.print("Other");
}
Correct!
Wrong — correct answer is .
day == 3 matches case 3 → "Wed", then break exits.
Q4
What happens if you omit break in a case?
Correct!
Wrong — correct answer is .
Without break, control falls through to subsequent cases.
Q5
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
char ch = 'a';
switch (ch) {
    case 'a':
    case 'e':
        System.out.println("Vowel");
        break;
    default:
        System.out.println("Other");
}
Correct!
Wrong — correct answer is .
Both 'a' and 'e' share the "Vowel" body via fall-through.
Q6
What is the output of this code?
char ch = 'a';
switch (ch) {
case 'a':
case 'e':
System.out.println("Vowel");
break;
default:
System.out.println("Other");
}
Correct!
Wrong — correct answer is .
Both 'a' and 'e' share the "Vowel" body via fall-through.
Q7
Which keyword provides the "none matched" branch?
Correct!
Wrong — correct answer is .
default runs when no case label matches.
Q8
What is the output of this code?
java
1
2
3
4
5
6
int n = 2;
switch (n) {
    case 1: System.out.print("One");
    case 2: System.out.print("Two");
    default: System.out.print("Default");
}
Correct!
Wrong — correct answer is .
case 2 matches, then falls through printing "Two" and "Default".
Q9
What is the output of this code?
int n = 2;
switch (n) {
case 1: System.out.print("One");
case 2: System.out.print("Two");
default: System.out.print("Default");
}
Correct!
Wrong — correct answer is .
case 2 matches, then falls through printing "Two" and "Default".
Q10
Can a switch label be a String?
Correct!
Wrong — correct answer is .
String switch is supported from Java 7 onward.
Q11
Which statement is required to exit a switch case early?
Correct!
Wrong — correct answer is .
break exits the switch; continue is for loops.
Q12
What is the output of this code?
java
1
2
3
4
5
int x = 5;
switch (x) {
    default: System.out.println("D");
    case 5: System.out.println("Five");
}
Correct!
Wrong — correct answer is .
case 5 matches, prints "Five"; default before it is skipped (and would fall through).
Q13
What is the output of this code?
int x = 5;
switch (x) {
default: System.out.println("D");
case 5: System.out.println("Five");
}
Correct!
Wrong — correct answer is .
case 5 matches, prints "Five"; default before it is skipped (and would fall through).
Q14
What does the arrow form -> in a switch do?
Correct!
Wrong — correct answer is .
Switch expressions with -> do not fall through.
Q15
What is the output of this code?
int x = 2;
switch (x) {
case 1: System.out.println("One");
case 2: System.out.println("Two");
}
Correct!
Wrong — correct answer is .
case 2 matches; since it is last, only "Two" prints (no fall-through target).
Q16
Which statement in a case is optional but prevents fall-through?
Correct!
Wrong — correct answer is .
break exits the switch block; without it execution continues to the next case.
Q17
What is the output of this code?
int x = 1;
switch (x) {
case 1:
case 2:
System.out.println("low");
break;
default:
System.out.println("other");
}
Correct!
Wrong — correct answer is .
Cases 1 and 2 share the same body, so x=1 prints "low".
Q18
Can a switch test a String in modern Java?
Correct!
Wrong — correct answer is .
String switch has been supported since Java 7.
Q19
What is the output of this code?
String s = "B";
switch (s) {
case "A": System.out.println("1");
case "B": System.out.println("2");
default: System.out.println("3");
}
Correct!
Wrong — correct answer is .
case "B" matches, then without a break it falls through and prints 2 then 3.
Q20
What values can a case label have?
Correct!
Wrong — correct answer is .
Case labels must be constants resolvable at compile time.
Q21
Which is the default branch keyword?
Correct!
Wrong — correct answer is .
default runs when no case label matches the switch value.
Q22
What is the output of this code?
char ch = 'x';
switch (ch) {
case 'a': System.out.print("vowel"); break;
default: System.out.print("consonant");
}
Correct!
Wrong — correct answer is .
No case matches 'x', so default prints "consonant".
Q23
What is fall-through in a switch?
Correct!
Wrong — correct answer is .
Without break, control flows into subsequent case blocks.
Q24
What is the output of this code?
int x = 3;
switch (x) {
case 1: System.out.print("A");
case 2: System.out.print("B");
default: System.out.print("C");
}
Correct!
Wrong — correct answer is .
No case matches 3, so only default prints "C".
Q25
Which is a valid switch label for an int?
Correct!
Wrong — correct answer is .
Integer constants are valid case labels; variables and doubles are not.
Q26
What is the output of this code?
int x = 1;
switch (x) {
default: System.out.print("D");
case 1: System.out.print("One");
}
Correct!
Wrong — correct answer is .
case 1 matches directly, so default is skipped and only "One" prints.
Q27
Which type does NOT work as a switch expression?
Correct!
Wrong — correct answer is .
Floating-point types are not allowed in switch expressions.
Q28
What is a switch expression in modern Java?
Correct!
Wrong — correct answer is .
Switch expressions evaluate to a value and avoid fall-through.
Q29
What is the output of this code?
int x = 2;
int r = switch (x) {
case 1 -> 10;
case 2 -> 20;
default -> 0;
};
System.out.println(r);
Correct!
Wrong — correct answer is .
case 2 maps to 20, which the switch expression returns.
Q30
Which keyword yields a value in an old-style switch case?
Correct!
Wrong — correct answer is .
yield returns a value from a switch expression (Java 14+).
Q31
How many case labels can match a single value?
Correct!
Wrong — correct answer is .
Duplicate or multiple matching labels would not compile; only one label matches.
Q32
What is the output of this code?
int x = 10;
switch (x) {
case 5: System.out.println("five"); break;
case 10: System.out.println("ten"); break;
default: System.out.println("other");
}
Correct!
Wrong — correct answer is .
x equals 10, matching case 10, which prints "ten".
Q33
Which best practice prevents accidental fall-through?
Correct!
Wrong — correct answer is .
An explicit break (or arrow form) stops execution from spilling into later cases.
Q34
What is the output of this code?
int x = 1;
switch (x) {
case 1: System.out.println("One");
case 2: System.out.println("Two");
default: System.out.println("Default");
}
Correct!
Wrong — correct answer is .
Without breaks, case 1 falls through printing all three lines.