Correct Answer: if (condition) { statements; }
Explanation: The correct syntax for an if statement in C includes the condition within parentheses followed by the statement block within curly braces.
Correct Answer: To execute code when the if condition is false
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.
Correct Answer: if (condition1) { if (condition2) { statements; } }
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.
#include <stdio.h>
int x = 10;
if (x > 5) {
printf("Greater than 5");
} else {
printf("Less than or equal to 5");
}
Correct Answer: Greater than 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.
#include <stdio.h>
int a = 10, b = 20;
if (a > b) {
printf("a is greater than b");
} else {
printf("b is greater than or equal to a");
}
Correct Answer: Prints “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”.
Correct Answer: &&
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.
#include <stdio.h>
int x = 5, y = 10;
if (x < y && y > 5) {
printf("Condition met");
} else {
printf("Condition not met");
}
Correct Answer: Condition met
Explanation: Both conditions x < y and y > 5 are true, so the if statement’s condition is met, and “Condition met” is printed.
Correct Answer: To provide an alternative condition if the initial if condition is false
Explanation: The else if statement allows for multiple conditions to be checked sequentially, providing alternatives if previous conditions are false.
Correct Answer: The switch statement can evaluate integer, character, and enumerated types
Explanation: In C, the switch statement can evaluate expressions of integer, character, and enumerated types, making it versatile for decision-making scenarios.
#include <stdio.h>
int num = 2;
switch (num) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Default");
}
Correct Answer: Two
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.
Correct Answer: else
Explanation: The else keyword is used to execute a block of statements if none of the preceding if or else if conditions are true.
#include <stdio.h>
int x = 7;
if (x % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
Correct Answer: 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”.
Correct Answer: if (x > 0 && x < 10) { statements; }
Explanation: The correct syntax uses the logical AND operator (&&) to check if both conditions are true.
#include <stdio.h>
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");
}
Correct Answer: a is positive and b is greater than 10
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”.
#include <stdio.h>
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");
}
Correct Answer: 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.
Correct Answer: if (condition1) { statements; } else if (condition2) { statements; }
Explanation: The if-else if statement correctly uses the else if keyword to check multiple conditions sequentially.
#include <stdio.h>
int x = 5;
if (x == 5) {
printf("Equal to 5");
} else if (x > 5) {
printf("Greater than 5");
} else {
printf("Less than 5");
}
Correct Answer: Equal to 5
Explanation: The condition x == 5 is true, so the first block is executed, printing “Equal to 5”.
Correct Answer: ||
Explanation: The logical OR operator (||) is used to execute a block of code if at least one of multiple conditions is true.
#include <stdio.h>
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");
}
Correct Answer: x is less than y
Explanation: Since x (5) is less than y (10), the else if block is executed, printing "x is less than y".
Correct Answer: When the if condition is false
Explanation: The else block is executed only when the if condition evaluates to false, providing an alternative set of statements to execute.
#include <stdio.h>
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");
Correct Answer: Error
Explanation: The if-else statement lacks braces {}, so it will result in a syntax error due to ambiguous control flow.
#include <stdio.h>
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");
Correct Answer: x is between 10 and 20
Explanation: The nested if statements evaluate sequentially, so when x is greater than 10 and less than 20, the first printf statement is executed.
Correct Answer: Ternary operator results in shorter, more concise code
Explanation: The ternary operator ?: allows for compact conditional expressions compared to if-else statements.
Correct Answer: (x > 0) ? "Positive" : "Negative"
Explanation: The ternary operator should be used in expressions to produce a value based on a condition, as shown in option A.
#include <stdio.h>
int x = 5;
printf("%s", (x > 0) ? "Positive" : "Negative");
Correct Answer: Positive
Explanation: Since x is greater than 0, the condition (x > 0) evaluates to true, so "Positive" is printed.
Correct Answer: expression2 is executed
Explanation: If the condition in the ternary operator evaluates to false, expression2 is executed.
#include <stdio.h>
int x = 15, y = 10;
printf("%d", (x > y) ? x : y);
Correct Answer: 15
Explanation: Since x (15) is greater than y (10), the ternary operator evaluates to x, so 15 is printed.
#include <stdio.h>
int x = 10;
char* result = (x % 2 == 0) ? "Even" : "Odd";
printf("%s", result);
Correct Answer: Even
Explanation: Since x is divisible by 2, the condition (x % 2 == 0) is true, so "Even" is assigned to the result pointer and printed.
Correct Answer: Ternary operator can lead to reduced code readability for complex conditions
Explanation: While the ternary operator offers concise code, it may reduce readability, especially for complex conditions.
Correct Answer: It evaluates the condition first and then executes one of the two expressions.
Explanation: The ternary operator evaluates the condition first and then chooses which expression to execute based on the condition.
#include <stdio.h>
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");
}
Correct Answer: x is greater than 5 and y is greater than 15
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".
#include <stdio.h>
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");
Correct Answer: x is greater than 10 and y is greater than 5
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".
Correct Answer: To handle complex decision-making scenarios with multiple conditions
Explanation: Nested if statements are used to handle situations where multiple conditions need to be evaluated in a hierarchical manner.
#include <stdio.h>
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");
Correct Answer: Error
Explanation: The lack of braces {} causes the else statement to be associated with the inner if statement, resulting in a syntax error.
Correct Answer: Both A and B
Explanation: Using braces {} and proper indentation enhances code readability and ensures correct execution of nested if-else statements.
#include <stdio.h>
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");
}
Correct Answer: x is greater than 5 and y is greater than 15
Explanation: Both conditions x > 5 and y > 15 are true, so the inner printf statement is executed.
Correct Answer: The inner if statement is skipped
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.
Correct Answer: Increased complexity and decreased readability
Explanation: Deeply nested if-else statements can make code difficult to understand and maintain due to increased complexity.
#include <stdio.h>
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");
Correct Answer: 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.
Correct Answer: To visually represent the hierarchical structure of the code
Explanation: Proper indentation helps in understanding the nesting levels of if-else statements and improves code readability.
Correct Answer: To handle multiple cases based on the value of an expression
Explanation: The switch statement is used to select one of many code blocks to be executed based on the value of an expression.
Correct Answer: float
Explanation: The switch statement in C requires integral expressions (int, char, enum) to determine which case to execute.
#include <stdio.h>
int x = 2;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Default");
}
Correct Answer: Two
Explanation: Since the value of x is 2, the case 2 block is executed, printing "Two".
Correct Answer: To exit the current case and execute the next case
Explanation: The break keyword is used to exit the current case block and prevent the execution of subsequent case blocks.
#include <stdio.h>
int x = 3;
switch (x) {
case 1:
printf("One");
case 2:
printf("Two");
case 3:
printf("Three");
default:
printf("Default");
}
Correct Answer: TwoThreeDefault
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.
Correct Answer: It is optional and can be omitted if not needed.
Explanation: The default case is optional and will be executed if none of the other cases match the value of the expression.
#include <stdio.h>
int x = 5;
switch (x) {
case 1:
case 2:
printf("Two");
break;
case 3:
case 4:
printf("Four");
break;
default:
printf("Other");
}
Correct Answer: Other
Explanation: Since there is no matching case for x=5, the default case is executed, printing "Other".
Correct Answer: A case label can be any constant expression, including floating-point values.
Explanation: In C, a case label can be any constant expression, including integers, characters, or enums.
#include <stdio.h>
switch (grade) {
case 'A':
case 'B':
case 'C':
printf("Pass");
break;
case 'D':
case 'E':
case 'F':
printf("Fail");
break;
default:
printf("Invalid grade");
}
Correct Answer: It determines if the given grade is a passing 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'.
Correct Answer: The switch statement can only be used with integer expressions.
Explanation: While integer expressions are commonly used in switch statements, char and enum types are also allowed.
#include <stdio.h>
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;
}
Correct Answer: Default
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.
Correct Answer: No, each case label must contain a single value.
Explanation: In C, each case label must contain a single value; multiple values cannot be combined in a single case label.
#include <stdio.h>
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent");
break;
case 'B':
printf("Good");
break;
case 'C':
printf("Average");
break;
default:
printf("Fail");
}
Correct Answer: It converts a numerical grade to a letter grade.
Explanation: The switch statement converts the given character grade to a descriptive term (Excellent, Good, Average, or Fail) based on the case labels.
#include <stdio.h>
int x = 3;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
}
Correct Answer: Three
Explanation: Since the value of x is 3, the case 3 block is executed, printing "Three".
Correct Answer: The program will continue executing the next case block.
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.
#include <stdio.h>
int x = 2;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
case 3:
printf("Three");
break;
}
Correct Answer: TwoThree
Explanation: Since there is no break statement after case 2, the program falls through to the case 3 block, resulting in "TwoThree" being printed.
Correct Answer: The expressions in case labels can only be constant integers.
Explanation: In C, the expressions in case labels must be constant integers; they cannot be evaluated at runtime.
#include <stdio.h>
int x = 5;
switch (x) {
case 4:
printf("Four");
break;
case 5:
printf("Five");
case 6:
printf("Six");
break;
}
Correct Answer: FiveSix
Explanation: Since there is no break statement after case 5, the program falls through to the case 6 block, resulting in "FiveSix" being printed.
Correct Answer: The program executes the first matching case block.
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.
Correct Answer: The default case can be placed anywhere within the switch statement.
Explanation: The default case should typically be placed at the end of the switch statement, although it is not mandatory.
Correct Answer: To simplify if-else statements
Explanation: The conditional operator provides a concise way to express conditional statements, often used as a shorthand for simple if-else constructs.
Correct Answer: It has higher precedence than the arithmetic operators.
Explanation: The conditional operator has higher precedence than most of the other operators, including arithmetic operators.
#include <stdio.h>
int x = 10, y = 5;
int z = (x > y) ? x : y;
Correct Answer: 10
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.
Correct Answer: value2 is assigned to the variable
Explanation: If the condition in the conditional operator evaluates to false, value2 is assigned to the variable.
#include <stdio.h>
int x = 15, y = 20;
int z = (x > y) ? (x - y) : (y - x);
printf("%d", z);
Correct Answer: -5
Explanation: Since x is not greater than y, the condition (x > y) evaluates to false, so (y - x) is calculated, resulting in -5.
Correct Answer: int result = (x > 0) ? 1 : 0;
Explanation: Option B demonstrates a correct usage where the conditional operator returns different values based on the condition.
#include <stdio.h>
int x = 10, y = 5;
printf("%d", (x > y) ? x : y);
Correct Answer: 10
Explanation: Since x is greater than y, the condition (x > y) evaluates to true, so the value of x (10) is printed.
#include <stdio.h>
int x = 5;
printf("%s", (x > 0) ? "Positive" : "Negative");
Correct Answer: Positive
Explanation: Since x is greater than 0, the condition (x > 0) evaluates to true, so "Positive" is printed.
Correct Answer: An error occurs
Explanation: The conditional operator expects expressions as value1 and value2, not functions. Attempting to use functions will result in a syntax error.
#include <stdio.h>
int x = 10, y = 20;
int z = (x > y) ? x++ : y--;
Correct Answer: 20
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.
#include <stdio.h>
int x = 10, y = 5;
int z = (x > y) ? (x *= 2, y *= 2) : (x /= 2, y /= 2);
printf("%d, %d", x, y);
Correct Answer: 20, 10
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).
Correct Answer: (x > y) ? (x++) : (y++);
Explanation: Option C demonstrates a valid usage where the conditional operator increments x if x is greater than y, otherwise increments y.
#include <stdio.h>
int x = 5, y = 10;
int z = (x > y) ? x : (y > x) ? (y *= 2, y) : (x *= 2, x);
printf("%d", z);
Correct Answer: 20
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.
Correct Answer: Both side effects are executed
Explanation: The conditional operator evaluates both value1 and value2, so if they have side effects, both will be executed.
#include <stdio.h>
int x = 10, y = 5;
int z = (x > y) ? (x++, y++) : (x--, y--);
printf("%d, %d", x, y);
Correct Answer: 11, 5
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.
#include <stdio.h>
int x = 10, y = 5;
int z = (x > y) ? x : y;
Correct Answer: It determines the larger of the two numbers, x and y.
Explanation: The conditional operator selects the value of x if x is greater than y, otherwise it selects the value of y.
#include <stdio.h>
int x = 5, y = 10;
int z = (x < y) ? (x += 2, y += 2) : (x -= 2, y -= 2);
printf("%d, %d", x, y);
Correct Answer: 7, 12
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.
Correct Answer: (x > y) ? 1 : 0;
Explanation: Option D demonstrates a correct usage where the conditional operator returns different values based on the condition.
#include <stdio.h>
int x = 10, y = 5;
int z = (x > y) ? (y += 2, x += y) : (x -= 2, y += x);
printf("%d", z);
Correct Answer: 17
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.
#include <stdio.h>
int x = 5, y = 10;
int z = (x > y) ? (x + 2, y - 2) : (x - 2, y + 2);
printf("%d, %d", x, y);
Correct Answer: 5, 10
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.