C If … Else MCQ

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

a) To iterate over a block of code
b) To execute a block of code based on a condition
c) To declare a variable
d) To terminate a loop

Answer:

b) To execute a block of code based on a condition

Explanation:

The 'if' statement is used to execute a block of code only if a specified condition is true.

2. How do you write an 'if' statement in C to check if a variable 'a' is equal to 10?

a) if a = 10 { }
b) if (a == 10) { }
c) if a == 10 { }
d) if (a = 10) { }

Answer:

b) if (a == 10) { }

Explanation:

In C, the '==' operator is used for comparison, and conditions in an 'if' statement are enclosed in parentheses.

3. What is the role of the 'else' statement in C?

a) To define an alternative block of code if the 'if' condition is false
b) To repeat a block of code
c) To create a loop
d) To define a function

Answer:

a) To define an alternative block of code if the 'if' condition is false

Explanation:

The 'else' statement is used to execute a different block of code if the 'if' condition is not met.

4. What will be the output of the following C code snippet?

int x = 5;
if (x > 10)
   printf("Greater");
else
   printf("Smaller");
a) Greater
b) Smaller
c) No output
d) Error

Answer:

b) Smaller

Explanation:

Since x is 5, which is not greater than 10, the 'else' block will execute, printing "Smaller".

5. Which of the following is a valid 'if…else if…else' statement in C?

a) if (condition) { } else if (condition) { } else { }
b) if condition { } else if condition { } else { }
c) if (condition) else if (condition) else { }
d) if condition else if condition else { }

Answer:

a) if (condition) { } else if (condition) { } else { }

Explanation:

The correct syntax includes parentheses around the conditions and braces for the blocks of code.

6. How do you write a nested 'if' statement in C?

a) if (condition) { if (condition) { } }
b) if condition { if condition { } }
c) if (condition) if (condition) { }
d) nested if (condition) { if (condition) { } }

Answer:

a) if (condition) { if (condition) { } }

Explanation:

Nested 'if' statements in C are written by placing an 'if' statement inside the block of another 'if' statement.

7. What will the following C code snippet print?

int num = 8;
if (num > 5)
   printf("Hi");
else if (num > 10)
   printf("Hello");
else
   printf("Hey");
a) Hi
b) Hello
c) Hey
d) No output

Answer:

a) Hi

Explanation:

Since num is greater than 5, the first 'if' condition is true, so "Hi" will be printed.

8. What is the correct way to check multiple conditions in a single 'if' statement in C?

a) if (condition1 && condition2) { }
b) if condition1 and condition2 { }
c) if (condition1) && (condition2) { }
d) if condition1 && condition2 { }

Answer:

a) if (condition1 && condition2) { }

Explanation:

The '&&' operator is used to check if both conditions are true in a single 'if' statement.

9. What does the 'else' block contain in an 'if…else' statement?

a) The condition to be checked
b) The code to be executed if the 'if' condition is true
c) The code to be executed if the 'if' condition is false
d) A mandatory return statement

Answer:

c) The code to be executed if the 'if' condition is false

Explanation:

The 'else' block contains code that is executed when the 'if' condition is not met.

10. Which of the following statements about the 'if…else' structure in C is true?

a) The 'else' part is mandatory in an 'if' statement.
b) An 'if' statement can have multiple 'else' blocks.
c) The 'if' condition can include logical, relational, and arithmetic expressions.
d) The 'else' block can exist without an 'if' block.

Answer:

c) The 'if' condition can include logical, relational, and arithmetic expressions.

Explanation:

The condition in an 'if' statement in C can be a complex expression involving logical, relational, and arithmetic operators. The 'else' part is not mandatory, and there can be only one 'else' block for each 'if'.

Leave a Comment

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

Scroll to Top