All Weeks
All Weeks
CLO 2: Solve basic problems using fundamental concepts of programming language (C++).
(For Week 1, this will be interpreted as formulating steps that could later be translated into
code.)
CLO 3: Design and implement real-world problems using selection statements, loops, and one-
dimensional arrays in C++. (For Week 1, we’ll focus on designing the solution steps/algorithm
rather than actual code structures.)
WEEK 1
Section B: True/False
13. (CLO 1) The ________ of a computer performs arithmetic and logical operations.
14. (CLO 1) ________ are steps or instructions written in a specific order to solve a problem.
15. (CLO 1) The ________ are devices through which a computer can take data from the
outside world (e.g., keyboard, mouse).
16. (CLO 2) Before writing code, a programmer often creates a(n) ________ to outline the
steps needed to solve the problem.
17. (CLO 3) When planning how a program will handle different conditions, one must first
clearly define the problem requirements and consider where ________ or repetitive
actions would be helpful.
18. (CLO 1) Define what a “problem” is in the context of computer science and explain why
it is important to understand the problem before attempting to solve it.
19. (CLO 1) List and briefly explain any three key steps in the problem-solving process.
20. (CLO 1) Why is it important that an algorithm be clear and unambiguous?
21. (CLO 2) Explain how the process of writing a simple set of instructions (like making tea)
can help you when you start writing C++ programs.
22. (CLO 3) Although you have not yet learned about loops and selection statements,
describe a scenario from real life (e.g., deciding what to wear based on the weather) and
outline the steps you would take. In your outline, indicate where you might eventually
use a selection (an “if” decision) or a repetition (a “loop”) when you learn these
programming constructs.
Section E: Long Answer / Scenario-Based Questions
23. (CLO 1) Consider the following everyday problem: “You need to wake up on time for
university.” Outline the steps (algorithm) you would take to ensure you wake up before
your first class. Make sure your steps are logical, sequential, and free from ambiguity.
24. (CLO 2) Take the problem of “converting a given length in feet to inches.” Write down
the problem definition, identify the input and output, and provide a sequence of steps
(without coding) that shows how you would solve this problem logically.
Week 1 Answer Key
MCQs
1. B
2. B
3. C
4. B
5. B
6. C (This is NOT a characteristic of a good algorithm)
7. A
True/False
8. True
9. True
10. True
11. True
12. True
13. CPU
14. Algorithms
15. Input devices
16. Algorithm (or Pseudo-code)
17. Selection (or Decision-making)
Short Answer
18. A problem is a defined task or question. Understanding it first ensures you know the goal
before solving.
19. Sample steps: (1) Understand the problem, (2) Plan the solution, (3) Implement and test.
20. Unambiguous algorithms ensure correctness and no confusion.
21. Breaking real tasks (like making tea) into steps teaches you to structure logic before
coding.
22. If hot: wear light clothes; else: wear warm clothes. This maps to using if conditions in
code.
Long Answer
23. Pseudo-code for waking up: Set alarm → Wake when alarm rings → No snooze →
Ensures no missed step.
24. Convert feet to inches: inches = feet * 12. Steps: Read feet → Calculate inches → Print
result.
25. Check temperature and use if/else. If > threshold → “Hot,” else “Cold.” Later implement
with selection statements.
WEEK 2
1. (CLO 1) Which of the following statements about a “C++ program’s execution flow” is
true?
A. A C++ program executes instructions from bottom to top.
B. A C++ program executes instructions in a sequence, generally top to bottom.
C. A C++ program executes instructions randomly.
D. A C++ program starts executing from the last line of code.
2. (CLO 1) The difference between syntax and semantics in a C++ program is that:
A. Syntax is about logic, semantics is about code formatting.
B. Syntax refers to the structure and format of code, while semantics refers to the
meaning of that code.
C. Syntax focuses on variable names, semantics focuses on execution time.
D. Syntax is only important in C, while semantics only matters in C++.
3. (CLO 2) What does the following line of C++ code do?
4. cout << "Hello World\n";
5. (CLO 2) Which of the following is a correct syntax for outputting a numeric constant in
C++?
A. cout << 10 << endl;
B. cout < 10 > endl;
C. echo 10;
D. print(10);
6. (CLO 2) A syntax error in C++ is:
A. An error caused by incorrect arithmetic calculation.
B. An error in the structure or spelling of code that prevents compilation.
C. A logic error that occurs at runtime.
D. A specific hardware malfunction.
7. (CLO 2) Given the arithmetic expression 2 + 3 * 4, which operator is evaluated first
according to standard operator precedence?
A. + is evaluated first
B. * is evaluated first
C. Both are evaluated simultaneously
D. None are evaluated since the expression is invalid.
8. (CLO 2) Which of the following is the correct way to output the result of the arithmetic
expression (5 - 2) * 3?
A. cout << ((5 - 2)*3) << endl;
B. cin << ((5 - 2)*3) << endl;
C. print << (5 - 2)*3;
D. output ((5 - 2)*3);
9. (CLO 3) While we haven’t used selection or loops yet, creating arithmetic expressions
helps set the foundation for:
A. Writing unclear and disorganized code.
B. Manipulating and processing data in more complex future programs.
C. Ignoring problem-solving steps.
D. Avoiding the use of variables altogether.
10. (CLO 1) Comments in a C++ program are used to:
A. Change the order of program execution.
B. Provide explanations for human readers without affecting the program’s execution.
C. Convert arithmetic operations into logical operations.
D. Prompt the user for input.
11. (CLO 2) Identify the arithmetic expression that correctly applies operator precedence:
A. 2 + 3 * 4 evaluates to 20
B. 2 * 3 + 4 evaluates to 14
C. 2 + (3 * 4) evaluates to 14
D. (2 + 3) * 4 evaluates to 14
Section B: True/False
11. (CLO 1) True/False: Syntax errors prevent the program from compiling successfully.
12. (CLO 2) True/False: Using cout is the standard way to produce output in a basic C++
program.
13. (CLO 1) True/False: Semantics deal with the meaning of an instruction, not just how it is
written.
14. (CLO 2) True/False: Arithmetic expressions in C++ follow a precedence similar to
mathematical operations.
15. (CLO 1) True/False: Comments can be used to outline the steps of a solution within the
code, helping clarify logic.
16. (CLO 3) True/False: Understanding how to form correct arithmetic expressions is a
foundational skill that will help when implementing loops or selections later.
17. (CLO 2) In C++, the standard output stream is represented by the object ________.
18. (CLO 1) A ________ error occurs when the program fails to follow the rules (grammar)
of the programming language.
19. (CLO 2) The ________ operator (*) has higher precedence than the addition operator (+).
20. (CLO 1) ________ are non-executable lines in the code used to explain and clarify,
starting with // in C++.
21. (CLO 2) To produce output followed by a newline, programmers often use the ________
manipulator after cout.
Section D: Short Answer Questions
22. (CLO 1) Explain what is meant by “syntax vs. semantics” using a simple example.
23. (CLO 2) Provide an example of a simple arithmetic expression in C++ and show how you
would output its result using cout.
24. (CLO 1) Why is it important to use comments in your code, especially when writing
complex arithmetic operations?
25. (CLO 2) Suppose you want to calculate the perimeter of a rectangle with length = 5 and
width = 3. Write down the arithmetic expression and the cout statement that displays the
answer.
26. (CLO 3) Briefly describe how understanding operator precedence now will help you
when you eventually need to write more complex programs that might include loops and
conditions.
27. (CLO 1) Consider a scenario where you need to determine the total price of items
purchased. You know the quantity and price per item. Outline the steps (in plain English)
you would follow to solve this problem before writing any code. Explain why having a
clear solution approach is beneficial.
28. (CLO 2) Write a short segment of code (just a few lines) that declares two integers (x and
y), assigns them values, and then uses cout to display the sum, difference, product, and
result of integer division. Explain each line with comments.
29. (CLO 3) Imagine a future program where you want to decide if a student passed or failed
based on their average test score. Although you cannot yet write the if statement, explain
how understanding arithmetic operations and output in Week 2 prepares you for
designing the logic (such as calculating the average score) that will eventually be
combined with selection statements.
Week 2 Answer Key
MCQs
True/False
17. cout
18. Syntax (or Compile-time)
19. double (for fractional values)
20. Comments
21. endl
Short Answer
22. Syntax is form, semantics is meaning. Example: int x; (correct syntax and meaning).
23. Example: int result = 2 + 3; cout << result << endl; prints 5.
24. Comments explain code logic for human readers, essential for complex arithmetic.
25. Perimeter of rectangle = 2*(length+width). If length=5, width=3: int perimeter =
2*(5+3); cout << perimeter; prints 16.
26. Understanding operator precedence now sets a foundation for complex expressions when
adding loops/conditions.
Long Answer
27. Summing items: Understand the input (quantity, price), multiply and sum before coding
ensures clarity.
28. Code snippet:
cpp
Copy code
int x=5,y=3;
cout<<"Sum:"<<x+y<<endl;
cout<<"Diff:"<<x-y<<endl;
cout<<"Prod:"<<x*y<<endl;
cout<<"Div:"<<x/y<<endl; // with comments explaining each line
29. Average test score scenario: Knowing arithmetic first lets you calculate average
(sum/count) and later use if for pass/fail.
30. Currency conversion scenario: Steps: read dollars, multiply by conversion rate, print
result. Later add conditions (if rate changes) or loops for multiple conversions.
WEEK 3
This line:
A. Assigns the value 0 to age.
B. Declares a variable age of type int but does not assign it a value.
C. Prints the value of age to the screen.
D. Declares a variable age of type float.
5. (CLO 1) The concept of “type” in a programming language like C++ refers to:
A. The way code is indented.
B. The kind of data a variable can store (e.g., int, double, char).
C. The name you give to a variable.
D. How fast the code runs.
6. (CLO 2) If you write:
int x = 5;
x = x + 3;
7. (CLO 2) Which statement correctly takes an integer input from the user and stores it in
variable num?
A. cin >> num;
B. cin << num;
C. cout >> num;
D. input(num);
8. (CLO 2) Which of the following is the correct syntax to initialize a variable length of
type int with the value 10?
A. int length 10;
B. int length = 10;
C. int length == 10;
D. length = int 10;
9. (CLO 3) Although we have not used loops or selection structures yet, having variables
allows us to:
A. Store values that can be changed during program execution, setting the stage for more
complex decision-making later.
B. Avoid writing any code to solve problems.
C. Implement loops without understanding them.
D. Make our programs run with no logic.
10. (CLO 1) The assignment operator = in C++:
A. Checks for equality between two variables.
B. Moves data from the right side into the variable on the left side.
C. Prints out the value of a variable.
D. Is used only for integers.
11. (CLO 2) If width is an int variable and height is another int variable, which of the
following correctly reads values for both from the keyboard?
A. cin >> width << height;
B. cin >> width >> height;
C. cin << width >> height;
D. cout >> width >> height;
Section B: True/False
11. (CLO 1) True/False: A variable must be declared with a type before it can be used in
C++.
12. (CLO 2) True/False: The assignment operator = changes the variable on the left to hold
the value of the expression on the right.
13. (CLO 2) True/False: In C++, cin by default reads data from the keyboard until it
encounters whitespace (like space, tab, or newline).
14. (CLO 1) True/False: Variable names are case-sensitive in C++.
15. (CLO 3) True/False: Understanding how to store and manipulate data using variables is
an essential building block for eventually writing programs with decisions and loops.
16. (CLO 2) True/False: If you do not initialize a variable, it starts with a guaranteed default
value of zero.
17. (CLO 1) True/False: A good practice in problem-solving is to determine what variables
(and their types) are needed before you write the code.
18. (CLO 2) In C++, ________ is used to read input from the keyboard and store it into
variables.
19. (CLO 2) An int variable can store whole numbers, while a double variable can store
________ values.
20. (CLO 1) When choosing variable names, it’s important they are ________ and
meaningful, describing the data they hold.
21. (CLO 2) Using cin, the extraction operator >> stops reading input at the first occurrence
of ________.
22. (CLO 1) Before solving a problem, deciding what data to store, what types to use, and
how to name variables is part of the ________ process.
23. (CLO 1) Explain why understanding data types is important when solving programming
problems.
24. (CLO 2) Write a short snippet of code that declares an integer variable age, takes input
from the user, and then prints “Your age is: ” followed by the value entered.
25. (CLO 2) Suppose you have two integer variables num1 and num2. Describe the steps to
read their values from the keyboard and then display their sum.
26. (CLO 1) What makes an identifier (variable name) valid in C++? List three rules.
27. (CLO 3) Describe a simple real-world problem, such as calculating the total cost of
buying multiple items, and explain how variables would be used to store and manipulate
data as part of the eventual complete solution (involving logic later).
28. (CLO 2) If you have a variable length that holds a length in meters and you want to
convert it to centimeters, write the arithmetic expression and corresponding cout
statement to display the result.
29. (CLO 1) Consider the problem: “You want to keep track of the number of books you
have read this month.” Describe how you would approach this problem, what variables
you might need, and why selecting proper variable types and names is important before
writing any code.
30. (CLO 2) Write a short C++ code segment (5-10 lines) that declares three variables:
length, width, and area (all integers). Prompt the user to enter length and width, read
them in using cin, compute area as length * width, and then display the result.
Include comments explaining what each line does.
(CLO 3) Think about a future program that will need to decide if a user’s input falls within a
certain range. For now, just describe how having variables and the ability to read input with cin
sets the stage for making decisions (like using an if statement) in later weeks. Give an example
scenario (e.g., checking if a user’s age qualifies them for a discount) and outline the steps you
would currently take up to this point (storing and outputting values).
True/False
18. cin
19. fractional (or real, double)
20. descriptive (meaningful)
21. whitespace
22. planning
Short Answer
23. Understanding data types ensures correct data handling (e.g., int for whole numbers,
double for decimals).
24.
cpp
Copy code
int age;
cin >> age;
cout << "Your age is: " << age << endl;
25. Steps: cin >> num1 >> num2; sum = num1 + num2; cout << sum;
26. Valid identifier rules: start with letter/underscore, no spaces/special chars, cannot be
keyword.
27. Buying items: price per item stored in variable, quantity in another, multiply and sum to
find total cost later integrated with logic.
28. Conversion: double lengthMeters; cin >> lengthMeters; double lengthCm =
lengthMeters * 100; cout << lengthCm;
Long Answer
29. Tracking books read: plan variable booksRead as int, choose meaningful names. Clarity
before coding avoids confusion.
30.
cpp
Copy code
int length, width, area;
cout << "Enter length and width: ";
cin >> length >> width;
area = length * width;
cout << "Area = " << area << endl;
(Comments each line: declare variables, read input, compute, print) 31. Checking range scenario:
variables store input, later use if to decide if input in range. Variables enable easy checks.
WEEK 4
int x = 10;
if (x > 5) {
cout << "x is greater than 5\n";
}
3. (CLO 2) Which of the following relational operators checks for equality between two
operands?
A. ==
B. =
C. !=
D. <=
4. (CLO 1) If you want to execute one set of statements if a condition is true and another set
if the condition is false, you should use:
A. A single if statement
B. An if...else statement
C. A while loop
D. A for loop
5. (CLO 2) Consider the code snippet:
int x = 5;
if (x == 5)
cout << "Equal\n";
else
cout << "Not Equal\n";
Section B: True/False
15. (CLO 2) True/False: The if statement only executes its body when the condition is true.
16. (CLO 1) True/False: Flow-charts use decision symbols (diamonds) to indicate where a
program can branch into two or more paths.
17. (CLO 2) True/False: == is used to compare two values, while = is used to assign a value
to a variable.
18. (CLO 1) True/False: A pseudo-code representation is language-specific and must follow
strict C++ syntax.
19. (CLO 2) True/False: The else part of an if...else statement executes only if the if
condition is false.
20. (CLO 3) True/False: Designing an algorithm with conditional steps now will make
implementing real-world problems with selection easier later.
21. (CLO 2) True/False: The switch statement can handle multiple cases of a single
variable’s value, replacing multiple if...else statements in some scenarios.
22. (CLO 1) True/False: When comparing floating-point numbers for equality, it’s safer to
check if they are “close enough” rather than exactly equal, due to potential precision
issues.
Section C: Fill in the Blanks
23. (CLO 2) In a flow-chart, decision points are typically represented by the ________ shape.
24. (CLO 2) The if...else structure allows the program to choose between two mutually
________ paths.
25. (CLO 1) Pseudo-code is an informal language that helps programmers plan an ________
before implementing it in an actual programming language.
26. (CLO 2) The switch statement often requires the use of the ________ keyword at the
end of each case to prevent the code from “falling through” to the next case.
27. (CLO 1) When testing conditions, using == instead of = ensures that you are performing a
________, not an assignment.
28. (CLO 2) In handling input failure, we might use cin.fail() or cin.clear() followed
by cin.ignore() to ________ the input stream before reading again.
36. (CLO 1) You need to create an algorithm to decide whether a user can access a certain
feature based on their age. Outline your algorithm in pseudo-code. Include steps for
checking if age >= 18. If true, print “Access Granted”; otherwise, print “Access
Denied.” Explain why planning this logic before coding is beneficial.
37. (CLO 2) Write a C++ code snippet (6-10 lines) that asks the user for their test score (0-
100), reads the input, and then uses an if...else if...else structure to print a grade
category (e.g., “Excellent” if score ≥ 90, “Good” if score is between 70 and 89, “Needs
Improvement” if score < 70). Include comments to explain each decision.
38. (CLO 3) Imagine a future scenario where you need to build a program that sorts items
into categories based on user input. Although you haven’t learned loops or arrays yet,
discuss how the selection statements you learned now will fit into that future program
design. Mention how making decisions based on input values sets the stage for more
complex logic.
39. (CLO 1) Describe a step-by-step solution (in pseudo-code) for checking two floating-
point numbers for “near equality.” For example, consider the numbers equal if their
absolute difference is less than 0.001. Explain why this approach is necessary rather than
using ==.
40. (CLO 2) Consider the problem of determining if the input is valid. If the user enters a
positive number, print “Valid Input”; otherwise, print “Invalid Input.” Write a short code
snippet that includes reading from cin, checking input validity, handling failures if the
input is not numeric, and printing the appropriate message. Add comments explaining
your logic.
Week 4 Answer Key
MCQs
True/False
23. diamond
24. mutually exclusive
25. algorithm
26. break
27. comparison
28. clear (or reset) the input stream (conceptually: handle input failures)
Short Answer
cpp
Copy code
if (num % 2 == 0)
cout << "Even\n";
else
cout << "Odd\n";
cpp
Copy code
if (x != 0 && (y/x)>2) ...
Long Answer
36. Algorithm age access: if age≥18 print "Access Granted" else "Denied". Planning prevents
missing conditions.
37. Code snippet reading score and assigning category with if...else if.
38. Future sorting scenario: selection decides actions based on conditions.
39. Near equality pseudo-code: if (|a-b|<0.001) same else different. Avoids floating errors.
40. Input validity: read input, if fail handle, if positive print “Valid”, else “Invalid”.
WEEK 5
int x = 10;
if (x > 0) {
if (x < 20)
cout << "x is between 1 and 19\n";
}
What is printed?
A. Nothing
B. x is between 1 and 19
C. x is greater than 0
D. Error at compile time
Section B: True/False
13. (CLO 1) True/False: Nested if statements allow you to handle multiple, layered
conditions.
14. (CLO 2) True/False: Using else if constructs enables multi-way selection in a clear and
structured way.
15. (CLO 2) True/False: Pre-increment (++x) and post-increment (x++) operators produce the
same final value of x, but differ in the value they return when used in an expression.
16. (CLO 2) True/False: ASCII values allow characters to be treated as integers for
arithmetic operations (e.g., char c = 'A'; int val = c;).
17. (CLO 1) True/False: Complex conditions should be simplified and possibly broken into
multiple steps or use parentheses to ensure correct evaluation order.
18. (CLO 3) True/False: Understanding nested selection will help implement real-world
decisions, like assigning grades or classifying inputs into categories.
19. (CLO 2) True/False: Type casting can be done using static_cast(variable) in C++ to
explicitly convert types.
20. (CLO 1) True/False: Multi-way selection structures are useful when a single decision is
not sufficient to determine the correct action.
27. (CLO 1) Describe a scenario where nested if statements might be necessary. Provide a
simple real-life example.
28. (CLO 2) Write a short code snippet (3-4 lines) using nested if statements to check if a
number x is positive and also less than 100. Print appropriate messages.
29. (CLO 1) Explain why using a chain of if...else if...else statements can be clearer
than multiple separate if statements.
30. (CLO 2) Given two integers a and b, write an expression that uses compound conditions
to check if a is greater than b and less than 100.
31. (CLO 2) Provide a code example where you use ++x and x++ on a variable and explain
the difference in the output of each operation.
32. (CLO 3) Consider a grading system that assigns letter grades (A, B, C, D, F) based on
scores. Describe the pseudo-code logic using multi-way selection.
33. (CLO 1) Explain why understanding operator precedence is essential when writing
conditions like (x > 0 && x < 10) || (x == 20).
34. (CLO 2) If you have char c = 'A'; and you want to convert it to its ASCII integer
equivalent and print it, how would you do it in code?
35. (CLO 3) Suppose you need to categorize a temperature input into “Very Cold”, “Cold”,
“Warm”, or “Hot” using nested or multi-way selection. Write a pseudo-code solution
outlining the conditions (e.g., temp < 0, 0 <= temp < 15, 15 <= temp < 30, temp >=
30).
36. (CLO 2) Write a C++ code snippet (6-10 lines) that reads an integer from the user and
then:
(CLO 2) Write a C++ snippet that demonstrates the use of parentheses to control operator
precedence in a compound condition. Explain how removing parentheses might change the logic
of the decision.
True/False
Short Answer
cpp
Copy code
int x;
cin >> x;
if(x>0){
if(x<100) cout<<"Positive and less than 100";
}
29. else if chain is clearer than separate if’s for multiple distinct conditions.
30. (a>b && a<100) checks a in a range.
31. ++x increments first, x++ returns old value. Example:
cpp
Copy code
int x=5;
cout<<++x<<endl; //6
cout<<x++<<endl; //6 but x=7 after this
Long Answer
1. (CLO 1) Which of the following best describes why repetition (looping) is needed in
programming?
A. To write fewer lines of code with no added benefit.
B. To perform the same tasks multiple times without rewriting the code each time.
C. To make the program run slower.
D. To avoid having any conditions in the program.
2. (CLO 2) Consider a while loop that prints numbers from 1 to 5:
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
while (x < 5) {
// loop body
}
Section B: True/False
17. (CLO 1) True/False: A sentinel value is a special value that signals the end of input, and
hence termination of the loop.
18. (CLO 2) True/False: A do...while loop executes at least once, even if the condition is
false initially.
19. (CLO 1) True/False: Designing loops involves deciding the type of loop (while, for,
do...while) and the controlling condition.
20. (CLO 2) True/False: In a for loop, the initialization, condition, and update are all
included inside the loop’s header.
21. (CLO 1) True/False: A flag-controlled loop uses a boolean variable that is changed inside
the loop to control when the loop ends.
22. (CLO 2) True/False: The break statement should be used sparingly and only when
necessary, as it can make code harder to follow.
23. (CLO 3) True/False: Understanding loops now prepares you to handle tasks that involve
processing multiple data values later.
24. (CLO 2) True/False: If the loop’s condition never becomes false, the loop may become
infinite and never terminate on its own.
31. (CLO 1) Explain the difference between a counter-controlled loop and a sentinel-
controlled loop. Provide an example scenario for each.
32. (CLO 2) Write a short code snippet (4-5 lines) that uses a while loop to print all even
numbers between 1 and 10.
33. (CLO 2) How would you modify a while loop into a for loop if you know exactly how
many times the loop should run?
34. (CLO 1) Why is it important to ensure that a loop eventually terminates? What might
happen if it does not?
35. (CLO 2) Given a for loop that sums integers from 1 to N, provide the code and explain
how it works.
36. (CLO 2) Show how you would use a do...while loop to repeatedly prompt the user for
input until they type a specific value (e.g., 0 to stop).
37. (CLO 1) Describe a real-world problem (e.g., reading scores until a sentinel -1 is entered)
and outline the pseudo-code steps.
38. (CLO 3) Explain how the loops you’ve learned now could be combined with decision-
making structures you learned earlier (e.g., if statements) to solve more complex
problems.
39. (CLO 2) Provide an example where you would use a break statement inside a loop. Why
might this be simpler than adjusting the loop’s condition?
40. (CLO 2) Write a small code segment that uses continue within a for loop to skip
printing the number 5 while printing numbers from 1 to 10.
41. (CLO 1) Consider you want to sum a series of positive integers input by the user until
they enter a negative number. Write the pseudo-code or flow-chart for this logic.
42. (CLO 2) Write a C++ program segment (6-10 lines) that prompts the user to enter a series
of numbers. Use a while loop to keep reading numbers until the user enters 999. Print the
average of all numbers entered (excluding 999).
43. (CLO 3) Think of a real-world scenario where you need to repeatedly process a list of
items (e.g., calculating the total price of items purchased). Describe how you would
design a loop structure, including how you decide when to stop the loop and what
conditions you might check each iteration.
44. (CLO 2) Suppose you have a loop that reads exam scores until -1 is entered. Once the
loop finishes, print the highest and lowest scores entered. Write a code segment showing
how you handle initialization, updates, and maintaining current min/max scores.
(CLO 3) Imagine a program that will need both selection and repetition to handle complex tasks
like sorting a list or counting occurrences of certain values. Describe how mastering loops now
sets the stage for creating these more advanced algorithms later. Give an example of a small step
you could implement today using loops, and how it will integrate with future constructs you will
learn.
True/False
25. condition
26. sentinel
27. pseudo-code (or flow-chart)
28. continue
29. do...while
30. iteration
Short Answer
cpp
Copy code
int i=2;
while(i<=10){cout<<i<<" ";i+=2;}
33. Known iterations: replace while with for (e.g. for(i=1;i<=10;i++) sum+=i;).
34. If not terminate, infinite loop occurs, program never ends.
35.
cpp
Copy code
int sum=0;
for(int i=1;i<=N;i++) sum+=i;
cpp
Copy code
int val;
do {
cin>>val;
//process val
} while(val!=0);
37. Scores until -1: pseudo-code uses a while and checks after input.
38. If combined with if statements, loops process many inputs and decide each outcome.
39. break inside a loop: if user enters -1, break to end early. Simpler than complex
conditions.
40. continue: e.g. for(int i=1;i<=10;i++){if(i==5)continue;cout<<i;} skips printing 5.
Long Answer
41. sum until negative pseudo-code: while num!=-1 sum+=num read num.
42. Code reading numbers until 999 and computing average.
43. Real-world repetition: e.g., summing multiple prices until sentinel. Clear end condition.
44. Track min/max scores with loop reading until -1. Keep updating min/max.
45. Understanding loops helps integrate selection & arrays for advanced algorithms later.
WEEK 7
1. (CLO 1) Which of the following is a key factor in choosing the right loop structure for a
problem?
A. The number of lines of code in the program
B. The number of times the task needs to repeat and whether it’s known in advance
C. The color scheme of your code editor
D. Whether your code uses cin or not
2. (CLO 2) Suppose you need to iterate exactly 10 times and print numbers 1 through 10.
Which loop is typically the best choice?
A. while loop
B. for loop
C. do...while loop
D. switch statement
3. (CLO 1) If you are not sure how many times you need to repeat a task but know you
should stop when a certain condition is met, you would likely use:
A. A counter-controlled loop
B. A sentinel-controlled loop
C. A for loop with a fixed iteration count
D. No loop at all
4. (CLO 2) The break statement inside a loop:
A. Skips the current iteration and continues with the next iteration
B. Immediately terminates the entire loop and continues with the statement after the loop
C. Is required in every loop
D. Only works in switch statements
5. (CLO 2) The continue statement inside a loop:
A. Immediately ends the loop
B. Skips the remaining statements in the current iteration and moves to the loop’s next
iteration
C. Acts like a break but only in while loops
D. Does nothing
6. (CLO 3) When solving real-world problems using loops, choosing the appropriate
looping structure helps ensure:
A. The code is as long as possible
B. The logic is clear, maintainable, and efficient
C. You never need to test your code
D. You can avoid using conditions altogether
7. (CLO 2) Consider the scenario: You need to process user input until the user enters a
negative number. A good approach is:
A. A for loop with a fixed number of iterations
B. A while loop checking if the input is not negative
C. A do...while loop without any conditions
D. Use break at the start of a for loop
8. (CLO 1) When choosing the right loop structure, what is the primary consideration?
A. The exact syntax of C++
B. The nature of the problem and how the stopping condition is determined
C. The popularity of the loop structure among other programmers
D. Whichever loop you learned first
9. (CLO 2) Using a do...while loop is most appropriate when:
A. You want the loop body to run at least once before checking the condition
B. You know the exact number of iterations required
C. You never want the loop to end
D. The condition must be checked before the loop body runs
10. (CLO 2) What is the effect of the following code snippet?
What is printed?
A. 1 2 3 4 5
B. 1 2 4 5
C. 1 3 4 5
D. 2 3 4 5
12. (CLO 2) If you need to read input until a sentinel value is encountered, but also must
ensure that the loop runs at least once regardless of the first input, which loop might you
choose?
A. for loop
B. while loop
C. do...while loop
D. No loop needed
13. (CLO 3) When implementing a real-world calculation (e.g., summing user inputs until
they enter 0), choosing the appropriate loop and conditions results in:
A. More confusion
B. Less reliable code
C. Clearer logic and fewer chances of infinite loops or incorrect termination
D. Slower program execution
14. (CLO 1) Problem-solving using loops often involves:
A. Identifying whether the repetition count is known or unknown
B. Avoiding any conditions inside the loop
C. Rewriting the loop multiple times
D. Using break and continue in every loop
15. (CLO 2) The presence of a break statement inside a loop’s body can:
A. Change the loop’s termination logic by exiting early
B. Be ignored by the compiler
C. Force the loop to run twice as many times
D. Replace the need for a loop condition
16. (CLO 2) The continue statement is useful when:
A. You want to end the entire loop immediately
B. You want to skip certain iterations based on a condition while allowing the loop to
continue running
C. You have no conditions at all in the loop
D. You need to exit the program
17. (CLO 3) As you approach more complex problems, combining loops with break and
continue gives you:
A. More control over loop execution and the ability to handle special cases elegantly
B. No advantages at all
C. Slower code performance
D. A reason to avoid testing
18. (CLO 1) Choosing the right looping structure in pseudo-code before writing actual code
helps you:
A. Make random decisions at runtime
B. Think through the logic clearly and avoid unnecessary complexity
C. Skip planning and jump directly to coding
D. Guarantee that you will never need to debug
19. (CLO 2) Which loop type allows initialization, condition, and update all in one place?
A. while loop
B. for loop
C. do...while loop
D. They all treat initialization, condition, and update the same way
20. (CLO 1) If the problem requires user input until a certain condition is met, and that
condition is only known after the user inputs something, which loop might be best if you
need at least one iteration?
A. for loop
B. do...while loop
C. while loop
D. No loop structure applies
Section B: True/False
21. (CLO 1) True/False: Choosing the right loop structure can simplify your problem’s logic.
22. (CLO 2) True/False: A break statement inside a loop always returns the execution to the
start of the loop.
23. (CLO 2) True/False: continue allows you to skip the remainder of the loop body for the
current iteration and proceed with the next iteration.
24. (CLO 1) True/False: Planning your looping structure in pseudo-code can help you decide
whether a for, while, or do...while loop is most suitable.
25. (CLO 3) True/False: Using loops effectively is essential in solving many real-world
computing problems that require repeated processing of data.
26. (CLO 2) True/False: The break statement can cause a loop to exit even if the loop’s
condition is still true.
27. (CLO 1) True/False: Choosing the wrong loop structure can lead to logic errors, infinite
loops, or less readable code.
28. (CLO 2) True/False: A do...while loop checks the condition at the end of the loop
body, ensuring at least one iteration.
29. (CLO 3) True/False: Becoming skilled at picking the right loop and using break and
continue will make it easier to develop more complex programs involving multiple
loops and conditions.
30. (CLO 2) True/False: Using continue inside a loop has no effect on the loop’s execution.
31. (CLO 1) Before coding, deciding which loop to use is part of the ________-solving
process.
32. (CLO 2) The ________ statement inside a loop causes execution to jump immediately to
the loop’s condition check (in a for or while) or to the update step (in a for).
33. (CLO 2) The ________ statement inside a loop causes the loop to terminate immediately,
skipping any remaining iterations.
34. (CLO 1) Using pseudo-code or flow-charts can help determine whether a while, for, or
________ loop is best for a given problem.
35. (CLO 2) In a problem where the user must be prompted at least once, a ________ loop
might be the best choice.
36. (CLO 1) When you know exactly how many times a loop should run, a ________ loop is
often ideal.
37. (CLO 3) Real-world problems often require reading multiple values until a certain
condition occurs, which can be handled by a ________-controlled loop.
38. (CLO 1) Choosing between using break and structuring your condition to end the loop
naturally is a design decision that affects code ________ and readability.
39. (CLO 2) continue is typically used to skip ________ execution in the current iteration
and move to the next iteration.
40. (CLO 2) A break statement inside nested loops only terminates the ________ loop in
which it appears, not all loops.
41. (CLO 1) Explain why planning out your loop structure before coding (e.g., using pseudo-
code) can help prevent logic errors.
42. (CLO 2) Write a short code snippet (4-5 lines) that uses a for loop to print numbers from
1 to 10, but uses continue to skip printing the number 5.
43. (CLO 2) Provide a simple code snippet (4-5 lines) where you use a while loop and a
break statement to stop the loop when a user enters a negative number.
44. (CLO 1) Describe a scenario where a do...while loop is preferable over a while loop.
Why?
45. (CLO 2) Suppose you have a for loop that iterates from i = 0 to i < 10. How would
you modify it to stop immediately when i equals 7?
46. (CLO 3) Consider a small real-world scenario: reading a list of prices until you encounter
a sentinel value (-1). Describe what loop structure you would use, and how break or
continue might help in refining the logic.
47. (CLO 1) Give an example of a problem where you do not know how many iterations you
need upfront, but you know a specific value that, when encountered, should end the loop.
Which loop structure and logic would you choose?
48. (CLO 2) Explain the difference in behavior between break and continue statements
inside a loop. Provide a small code example that demonstrates both.
49. (CLO 3) Think of a future scenario where you need to process user input and skip invalid
entries while continuing to read more data. How would understanding continue now
help you implement this logic?
50. (CLO 1) Why is it important to know how to choose the right looping structure when you
start writing more complex algorithms that may combine loops with conditions and
calculations?
51. (CLO 1) Write a pseudo-code solution for a problem where you repeatedly ask the user
for a password until they type the correct one. Discuss which loop structure you chose
and why.
52. (CLO 2) Write a C++ code segment (6-8 lines) that uses a while loop to read integers
from the user. Stop reading when the user enters -1. If the user enters 0 at any point, skip
that iteration (don’t add to the sum) and continue reading more numbers. At the end, print
the sum of all non-zero numbers.
53. (CLO 3) Consider a scenario where you need to classify user inputs into categories (for
example, positive, negative, and zero values) until a sentinel (-999) is entered. Describe
your approach, including which loop structure you would use and how you might utilize
break or continue to handle special cases.
54. (CLO 1) Explain how thinking about loops as separate from the code (i.e., first planning
logically) can help when you start dealing with more advanced programming concepts,
such as nested loops and complex conditionals.
(Check consistency with original sets. If any discrepancy, assume the logic from original
content.)
True/False
21. True
22. False (break doesn’t return to start, it exits loop)
23. True (continue skips rest of current iteration)
24. True (Pseudo-code helps choose correct loop)
25. True (Loop mastery is essential for real-world problems)
26. True (break can exit loop even if condition still true)
27. True (Wrong loop choice → logic errors)
28. True (do...while checks condition at end)
29. True (Mastery of break/continue helps future complexity)
30. False (continue does have effect; it skips current iteration’s remainder)
31. problem
32. continue
33. break
34. do...while
35. do...while
36. for
37. sentinel
38. readability (or clarity)
39. remaining
40. innermost
Short Answer
cpp
Copy code
for(int i=1;i<=10;i++){
if(i==5) continue;
cout<<i<<" ";
}