We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 19
@USP
‘THE UNIVERSITY OF THE
SOUTH PACIFIC
CS111: Introduction to Computing Science
Faculty of Science, Technology and Environment / School of Computing,
Information and Mathematical Sciences
Final Examination
Semester 2 2020
F2F and Online Mode
Duration of Exam: 3 hours + 10 minutes
Reading Time: 10 minutes
Writing Time: 3 hours
Instructions:
There are 3 sections in this test. All sections and questions are compulsory.
All answers are to be written directly on the Question paper in the spaces provided
The testis worth 80 marks and contributes 40% towards the final Grade
Number of pages -19
This is a closed book exam
You may use your calculator in this exam
Name: Campus:
Student ID: Mode:Section A: Use the grid below to answer Multiple Choice Questions
10
1
12
13
14
15
16
17
18
19
20Section A: Multiple Choice Questions (20 marks)
There are a total of 20 question in this section. Each question has exactly one
correct answer and is worth | mark. Circle the correct answer in the
answering grid provided
Part 1: Introduction
1. Who created the Turing Machine?
A. Blame Stroustrup
B. Bill Gates
C. Alan Turing
D. Steve Turing
2. Which library provides routines for input and output?
A. string
B. iostream
C. ctime
D. cmath
Part 2: Fundamental Data Types
3. What would be the output of the following code snippet:
int i = 9.2;
double j = 4/2;
cout << "j=" << j <¢ endl;
A. 4.60
B. 4.6
C. 4.666
D. 4
4. Which one of the following is the correct method of declaring and
initializing a double variable with name integer?
A. double integer = 16.8:
B. int number = 16.8;
C. intinteger = 16;
D. none of the abovePart 3: Decisions
5. IFA=5,B=9 and C=7, then which of the following Boolean expression is
true?
A. (A==5) || (C>=B8)
B. (A<5) && (C>=B)
C.(A>5) 1] (C>=B)
D. (A<5) &&!(C >=B)
6. What is the output of the following code snippet?
int i
if (i == “two”) cout << “2” << endl;
else cout << “two” << endl;
A. two
B. compiler error
c.2
D. None of the above
7. Which statement is incorrect in regards to the code snippet below?
int index = 1;
switch (index)
{
case @ : cout << “Zero ”; break;
case 1: cout << “One ”;
default : cout << “Default ”;
+
A. The code will produce compiler error.
B. The output will be “One Default”.
C. The output will be “One”.
D. There will be no output.Part 4: Loops
8. Which of the options shows the correct output of the following code
snippet?
int max = 10;
for(int i = 5; i < max; itt)
{
cout << ice", "5
i
9, 10,
9,
ra
, 6,
, 6,
7,
9OP>
@NN
OD @
. This results in an infinite loop.
9. How many times would the body of the following loop execute?
const int DAY = 11;
int hours = 0;
do
{
cout << "tik-tok" << endl;
hours++;
Jwhile(hours <= DAY);
A. | time
B. 12 times
C. 11 times
D. Infinite number of times
10. Select the correct output of the code snippet below.
for(int i=0; ic3; i++) {
for(int j=is j<23 j++) {
cout <«< " * "5
cout << endl;Part 5: Functions
11. What is the output of the following code?
int transform(int num, int num2)
{
return num + num2;
+
int main()
{
int y = 53
y = transformy, 6);
cout << y <« endl;
return 0;
}
AS
By
ci
D. None of the above
12, What is not a component of the function header?
A. Return value
B. Function name
C. Parameters list
D. Return type13. What is the return value of the function even_numbers below?
void even_numbers(int max)
{
for(int i=0; i < max; it)
{
if (G%2==0) cout cic "5
return;
+
int main()
{
int n = 13;
even_numbers(n) 3;
return 0;
+
A. There is an error in function even_numbers
B.7
C.13
D. No return value
14. What value of nis printed out at the end of the code snippet below:
int n= 15
int main()
{
{
int n= 55
ne.
+
nets
cout << n5
return 0;
+
A.2
B. 4
Cc.
D. None of the above15.Considering the code snippet below, which statement is correct?
int doublenum(int a)
{
a=2*a;
return a/33
+
int main()
{
int mark,num = 215
mark = doublenum(num) ;
cout << "Result: " << num;
return @;
+
A. The output will be Result: 21
B. The program will not compile
C. The parameter a should have been passed by value
D. The parameter
16. Which parameter list is correct for the transfer function in the code
snippet below?
‘ account1 = account1 - amount;
account2 = account2 + amount ;
}
int main()
{
double savings_acc = 1000;
double cheque_ace = 500;
‘transfer(savings_acc, cheque_acc, 100);
return 05
+
A. (double& account1, double& account2, double amount)
B. (&double account1, &double account2, double amount)
C. (double account1, double account2, &double amount)
D. None of the abovePart 6: Arrays
17. Which of the statements below about C++ arrays is incorrect?
A. Arrays are data structures.
B. All elements of an array must be of the same data type.
C. The elements of an array are stored sequentially in memory.
D. The size of an array need not be constant.
18. In the code below, the user will provide the elements of the array values
within the for loop. Which of the options below is the correct body of the
for loop?
const int NUMBER_OF_INPUTS = 3;
double values[NUMBER_OF_INPUTS];
for (int i = 0; i < NUMBER_OF_INPUTS; i++)
{
PPepPrr?
2? ?
+
cout << values[i];
cin >> values[i];
cout << values[i];
None of the above
coop
Part 7:1. File Input/Output
19. The steps for using a file for input or output in C++ are listed below. Which
of the sequences is the correct sequence?
|. Declare file stream variables.
Il. Include the header file fstream in the program.
Ill, Open the file.
IV. Associate the file stream variables with the input/output sources.
V. Use the file stream variables with >>, <<, or other input/output functions.
VI. Close the file.
Aw ALL AV, IIL V, VI
B. 1, 1V, Ill Il, V, VI
c. UL IV. AL Vv. VE
D. ILIV, II, VLV.20. Which statement about files is not correct?
A. In case of input files, the file need not exist before the open
statement executes.
B. If the input file does not exist, the open statement fails and the input
stream enters the fail state.
C. If the output file does not exist, the computer prepares an empty file
for output
D. All statements are correct.
Section B: Short Answers (20 marks)
1. Explain the difference between variable declaration and initialization.
(2 marks)
2. Explain the difference belween the for loop, while loop and do while loop.
(3 marks)3. In C++, the rand() function generates pseudorandom numbers. What
does this mean as opposed to true random numbers? (2 marks)
4, How can a program ensure to generate new sets of random numbers in
every run of the program? (2 mark)
5. Discuss what an array is in C++. (2 marks)
6. Whatis the difference between passing by value and passing by
reference in regards to function parameters. (2 marks)
107. Explain what a pointer is in C+. (2 marks}
8. Explain what this C++ statement means: string names[33]; (1 mark)
9. Acommon error that occurs when using arrays is the “bounds" error.
Explain what this error is with an example. (3 marks)
10. Why must you be careful when passing arrays to functions as parameters?
(1 mark)
1Section C: Programming (40 marks)
1
Use a looping structure to get 10 numbers from the user (you do not need
to do any input validation). Keep a count of even and odd numbers
entered by the user. At the end, display the number of even and odd
numbers entered by the user. (5 marks)
int main()
{
return 0;
22. You have to implement a while loop to search for a username from a list
of names. The string array containing the names is already declared. Write
C++ code to find the usemame in the array. When the username is found,
display “Search successful”. (5 marks)
int main()
{
const int SIZE = 75
string names[] = {"Tony", "Thor", "Steve", "Wanda", "Natasha" };
string username = "Thor";
int pos = 0;
bool found = false;
return 0;
33. You are required to design and implement a program that reads a log file
then performs some analysis on the data found in the file. The log file is a
plain text file and contains the data shown below:
Bicgnt- Notepad - o x
File Exit Format View Help
Tuesday 2200 2245
Tuesday 2330 2352
Wednesday @615 1600
Thursday @800 1020
a. In order to process this file, the contents need to be read into
appropriate arrays. The first column records the day, the second
column records the login time and the last column records the
logout time for a user. In the space below, write C++ code to
declare a maximum capacity of the arrays, and the three arrays of
this capacity. (3 marks)
b. Which header file is needed to facilitate the file reading? (1 mark)
>
winclude <
14cc. Within your main() function, open the log file and read all its
contents into the arrays created above in part a. You do not need
tore-declare those arrays, if you need any additional variables and
constants then you must declare them. (6 marks)
int main()
{
return 0;
+
15,4, Implement a function called maximum() which receives an integer atray
and its capacity as parameters and retums the largest number in the array
at the end of the program. (5 marks}
5. a. Write a function called swap!) which accepts two char parameters by
reference and swaps their char values. (5 marks)
b. Write the function callin your main() function. Pass the char variables
'X' and 'Y' as the two characters to be swapped by the swap() function.
(2 marks)
int main()
{
return 0;
166. As the software developer for your local town council, you are responsible
for creating an automated business registration system for the council. This
will be a console based system that allows the clerk to enter individual
business details and stored the data in a plain text file. Each new business
being registered will create a new file by the name of the business. The
details gathered include Business name, Owner's name and type of
business (example, restaurant, hardware, bank, etc).
a. Write down the pseudocode for this program. (3 marks)
b. Write the C++ code for this program. (5 marks)
itinclude
#include
using namespace std;
int main()
{
vreturn 0;
The End
18