[go: up one dir, main page]

0% found this document useful (0 votes)
157 views9 pages

SEHH2042 Test VersionB QP

The document is a mid-term test paper for a computer programming subject. It contains two sections - Section A with 4 short questions and Section B with 2 long questions. The questions test concepts in C++ programming such as loops, functions, conditional statements, random numbers etc. and require students to write code snippets to solve the problems.

Uploaded by

LL 3708
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)
157 views9 pages

SEHH2042 Test VersionB QP

The document is a mid-term test paper for a computer programming subject. It contains two sections - Section A with 4 short questions and Section B with 2 long questions. The questions test concepts in C++ programming such as loops, functions, conditional statements, random numbers etc. and require students to write code snippets to solve the problems.

Uploaded by

LL 3708
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/ 9

Name: ___________________________ Student ID: __________________

Lecture Class: 102 Tutorial Group: A / B / C / D

THE HONG KONG POLYTECHNIC UNIVERSITY


HONG KONG COMMUNITY COLLEGE

Subject Title : Computer Programming Subject Code : SEHH2042

Session : Semester 1, 2022/23 Time Allowed : 1 hour 15 mins

Date : 25 October 2022 (Tuesday) Time : 14:15 – 15:30

Subject : Dr Ken TSANG Mr Jimmy HUI Mr Michael YEUNG


Examiner(s)

Mid-term Test (Version B)

This question paper has a total of NINE pages (including this covering page).

Instructions to Candidates:

1. There are TWO sections in this paper.


Section A (40%) – Short Questions. Answer ALL questions in this section in the space
provided in the answer sheet. Each question carries 10 marks.
Section B (60%) – Long Questions. Answer ALL questions in this section in the space
provided in the answer sheet. Each question carries 20 marks.
2. The programming language used in this paper is C++.
3. This is an individual test. Any form of plagiarism is strictly prohibited.

Authorised Materials:
YES NO
CALCULATOR [] [ ]
SPECIFICALLY PERMITTED ITEMS [ ] []

DO NOT TURN OVER THE PAGE UNTIL YOU ARE TOLD TO DO SO

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 1 of 9


Section A (40%) – Short Questions

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)

int sum = 80;


for (int k = 3; k <= 10; k += 2) {
sum -= k;
}

(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;
}

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 2 of 9


Question A2

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:

 Floating point value(s) are displayed in 3 decimal places.


 Floating point value(s) are displayed using 7 character-width.
 Character(s) are displayed using 3 character-width. (10 marks)

#include <iostream>
#include <iomanip>
using namespace std;

int main(){

double x;
char y, z;

// <1>: Your codes for Question A2 should be inserted here

return 0;
}

Sample output
Your input: 2.66 * #
Display:
* 2.660 #

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 3 of 9


Question A3

Consider the partial-completed program below that concerns with random numbers in C++.

#include <iostream>
#include <cstdlib>
using namespace std;

int main(){

// <1>: Your codes for Question A3(a) should be inserted here

for (int i = 1; i <= 10; i++) {


int num;

// <2>: Your codes for Question A3(b) should be inserted here

cout << num << " ";


}
cout << endl;

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)

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 4 of 9


Question A4

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 << "Size: ";


cin >> n;

for (int i = 1; i <= n; i++) {


for (int j = n; j >= 1; j--) {

// <1>: Your codes for Question A4 should be inserted here

}
cout << endl;
}

return 0;
}

Sample output 1:
Size: 5
====
=== *
== **
= ***
****

Sample output 2:
Size: 3
==
=A*
A**

- End of Section A -

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 5 of 9


Section B (60%) – Long Questions

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:

𝝅
𝒓𝒂𝒅𝒊𝒂𝒏 𝒅𝒆𝒈𝒓𝒆𝒆
𝟏𝟖𝟎

Use the given PI variable in your answer. (20 marks)

#include <iostream>
#include <cmath>
using namespace std;

int main() {
const double PI = 3.14159;

// Your codes for Question B1 should be inserted here

return 0;
}

Sample output
Enter x and y: 5 9
Enter A and B: 20 80
Answer is 26.8135

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 6 of 9


Question B2

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;

void swapNum(int & a, int & b) {

// <1>: Your codes for Question B2(a) should be inserted here


// Complete the function body of swapNum

// <2>: Your codes for Question B2(b) should be inserted here


// Implement the function ascending

// <3>: Your codes for Question B2(c) should be inserted here


// Implement the function formEven

int main() {
int a, b, c;
int ans;

cout << "Enter three digits: ";


cin >> a >> b >> c;

ascending(a, b, c);

cout << "Digits in ascending order: ";


cout << a << b << c << endl;

ans = formEven(a, b, c);


if (ans == -1) {
cout << "Cannot find smallest even value!" << endl;
}
else {
cout << "The smallest even value is " << ans << endl;
}

return 0;
}
Sample output 1
Enter three digits: 8 4 3
Digits in ascending order: 348
The smallest even value is 348

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 7 of 9


Question B2 (continued)

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)

SEHH2042 22-23 Semester 1 Mid-term Test (Version B) Page 8 of 9


Question B3

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;

// <2>: Your codes for Question B3(b) should be inserted here


// Function prototype of function addSum

int main() {

int n, sum = 0;
cout << "n: ";
cin >> n;

// <1>: Your codes for Question B3(a) should be inserted here

cout << sum << endl;


cout << addSum(n) << endl;

return 0;
}

// <3>: Your codes for Question B3(c) should be inserted here


// Implement the function addSum by recursion

Sample output 1: Sample output 2:


n: 5 n: 11
37 254
37 254

(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

You might also like