0% found this document useful (0 votes)
200 views16 pages

Test1 - SECJ1013 - 20192020 - 01

Uploaded by

Yusra Hatim
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)
200 views16 pages

Test1 - SECJ1013 - 20192020 - 01

Uploaded by

Yusra Hatim
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/ 16

CONFIDENTIAL

UNIVERSITI TEKNOLOGI MALAYSIA

TEST 1 (WRITTEN)

SEMESTER I 2019/ 2020

SUBJECT CODE : SECJ/ SCSJ 1013


SUBJECT NAME : PROGRAMMING TECHNIQUE I
YEAR/COURSE : 1 (SECJ/ SECV/ SECB / SECR / SECP/ SCSJ/ SCSR)
TIME : 2.30 PM – 5.00 PM (2½ HOURS)
DATE : 22nd OCTOBER 2019 (TUESDAY)
VENUE : BK1 – BK7, N28

INSTRUCTIONS TO THE STUDENTS:

This test book consists of two parts:

PART A: 6 QUESTIONS (50 MARKS)


PART B: 6 QUESTIONS (50 MARKS)

TOTAL (100 MARKS)

ANSWER ALL QUESTIONS IN THIS BOOKLET IN THE SPACES PROVIDED.

Name

Matric No.

Section

Lecturer’s Name

This question booklet consists of 16 pages inclusive of the cover page.

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

BookUnits BookRetailCost BookTotalCost Output

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.

Write your answers by completing Table 2 and Table 3.

Start
End

Read weeks
Display 0.00

count = 0, bill = 0, True


totEnergy = 0
False
bill <= 30? Display bill
Read energy

bill = totEnergy * 0.57


totEnergy += energy
False
True
count++ totEnergy <= 300? bill = totEnergy * 0.43

False
True False True
count < weeks? totEnergy <= 200? bill = totEnergy * 0.218

Figure 1: Flowchart for Question 4

Table 2: Tracing table for Question 4(a)

weeks count energy totEnergy bill Output

4 0 - 0 0

Page 4 of 16
Table 3: Tracing table for Question 4(b)

weeks count energy totEnergy bill Output

3 0 - 0 0

QUESTION 5 (12 MARKS)


Flowchart in Figure 2 with subroutine in Figure 3 are used to check whether a number entered by the
user is prime number or not. Then, it will calculate the number of prime numbers. Complete the tracing
in Table 4 if the input data is: 5, 3, 0, 6.

Start checkPrime(n)

numPrime = 0, i = 0 k=n/2

False
i < 3? p = true, j = 2
True

Read num False


(j <= k) && (p == true)?
True
num <= 1?
True
False
status = n % j == 0?
checkPrime(num)
True False

False p = false
status == true? i++
True
j++
numPrime++

Return p
End

Figure 2: Main flowchart Figure 3: Subroutine

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.

Below is the description of identifiers use in the flow chart:


Identifiers Descriptions
item_count Count the number of items being processed.
item_weight Weight of item entered by the user.
cost Postage cost of item calculated by postageCost(weight) function.
item_charged Total number of items being charged with postage cost.
sum_cost Sum of overall cost for items being charged with 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

Figure 4: Incomplete flowchart for Question 6

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.

Table 6: Program segment and output for Question 1


Line Code Output
1 int a = 10, b = 4, c, d;
2 float x = 3.5, y;
3 c = a++ % static_cast<int> (x) / 4;
4 d = x * a + --c;
5 a /= d % b++ * x;
6 y = a * c + b / x;
7 b += -c * static_cast<int> (++y);
8 x = y-- * a - x;
9
10 cout << "a = " << a << endl;

11 cout << "b = " << b << endl;

12 cout << "c = " << c << endl;

13 cout << "d = " << d << endl;

14 cout << "x = " << x << endl;

15 cout << "y = " << y << endl;

QUESTION 3 (5 MARKS)
Write a code segment based on voting system as shown in Table 7 using selection statement.

Table 7: Voting system


Age Voting
0 - 17 Not allowed
18 - 65 Allowed without helper
66 - unlimited Allowed with helper

The example of outputs for this code segment as shown in Figure 5.

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

Figure 5: Example of outputs for 4 runs

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.

Table 8: Program segment and output for Question 4


Line Code Output
1 int p = 65, q = 100;
2 char x = 'A', y = 'd';
3 bool m = false, n;
4

5 cout << ((x + 35) == q) << endl;

6 cout << (!x || m) << endl;

7 cout << (((m + x - 100) < (p + y)) && !m)

8 << endl;
9 n = x + q;

10 q = y + n;

11 y = static_cast <char> (y + 1);;

12

13 cout << "n = " << n << endl;

14 cout << "q = " << q << endl;

15 cout << "y = " << y << endl;

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.

Table 9: List of fruits


Item Id Item Name Cost per Unit
A Apple RM 2.00
O Orange RM 2.50
L Lemon RM 1.80

Enter the item id: A


Enter the quantity of item: 5

Enter the item id: O


Enter the quantity of item: 4

Enter the item id: L


Enter the quantity of item: 3

Enter the item id: K


The total price is: RM25.4

Figure 6: Example of outputs for Question 6

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

You might also like