Explanation: The correct syntax for an if statement in C includes the condition within parentheses followed by the statement block within curly braces.
Explanation: The else statement in C is used to specify a block of code to be executed if the condition in the if statement evaluates to false.
Explanation: In C, nested if statements are used to check multiple conditions and should be structured properly with each if statement within its own block of curly braces.
int x = 10;
if (x > 5) {
printf("Greater than 5");
} else {
printf("Less than or equal to 5");
}
Explanation: Since the value of x is 10, which is greater than 5, the condition in the if statement is true, so “Greater than 5” will be printed.
int a = 10, b = 20;
if (a > b) {
printf("a is greater than b");
} else {
printf("b is greater than or equal to a");
}
Explanation: Since a is not greater than b, the else block is executed, printing “b is greater than or equal to a”.
Explanation: The logical AND operator (&&) is used to combine multiple conditions in an if statement in C, ensuring that all conditions must be true for the overall condition to be true.
int x = 5, y = 10;
if (x < y && y > 5) {
printf("Condition met");
} else {
printf("Condition not met");
}
Explanation: Both conditions x < y and y > 5 are true, so the if statement’s condition is met, and “Condition met” is printed.
Explanation: The else if statement allows for multiple conditions to be checked sequentially, providing alternatives if previous conditions are false.
Explanation: In C, the switch statement can evaluate expressions of integer, character, and enumerated types, making it versatile for decision-making scenarios.
int num = 2;
switch (num) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Default");
}
Explanation: The variable num has a value of 2, so the case 2 block is executed, printing “Two”. The break statement then terminates the switch statement.
Explanation: The else keyword is used to execute a block of statements if none of the preceding if or else if conditions are true.
int x = 7;
if (x % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
Explanation: The value of x is 7, which is not divisible by 2 (x % 2 is not equal to 0), so the else block is executed, printing “Odd”.
Explanation: The correct syntax uses the logical AND operator (&&) to check if both conditions are true.
int a = 5, b = 15;
if (a > 0) {
if (b > 10) {
printf("a is positive and b is greater than 10");
} else {
printf("a is positive but b is not greater than 10");
}
} else {
printf("a is not positive");
}
Explanation: Both conditions a > 0 and b > 10 are true, so the innermost if block is executed, printing “a is positive and b is greater than 10”.
int x = 3, y = 10;
if (x > 5) {
printf("x is greater than 5");
} else if (y > 5) {
printf("y is greater than 5");
} else {
printf("Neither x nor y is greater than 5");
}
Explanation: The first condition x > 5 is false, so the else if condition y > 5 is evaluated, which is true, hence “y is greater than 5” is printed.
Explanation: The if-else if statement correctly uses the else if keyword to check multiple conditions sequentially.
int x = 5;
if (x == 5) {
printf("Equal to 5");
} else if (x > 5) {
printf("Greater than 5");
} else {
printf("Less than 5");
}
Explanation: The condition x == 5 is true, so the first block is executed, printing “Equal to 5”.
Explanation: The logical OR operator (||) is used to execute a block of code if at least one of multiple conditions is true.
int x = 5, y = 10;
if (x > y) {
printf("x is greater than y");
} else if (x < y) {
printf("x is less than y");
} else {
printf("x is equal to y");
}
Explanation: Since x (5) is less than y (10), the else if block is executed, printing "x is less than y".
Explanation: The else block is executed only when the if condition evaluates to false, providing an alternative set of statements to execute.
int x = 10;
if (x > 5)
printf("x is greater than 5");
printf("This statement is always executed");
else
printf("x is less than or equal to 5");
Explanation: The if-else statement lacks braces {}, so it will result in a syntax error due to ambiguous control flow.
int x = 15;
if (x > 10)
if (x < 20)
printf("x is between 10 and 20");
else
printf("x is greater than or equal to 20");
else
printf("x is less than or equal to 10");
Explanation: The nested if statements evaluate sequentially, so when x is greater than 10 and less than 20, the first printf statement is executed.
Explanation: The ternary operator ?: allows for compact conditional expressions compared to if-else statements.
Explanation: The ternary operator should be used in expressions to produce a value based on a condition, as shown in option A.
int x = 5;
printf("%s", (x > 0) ? "Positive" : "Negative");
Explanation: Since x is greater than 0, the condition (x > 0) evaluates to true, so "Positive" is printed.
Explanation: If the condition in the ternary operator evaluates to false, expression2 is executed.
int x = 15, y = 10;
printf("%d", (x > y) ? x : y);
Explanation: Since x (15) is greater than y (10), the ternary operator evaluates to x, so 15 is printed.
int x = 10;
char* result = (x % 2 == 0) ? "Even" : "Odd";
printf("%s", result);
Explanation: Since x is divisible by 2, the condition (x % 2 == 0) is true, so "Even" is assigned to the result pointer and printed.
Explanation: While the ternary operator offers concise code, it may reduce readability, especially for complex conditions.
Explanation: The ternary operator evaluates the condition first and then chooses which expression to execute based on the condition.
int x = 10, y = 20;
if (x > 5) {
if (y > 15) {
printf("x is greater than 5 and y is greater than 15");
} else {
printf("x is greater than 5 but y is not greater than 15");
}
} else {
printf("x is not greater than 5");
}
Explanation: Both conditions x > 5 and y > 15 are true, so the innermost if block is executed, printing "x is greater than 5 and y is greater than 15".
int x = 15, y = 10;
if (x > 10)
if (y > 5)
printf("x is greater than 10 and y is greater than 5");
else
printf("x is greater than 10 but y is not greater than 5");
else
printf("x is not greater than 10");
Explanation: Both conditions x > 10 and y > 5 are true, so the innermost if block is executed, printing "x is greater than 10 and y is greater than 5".
Explanation: Nested if statements are used to handle situations where multiple conditions need to be evaluated in a hierarchical manner.
int x = 10, y = 5;
if (x > 5)
if (y > 10)
printf("x is greater than 5 and y is greater than 10");
else
printf("x is not greater than 5 or y is not greater than 10");
Explanation: The lack of braces {} causes the else statement to be associated with the inner if statement, resulting in a syntax error.
Explanation: Using braces {} and proper indentation enhances code readability and ensures correct execution of nested if-else statements.
int x = 10, y = 20;
if (x > 5) {
if (y > 15)
printf("x is greater than 5 and y is greater than 15");
} else {
printf("x is not greater than 5");
}
Explanation: Both conditions x > 5 and y > 15 are true, so the inner printf statement is executed.
Explanation: If the condition in an outer if statement is false, the inner if statement associated with it is skipped, and the program moves to the next statement.
Explanation: Deeply nested if-else statements can make code difficult to understand and maintain due to increased complexity.
int x = 5, y = 10;
if (x > 3)
if (y > 5)
printf("x is greater than 3 and y is greater than 5");
else
printf("x is not greater than 3 or y is not greater than 5");
Explanation: The lack of braces causes the else statement to be associated with the inner if statement, resulting in "x is not greater than 3 or y is not greater than 5" being printed when x > 3 but y is not greater than 5.
Explanation: Proper indentation helps in understanding the nesting levels of if-else statements and improves code readability.
Explanation: The switch statement is used to select one of many code blocks to be executed based on the value of an expression.
Explanation: The switch statement in C requires integral expressions (int, char, enum) to determine which case to execute.
int x = 2;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Default");
}
Explanation: Since the value of x is 2, the case 2 block is executed, printing "Two".
Explanation: The break keyword is used to exit the current case block and prevent the execution of subsequent case blocks.
int x = 3;
switch (x) {
case 1:
printf("One");
case 2:
printf("Two");
case 3:
printf("Three");
default:
printf("Default");
}
Explanation: Since there are no break statements, once a matching case is found (case 3), all subsequent cases are executed until a break statement is encountered.
Explanation: The default case is optional and will be executed if none of the other cases match the value of the expression.
int x = 5;
switch (x) {
case 1:
case 2:
printf("Two");
break;
case 3:
case 4:
printf("Four");
break;
default:
printf("Other");
}
Explanation: Since there is no matching case for x=5, the default case is executed, printing "Other".
Explanation: In C, a case label can be any constant expression, including integers, characters, or enums.
switch (grade) {
case 'A':
case 'B':
case 'C':
printf("Pass");
break;
case 'D':
case 'E':
case 'F':
printf("Fail");
break;
default:
printf("Invalid grade");
}
Explanation: The switch statement checks the value of the grade variable and prints "Pass" if it is 'A', 'B', or 'C', and "Fail" if it is 'D', 'E', or 'F'.
Explanation: While integer expressions are commonly used in switch statements, char and enum types are also allowed.
int x = 5;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
default:
printf("Default");
break;
case 4:
printf("Four");
break;
}
Explanation: The default case is executed since there is no matching case for x=5. The program does not continue to the case 4 block.
Explanation: In C, each case label must contain a single value; multiple values cannot be combined in a single case label.
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent");
break;
case 'B':
printf("Good");
break;
case 'C':
printf("Average");
break;
default:
printf("Fail");
}
Explanation: The switch statement converts the given character grade to a descriptive term (Excellent, Good, Average, or Fail) based on the case labels.
int x = 3;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
}
Explanation: Since the value of x is 3, the case 3 block is executed, printing "Three".
Explanation: If there is no break statement after a case block, the program will continue executing the code in subsequent case blocks until a break statement is encountered.
int x = 2;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
case 3:
printf("Three");
break;
}
Explanation: Since there is no break statement after case 2, the program falls through to the case 3 block, resulting in "TwoThree" being printed.
Explanation: In C, the expressions in case labels must be constant integers; they cannot be evaluated at runtime.
int x = 5;
switch (x) {
case 4:
printf("Four");
break;
case 5:
printf("Five");
case 6:
printf("Six");
break;
}
Explanation: Since there is no break statement after case 5, the program falls through to the case 6 block, resulting in "FiveSix" being printed.
Explanation: In a switch statement, when a case label's expression matches multiple case labels, the program executes the first matching case block and then exits the switch statement.
Explanation: The default case should typically be placed at the end of the switch statement, although it is not mandatory.
Explanation: The conditional operator provides a concise way to express conditional statements, often used as a shorthand for simple if-else constructs.
Explanation: The conditional operator has higher precedence than most of the other operators, including arithmetic operators.
int x = 10, y = 5;
int z = (x > y) ? x : y;
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so z will be assigned the value of x, which is 10.
Explanation: If the condition in the conditional operator evaluates to false, value2 is assigned to the variable.
int x = 15, y = 20;
int z = (x > y) ? (x - y) : (y - x);
printf("%d", z);
Explanation: Since x is not greater than y, the condition (x > y) evaluates to false, so (y - x) is calculated, resulting in -5.
Explanation: Option B demonstrates a correct usage where the conditional operator returns different values based on the condition.
int x = 10, y = 5;
printf("%d", (x > y) ? x : y);
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so the value of x (10) is printed.
int x = 5;
printf("%s", (x > 0) ? "Positive" : "Negative");
Explanation: Since x is greater than 0, the condition (x > 0) evaluates to true, so "Positive" is printed.
Explanation: The conditional operator expects expressions as value1 and value2, not functions. Attempting to use functions will result in a syntax error.
int x = 10, y = 20;
int z = (x > y) ? x++ : y--;
Explanation: Since x is not greater than y, the condition (x > y) evaluates to false, so y-- is executed, resulting in z being assigned the value of y, which is 20.
int x = 10, y = 5;
int z = (x > y) ? (x *= 2, y *= 2) : (x /= 2, y /= 2);
printf("%d, %d", x, y);
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so the expression (x *= 2, y *= 2) is executed, resulting in x being doubled (20) and y remaining unchanged (5).
Explanation: Option C demonstrates a valid usage where the conditional operator increments x if x is greater than y, otherwise increments y.
int x = 5, y = 10;
int z = (x > y) ? x : (y > x) ? (y *= 2, y) : (x *= 2, x);
printf("%d", z);
Explanation: Since x is not greater than y, the condition (x > y) evaluates to false. Then, the second condition (y > x) evaluates to true, so y is doubled (20) and assigned to z.
Explanation: The conditional operator evaluates both value1 and value2, so if they have side effects, both will be executed.
int x = 10, y = 5;
int z = (x > y) ? (x++, y++) : (x--, y--);
printf("%d, %d", x, y);
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so the expression (x++, y++) is executed, incrementing both x and y.
int x = 10, y = 5;
int z = (x > y) ? x : y;
Explanation: The conditional operator selects the value of x if x is greater than y, otherwise it selects the value of y.
int x = 5, y = 10;
int z = (x < y) ? (x += 2, y += 2) : (x -= 2, y -= 2);
printf("%d, %d", x, y);
Explanation: Since x is less than y, the condition (x < y) evaluates to true, so the expression (x += 2, y += 2) is executed, incrementing both x and y.
Explanation: Option D demonstrates a correct usage where the conditional operator returns different values based on the condition.
int x = 10, y = 5;
int z = (x > y) ? (y += 2, x += y) : (x -= 2, y += x);
printf("%d", z);
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so the expression (y += 2, x += y) is executed, incrementing y by 2 and then adding it to x.
int x = 5, y = 10;
int z = (x > y) ? (x + 2, y - 2) : (x - 2, y + 2);
printf("%d, %d", x, y);
Explanation: Since x is not greater than y, the condition (x > y) evaluates to false. In the else part, the expressions (x - 2, y + 2) are evaluated but the result is not assigned to any variable, so x and y remain unchanged.