Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a switch statement on the variable choice.
C
switch([1]) { case 1: printf("One\n"); break; default: printf("Other\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared.
Forgetting the parentheses after switch.
✗ Incorrect
The switch statement must use the variable
choice to decide which case to execute.2fill in blank
mediumComplete the code to print "Two" when the variable num equals 2.
C
switch(num) {
case [1]:
printf("Two\n");
break;
default:
printf("Other\n");
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number in the case label.
Forgetting the colon after the case value.
✗ Incorrect
The case label must match the value 2 to print "Two".
3fill in blank
hardFix the error in the switch statement to correctly handle the default case.
C
switch(option) {
case 1:
printf("One\n");
break;
default[1]:
printf("Unknown\n");
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon after default instead of a colon.
Omitting the colon after default.
✗ Incorrect
The default label must be followed by a colon, not a semicolon or other punctuation.
4fill in blank
hardFill both blanks to complete the switch statement that prints "Zero" or "Positive" based on num.
C
switch(num) {
case [1]:
printf("Zero\n");
break;
case [2]:
printf("Positive\n");
break;
default:
printf("Negative\n");
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative numbers for positive cases.
Forgetting the break statements.
✗ Incorrect
Case 0 prints "Zero" and case 1 prints "Positive".
5fill in blank
hardFill all three blanks to create a switch that prints the day name for day values 1, 2, or 3.
C
switch([1]) { case [2]: printf("Monday\n"); break; case [3]: printf("Tuesday\n"); break; default: printf("Other day\n"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in switch.
Mixing up case numbers.
✗ Incorrect
The switch uses variable
day, with cases 1 and 2 for Monday and Tuesday.