Here are 30 interview questions along with their answers on Chapter 3: Control Flow:
if
statement in C?if
statement is used to test a condition. If the condition evaluates to true, the block of code inside the if
statement is executed. Example:c if (a > b) { printf("a is greater than b"); }
if-else
statement in C?if-else
statement is used when you want to execute one block of code if the condition is true and another block if the condition is false. Example:c if (a > b) { printf("a is greater than b"); } else { printf("b is greater than or equal to a"); }
else-if
statement in C?else-if
statement is used to check multiple conditions sequentially. It is an extension of the if-else
statement. Example:c if (a > b) { printf("a is greater than b"); } else if (a < b) { printf("a is less than b"); } else { printf("a is equal to b"); }
switch
statement in C?switch
statement is used to execute one of many code blocks based on the value of a variable or expression. Example:c switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Invalid day"); }
break
statement in control flow?break
statement is used to exit from a loop or switch
statement prematurely, without evaluating the remaining conditions.continue
statement in control flow?continue
statement is used to skip the current iteration of a loop and move to the next iteration.goto
statement in C?goto
statement is used to transfer control to a specific labeled statement within the program. However, it is generally discouraged due to its potential to make code difficult to follow.for
loop in C?for
loop consists of three components: initialization, condition, and increment/decrement. Example:c for (int i = 0; i < 10; i++) { printf("%d\n", i); }
while
loop in C?while
loop runs as long as the specified condition is true. Example:c int i = 0; while (i < 10) { printf("%d\n", i); i++; }
do-while
loop in C?do-while
loop is similar to a while
loop, but it guarantees at least one execution of the loop before checking the condition. Example:c int i = 0; do { printf("%d\n", i); i++; } while (i < 10);
for
, while
, and do-while
loops in C?for
loop: Condition is checked before the loop starts.while
loop: Condition is checked before each iteration.do-while
loop: Condition is checked after the loop executes, ensuring at least one iteration.break
in a switch
statement?break
statement, control will “fall through” to the next case
statement, executing multiple cases unintentionally.return
statement in a function?return
statement is used to exit a function and return a value to the calling function. If no value is returned, the function returns void
.if
statement work in terms of logic?if
statement evaluates the condition inside its parentheses. If the condition is true (non-zero), the block of code following it is executed; otherwise, it is skipped.if
statements in a single program?if
statements can be used to evaluate multiple conditions in a program.if
and switch
statements?if
statement is used for evaluating boolean expressions, while the switch
statement is used to evaluate an integer or character variable against a series of possible values (cases).else
in control flow?else
statement provides an alternative block of code to execute if the condition in the if
statement evaluates to false.switch
statement do when the default
keyword is used?default
case in a switch
statement is executed when none of the case
labels match the evaluated expression.continue
statement in loops?continue
statement skips the current iteration of a loop and continues with the next iteration, checking the loop condition again.switch
statements handle multiple values for a case?switch
statement can only match one value per case
. To handle multiple values, you would need to use multiple case
labels or if-else
statements.switch
case executes only once?break
statement at the end of each case. This prevents fall-through behavior and ensures only one case is executed.goto
statement?goto
statement allows jumping to any part of the program by specifying a label. However, it should be avoided due to its potential to create confusing and unmanageable code.while(1)
or for(;;)
.break
and continue
statements in loops?break
exits the loop entirely, while continue
skips the current iteration and moves to the next iteration of the loop.switch
statement evaluate?switch
statement evaluates an expression (usually an integer or a character) and matches it against the case
labels. If a match is found, the corresponding block is executed.if
statements inside a switch
statement?if
statements inside a switch
statement, but this is not common practice. It is more efficient to handle multiple conditions using if-else
or switch
itself.condition
in an if
statement is not met?condition
in an if
statement is false, the code block inside the if
is skipped and execution continues after the if
block.&&
and ||
operators in C?&&
is the logical AND operator; it returns true if both conditions are true.||
is the logical OR operator; it returns true if at least one of the conditions is true.These questions cover the key concepts of control flow in C programming, focusing on conditional statements, loops, and program structure.