SEFAKO MAKGATHO HEALTH SCIENCES UNIVERSITY
School of Science and Technology
Computer Science and Information Technology
TEST 2
Module: MCOA012
Part I: Structured Programming
Date: 12 September 2024
Total Marks: 75
Duration: 90 Minutes
Instructions:
1. Answer all questions.
2. Start each question on a new page.
3. Multiple Choice Questions (MCQ): Write all answers in capital letters.
4. You may answer the questions in any preferred order. However, DO NOT MIX the
numbering order of a question.
5. All rough work should be done in the back of the answer book and indicated as such.
Page 1 of 9
QUESTION 1 MODIFIED TRUE/FALSE [13]
State if the statement is true or false, if false provide the correct answer or justification.
1. In a do-while loop, when a Boolean expression evaluates to false, the loop body executes
one last time.
ANS: False PTS: 2 REF: 124, 137
In a do-while loop (or any other loop), when a Boolean expression evaluates to false, the
loop body is not entered.
2. In a program that accumulates values and displays a total, the total variable must typically
be initialized to 0.
ANS: True PTS: 1 REF: 136
3. The value 202412345 would be an appropriate sentinel value when entering SMU student
numbers into an array.
ANS: False PTS: 2 REF: 126, 179
The value (any value that is not a valid SMU student number) would be an appropriate
sentinel value when entering SMU student numbers into an array. OR The value
202412345 is a possible SMU student number. Therefore, it is not appropriate to use it as
a sentinel value when entering SMU student numbers into an array.
4. C++ programmers refer to any array of characters as a string.
ANS: False PTS: 2 REF: 189
C++ programmers refer to an array of characters as a string only if the last usable character
in the array is the null character (‘\0’).
5. A variable’s address is a constant, but a pointer is a variable.
ANS: True PTS: 1 REF: 200 – 201
6. If integers take four bytes of storage, then int days[31]; reserves 120 bytes of memory.
ANS: False PTS: 2 REF: 168 – 169
If integers take four bytes of storage, then int days[31]; reserves 124 bytes of memory.
7. When a program has two arrays with the same number of elements, they are considered
parallel arrays.
ANS: False PTS: 2 REF: 180 - 182
When a program has two arrays with the same number of elements, they are considered
parallel arrays only if the elements in one array corresponds to elements in the same
relative positions in the other array.
8. Any C++ for loop can be rewritten as a while loop, and vice versa.
Page 2 of 9
ANS: True PTS: 1 REF: 137
QUESTION 2 MULTIPLE CHOICE [10]
Select the best choice that completes the statement or answers the question.
1. If you know you need to execute a loop exactly 100 times, then it is a(n) ___________
loop.
a) definite c) indefinite
b) specific d) infinite
2. If you declare an integer pointer as int* pt; and you declare an integer variable as int num;,
then which of the following is legal?
a) num = &pt; c) pt = #
b) *num = *pt; d) &num = pt;
3. What is the output produced by the following program segment?
int x = 0;
while(x < 7)
cout << x << " ";
++x;
cout << endl;
a) 0 1 2 3 4 5 6 c) 0 1 2 3 4 5 6 7
b) 0 0 0 0 0 . . . d) none of the above
4. _________ is a single item in an array.
a) position c) address
b) size d) element
5. In a ________ loop, the Boolean expression is always evaluated at-least once.
a) for c) do-while
b) while d) all of the above
6. If you declare an array as int numbers[10];, then numbers[1] represents ___________ .
a) the first element in the array c) the address of the array
b) the second element in the array d) a new array with one element
Page 3 of 9
7. A(n) ________ variable is one that you use to keep track of whether an event has occurred.
a) flag c) Boolean
b) control d) iteration
8. When two character arrays are equal, then the method strcmp() returns.
a) 0 c) a negative value
b) a positive value d) true
9. When a loop is contained within another, the loops are ___________ .
a) shelled c) cased
b) switched d) nested
10. _________ are arrays in which the values are arranged in a table form.
a) parallel arrays c) two-dimensional arrays
b) one-dimensional arrays d) nested arrays
QUESTION 3 SHORT ANSWERS [30]
1. Name any three string-handling problems faced when using character array strings,
and how these problems can be eliminated by using the string class. (6)
ANS: PTS: 6 REF: 190 – 199
- Trying to assign one character string to another using the assignment operator (=);
solution: strcpy() method.
- Trying to compare character strings using the equal-to comparison operator (==);
solution: strcmp() method.
- Exceeding the bounds of an array;
solution: string class automatically increases size as needed.
- Null character requirement: Character array strings require that arrays end with a
null character ('\0'), which can be problematic to manage manually.
Solution: The null character is managed automatically when using the string class.
- Trying to input a string value that includes whitespace;
solution: getline() method
- string class automatically solves copy and compare problems; you can use
= and == with the string class.
Page 4 of 9
2. Consider the following program segment:
for(int i = 5; i >= 1; i--){
for(int j = 1; j <= i; j++){
cout << "*";
}
cout << endl;
}
a) Draw a flowchart diagram. (8)
b) What is the output produced by the program segment? (6)
***** ✔✔
**** ✔
*** ✔
** ✔
*✔
Page 5 of 9
3. Consider the following statements (top to bottom):
const int NUM = 4;
int testScores[NUM] = {67, 83, 96};
char school[] = "Sefako";
double rates[NUM];
rates[1] = 2.34;
char alpha[3][3] = {{'A', 'C', 'D'},{'E', 'F', 'G'}, {'H', 'I', 'J'}};
What are the values in the following positions? (6)
a) rates[1]; d) alpha[2][3];
b) rates[2]; e) school[6];
c) alpha[1][1]; f) testScores[3];
ANS: PTS: 6
a) 2.34 REF: 170
b) Garbage value REF: 170
c) F or ‘F’ REF: 186
d) Garbage value – out of bounds REF: 176 - 177
e) \0 or null character REF: 189
f) 0 REF: 171
Page 6 of 9
4. Write a C++ program segment from the flowchart diagram below. (4)
ANS: PTS: 4 REF: 137, 141
SOLUTION 1:
number = 1; ✔
while(number <= 10); ✔
{
cout << number << endl; ✔
++number; ✔
}
SOUTION 2:
number = 1;
while(number <= 10){
}
cout << number << endl;
++number;
SOUTION 3:
for(int number =1; number <= 10; );
{
cout << number << endl;
++number;
}
Page 7 of 9
Notes: Any while loop can be rewritten into a for loop (vice versa). The solution to the
question can be a for loop or a while loop but not a do-while loop. When using a for loop;
any part, more or all (initialize, evaluate and alter) can be left out, but the for loop must
always have the two semicolons. Meaning in C++, for(;;) is a valid for loop.
QUESTION 4 PROGRAMMING [22]
Follow C++ recommended practice when designing and writing solutions.
1. Write a program segment that allows the user to enter an integer that represents the
number of times a message will display, then displays a greeting message of your choice
as many times as requested. [Code only: 07]
ANS: PTS: 7 REF: CH4: 124 – 126, 137
SOLUTION 1:
int number; ✔
int count = 0; ✔
cout<<"Enter the number: "; ✔
cin>>number; ✔
while(count < number){ ✔
cout<<"Greeting message of your choice."<<endl; ✔
count++; ✔
}
SOLUTION 2:
const int STOP = 1; ✔
int number; ✔
cout<<"Enter the number: "; ✔
cin>>number; ✔
while(number >= STOP){ ✔
cout<<"Greeting message of you choice."<<endl; ✔
number--; ✔
}
Page 8 of 9
Notes: Any while loop can be rewritten into a for loop (vice versa). The solution to the
question can be a for loop or a while loop but not a do-while loop. However, because
we do not know how many times the loop will be repeated it can be 10x, 20x, 100x, it is
best to use a while loop (i.e. the loop is indefinite).
2. Assume an array named scores has been declared and user-populated with 15 integer
values. Design and write a program segment to find and display the highest value stored
on scores. [Design: 07] [Code: 08]
ANS: PTS: 15 REF: CH5: 167 – 177
DESIGN: Algorithm, flowchart or a combination.
1) Assume the first value on the array is the highest value. ✔✔
2) Traverse the array / iterate the array / go through the array starting at the second
element ✔ checking if that element is greater than the current highest value ✔,
if it is, update the highest value to the value of that element. ✔✔
3) Display the highest value. ✔
CODE:
int max = scores[0]; ✔✔
for(int pos = 1; pos < 15; pos++){ ✔✔
if(scores[pos] > max){ ✔✔
max = scores[pos]; ✔
cout<<"Highest value: "<<max<<endl; ✔
Notes: Any while loop can be rewritten into a for loop (vice versa). However, for this
solution it is best to use a for loop, because we already know how many times we
want to repeat the loop (i.e. the loop is definite).
*** END OF TEST MEMO ***
Page 9 of 9