Bird
Raised Fist0
Cprogramming~10 mins

Ternary operator in C - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' will assign the larger number.
Using '==' or '!=' will not correctly compare sizes.
2fill in blank
medium

Complete 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'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' will assign 'even' to odd numbers and vice versa.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix 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'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' will incorrectly negate positive numbers.
Using '==' or '!=' does not check negativity.
4fill in blank
hard

Fill 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'
A>
B"non-positive"
C"negative"
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in the first blank reverses the logic.
Assigning 'negative' instead of 'non-positive' changes the meaning.
5fill in blank
hard

Fill 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'
A>
Bx
Cy
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the logic and assigns the minimum.
Swapping 'x' and 'y' in the assignments changes the result.

Practice

(1/5)
1. What does the ternary operator condition ? value_if_true : value_if_false do in C?
easy
A. It chooses one of two values based on the condition's truth.
B. It creates a loop that runs twice.
C. It declares a new variable.
D. It comments out code.

Solution

  1. Step 1: Understand the ternary operator structure

    The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
  2. Step 2: Determine its purpose

    It returns the first value if the condition is true, otherwise the second value.
  3. Final Answer:

    It chooses one of two values based on the condition's truth. -> Option A
  4. Quick Check:

    condition ? true_value : false_value = chooses value [OK]
Hint: Remember: condition ? true : false picks one value [OK]
Common Mistakes:
  • Thinking it creates loops
  • Confusing it with variable declaration
  • Assuming it comments code
2. Which of the following is the correct syntax for the ternary operator in C?
easy
A. condition : value_if_true ? value_if_false;
B. condition ? value_if_true : value_if_false;
C. condition ? value_if_true, value_if_false;
D. condition ? (value_if_true : value_if_false);

Solution

  1. Step 1: Recall ternary operator syntax

    The correct syntax is: condition ? value_if_true : value_if_false;
  2. Step 2: Check each option

    condition ? value_if_true : value_if_false; matches the correct syntax exactly; others have misplaced symbols or punctuation.
  3. Final Answer:

    condition ? value_if_true : value_if_false; -> Option B
  4. Quick Check:

    Syntax is condition ? true : false; [OK]
Hint: Remember: question mark before colon in ternary [OK]
Common Mistakes:
  • Swapping question mark and colon
  • Using comma instead of colon
  • Adding extra parentheses incorrectly
3. What is the output of this C code?
int x = 5;
int y = (x > 3) ? 10 : 20;
printf("%d", y);
medium
A. 10
B. 20
C. 5
D. Syntax error

Solution

  1. Step 1: Evaluate the condition (x > 3)

    Since x is 5, 5 > 3 is true.
  2. Step 2: Apply ternary operator

    Because condition is true, y is assigned 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    (5 > 3) ? 10 : 20 = 10 [OK]
Hint: Check if condition is true, then pick first value [OK]
Common Mistakes:
  • Choosing the false value by mistake
  • Confusing variable values
  • Thinking it causes syntax error
4. Identify the error in this C code using the ternary operator:
int a = 10;
int b = 20;
int max = a > b ? a, b;
medium
A. Missing colon ':' between values
B. Incorrect variable declaration
C. No error, code is correct
D. Using comma instead of colon in ternary

Solution

  1. Step 1: Check ternary operator syntax

    The ternary operator requires a colon ':' between the two possible values.
  2. Step 2: Identify the mistake

    The code uses a comma ',' instead of a colon ':', which is incorrect syntax.
  3. Final Answer:

    Using comma instead of colon in ternary -> Option D
  4. Quick Check:

    Ternary needs ':' not ',' [OK]
Hint: Ternary uses ':' not ',' between values [OK]
Common Mistakes:
  • Using comma instead of colon
  • Forgetting colon entirely
  • Assuming code is correct
5. Given this code snippet, what will be the value of result?
int a = 0;
int b = 5;
int result = (a != 0) ? (b / a) : (b * 2);
printf("%d", result);
hard
A. 0
B. Division by zero error
C. 10
D. 5

Solution

  1. Step 1: Evaluate the condition (a != 0)

    a is 0, so (0 != 0) is false.
  2. Step 2: Apply ternary operator

    Since condition is false, result is assigned (b * 2) = 5 * 2 = 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Condition false, so result = b*2 = 10 [OK]
Hint: Check condition first to avoid division by zero [OK]
Common Mistakes:
  • Assuming division happens causing error
  • Ignoring false condition path
  • Confusing variable values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard