Test Your Knowledge (Ch 9)

The following questions are based on what we have learned so far in Chapter 9 – Conditionals in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

What is the primary purpose of using conditional statements in C?

To declare variables
To repeat a block of code
To execute certain code based on a condition
To define functions
explanation

the primary purpose of conditional statements in C is to execute a certain code based on a condition.

Question 2

Which of the following expressions is valid in a switch statement?

case 1.5:
case ‘A’:
case “Hi”:
case true:
explanation

the switch statement allows only integral types (int and char) as case expressions.

Question 3

What is the purpose of the default case in a switch statement?

It specifies the default value for the switch expression.
It is used for type casting.
It is mandatory in every switch statement.
It is executed when no other case matches.
explanation

the default case is executed when none of the previous cases match the value in the switch expression.

Question 4

What is the result of the condition: (10 > 5) && (7 < 3)?

True
False
Undefined
Error
explanation

The && (logical AND) operator requires both conditions to be true for the overall expression to be true.

Question 5

Determine the output of the following C program:

#include <stdio.h>

int main() {
    int x = 10, y = 0;
    if (y != 0) 
        if (x != 0)
            printf("%.2f", x/y);
    else
        printf("Division by zero!");

    return 0;
}
Division by zero!
10
No output
None of the above
explanation

This issue is known as the dangling else problem. It looks like the else block is part of the outer if statement (through the way the code is indented), but C follows the rule that the else block always belongs to the nearest if statement. So, if y is 0, nothing is printed.

Question 6

Let us suppose that x and y are the two variables where the type of x is int and that of y is float. What will be the type of the conditional statement (x > y ? x : y)?

int
float
double
Can’t say
explanation

When there is a type mismatch like this, then the type of the conditional statement depends upon the common type that can accommodate both x and y. Therefore, the type of the conditional statement (x > y ? x : y) is float because float type can represent both int and float types.

Question 7

Consider the following if statement:

if (x >= 1 <= 20)
	    printf(“1 lies between %d and 20”, x);

Is the above if statement legal (assuming the value of x is 10)?

Yes
No
explanation

The if statement is legal, but it may not work as intended. Let’s break down the evaluation to understand this better:
1. (x >= 1) is evaluated which gives True (1 in C).
2. Then, (1 <= 20) is evaluated to True. 

Hence, the final output is “1 lies between 10 and 20.” which is logically not correct. However, syntactically, there is no issue in the above code.

Question 8

In a switch statement, what will happen if there is no break statement after a case?

It will result in a compilation error.
It will continue to execute the code in the next case.
It will skip the current case and move to the default case.
It will terminate the switch statement.
explanation

Without a break statement, the control will fall through to the next case, and subsequent code will be executed until the break statement or the end of the switch statement is reached.

Question 9

Consider the following code:

int x = 15;
int result = (x > 10) ? (x * 2) : (x / 2);

What will be the value of the result variable?

7
15
20
30
explanation

Since x is greater than 10, therefore (x * 2) is evaluated, resulting in 20.

Question 10

In a compound condition, what does the short-circuit evaluation mean?

Evaluating only the first condition
Skipping the last condition
Evaluating only necessary conditions
Ignoring the else block
explanation

Short-circuit evaluation involves evaluating only the conditions necessary to determine the overall outcome of a compound condition.

Your score is out of



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.