Section A — 1 Mark Questions
1. What is the syntax of the simple if statement? [1 Mark]
Answer: if (condition) { statements; }
2. Give the purpose of the else if ladder. [1 Mark]
Answer: It is used to evaluate multiple mutually exclusive conditions sequentially.
3. What is a nested if statement? [1 Mark]
Answer: An if or else-if statement placed inside another if or else statement.
4. Which operator is known as the conditional or ternary operator? [1 Mark]
Answer: The (? :) operator.
5. How many operands does the conditional operator take? [1 Mark]
Answer: Three operands.
6. What is the main use of the switch statement? [1 Mark]
Answer: To select one of many code blocks for execution based on the value of a single variable or expression.
7. What keyword is used to terminate a case in a switch statement? [1 Mark]
Answer: break
8. What is the role of the default case in a switch statement? [1 Mark]
Answer: It executes when none of the preceding case constants match the switch expression.
9. Name the three core looping constructs in C and C++. [1 Mark]
Answer: for, while, and do-while loops.
10. Which loop is known as an entry-controlled loop where the test condition is evaluated before executing the loop body? [1 Mark]
Answer: The while loop.
11. Which loop is known as an exit-controlled loop? [1 Mark]
Answer: The do-while loop.
12. State the minimum number of times a do-while loop executes its body. [1 Mark]
Answer: At least once.
13. What are the three main components of a for loop header? [1 Mark]
Answer: Initialization, condition, and increment/decrement.
14. What is a nested loop? [1 Mark]
Answer: A loop inside the body of another loop.
15. Which jump statement is used to immediately terminate the execution of a loop? [1 Mark]
Answer: break
16. What does the continue statement do inside a loop? [1 Mark]
Answer: It skips the remaining statements in the current iteration and forces the next iteration to begin.
17. Can a switch expression evaluate to a floating-point type in standard C/C++? [1 Mark]
Answer: No, switch expressions typically require integer or character types.
18. What happens if a break statement is omitted in a switch case? [1 Mark]
Answer: Program execution falls through to the next case without checking its label.
19. Write the general form of the conditional operator. [1 Mark]
Answer: condition ? expression1 : expression2;
20. Which jump statement can transfer control unconditionally to a labeled statement anywhere in the function? [1 Mark]
Answer: goto
Section B — 2/3 Mark Questions
1. Explain the working of the if-else statement with a short example. [2 Marks]
Answer: An if-else statement evaluates a boolean condition. If the condition evaluates to true, the block of code inside the if part is executed; otherwise, the block inside the else part is executed. Example: if (x > 0) cout << "Positive"; else cout << "Non-positive";
2. Differentiate between entry-controlled and exit-controlled loops. [2 Marks]
Answer: In an entry-controlled loop (like for or while), the condition is checked before entering the loop body, meaning the loop might not execute at all. In an exit-controlled loop (like do-while), the condition is checked at the end of the loop body, ensuring that the body executes at least once.
3. What is the purpose of the break statement in loops? Give an example. [2 Marks]
Answer: The break statement is used to prematurely exit from a loop or switch statement, ignoring the remaining iterations or cases. Example: for(int i=0; i<10; i++) { if(i == 5) break; }
4. Explain the conditional operator with a syntax and example. [3 Marks]
Answer: The conditional operator (? :) is a ternary operator used as a shorthand for simple if-else statements. Syntax: variable = (condition) ? expressionTrue : expressionFalse; Example: int max = (a > b) ? a : b; This assigns a to max if a is greater than b, otherwise it assigns b.
5. Write a short note on nested loops with an example of structure. [3 Marks]
Answer: A nested loop is a loop placed inside the body of another loop. For each iteration of the outer loop, the inner loop executes completely. They are commonly used for handling multidimensional data like matrices. Example structure: for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { statement; } }
6. Compare the while loop and the do-while loop. [3 Marks]
Answer: 1. While is entry-controlled; do-while is exit-controlled. 2. While loop body may execute zero times if the condition is initially false; do-while executes at least once. 3. While checks the condition at the top; do-while checks the condition using a trailing semicolon at the bottom.
7. Explain the role of the continue statement with an example. [2 Marks]
Answer: The continue statement skips the current iteration of a loop and immediately proceeds to the next iteration step. Example: for(int i=0; i<5; i++) { if(i == 2) continue; cout << i; } This will print 0, 1, 3, 4, omitting 2.
8. What is an else if ladder? Why is it preferred over multiple independent if statements? [3 Marks]
Answer: An else if ladder chains multiple conditions together (if...else if...else). It is preferred over multiple independent if statements because it stops evaluating further conditions as soon as one match is found, improving execution efficiency and logical clarity for mutually exclusive choices.
9. Outline the rules for writing a switch statement in C/C++. [2 Marks]
Answer: 1. The switch expression must evaluate to an integral or character type. 2. Case labels must be constant expressions and unique. 3. The break statement is optional but recommended to prevent fall-through. 4. The default label is optional.
10. Explain how the for loop execution flow works step-by-step. [3 Marks]
Answer: 1. Initialization: Control variables are initialized once at the start. 2. Condition check: The test expression is evaluated. If true, the loop body runs; if false, the loop terminates. 3. Update: After executing the body, the increment/decrement expression updates the control variable, and step 2 repeats.
Section C — 5 Marks Questions
1. Explain the switch statement in detail, including its syntax, working mechanism, rule limitations, and an example. [5 Marks]
Answer:
Introduction
The switch statement is a multi-way branch statement that simplifies complex nested if-else ladders by testing a single variable or expression against a list of constant values.
Syntax and Working
The expression inside the switch is evaluated once. Its value is compared with the values of each case label. When a match is found, the statements following that case are executed until a break statement is encountered or the switch block ends.
Key Rules
- The expression in switch must evaluate to an integer or character type.
- Case labels must be unique and compile-time constants.
- The default case is optional and handles unmatched values.
- Fall-through occurs if break is omitted, executing subsequent cases automatically.
Example
switch(day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; default: cout << "Invalid"; }
Conclusion
The switch statement provides cleaner, more readable code than long else-if ladders when dealing with discrete fixed choices.
2. Discuss the classification, syntax, and working of all three looping constructs (for, while, do-while) in C/C++. [5 Marks]
Answer:
Introduction
Loops are control flow statements used to execute a block of code repeatedly until a specified termination condition becomes false.
1. The while Loop
An entry-controlled loop where the condition is evaluated before executing the body. Syntax: while(condition) { statements; }
2. The do-while Loop
An exit-controlled loop where the condition is evaluated at the end of the block, guaranteeing at least one execution. Syntax: do { statements; } while(condition);
3. The for Loop
A compact entry-controlled loop ideal for counted iterations, combining initialization, condition, and update in the header. Syntax: for(init; condition; update) { statements; }
Comparison and Application
While and for loops are preferred when the loop might run zero times based on initial data. Do-while loops are specifically useful in menu-driven programs where the menu must display at least once before user input is tested.
3. Explain nested if-else and the else if ladder with clear distinctions, appropriate code structures, and scenarios where each is best applied. [5 Marks]
Introduction
When programs require complex decision-making based on multiple dependent or sequential conditions, simple if statements are insufficient, necessitating nested if-else constructs or else if ladders.
Else If Ladder
An else if ladder tests conditions sequentially from top to bottom. As soon as one condition is true, its block executes and the rest of the ladder is bypassed. Structure: if(cond1) { statements; } else if(cond2) { statements; } else { statements; }
Nested If-Else
A nested if-else involves placing an entire if-else construct inside another if or else block. This is used when a secondary condition depends upon the primary condition being true first. Structure: if(cond1) { if(cond2) { statements; } } else { statements; }
Distinction and Use Cases
Else if ladders are best for mutually exclusive grading or range checks (e.g., marks grading). Nested if-else statements are best for hierarchical criteria, such as checking if a user is logged in, and then checking if their security clearance level is sufficient.
4. Describe jump statements in C/C++ (break, continue, and goto) with their syntax, behavior, and proper use cases within loops. [5 Marks]
Introduction
Jump statements alter the normal sequential flow of control within loops or functions unconditionally or conditionally.
1. The break Statement
Terminates the execution of the nearest enclosing loop or switch block immediately, transferring control to the statement immediately following the loop.
2. The continue Statement
Skips the remaining statements in the current iteration of a loop and forces the control to jump directly to the condition or update expression for the next iteration.
3. The goto Statement
Enables unconditional transfer of program control to a labeled statement anywhere within the same function.
Applications and Warnings
Break and continue are standard structured programming tools used for error handling and data filtering. Goto, however, breaks structured programming principles, often resulting in complex and unmaintainable code known as spaghetti code, and should generally be avoided.
5. Discuss the concepts of entry-controlled versus exit-controlled loops in detail, contrasting their mechanisms, execution guarantees, and practical programming examples. [5 Marks]
Introduction
Looping constructs in programming languages are fundamentally categorized based on the timing of condition evaluation relative to the execution of the loop body.
Entry-Controlled Loops
In entry-controlled loops, the test condition is evaluated before the execution of the loop body. If the condition evaluates to false on the very first check, the body is skipped entirely. Examples include the `for` loop and the `while` loop.
Exit-Controlled Loops
In exit-controlled loops, the loop body executes first, and the condition is evaluated at the end of the iteration. This ensures that the loop body always executes at least once, regardless of whether the condition is initially true or false. The primary example is the `do-while` loop.
Comparative Analysis
- Condition Check Timing: Top of the loop vs bottom of the loop.
- Minimum Executions: 0 executions vs 1 execution.
- Primary Use Case: General counting and data processing vs menu-driven interfaces and input validation where initial prompt execution is mandatory.
Example Scenario
Validating user input between 1 and 10 is best handled by a do-while loop because the prompt must be shown to the user at least once before any validation check can occur.