Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign the smaller of two numbers to min using the ternary operator.
C++
int a = 10, b = 20; int min = (a < b) ? a : [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable after the colon.
Forgetting the colon in the ternary operator.
✗ Incorrect
The ternary operator chooses
a if a < b is true; otherwise, it chooses b.2fill in blank
mediumComplete the code to assign result to "Even" or "Odd" based on the value of num.
C++
int num = 7; std::string result = (num % 2 == 0) ? "Even" : [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase "odd" instead of "Odd".
Using a phrase like "Odd number" instead of "Odd".
✗ Incorrect
If
num is not divisible by 2, the ternary operator assigns "Odd" to result.3fill in blank
hardFix the error in the ternary operator expression to correctly assign the absolute value of x to absVal.
C++
int x = -5; int absVal = x >= 0 ? x [1] -x;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon or comma instead of a colon.
Omitting the colon entirely.
✗ Incorrect
The ternary operator requires a colon ':' between the true and false expressions.
4fill in blank
hardFill both blanks to create a ternary operator that assigns grade "Pass" if score is 50 or more, otherwise "Fail".
C++
int score = 65; std::string grade = (score [1] 50) ? "Pass" : [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' for the condition.
Using lowercase "fail" instead of "Fail".
✗ Incorrect
The condition checks if
score is greater than or equal to 50; if true, "Pass" is assigned, else "Fail".5fill in blank
hardFill all three blanks to create a nested ternary operator that assigns category based on age: "Child" if under 13, "Teen" if 13 to 19, otherwise "Adult".
C++
int age = 15; std::string category = (age < [1]) ? "Child" : ((age [2] 19) ? "Teen" : [3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong age limits or comparison operators.
Forgetting to quote the string "Adult".
✗ Incorrect
The first condition checks if age is less than 13 for "Child". The nested condition checks if age is less than or equal to 19 for "Teen"; otherwise, "Adult" is assigned.