C If/Else and Switch Case Quiz - MCQ Questions and Answers

1. What is the purpose of an if statement in C?

a) To iterate over a block of code
b) To declare a variable
c) To execute a block of code conditionally
d) To define a function

Answer:

c) To execute a block of code conditionally

Explanation:

The if statement in C is used to conditionally execute a block of code based on the evaluation of an expression.

2. What is the correct syntax for an if-else statement in C?

a) if (condition) { /* code */ } else { /* code */ }
b) if condition { /* code */ } else { /* code */ }
c) if (condition) then { /* code */ } else { /* code */ }
d) if condition then { /* code */ } else { /* code */ }

Answer:

a) if (condition) { /* code */ } else { /* code */ }

Explanation:

The correct syntax for an if-else statement includes the 'if' keyword, followed by the condition in parentheses, a block of code to execute if the condition is true, the 'else' keyword, and a block of code to execute if the condition is false.

3. What is a switch case statement used for in C?

a) To define constants
b) To perform different actions based on different conditions
c) To create loops
d) To declare variables

Answer:

b) To perform different actions based on different conditions

Explanation:

A switch case statement in C is used to perform different actions based on different conditions. It's an alternative to multiple if-else statements.

4. Which keyword is used to end a case in a switch statement in C?

a) end
b) break
c) stop
d) exit

Answer:

b) break

Explanation:

The 'break' keyword is used at the end of each case in a switch statement to prevent the execution from falling through to the next case.

5. What is the output of the following C code?

   int x = 2;
   if (x == 2)
       printf("Yes");
   else
       printf("No");
a) Yes
b) No
c) Error
d) Nothing is printed

Answer:

a) Yes

Explanation:

The condition x == 2 evaluates to true, so "Yes" is printed.

6. What does the 'default' keyword do in a switch statement in C?

a) Sets the default value of a variable
b) Executes if no case is matched
c) Is required in every switch statement
d) Declares default parameters

Answer:

b) Executes if no case is matched

Explanation:

The 'default' keyword in a switch statement provides a block of code that is executed if none of the cases match the switch expression.

7. How can multiple conditions be combined in an if statement in C?

a) Using the '+' operator
b) Using the '&&' and '||' operators
c) By nesting if statements
d) By separating conditions with commas

Answer:

b) Using the '&&' and '||' operators

Explanation:

The logical AND ('&&') and logical OR ('||') operators are used to combine multiple conditions in an if statement.

8. What is the output of the following C code?

   int a = 10, b = 20;
   if (a > 5 || b < 15)
       printf("True");
   else
       printf("False");
a) True
b) False
c) Error
d) Nothing is printed

Answer:

a) True

Explanation:

The condition a > 5 is true, so the logical OR expression evaluates to true, and "True" is printed.

9. Can a switch statement in C be used with floating-point numbers?

a) Yes
b) No
c) Only with double
d) Only with float

Answer:

b) No

Explanation:

A switch statement in C can only be used with integral types (like int) and characters. Floating-point numbers are not allowed.

10. What is the output of the following C code?

   int x = 2;
   switch (x) {
       case 1: printf("One");
       case 2: printf("Two");
       case 3: printf("Three");
       default: printf("Default");
   }
a) One
b) Two
c) TwoThreeDefault
d) Default

Answer:

c) TwoThreeDefault

Explanation:

Since there is no 'break' statement after each case, the execution falls through and prints "TwoThreeDefault".

11. How do you ensure that an else statement is associated with the correct if statement?

a) By using indentation
b) By placing the else statement immediately after the if statement
c) By using brackets {}
d) By using the 'match' keyword

Answer:

c) By using brackets {}

Explanation:

To ensure that an else statement is associated with the correct if statement, use brackets {} to clearly define the scope of the if statement.

12. What is the output of the following C code?

   int x = 10;
   if (x > 5)
       if (x < 20)
           printf("Yes");
       else
           printf("No");
a) Yes
b) No
c) Error
d) Nothing is printed

Answer:

a) Yes

Explanation:

The nested if conditions both evaluate to true, so "Yes" is printed.

13. What happens if the condition in an if statement in C is left blank?

a) It's treated as true
b) It's treated as false
c) Compilation error
d) Runtime error

Answer:

c) Compilation error

Explanation:

Leaving the condition in an if statement blank results in a compilation error as the condition is required for the if statement to evaluate.

14. Can the else if ladder be used instead of the switch case in C?

