0% found this document useful (0 votes)
11 views15 pages

Final Question + Answer FSK1015 3202223

The document is a final examination paper for the ICT Programming and Logic Essentials course (FSK1015) conducted by Dr. Tay Chai Jian on June 20, 2023. It consists of eleven questions covering programming outputs, array validity, true/false statements, flowchart design, and programming tasks related to calculations and outputs. Candidates are instructed to answer all questions within a 3-hour duration, using an answer booklet.

Uploaded by

Lee Kah Hing
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)
11 views15 pages

Final Question + Answer FSK1015 3202223

The document is a final examination paper for the ICT Programming and Logic Essentials course (FSK1015) conducted by Dr. Tay Chai Jian on June 20, 2023. It consists of eleven questions covering programming outputs, array validity, true/false statements, flowchart design, and programming tasks related to calculations and outputs. Candidates are instructed to answer all questions within a 3-hour duration, using an answer booklet.

Uploaded by

Lee Kah Hing
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/ 15

FINAL EXAMINATION

COURSE : ICT PROGRAMMING AND LOGIC


ESSENTIALS

COURSE CODE : FSK1015

COURSE COORDINATOR : DR TAY CHAI JIAN

DATE : 20 JUNE 2023

DURATION : 3 HOURS

SESSION/SEMESTER : SESSION 2022/2023 SEMESTER III

INSTRUCTIONS TO CANDIDATES:
1. This examination paper consists of ELEVEN (11) questions. Answer ALL questions.
SESSION/SEMESTER : SESSION 2020/2021 SEMESTER II
2. All answers to a new question should start on a new page.
3. All calculations and assumptions must be clearly stated.
4. Candidates are not allowed to bring any materials other than those allowed by the
invigilator into the examination room.

EXAMINATION REQUIREMENTS:
1. Answer booklet

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

This examination paper consists of NINE (9) printed pages including the front page.
CONFIDENTIAL 2223III/FSK1015

QUESTION 1 [12 MARKS]

Write the output of the following programs:

(a)
#include <stdio.h>

int main(void) {

int i = 2;
do
{
if(i++ == 4)
continue;
printf("%d\n", i);
i += 1;
} while(i < 10);

return 0;
}

Answer
3
6
8
10
[2 Marks]

(b)
#include <stdio.h>

