Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print "One" when the variable num is 1.
C++
int num = 1; switch (num) { case [1]: std::cout << "One" << std::endl; break; default: std::cout << "Other" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a case label that does not match the variable value.
Using 'default' as a case label.
✗ Incorrect
The case label must match the value of num to execute the correct block. Here, case 1: matches num = 1.
2fill in blank
mediumComplete the if statement to print "Two" when num equals 2.
C++
int num = 2; if (num [1] 2) { std::cout << "Two" << std::endl; } else { std::cout << "Not Two" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment operator '=' instead of comparison '=='.
Using inequality operator '!='.
✗ Incorrect
The equality operator == checks if num is equal to 2.
3fill in blank
hardFix the error in the switch statement to correctly handle the variable grade.
C++
char grade = 'B'; switch (grade) { case [1]: std::cout << "Good" << std::endl; break; default: std::cout << "Other grade" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
Using unquoted letters.
✗ Incorrect
Character constants in C++ use single quotes. Double quotes are for strings.
4fill in blank
hardFill both blanks to create a map from numbers to words using if statements.
C++
int num = 3; if (num [1] 1) { std::cout << "One" << std::endl; } else if (num [2] 3) { std::cout << "Three" << std::endl; } else { std::cout << "Other" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality operator '!=' instead of '=='.
Using greater or less than operators incorrectly.
✗ Incorrect
Use the equality operator == to check if num equals 1 or 3.
5fill in blank
hardFill all three blanks to create a switch statement that prints the day of the week for 1, 2, and 3.
C++
int day = 2; switch (day) { case [1]: std::cout << "Monday" << std::endl; break; case [2]: std::cout << "Tuesday" << std::endl; break; case [3]: std::cout << "Wednesday" << std::endl; break; default: std::cout << "Other day" << std::endl; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong numbers for case labels.
Missing break statements (though not asked here).
✗ Incorrect
The case labels must match the values 1, 2, and 3 to print the correct day.