1. What is the purpose of the 'if' statement in C?
Answer:
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?
Answer:
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?
Answer:
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");
Answer:
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?
Answer:
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?
Answer:
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");
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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'.