Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign the smaller of two numbers to the variable min using the ternary operator.
C
int a = 5, b = 10; int min = (a [1] b) ? a : b;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' will assign the larger number.
Using '==' or '!=' will not correctly compare sizes.
✗ Incorrect
The ternary operator checks if 'a < b'. If true, 'min' gets 'a'; otherwise, it gets 'b'.
2fill in blank
mediumComplete the code to assign the string "even" or "odd" to result based on whether num is even or odd using the ternary operator.
C
int num = 7; char *result = (num % 2 [1] 0) ? "odd" : "even";
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' will assign 'even' to odd numbers and vice versa.
Using '>' or '<' does not correctly check for evenness.
✗ Incorrect
The condition checks if 'num % 2 != 0' to determine if the number is odd.
3fill in blank
hardFix the error in the ternary operator condition to correctly assign the absolute value of num to abs_val.
C
int num = -8; int abs_val = (num [1] 0) ? -num : num;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' will incorrectly negate positive numbers.
Using '==' or '!=' does not check negativity.
✗ Incorrect
The condition should check if 'num < 0' to negate it and get the absolute value.
4fill in blank
hardFill both blanks to create a ternary operator that assigns "positive" if n is greater than zero, otherwise "non-positive".
C
int n = 3; char *sign = (n [1] 0) ? "positive" : [2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in the first blank reverses the logic.
Assigning 'negative' instead of 'non-positive' changes the meaning.
✗ Incorrect
The condition checks if 'n > 0'. If true, assign 'positive'; else assign 'non-positive'.
5fill in blank
hardFill all three blanks to create a ternary operator that assigns the maximum of x and y to max_val.
C
int x = 4, y = 9; int max_val = (x [1] y) ? [2] : [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the logic and assigns the minimum.
Swapping 'x' and 'y' in the assignments changes the result.
✗ Incorrect
The condition checks if 'x > y'. If true, 'max_val' is 'x'; otherwise, it is 'y'.