Professional Documents
Culture Documents
FinalExam Question
FinalExam Question
This examination question paper consists of NINE (9) pages including the cover page.
This examination contributes 50% towards the total mark of the course.
INSTRUCTIONS TO CANDIDATE:
1. Answer ALL FIVE (5) questions. All questions carry equal marks.
2. All working steps, answer, and codes must be handwritten. Cross through any work
that you do not wish to be marked.
3. Answers to exam questions written in a programming language other than C++ will
not receive credit.
4. Write your answers on A4 size paper, either in Black or Blue colour pen – any
unreadable answer / information will be voided.
1
5. Write your name and SEGi Student ID on every page (top right corner) of your
answer sheets, and number all your answer sheets (bottom right corner) in
chronological order.
6. Scan all your answer sheets into a single PDF (together with the Student’s Answer
Script Cover Page provided), and name it under the Module Code, followed by your
SEGi Student ID, eg: EME3283_SUKD123456.pdf
7. Upload your single PDF file to the Blackboard submission page before 6 pm on
22 August 2020 (Malaysia time).
INFORMATION TO CANDIDATE:
2. Test 2 / /
3. Assignment /
4. Final Exam / /
Question CO
1 1
2 1
3 2
4 2
5 2
2
Answer all FIVE (5) questions
Question 1
(10 marks)
(b) Analyze whether the following variable names are valid or invalid. If they are
invalid, state the reason.
(i) 19meter
(ii) cos_1
(iii) switch
(iv) r3su1t
(v) _20year old
(5 marks)
3
(c) In the following program, analyze any errors that would prevent the program from
compiling or running logically and write ONLY the necessary corrections.
Copy Table Q1(c) in your answer script and fill it up. Provide your answer which
includes the corrected codes and their corresponding line numbers ONLY in
Table Q1(c).
Hint: some lines may have multiple errors while some lines may have no error, or
there might be missing code in between two lines.
Table Q1(c)
Line No. Corrections
(5 marks)
(Total: 20 marks)
4
Question 2
(a) Part of program development is the visualization of the program using a flowchart.
Draw the flowchart for the following pseudocodes:
Write a program that prompts the user for the necessary information, performs the
calculation for distance travelled by an accelerating vehicle, and displays the
distance travelled with 2 decimal places. Ensure that the user inputs the values with
correct units.
(10 marks)
(Total: 20 marks)
5
Question 3
(a) Apply the if-else flow control structure to write a program that performs the
following tasks: prompts the user to input a number, then displays the number with
a message saying whether the number is positive, negative or zero.
(10 marks)
(b) Apply the while and switch flow control structure to write a program that produces
the outputs corresponding to the relevant inputs as shown in
Table Q3(b). Invalid marks entered (< 0 or > 100) should generate an error message
and then terminate the program.
Hint: assume the input as integers and divide by 10 for the switch case.
Table Q3(b)
Input: Marks Output: Grades
80 – 100 HD
70 – 79 D
60 – 69 C
50 – 59 P
0 – 49 F
(10 marks)
(Total: 20 marks)
6
Question 4
(a) Write a program that performs the following task 5 times using the for-loop flow
control structure to: obtain an integer number from user, calculate the average of
all integers entered so far, and display the average. Below is an example of the input
and output of the program with some explanations.
Enter an integer: 1
Average so far: 1 Average = 1 / 1
Enter an integer: 2
Average so far: 1.5 Average = (1 + 2) / 2 = 1.5
Enter an integer: 3
Average so far: 2 Average = (1 + 2 + 3) / 3 = 2
(10 marks)
7
(b) An engineer wants to create the following 8 x 8 array 𝐴 from a 2 x 2 array 𝐵:
1 2 0 0 0 0 0 0
⎡3 4 0 0 0 0 0 0⎤
⎢ ⎥
0 0 1 2 0 0 0 0
⎢ ⎥
0 0 3 4 0 0 0 0⎥ 1 2
𝐴= ⎢ ; 𝐵=
⎢0 0 0 0 1 2 0 0⎥ 3 4
⎢0 0 0 0 3 4 0 0⎥
⎢0 0 0 0 0 0 1 2⎥
⎣0 0 0 0 0 0 3 4⎦
To do this, the engineer wrote the following code to duplicate copies of array 𝐵 to
form 𝐴. However, the code does not give the result wanted. In the following code,
analyze any errors and write ONLY the necessary corrections in Table Q4(b).
Copy Table Q4(b) in your answer script and fill it up. Provide your answer which
includes the corrected codes and their corresponding line numbers ONLY.
Table Q4(b):
Line No. Corrections
(10 marks)
(Total: 20 marks)
8
Question 5
The electrical output voltage (𝑉) of a temperature sensor has been calibrated with various
temperatures (𝑇). The calibration result is shown in Table Q5.
Table Q5
Temperature 𝑻 (℃) 2 5 13 20 29 35 43
Voltage 𝑽 (𝐦𝐕) 3 5 8 12 14 17 21
The data in Table Q5 is fitted using linear regression model to obtain the best fit line:
𝑉 = 𝑎 + 𝑎 𝑇, where the coefficients 𝑎 and 𝑎 are given by:
𝑛 ∑(𝑇 𝑉 ) − ∑ 𝑇 ∑ 𝑉
𝑎 =
𝑛 ∑(𝑇 ) − (∑ 𝑇 )
𝑎 =𝑉−𝑎 𝑇
∑ ∑
with 𝑛 = 7, 𝑉 = is the mean of 𝑉, and 𝑇 = is the mean of 𝑇.
Write a program that applies arrays, function and loop structures to perform the following:
(a) Declare two arrays called temp and volts and initialize them respectively
with the values of Temperature 𝑇 and Voltage 𝑉 as shown in Table Q5.
(4 marks)
(b) Write a function named Sum() that apply a loop structure to calculate and return
the sum of the elements of a given array.
(6 marks)
(c) Assume that the values of 𝑇 𝑉 and 𝑇 have been calculated and stored in arrays
TV and Tsq respectively, write the statements to perform calculation for the
coefficient 𝑎 , and 𝑎 for the linear regression model using the function written in
Part (c) where necessary.
(4 marks)
Note: you may assume all necessary libraries are already #included for your answer.
(Total: 20 marks)