You are on page 1of 10

CONFIDENTIAL EE/JAN 2022/ECE128

UNIVERSITI TEKNOLOGI MARA


WRITTEN TEST

COURSE : COMPUTER PROGRAMMING


COURSE CODE : ECE128
EXAMINATION : 5 JANUARY 2022
TIME : 1.5 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of 4 Questions

2. Answer ALL questions.

3. Open-source test but strictly NO CHEATING and DISCUSSION.

NAME

STUDENT ID GROUP

LECTURER

QUESTION CLO-PLO MARKS

1 CLO1-PLO1

2 CLO1-PLO1

3 CLO1-PLO1

4 CLO1-PLO1

TOTAL MARKS

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 10 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 E/JAN2022/ECE128

QUESTION 1

a) Identify the following variable name is valid or invalid.


i) 5_Number
ii) _price
iii) @student
iv) ECE128

(2 marks)

Answer
(i)
(ii)
(iii)
(iv)

b) Match the following shape with correct purpose.


(2 marks)

Function
• •

Point/terminal
• •

Condition/Decision
• •

Process/Data
• •

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 E/JAN2022/ECE128

c) Determine the value is assigned for each variable after the execution of each line of the
statement.
(6 marks)

a b c

int a, b, c;

a=4, b=2, c=7; 4 2 7

b++;

b=b-2;

++a;

c=7%3+4*2-(1+12)/4;

b=(c++) - a;

QUESTION 2

a) Using if…else if statement, write the source code to complete Listing Q2a to display a
message based on Table Q2a. Display “Invalid value” if the temperature is not in
the list.
(4 marks)

Table Q2a
Temperature Message

<=35 Hypothermia

35 – 37.5 Normal

> 37.5 Not allowed to enter this premise

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 E/JAN2022/ECE128

#include <stdio.h>
int main()
{
float temp;
printf(" Enter your body temperature: ");
scanf("%f", &temp);
printf(" entered value = %f\n", temp);

if ( (i) )
printf(" Hypothermia\n");
else if ( (ii) )
printf(" Normal \n");
else if ( (iii) )
printf("Fever \n");
else
(iv) )
}
Listing Q2a

Answer:

(i)

(ii)

(iii)

(iv)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 E/JAN2022/ECE128

b) Modify the program in Listing Q2b with an equivalent program by replacing if..else with
switch..case statement.
(8 marks)

#include<stdio.h>
int main(){
int id;
float salary, Annual_Salary;
printf("WELCOME TO BZ TRAVEL\n");
printf("Please key-in your ID:\t");
scanf("%d",&id);

/* Part A: Determine the salary */


if(id == 197866)
salary =1789.56;
else if ( id == 205705)
salary =1950.00;
else if ( id == 249883)
salary =3457.18;
else printf("\n Invalid ID\n");

/* Part B: Calculate the annual salary */


Annual_Salary = salary* 12;
printf("--------------------------------------\n");
printf("Monthly Salary : %.2f \n", salary);
printf("Annual Salary : %.2f\n",Annual_Salary);
printf("--------------------------------------\n");
}
Listing Q2b

Answer:
#include<stdio.h>
int main(){
int id;
float salary, Annual_Salary;
printf("WELCOME TO BZ TRAVEL\n");
printf("Please key-in your ID:\t");
scanf("%d",&id);
/* Part A: Determine the salary */

/* Part B: Calculate the annual salary */


Annual_Salary = salary* 12;
printf("--------------------------------------\n");
printf("Monthly Salary : %.2f \n", salary);
printf("Annual Salary : %.2f\n",Annual_Salary);
printf("--------------------------------------\n");
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 E/JAN2022/ECE128

QUESTION 3

a) Rewrite a new program that is equivalent to the program in Listing Q3a by replacing for
loop with do...while loop statement.
(6 marks)

void main()
{
int i, j, sum;
for(i=1; i<=3; i++)
{
sum=0;
do
{
sum +=5;
printf(“Sum = %d\n”,sum);
}
while (sum<=10);
printf(“Loop = %d\n\n”,i);
}
return 0;
}

Listing Q3a

Answer:

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 E/JAN2022/ECE128

b) Figure Q3b shows the flow chart of the program to detect value seven (7) when the user
enters a number between one (1) until ninety-nine (99). Based on Figure Q3b construct
the complete C statement by using while loop and break statement.

(6 marks)

START

Declare variable name


valCheck

Display Enter
any value
between 1 - 99

Store value in
valCheck variable.

YES Store value in


Display Detected! valCheck == 7?
valCheck variable.

NO

END
Display Enter
again!

Figure Q3b

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 E/JAN2022/ECE128

Answer:

int main()
{

printf("Enter any value between 1 - 99\n");


scanf("%d",&valCheck);

{
if

else
{

}
}

return 0;
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 E/JAN2022/ECE128

QUESTION 4

Listing Q4 shows a simple source code using a selective statement. Re-write the source code
by splitting the selective statement into a function named display(int choice).

(6 marks)

#include <stdio.h>
int main()
{
int choice;
printf("Enter your choice:\n");
scanf("%d",&choice);

/* Part A: DISPLAY */
if (choice ==1)
printf("Booking\n");
else if (choice== 2)
printf("Cancel Booking\n");
else
printf("None of the above\n");
return 0;
}

Listing Q4

Answer:
#include <stdio.h>

int main()
{
int choice;
printf("Enter your choice:\n");
scanf("%d",&choice);

return 0;
}

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 E/JAN2022/ECE128

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like