int main(void) {
int x = 4, y = 8;
switch(y > x || x – y > 0)
{
case 1:
x *= x + 3;
printf("x is %d", x);
break;

case 0:
y –= 3;
printf("y is %d", y);
break;

default:
printf(" Hello bye ");

2
CONFIDENTIAL 2223III/FSK1015

return 0;
}

Answer
x = 4*7 = 28
x is 28
[2 Marks]
(c)
#include <stdio.h>

int main(void) {
int x = 10;

if(x! = 10)
x++;
else
x– –;

printf("The value of x is %d", x);

return 0;
}

Answer
The value of x is 9
[2 Marks]

(d)
#include <stdio.h>

int main(void) {
int i = 0;

while (++i) {
printf(“H”);
}

return 0;
}

Answer
Unlimited H will be printed/infinite loop of H
[2 Marks]

3
CONFIDENTIAL 2223III/FSK1015

(e)
#include <stdio.h>

int main(void) {
int x, count=0;

for (x = 2; x <= 10; ++x) {


count++;
if (x == 4)
continue;
printf("x is %d\n", x);
}
return 0;
}

Answer
x is 2
x is 3
x is 5
x is 6
x is 7
x is 8
x is 9
x is 10
[4 Marks]

QUESTION 2 [6 MARKS]

Based on the following program, what is the output if:

#include <stdio.h>

int main(void) {
int x, y;
if (y == 8)
if (x == 5)
printf("@@@@@\n");
else
printf("#####\n");
printf("$$$$$\n");
printf("&&&&&");

return 0;
}

4
CONFIDENTIAL 2223III/FSK1015

(a) x = 5, y =8
Answer
@@@@@
$$$$$
&&&&&
[2 Marks]
(b) x = 6, y =8
Answer
#####
$$$$$
&&&&&
[2 Marks]
(c) x = 5, y =9
Answer
$$$$$
&&&&&
[2 Marks]

QUESTION 3 [6 MARKS]

Identify whether the following declarations and initializations of arrays in C are valid or
invalid:
(a) int size;
int marks[size];
Invalid
(b) int marks[105];
Valid
(c) #define size 105
:
int marks[size];
Valid
(d) int result[5] = {34, 48, 78};
Valid

5
CONFIDENTIAL 2223III/FSK1015

(e) int result[ ] = {12, 29, 34, 48, 78};


Valid
(f) int result[5] = {5, 12, 29, 34, 48, 78};
Invalid
[6 Marks]

QUESTION 4 [6 MARKS]

State whether the following are true or false:


(a) An array can store many different types of values.
False
(b) An array subscript can be of data type double.
False
(c) It is an error if an initializer list contains more initializers than the elements in the array.
True
(d) Function fscanf cannot be used to read data from the standard input.
False
(e) We must explicitly use fopen to open the standard input, standard output and standard
error streams.
False
(f) Function fprintf can be used to write to the standard output.
True
[6 Marks]
[CO1, PO5]

6
CONFIDENTIAL 2223III/FSK1015

QUESTION 5 [15 MARKS]

Design a flowchart for the steps in baking cookies below. (Hint: the underline word is the
keyword for the flowchart)

(1) Prepare ingredients such as flour, sugar, butter, cream, salt and baking soda for baking
cookies.
(2) Check the temperature of the oven. If the temperature of the oven is 180°C, proceed to
the next step. If not, preheat the oven to 180°C.
(3) Mix the sugar, butter, cream in a mixer.
(4) Beat the mixture.
(5) Check if the mixture is creamy. If the mixture is creamy, proceed to the next step. If
not, continue to beat the mixture.
(6) Sift the flour, salt and baking soda into the mixture.
(7) Stir the mixture.
(8) Shape the mixture by using a tablespoon or an ice cream scooper, depending on your
desired cookie size.
(9) Place the mixture on a greased cookie sheet.
(10) Bake the mixture for 15 minutes.
(11) Cookies are ready to eat.

7
CONFIDENTIAL 2223III/FSK1015

Answer

[15 Marks]

8
CONFIDENTIAL 2223III/FSK1015

QUESTION 6 [10 MARKS]

Write the output of the following program. Show all your steps before arriving to final answer.

#include <stdio.h>

int main(void) {

int i;
double x[5], z[5];
double y[5] = {1, 3, –1, 1, –1};

x[0] = 2.35; x[1] = –1; x[2] = 0.8; x[3] = 0.95; x[4] = –5.5;

for (i = 0; i <= 4; i++)


printf("x[%d] = %.2lf, y[%d] = %.2lf\n", i, x[i], i, y[i]);
printf("\nSum of vectors x and y:\n");

for (i = 0; i <= 4; i++)


{
z[i] = x[i] + y[i];
printf("z[%d] = %.2lf\n", i, z[i]);

return 0;
}

Answer

[10 Marks]

9
CONFIDENTIAL 2223III/FSK1015

QUESTION 7 [5 MARKS]

Based on the following program, complete Table 1 and Table 2.

#include <stdio.h>

int main(void) {

int i, z, p;
int x = 2, y = 3;
int m = 5, n = 6;

for(i = 1; i<= 2; i++)


z = – –x + y++;
printf("The values of x and y are %d and %d\n", x, y);

for(i = 1; i <= 3; i++)


p = m + n– – – m + m++ – – –n;
printf("The values of m and n are %d and %d", m, n);

return 0;
}

Table 1
i x y z
1 2 4 5
2 1 5 5
[2 Marks]

Table 2
i m n p
1 6 4 7
2 7 2 8
3 8 0 9
[3 Marks]

10
CONFIDENTIAL 2223III/FSK1015

QUESTION 8 [10 MARKS]

Write a program to ask user to enter two numbers, which are denoted by x and y, then

(a) Calculate the product of x and y.

(b) Calculate the value of x divides y.

(c) Calculate the average value of x and y.

Answer

#include <stdio.h>

int main(void) {

float x, y, z1, z2, Avg;

printf("Please enter two numbers: ");


scanf("%f %f", &x, &y);
z1 = x*y;
z2 = y/x;
Avg = (x + y)/2;

printf("The product of %.1f and %.1f is %.1f\n", x, y, z1);


printf("%.1f divides %.1f is %.1f\n", x, y, z2);
printf("The average is %.1f", Avg);

return 0;
}

[10 Marks]

11
CONFIDENTIAL 2223III/FSK1015

QUESTION 9 [10 MARKS]

Write a program to calculate the water bill for customers. The bill comprises of RM 3 basic
charge and the charge for total water used. The rate of the water cost is RM 0.80 for each
thousand gallon used. Amount of water used is calculated by subtracting the meter reading (in
gallon thousand unit) for the previous month from the current month. The meter readings for
previous and current months should be input by users. Your program should display the final
amount to be paid by the customer.

Answer
#include <stdio.h>

int main(void) {

int R1, R2, R3;


float charge;

printf("Please enter previous meter reading: ");


scanf("%d", &R1);
printf("Please enter current meter reading: ");
scanf("%d", &R2);
R3 = R2 - R1;
charge = 3 + (0.8*R3);

printf("The amount to be paid is %.2f", charge);

return 0;
}

[10 Marks]

12
CONFIDENTIAL 2223III/FSK1015

QUESTION 10 [8 MARKS]

Write a program to produce the output in Figure 1 below. (Hint: use nested for loops)

Figure 1

Answer

#include <stdio.h>

int main(void) {
int i, j, k;

for(i = 1; i <= 5; i++)


{
for(j = 1; j <= 4; j++)
{
k = i + j;
printf("%d ", k);
}
printf("\n");
}

return 0;
}

[8 Marks]

13
CONFIDENTIAL 2223III/FSK1015

QUESTION 11 [12 MARKS]

Write a program to ask user to enter the values of radius and height of an object, then calculate
the volumes of a cylinder and a sphere, respectively. The calculation of volumes should be
performed in two separate functions as below:

float cylinder(float r, float h)


float sphere(float r)

A sample output is shown in Figure 2:

Figure 2

4 3
The volume of a cylinder is given by  r 2 h while the volume of a sphere is  r . Display the
3
volumes in 2 decimal places.

(Note: π = 3.142)

14
CONFIDENTIAL 2223III/FSK1015

Answer
#include <stdio.h>
#define pi 3.142

float cylinder(float r, float h);


float sphere(float r);

int main(void) {

float r, h, Volume1, Volume2;

printf("Please enter the radius and height: ");


scanf("%f%f", &r, &h);
Volume1 = cylinder(r, h);
Volume2 = sphere(r);

printf("The volume of a cylinder is %.2f\n", Volume1);


printf("The volume of a sphere is %.2f", Volume2);

return 0;
}

float cylinder(float r, float h)


{
float Volume;
Volume = pi*r*r*h;
return Volume;

float sphere(float r)
{
float Volume;
Volume = (4.0/3)*pi*r*r*r;
return Volume;

[12 Marks]

END OF QUESTION PAPER

15

You might also like