Interview Questions on Chapter 3: Control Flow

Here are 30 interview questions along with their answers on Chapter 3: Control Flow:

Chapter 3: Control Flow

1. What is a control flow in C?

  • Answer: Control flow in C refers to the order in which individual statements, instructions, or function calls are executed or evaluated. It determines the sequence in which program statements are run based on certain conditions.

2. What is an if statement in C?

  • Answer: The 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"); }

3. What is an if-else statement in C?

  • Answer: The 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"); }

4. What is an else-if statement in C?

  • Answer: The 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"); }

5. What is a switch statement in C?

  • Answer: The 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"); }

6. What is the purpose of the break statement in control flow?

  • Answer: The break statement is used to exit from a loop or switch statement prematurely, without evaluating the remaining conditions.

7. What is the purpose of the continue statement in control flow?

  • Answer: The continue statement is used to skip the current iteration of a loop and move to the next iteration.

8. What is a goto statement in C?

  • Answer: The 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.

9. What is the syntax of a for loop in C?

  • Answer: The syntax of a for loop consists of three components: initialization, condition, and increment/decrement. Example:
    c for (int i = 0; i < 10; i++) { printf("%d\n", i); }

10. What is the syntax of a while loop in C?

  • Answer: The while loop runs as long as the specified condition is true. Example:
    c int i = 0; while (i < 10) { printf("%d\n", i); i++; }

11. What is the syntax of a do-while loop in C?

  • Answer: The 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);

12. What is the difference between for, while, and do-while loops in C?

  • Answer: The main difference is when the condition is checked:
    • 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.

13. What happens if there is no break in a switch statement?

  • Answer: Without a break statement, control will “fall through” to the next case statement, executing multiple cases unintentionally.

14. What is a return statement in a function?

  • Answer: The 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.

15. How does the if statement work in terms of logic?

  • Answer: The 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.

16. Can we use multiple if statements in a single program?

  • Answer: Yes, multiple if statements can be used to evaluate multiple conditions in a program.

17. What is the difference between if and switch statements?

  • Answer: The 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).

18. What is the purpose of else in control flow?

  • Answer: The else statement provides an alternative block of code to execute if the condition in the if statement evaluates to false.

19. What does the switch statement do when the default keyword is used?

  • Answer: The default case in a switch statement is executed when none of the case labels match the evaluated expression.

20. What is the role of the continue statement in loops?

  • Answer: The continue statement skips the current iteration of a loop and continues with the next iteration, checking the loop condition again.

21. Can switch statements handle multiple values for a case?

  • Answer: No, a 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.

22. How do you ensure that a switch case executes only once?

  • Answer: By using the break statement at the end of each case. This prevents fall-through behavior and ensures only one case is executed.

23. What is the significance of a goto statement?

  • Answer: The 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.

24. What is a nested loop in C?

  • Answer: A nested loop is a loop inside another loop. The inner loop runs completely for each iteration of the outer loop.

25. What is an infinite loop?

  • Answer: An infinite loop is a loop that never terminates. It occurs when the loop condition is always true, such as in while(1) or for(;;).

26. What is the difference between break and continue statements in loops?

  • Answer: break exits the loop entirely, while continue skips the current iteration and moves to the next iteration of the loop.

27. What does the switch statement evaluate?

  • Answer: The 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.

28. Can you use if statements inside a switch statement?

  • Answer: Yes, you can place 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.

29. What happens if the condition in an if statement is not met?

  • Answer: If the condition in an if statement is false, the code block inside the if is skipped and execution continues after the if block.

30. What is the difference between && and || operators in C?

  • Answer:
    • && 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.

Scroll to Top