a) Yes
b) No
c) Only in certain cases
d) Only if there are less than 3 cases

Answer:

a) Yes

Explanation:

An else if ladder can be used as an alternative to a switch case for multiple conditions. It's often a matter of readability and preference.

15. What is the output of the following C code?

   int x = 5;
   switch (x) {
       default: printf("Default");
       case 5: printf("Five");
               break;
   }
a) Default
b) Five
c) DefaultFive
d) No output

Answer:

b) Five

Explanation:

Even though the default case comes first, the case 5 matches the value of x, so "Five" is printed and execution stops due to the break statement.

16. In a switch statement, what is the significance of the break statement?

a) It breaks out of the program
b) It stops the case from being executed
c) It prevents fall-through from one case to another
d) It exits the switch statement and resumes execution at the next statement

Answer:

c) It prevents fall-through from one case to another

Explanation:

The break statement in a switch case stops the fall-through behavior, making the program jump to the statement following the switch case.

17. What is the output of the following C code?

   int x = 1;
   if (x)
       printf("True");
   else
       printf("False");
a) True
b) False
c) Error
d) Nothing is printed

Answer:

a) True

Explanation:

In C, non-zero values are treated as true. Since x is 1, the condition in the if statement is true, so "True" is printed.

18. What does the following code do?

   int a = 5, b = 10;
   int max = (a > b) ? a : b;
a) Assigns the larger of a or b to max
b) Compares a and b
c) Adds a and b
d) Subtracts b from a

Answer:

a) Assigns the larger of a or b to max

Explanation:

The code uses the ternary operator to assign the larger of a or b to the variable max.

19. How does an if-else statement differ from a switch case statement in C?

a) if-else can only evaluate boolean expressions, switch case can evaluate any expression
b) if-else is faster than switch case
c) switch case is more flexible than if-else
d) switch case can only evaluate integral or character type expressions

Answer:

d) switch case can only evaluate integral or character type expressions

Explanation:

A switch case is limited to evaluating integral or character type expressions, while if-else statements can evaluate any boolean expression.

20. Can a switch statement be nested inside another switch statement in C?

a) Yes
b) No
c) Only if the outer switch is in the default case
d) Only if the inner switch has no default case

Answer:

a) Yes

Explanation:

Switch statements can be nested within other switch statements or within other control structures like if, for, while, etc.

21. What is the output of the following C code?

   char grade = 'B';
   switch (grade) {
       case 'A': printf("Excellent");
       case 'B': printf("Very Good");
       case 'C': printf("Good");
       case 'D': printf("Fair");
       default: printf("Ungraded");
   }
a) Very Good
b) Good
c) FairUngraded
d) Very GoodGoodFairUngraded

Answer:

d) Very GoodGoodFairUngraded

Explanation:

In the absence of break statements, the switch statement executes all cases from the matched case (B) to the end, including the default.

22. What is the output of the following C code?

   int a = 5, b = 5;
   if (a == b)
       printf("Equal");
   else if (a > b)
       printf("Greater");
   else
       printf("Lesser");
a) Equal
b) Greater
c) Lesser
d) Error

Answer:

a) Equal

Explanation:

Since a and b have the same value, the condition a == b is true and "Equal" is printed.

23. In C, what is the default return value of a switch statement if no case matches and there is no default case?

a) 0
b) -1
c) The value of the last case
d) No value is returned

Answer:

d) No value is returned

Explanation:

If no case matches in a switch statement and there is no default case, the switch statement completes without returning any value.

24. Which of the following is a valid if statement in C?

a) if (x = 5) { /* code */ }
b) if (5 = x) { /* code */ }
c) if (x == 5) { /* code */ }
d) if 5 == x { /* code */ }

Answer:

c) if (x == 5) { /* code */ }

Explanation:

The correct syntax for an if statement includes the 'if' keyword, followed by a condition in parentheses. Option c) uses the correct equality operator (==) and syntax.

25. What will be the behavior of the following C code?

   int x = 10;
   if (x < 15)
       printf("Less than 15\n");
   else if (x < 20)
       printf("Less than 20\n");
a) Prints "Less than 15"
b) Prints "Less than 20"
c) Prints both "Less than 15" and "Less than 20"
d) Error

Answer:

a) Prints "Less than 15"

Explanation:

Since the first condition x < 15 is true, it executes the corresponding printf statement and skips the else if part, printing "Less than 15".

Comments