Test1 - SECJ1013 - 20192020 - 01
Test1 - SECJ1013 - 20192020 - 01
TEST 1 (WRITTEN)
Name
Matric No.
Section
Lecturer’s Name
Page 1 of 16
PART A: PROBLEM-SOLVING QUESTIONS [50 MARKS]
QUESTION 1 (6 MARKS)
Complete the following pseudo code that adds two integer numbers between 1 and 5 (1 and 5 are
included). The pseudo code will require an input of two integer numbers. The program able to request
repetitively from the user to key in the number again if the number entered is out of range from 1 to 5.
Use a proper input validation to handle this situation. Then, the pseudo code will continue to calculate
the total of two integer numbers after valid numbers entered. Finally, the pseudo code will display the
total of two numbers.
1. Start
2. Read num1
3. If __________________________________________________
3.1 __________________________________________________
4. End If
5. Read num2
6. If __________________________________________________
6.1 __________________________________________________
7. End If
8. _____________________________________________________
9. Display total
10. End
QUESTION 2 (6 MARKS)
Determine the output for each run of the following pseudo code for the given inputs in Table 1. Write
your answers by completing the Table 1.
1. Start
2. Read BookUnits
3. Set BookRetailPRICE = 99
4. If (BookUnits >= 1) AND (BookUnits < 5)
4.1 BookTotalCost = BookUnits x BookRetailPRICE
5. Else_If (BookUnits >= 5) AND (BookUnits < 15)
5.1 BookRetailCost = BookUnits x BookRetailPRICE
5.2 BookTotalCost = BookRetailCost - (BookRetailCost x .20)
6. Else_If (BookUnits >= 15)
6.1 BookRetailCost = BookUnits x BookRetailPRICE
6.2 BookTotalCost = BookRetailCost - (BookRetailCost x .40)
7. Else
7.1 Display “No book been purchased”
7.2 Goto 10
8. End_If
9. Display BookTotalCost
10. End
Page 2 of 16
Table 1: Tracing table for Question 2
12
25
QUESTION 3 (8 MARKS)
Draw a flowchart to find the largest number among the three different numbers entered at one time by
user. Note: You are not allowed to use logical operator (AND, OR, NOT).
Page 3 of 16
QUESTION 4 (8 MARKS)
Trace the value of the variables and determine the output of flowchart in Figure 1 if the input for the
weeks and energy (in kWh) are as follows:
a) 4 weeks, the first week of energy used was 64 kWh and the energy consumed for 4 consecutive
weeks increased by 10 kWh per week.
b) 3 weeks, the energy consumed in first week is 7.5 kWh per day and it will be decreased 2 kWh
per day in the following weeks.
Start
End
Read weeks
Display 0.00
False
True False True
count < weeks? totEnergy <= 200? bill = totEnergy * 0.218
4 0 - 0 0
Page 4 of 16
Table 3: Tracing table for Question 4(b)
3 0 - 0 0
Start checkPrime(n)
numPrime = 0, i = 0 k=n/2
False
i < 3? p = true, j = 2
True
False p = false
status == true? i++
True
j++
numPrime++
Return p
End
Page 5 of 16
Table 4: Tracing table for Question 5
Variables Conditions
i num numPrime status n k p j i<3 num <= 1 status == true (j <= k) && (p == true) n % j == 0
Page 6 of 16
QUESTION 6 (10 MARKS)
You are requested to write a program by the client to sum the total postage cost of five items. Below is
the list of requirements given by the client.
a) The program should able to detect if item’s weight entered is valid. Item should not weigh more
than 1 kg. Zero (0) or less value entry is considered as an invalid input of item weight.
b) Use function named postageCost() to calculate postage cost to be charged to the item. Based on
the item weight, item may not be charged thus, this function may return a zero (0) value of
postage cost.
c) The final output of the program is the total number of item being charged with postage cost and
the sum of the overall postage cost.
Complete flowchart in Figure 4 by filling the blanks labelled (a) to (i) with appropriate instruction/
statement. Write your answers in Table 5.
Start (h)
True
item_count = 0, item_charged = 0 w <= 0.25 c=0
False
False
(a) True
w <= 0.5 c=2
True False
Read item_weight c=5
True (i)
(b)
False
cost = postageCost(item_weight)
False
(f) (c)
True
(g)
(d)
(e) End
Page 7 of 16
Table 5: Answer for Question 6
No Instruction/ Statement
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
Page 8 of 16
PART B: PROGRAMMING QUESTIONS [50 MARKS]
QUESTION 1 (6 MARKS)
The following program has syntax and/ or logical error(s). Find the error(s) as many as you can and
write the correct program in the space provided.
1 include <iostream>
2 using namespace std;
3
4 int main
5 {
6 float double = 7.8;
7
8 /* Print a value of 'double'
9 return 0;
10
11 cout << "double = " << double
12 << endl;
13 }
Answer:
Page 9 of 16
QUESTION 2 (6 MARKS)
Determine the output for the program segment given in Table 6. Write your output with two decimal
points if applicable in Output column in Table 6.
QUESTION 3 (5 MARKS)
Write a code segment based on voting system as shown in Table 7 using selection statement.
Page 10 of 16
Run 1 Run 2
Please enter your age: 16 Please enter your age: 20
You can't vote You can vote without helper
Run 3 Run 4
Please enter your age: 90 Please enter your age: 50
You can vote with helper You can vote without helper
Answer:
Page 11 of 16
QUESTION 4 (6 MARKS)
Determine the output for the program segment given in Table 8. Write your output in Output
column in Table 8. Note: ASCII value of ‘A’ = 65 and ‘d’ = 100.
8 << endl;
9 n = x + q;
10 q = y + n;
12
QUESTION 5 (9 MARKS)
Trace the output of the following program segments:
a)
1 int i = 0, j = 1, k;
2
3 while (i < 4)
4 {
5 (i > 1) ? j = j * 2 : j = j * 1;
6 for (k = 0; k < j; k++)
7 cout << j;
8
9 cout << endl;
10 i++;
11 }
Page 12 of 16
Answer:
b)
1 int i = 10, j;
2 while (i > 0)
3 {
4 for (j = 1; j < i; j*=5)
5 {
6 cout << i << " " << j << endl;
7 if (j > 5)
8 break;
9 else
10 continue;
11 }
13 i -= 3;
14 }
Answer:
Page 13 of 16
QUESTION 6 (18 MARKS)
You have a program to calculate the total price of fruits. The program will receive item id as a user
input. Table 9 shows the list of fruits. The process to calculate the total price will be repeated until the
user enter invalid item id (refer Table 9). Finally, the program will display the total price of fruits. Figure
6 shows example of output for the program.
Page 14 of 16
Complete and write the code segment of main() function for the program using:
a) Post-test loop and if statement
int main()
{
char choice;
int qty;
float price, tot_Price = 0;
return 0;
}
Page 15 of 16
b) Pre-test loop and switch statement
int main()
{
char choice;
int qty;
float price, tot_Price = 0;
return 0;
}
Page 16 of 16