1. What is the purpose of an if statement in C?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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");
Answer:
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?
Answer:
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?
Answer:
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");
Answer:
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?
Answer:
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");
}
Answer:
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?
Answer:
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");
Answer:
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?
Answer:
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?
Answer:
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;
}
Answer:
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?
Answer:
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");
Answer:
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;
Answer:
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?
Answer:
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?
Answer:
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");
}
Answer:
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");
Answer:
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?
Answer:
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?
Answer:
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");
Answer:
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
Post a Comment
Leave Comment