SEHH2042 Test VersionB QP
SEHH2042 Test VersionB QP
This question paper has a total of NINE pages (including this covering page).
Instructions to Candidates:
Authorised Materials:
YES NO
CALCULATOR [] [ ]
SPECIFICALLY PERMITTED ITEMS [ ] []
Answer ALL questions in this section in the space provided in the answer sheet. Each
question carries 10 marks.
Question A1
(a) Find the value of sum after the program segment below is successfully executed. (3 marks)
(b) Find the value of ans after the program segment below is successfully executed. (3 marks)
int ans;
int b, c, d;
double e, f;
b = 5;
c = 12;
d = 6.7;
e = 4;
f = 5.5;
ans = d * f / e + c % b;
(c) Find the value of total after the program segment below is successfully executed. (4 marks)
int total = 0;
char x = 'X';
switch (x) {
case 'W': total += 1;
case 'X': total += 2;
case 'w': total += 3; break;
case 'x': total += 4;
default: total += 5;
}
Fill in your codes for blank <1> in the partial-completed program below so that it gives the sample
output when it is successfully executed.
The program accepts one floating point, and two characters, as inputs. These data are then
displayed according to the format below:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x;
char y, z;
return 0;
}
Sample output
Your input: 2.66 * #
Display:
* 2.660 #
Consider the partial-completed program below that concerns with random numbers in C++.
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
return 0;
}
Sample output
1 3 1 9 9 9 7 5 9 9
(a) Fill in your codes in blank <1> to set the seed value for random number generation to 100.
(2 marks)
(b) Fill in your codes in blank <2> to generate random numbers that are odd numbers between
the range 1 to 10 inclusively. (5 marks)
(c) Instead of a fixed seed value, the “current time” is used as the seed value. Suggest what
header file should be added to the program. (1 mark)
(d) Write down the function call statement for getting the current time value. (2 marks)
Fill in your codes for blank <1> in the partial-completed program below so that it prints the pattern
according to the sample output when it is successfully executed. Use the given variables in your
answer. (10 marks)
#include <iostream>
using namespace std;
int main(){
int n;
char star = '*';
char equal = '=';
char space = ' ';
}
cout << endl;
}
return 0;
}
Sample output 1:
Size: 5
====
=== *
== **
= ***
****
Sample output 2:
Size: 3
==
=A*
A**
- End of Section A -
Answer ALL questions in this section in the space provided in the answer sheet. Each
question carries 20 marks.
Question B1
Fill in your codes in the main function below so that it gives the sample output when successfully
executed.
The program accepts the following inputs: two int-typed values x and y, and two int-typed degree
values A and B. It calculates and prints the ans value according to the formula given below:
𝟐
𝒚 𝒔𝒊𝒏 𝑨 𝒄𝒐𝒔 𝑩
𝒂𝒏𝒔 𝒙
𝟑
Note: In the <cmath> header file, the sin and cos function takes in radian values as argument.
Degree-to-radian conversion formula:
𝝅
𝒓𝒂𝒅𝒊𝒂𝒏 𝒅𝒆𝒈𝒓𝒆𝒆
𝟏𝟖𝟎
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = 3.14159;
return 0;
}
Sample output
Enter x and y: 5 9
Enter A and B: 20 80
Answer is 26.8135
The partial-completed program below accepts 3 single-digit, non-zero integers inputs from the
user. It then (1) displays the digits in ascending order, and (2) finds and displays the smallest
even value (if such number exists) that is formed by the three input digits.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int ans;
ascending(a, b, c);
return 0;
}
Sample output 1
Enter three digits: 8 4 3
Digits in ascending order: 348
The smallest even value is 348
Sample output 2
Enter three digits: 2 5 1
Digits in ascending order: 125
The smallest even value is 152
Sample output 3
Enter three digits: 3 9 5
Digits in ascending order: 359
Cannot find smallest even value!
(a) Fill in your codes in blank <1> to complete the function swapNum( ). It swaps (i.e.,
interchanges) the values of the two parameters a and b. Pass-by-reference is used in the
parameters.
Example:
Assume a is 5 and b is 2. After calling swapNum(a, b), a becomes 2 and b becomes 5.
(4 marks)
(b) Fill in your codes in blank <2> to implement the function ascending( ). The function takes 3
reference to integer arguments a, b and c. After the function finishes, values stored in a, b and
c are arranged in ascending order.
Use suitable if-else statements and call the function swapNum appropriately in your answer.
(8 marks)
(c) Fill in your codes in blank <3> to implement the function formEven( ). The function takes 3
integer arguments a, b and c, which are already sorted (a ≤ b ≤ c). The function finds and
returns the smallest even value. If such number does not exist, it returns -1. (8 marks)
You are asked to write a program to calculate the result of the formula below.
𝒔𝒖𝒎 𝟏𝟐 𝟐𝟐 𝟑𝟐 𝟒𝟐 𝟓𝟐 𝟔𝟐 𝟕𝟐 ⋯
Complete parts (a) to (c) below so that the program can print the results according to the given
instructions and sample outputs.
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "n: ";
cin >> n;
return 0;
}
(a) Fill in your codes in blank <1> that uses sum to store the result of the formula, which is
calculated using a for-loop. Your loop design should consider the terms in the formula
one-by-one. Input n specifies the number of terms used in the calculation. (7 marks)
(b) Fill in your codes in blank <2> to insert the function prototype of the function addSum.
(3 marks)
(c) Fill in your codes in blank <3> that calculates and returns the result of the fomula using
recusion. No marks will be given for answers with a non-recursive approach. . (10 marks)
- End of Section B -
- End of Paper -
SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 9 of 9