You are on page 1of 13

CONFIDENTIAL EE/JAN 2021/ECE128

UNIVERSITI TEKNOLOGI MARA


WRITTEN TEST

COURSE : COMPUTER PROGRAMMING


COURSE CODE : ECE128
EXAMINATION : 14 JANUARY 2021
TIME : 1.5 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of 6 Questions

2. Answer ALL questions in the Answer Sheet.

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
5 CLO1-PLO1
6 CLO1-PLO1

TOTAL MARKS

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


This examination paper consists of 13 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 EE/JAN 2021/ECE128

QUESTION 1

Refer to the Listing Q1, identify the missing statement or expression inside each of the
following programs so that the given output as given in Figure Q1 is obtained.
(4 marks)

Source Code Answer


void main() (i)
{ (ii)
int x = 2;
float y = 5.2;
int z;
printf(“ (i) ”,x);
printf(“Enter a number for value z:”);
(ii) ;
y = x-y+z;
printf(“%.2f”,y);
return 0;
}
Listing Q1

Number assigned for x is 2


Enter a number for value z:5
1.80
Figure Q1

QUESTION 2

a) Consider the following program in Listing Q2a. Draw a flowchart to represent the if
statement used in the blue box given.

(6 marks)

int main()
{
int x;
printf("Please enter a number:");
scanf ("%d",&x);
if(x > 0)
{
printf("\nThe number %d is greater than 0",x);
}
else
{
printf("\nThe number %d is less than or equal zero",x);
}
return 0;
}
Listing Q2a

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 EE/JAN 2021/ECE128

Answer:

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 EE/JAN 2021/ECE128

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

int main() {
int input;
printf( "1. Play game\n" );
printf( "2. Play multiplayer\n" );
printf( "3. Exit\n" );
printf( "Selection: " );
scanf( "%d", &input );
switch ( input ) {
case 1:
printf( "Option 1 selected\n" );
break;
case 2:
printf( "Option 12 selected\n" );
break;
case 3:
printf( "Option 3 selected\n" );
break;
default:
printf( "Bad input, quitting!\n" );
break; }
return 0; }
Listing Q2b

Answer:

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 EE/JAN 2021/ECE128

QUESTION 3

Repetition is a sequence of statements that keep repeating until certain condition is reached.
Listing Q3 shows a source code that used repetition statement to accomplish a specific task.
a) List and describe two types of loop.
(3 marks)

b) With the aid of a diagram, illustrate the flowchart of the repetitive statement in Listing Q3.

(3 marks)

c) As referring to Listing Q3, if a user enters value ‘10’ during line 5 execution, predict the
answer that will be displayed as output during line 10 execution.
(2 marks)

d) Explain your answer in Question 3c, hence, find an appropriate word/sentence for the
blank part in line 6.
(3 marks)

1. int main()
2. {
3. int i,n;
4. printf(“\nEnter A Number: “);
5. scanf(“%d”,&n);
6. printf(“\n BETWEEN 1 AND %d ARE: \n”,n);
7.
8. for(i=1;i<n;i+=2)
9. {
10. printf(“%d”,i);
11. }
12. }

Listing Q3

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 EE/JAN 2021/ECE128

Answer:
a)

b)

c)

d)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 EE/JAN 2021/ECE128

QUESTION 4

a) List two (2) advantages of using functions in programming.


(2 marks)

Answer:

b) Find the missing statement in the blanks (i) – (ii) in Listing Q4b to produce the output as
shown in Figure Q4b. Hence, identify a global variable from the listing.
(4 marks)
#include <stdio.h> Answer:
#include <stdlib.h>
int x = 5; i)
void dip1(); ii)
void dip2(int);
int main()
{
global variable:

printf("ECE128: C Programming\n");
(i) ;
for (int i = 1; i <6;i++)
{
if (i==2)
continue;
(ii) ;
}
return 0;
}
void dip1()
{
printf("Hello All %d Students\n\n",x);
}
void dip2(int i)
{
printf("Hello Student %d\n",i);
}
Listing Q4b

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 EE/JAN 2021/ECE128

ECE128: C Programming
Hello All 5 Students

