Section A — 1 Mark Questions
1. Define a control structure. [1 Mark]
Answer: A control structure is a block of programming that analyzes variables and chooses a direction in which to go based on given parameters.
2. What is the purpose of an if-statement? [1 Mark]
Answer: It allows the program to execute a specific block of code only if a given condition is true.
3. Name one type of iterative control structure. [1 Mark]
Answer: The 'for' loop.
4. What is a jumping control structure? [1 Mark]
Answer: A structure that allows the transfer of control from one part of the program to another, such as 'break' or 'continue'.
5. What does the 'else' keyword signify in decision-making? [1 Mark]
Answer: It defines a block of code to be executed if the condition in the associated 'if' statement is false.
6. Which loop structure is known as a pre-test loop? [1 Mark]
Answer: The 'while' loop.
7. What is the primary function of the 'break' statement? [1 Mark]
Answer: It terminates the execution of the nearest enclosing loop or switch statement immediately.
8. Define iteration. [1 Mark]
Answer: Iteration is the process of repeating a set of instructions a specific number of times or until a condition is met.
9. What is a nested loop? [1 Mark]
Answer: A nested loop is a loop placed inside the body of another loop.
10. What is the function of the 'continue' statement? [1 Mark]
Answer: It skips the current iteration of a loop and proceeds to the next iteration.
11. Which statement is used for multiple-branch decision making? [1 Mark]
Answer: The 'switch' statement.
12. What is a condition in a control structure? [1 Mark]
Answer: A boolean expression that evaluates to either true or false.
13. How many times does a 'do-while' loop execute at least? [1 Mark]
Answer: It executes at least once.
14. What happens when a 'return' statement is executed within a function? [1 Mark]
Answer: It exits the current function and optionally returns a value to the caller.
15. What is an infinite loop? [1 Mark]
Answer: A loop that runs indefinitely because its termination condition is never met.
16. What is the syntax for a basic 'if' condition? [1 Mark]
Answer: if (condition) { // code to execute }
17. Is 'switch' considered a decision-making structure? [1 Mark]
Answer: Yes, it is a multi-way decision-making control structure.
18. What does a counter variable do in a 'for' loop? [1 Mark]
Answer: It tracks the number of iterations performed by the loop.
19. Define a block of code. [1 Mark]
Answer: A collection of statements enclosed within curly braces {} that are treated as a single unit.
20. What is the purpose of a 'default' case in a switch? [1 Mark]
Answer: It provides code to execute if none of the cases match the switch expression.
Section B — 2/3 Mark Questions
1. Differentiate between 'if' and 'if-else' statements. [2 Marks]
Answer: An 'if' statement executes a block of code only when the condition is true. An 'if-else' statement provides an alternative path, executing a different block of code when the condition is false.
2. Explain the concept of a 'for' loop with its structure. [3 Marks]
Answer: A 'for' loop is used for iterating over a block of code a known number of times. It consists of three parts: initialization (setting the start value), condition (checking when to stop), and increment/decrement (updating the counter).
3. What is the difference between 'break' and 'continue'? [2 Marks]
Answer: 'Break' terminates the entire loop execution immediately. 'Continue' only terminates the current iteration and jumps to the next iteration of the loop.
4. Why is a 'do-while' loop called a post-test loop? [2 Marks]
Answer: It is called a post-test loop because the condition is evaluated after the block of code inside the loop has been executed at least once.
5. Explain the role of the switch-case structure. [3 Marks]
Answer: The switch statement allows a variable to be tested for equality against a list of values (cases). It is more readable than multiple 'if-else' statements when checking a single variable against many discrete values.
6. Describe how an infinite loop can be created accidentally. [2 Marks]
Answer: An infinite loop is created if the loop condition never evaluates to false, often due to an incorrect condition expression or forgetting to update the control variable within the loop body.
7. List the three types of control structures. [3 Marks]
Answer: The three types are: 1. Decision-making controls (e.g., if, switch), 2. Iterative controls (e.g., for, while), and 3. Jumping controls (e.g., break, continue, return).
8. What is a nested 'if' statement? [2 Marks]
Answer: A nested 'if' statement is an 'if' structure placed inside another 'if' or 'else' block, used to test multiple related conditions sequentially.
9. When should you use a 'while' loop over a 'for' loop? [3 Marks]
Answer: A 'while' loop is preferred when the number of iterations is not known beforehand and the loop depends entirely on a changing condition, whereas 'for' loops are better suited for scenarios with a fixed number of iterations.
10. Explain the importance of the 'default' keyword in a switch statement. [2 Marks]
Answer: The 'default' keyword is important because it ensures that the program has a defined behavior if none of the explicit 'case' values match the variable, preventing undefined states.
Section C — 5 Mark Questions
1. Explain the working of decision-making control structures with examples. [5 Marks]
Answer:
Introduction
Decision-making structures allow a program to change its execution path based on the evaluation of conditions.
Main Types
- If-Statement: Evaluates a boolean condition. If true, the code executes.
- If-Else-If Ladder: Used for checking multiple conditions sequentially.
- Switch Statement: Efficient for comparing a variable against a set of constant values.
Example
If (score > 50) { print 'Pass'; } else { print 'Fail'; }. This structure evaluates the score variable and branches the output accordingly.
Application
These are essential for handling user input, validating data, and implementing business logic in software.
2. Discuss the types of iterative control structures and their differences. [5 Marks]
Answer:
The Three Iterative Structures
- For Loop: Best for scenarios where the exact number of iterations is known. It combines initialization, condition, and update in one line.
- While Loop: A pre-test loop that continues as long as the condition remains true. If the condition is false initially, the body never runs.
- Do-While Loop: A post-test loop. It ensures that the loop body executes at least once regardless of the condition.
Comparison Table
| Feature | For Loop | While Loop |
|---|---|---|
| Iteration count | Known | Unknown |
| Test timing | Pre-test | Pre-test |
Each loop serves a specific purpose depending on whether the termination criteria involve counters or boolean flags.
3. Elaborate on jumping control structures with their practical applications. [5 Marks]
Answer:
Definition
Jumping statements alter the normal sequential flow of a program by transferring control to another part of the code.
Key Jumping Statements
- Break: Used to exit a loop or switch case prematurely. Essential for stopping a search loop once an item is found.
- Continue: Used to skip the remainder of the current iteration and jump to the next one. Useful for ignoring invalid data entries in a loop.
- Return: Exits the current function, passing control back to the point of origin.
Importance
These structures provide finer control over loops, enabling developers to handle exceptions or specific conditions that require immediate termination of an activity without stopping the entire program.
4. Explain the concept of nested loops with a suitable example. [5 Marks]
Answer:
Concept
A nested loop occurs when one loop is placed inside the body of another. The inner loop completes its full cycle for every single iteration of the outer loop.
How it Works
The outer loop acts as the primary controller, while the inner loop manages secondary tasks. For each step of the outer loop, the inner loop restarts its logic.
Example
Printing a 3x3 matrix. The outer loop handles the rows, and the inner loop handles the columns. For (i=1 to 3) { For (j=1 to 3) { print(i, j); } }. This structure is highly useful for processing 2D arrays and complex table-based data structures.
5. Compare 'if-else' ladder and 'switch' statements. [5 Marks]
Answer:
Comparison
Both allow multi-way branching, but they are used in different contexts.
Differences
- Flexibility: If-else can test complex boolean expressions (e.g., a > 10 && b < 5), while switch only tests equality against constants.
- Readability: Switch is cleaner when checking a single variable against many possible values.
- Performance: Compilers often optimize switch statements into jump tables, making them faster than long chains of if-else checks.
Conclusion
Use 'if-else' for range-based or complex conditional logic, and use 'switch' when dealing with discrete, fixed, or enumerated values to ensure cleaner and more maintainable code.