Professional Documents
Culture Documents
Pre-Test1 QuestionSet2
Pre-Test1 QuestionSet2
Pre-Test1 QuestionSet2
a) Identify three (3) lines of codes with syntax errors in the following C++ program
(see Figure Q1a). Then, fix and rewrite the incorrect lines with the correct syntax.
1
#include <iostream>
2
using namespace std;
3
4
int main() {
5
int length, width, area;
6
7
// Prompt user for input
8
cout << "Enter the length: ";
9
cin << length;
10
11
cout << "Enter the width: ";
12
cin >> width;
13
14
// Calculate the area
15
area = length * width
16
17
// Print the output
18
cout >> "The area is: " >> area >> endl;
19
20
return 0;
21
}
22
Figure Q1a
(5 marks)
b) Identify and write the output of the C++ program in Figure Q1b.
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int x = 8, y = 3;
6 float a = 5.0, b = 2.5;
7
8 // Arithmetic operations
9 cout << "x + y = " << x + y << endl;
10 cout << "a - b = " << a - b << endl;
11 cout << "x * b = " << x * b << endl;
12
13 // Modulus operation
14 cout << "x % y = " << x % y << endl;
15
16 return 0;
17 }
Figure Q1b
(5 marks)
1
QUESTION 2
Answer the following questions based on the C++ program in Figure Q2.
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 const float regular_price = 2.50;
6 const float discount_price = 2.00;
7 const float bulk_discount_price = 1.50;
8
9 int purchased_apples = 0;
10
11 cout << "How many apples would you like to buy? : ";
12 cin >> purchased_apples;
13
14 float item_price;
15
16 if (purchased_apples < 5) {
17 item_price = regular_price;
18 } else if (purchased_apples >= 5 && purchased_apples < 10) {
19 item_price = discount_price;
20 } else {
21 item_price = bulk_discount_price;
22 }
23
24 // Calculate total price
25 float total_price = purchased_apples * item_price;
26
27 cout << "Item Price: $" << item_price << endl;
28 cout << "Total Price: $" << total_price << endl;
29
30 return 0;
31 }
Figure Q2
a) Write the output of the program when the user inputs the following values as the
quantity of apples:
• 3
• 7
• 15
(6 marks)
2
b) What line of codes would you add if you need to modify this program to fulfill new
requirements as follows:
Note:
To answer this question, you don't need to rewrite the entire program. Simply
write the additional code and specify where it should be placed between which
lines.
(6 marks)
QUESTION 3
Write a C++ program that prompts the user to enter their age. Based on their age, use
a switch statement to perform the following actions:
Make sure to handle each case appropriately using the switch statement.
(10 marks)
3
QUESTION 4
Figure Q4 shows a C++ program that calculates the factorial based on user input and
then displays the result.
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int num;
6 int factorial = 1;
7
8 cout << "Enter a positive integer: ";
9 cin >> num;
10
11 if (num < 0) {
12 cout << "Please enter a positive integer.\n";
13 return 1; // Exit the program with an error code
14 }
15
16 int originalNum = num; // Save the original value for display later
17
18 while (num > 1) {
19 factorial *= num;
20 num--;
21 }
22
23 cout << "The factorial of " << originalNum << " is: " << factorial <<
24 endl;
25
26 return 0;
27 }
Figure Q4
a) Replace the while loop in lines 18-21 with a for loop. For your answer, just
write the modified for loop block.
(4 marks)
b) Replace the while loop in lines 18-21 with a do...while loop. For your
answer, just write the modified do...while loop block.
(4 marks)
4
QUESTION 5
1. Declare and initialize an int array named myArray with 5 values below:
2. Then, declare another int array variable named reversedArray with a size of
5 as well.
3. Using a loop, fill reversedArray with the values from myArray but in reverse
order.
4. Then, in another loop, display the values of both arrays as follows:
(10 marks)
Declare what values would be displayed by cout functions for the following
variables:
(6 marks)