Hello Student 1
Hello Student 3
Hello Student 4
Hello Student 5

Figure Q4b

c) By referring to Figure Q4c(i), write the missing statement in Listing Q4c using for loop to
ask the user to enter any three (3) integers between 10 and 50 inclusively. All entered
integers will be passed to a function named "MULL" that will multiply all three integers that
has been entered by the user. The function returns the result to the main function and
displayed the output shown in Figure Q4c(ii).
(5 marks)

start

Start MULL(int
x)
Function MULL
declaration

Calculate total
Declare global variable
total as integer and
initialize with 1

Return total
Declare int result, and
temp. Initilize temp = 0

Display instructions for


user to enter any 3 number
between 10 to 50

Declare and Initialize


counter variable with 1

No
Display
i<4?
Result

Yes

Ask user to enter


each number End

Result=MULL(temp)

i++

Figure Q4c(i)
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 9 EE/JAN 2021/ECE128

#include <stdio.h>
#include <stdlib.h>
int ;

int total = 1;
int main()
{
int result,temp = 0;
printf(“Please enter any 3 numbers between 10 to 50\n”);

for

{
printf(“Please enter %d number = “,i);
scanf(“%d”,&temp);
result= MULL(temp);
}
printf(“Your result = %d”,result);
}

{
total = total * x;
return total;
}

Listing Q4c

Please enter any 3 numbers between 10 to 50


Please enter 1 number = 10
Please enter 2 number = 20
Please enter 3 number = 30

Your result = 6000


Figure Q4c(ii)

Figure Q4c(ii)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 EE/JAN 2021/ECE128

QUESTION 5

a) Write C statements(s) to accomplish each of the following:

i) Declare an array letters with ten (10) elements of type char.


ii) Assign the character value 'Z' to the fourth element of the letters array.
iii) Use a printf statement to print out the contents of the character array words.

(6 marks)
Answer:
i)
ii)
iii)

b) Based on Figure Q5b(i), construct the statement to display each element in array arr[ ]
as shown in Figure Q5b(ii) using for loop statement.
(6 marks)

arr[ ] 4.7 5.5 7.891 14.3 2.0 9.8 20.1


Figure Q5b(i)

arr =[4.7 5.5 7.9 14.3 2.0 9.8 20.1]

Figure Q5b(ii)
Answer:

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 11 EE/JAN 2021/ECE128

QUESTION 6

A pointer is used to reference a location in the memory.

a) Write a statement for the following:


i) Declare a variable pointer name Var1 with type integer.
ii) Print output for the content that pointed by Var1.
(3 marks)

Answer:
i)

ii)

b) Write the printf statement in Listing Q6b to display each array element’s value of price
as shown in Figure Q6b.
(3 marks)
int main () {

/* an array with 5 elements */


float price[5] = {8.0, 2.5, 6.45, 13.0, 34.2};
float *p;
int i;

p = price;

/* output each array element's value */


printf("Array values using pointer\n");

for ( i = 0; i < 5; i++ ) {


printf( );
}
}

Listing Q6b

Array values using pointer


*(p + 0) : 8.00
*(p + 1) : 2.50
*(p + 2) : 6.45
*(p + 3) : 13.00
*(p + 4) : 34.20

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 12 EE/JAN 2021/ECE128

Figure Q6b
c) Complete the program in Listing Q6c to produce an expected output as shown in
Figure Q6c:
(5 marks)

int main()
{
int *ptr;
int x;

ptr= (i) ; //assign x to ptr


*ptr = 0;

printf(" x = %d\n", x);


printf(" *ptr = %d\n", (ii) );

*ptr (iii) ; //update the content of x using *ptr


printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);

( (iv) )++; //update the content of x using *ptr


printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);

(*ptr) (v) ; //update the content of x using *ptr


printf(" x = %d\n", x);
printf(" *ptr = %d\n", *ptr);
return 0;
}

Listing Q6c

x=0
*ptr = 0
x =5
*ptr = 5
x=6
*ptr = 6
x=5
*ptr = 5
Figure Q6c

Answer:
i)
ii)
iii)
iv)
v)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 13 EE/JAN 2021/ECE128

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like