0% found this document useful (0 votes)
52 views

Pre-Test1 QuestionSet2

The document contains 5 questions regarding C++ programming. Question 1 has parts (a) and (b) regarding identifying syntax errors in code snippets and writing the output. Question 2 has parts (a) and (b) about modifying a program to fulfill new requirements. Question 3 provides requirements to write a program using a switch statement. Question 4 has parts (a) and (b) about replacing a loop with other loops. Question 5 has parts (a) and (b) involving arrays.

Uploaded by

2023262198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Pre-Test1 QuestionSet2

The document contains 5 questions regarding C++ programming. Question 1 has parts (a) and (b) regarding identifying syntax errors in code snippets and writing the output. Question 2 has parts (a) and (b) about modifying a program to fulfill new requirements. Question 3 provides requirements to write a program using a switch statement. Question 4 has parts (a) and (b) about replacing a loop with other loops. Question 5 has parts (a) and (b) involving arrays.

Uploaded by

2023262198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

QUESTION 1

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:

• Discount price 1 (RM2.00) is given to buyers who purchase apples in


quantities of 5-9.
• New discount price 2 is RM1.80 for apple quantities 10-19.
• Bulk discount price (RM1.50) is only applicable for apple quantities 20 and
above.

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:

• If the age is between 13 and 19 (inclusive), print "You are a teenager."


• If the age is less than 13, print "You are a child."
• If the age is 20 or older, print "You are an adult."
• If the age is exactly 65, print "You are eligible for retirement."
• For any other age, print "Enjoy your age!"

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

a) Write a C++ program that contains the following steps:

1. Declare and initialize an int array named myArray with 5 values below:

56, 14, -8, 25, 9

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:

myArray: 56 > 14 > -8 > 25 > 9


reversedArray: 9 > 25 > -8 > 14 > 56

(10 marks)

b) A program contains the declaration and initialization of a char 2D array as


follows:
char charArray[10][10] = {
{'E', 'Q', 'G', 'F', 'O', 'R', 'W', 'M', 'G', 'C'},
{'K', 'L', 'Z', 'U', 'P', 'I', 'L', 'Y', 'Z', 'X'},
{'H', 'R', 'P', 'I', 'S', 'E', 'X', 'Z', 'C', 'I'},
{'V', 'U', 'N', 'L', 'M', 'B', 'P', 'R', 'E', 'N'},
{'G', 'O', 'J', 'W', 'B', 'N', 'T', 'R', 'O', 'X'},
{'Z', 'U', 'H', 'R', 'T', 'Q', 'C', 'Z', 'K', 'J'},
{'X', 'D', 'O', 'F', 'A', 'V', 'H', 'S', 'U', 'T'},
{'N', 'L', 'Q', 'I', 'F', 'H', 'G', 'Z', 'R', 'M'},
{'J', 'U', 'K', 'C', 'X', 'L', 'P', 'Y', 'Q', 'S'},
{'T', 'A', 'Y', 'B', 'V', 'D', 'G', 'F', 'N', 'A'}
};

Declare what values would be displayed by cout functions for the following
variables:

• cout << charArray[2][5] << endl;


• cout << charArray[8][4] << endl;
• cout << charArray[7][6] << endl;

(6 marks)

You might also like