0% found this document useful (0 votes)
171 views18 pages

CS111 Exam

This document outlines instructions for a final examination for an introductory computer science course. It provides information on the exam format, including three sections worth a total of 60 marks. Instructions specify that students must answer all questions in the provided booklet and materials permitted are unprogrammable calculators. The document also includes a sample answer sheet for section A, which contains 10 multiple choice questions.

Uploaded by

Tetz
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)
171 views18 pages

CS111 Exam

This document outlines instructions for a final examination for an introductory computer science course. It provides information on the exam format, including three sections worth a total of 60 marks. Instructions specify that students must answer all questions in the provided booklet and materials permitted are unprogrammable calculators. The document also includes a sample answer sheet for section A, which contains 10 multiple choice questions.

Uploaded by

Tetz
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/ 18

Student Name: ____________________ Student Number:___________________ Seat: ______

CS111: Introduction to Computer Science

Faculty of Science, Technology and Environment


School of Computing, Information and Mathematical Sciences

Final Examination
Semester 1 2017
F2F and Online Mode
Duration of Exam: 3 hours + 10 minutes

Reading Time: 10 minutes

Writing Time: 3 hours

Total Marks: 60 (50% of final grade)

Instructions:

1. This is a closed book exam


2. This exam has three sections:
 Section A : 20 marks
 Section B : 20 marks
 Section C : 20 marks
3. Answer all the questions in sections A, B and C. There are no choices.
4. Answer all questions in the space provided in this booklet.
5. For Section A – use page 7 of this booklet:
 Select a multiple-choice answer by putting a circle around that letter.
 Correct mistakes by putting an “X” through the circle or erasing it
completely.
 If you put an “X” through a circle, it will be considered unmarked.
 Ambiguous selection will be counted as incorrect.
6. Please hand in this booklet to the examination supervisor before leaving the
examination room
7. The total number of pages including this cover sheet is 18

Materials permitted: Unprogrammable Calculators


Section A [20 Marks - 2 marks each]

Question 1)

CS111 Exam today!


Good Luck

Select the statement which will EXACTLY reproduce the line of text above.

(A) cout << "CS111 Exam today!\n";


cout << "Good Luck";
(B) cout << "CS111 Exam today! \nGood Luck";
(C) cout << "CS111 Exam today!” <<endl <<”Good Luck";
(D) All of the above

Question 2)

Which of the following is a correct “comment” statement in C++?


(A) */ Comments */
(B) ** Comment **
(C) /* Comment /*
(D) None of the above

Question 3)

Consider the following snippet

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


for (int j = 1; j <= 4; j++) {
cout << " * ";
}
cout << endl;
}

What is its output?

(A) (B)

** ****
** ****
**
**

(C) (D)

**** *
*** **
** ***
* ****

Page 2 of 18
Question 4)

The Pythagoras theorem states that the square of the hypotenuse (the side opposite the right angle) is
equal to the sum of the squares of the other two sides.

i.e. 𝑎2 + 𝑏 2 = 𝑐 2

Given that the values of 𝑎 = 3.1 and 𝑐 = 5.5,


which of the snippets below will correctly
compute the value of b?

(A) (B)

int a = 3.1; double a = 3.1;

int c = 5.5; double c = 5.5;

int b = sqrt(c*c –a*a); double b = sqrt(c*c )– sqrt (a*a);

cout << "b = " << b; cout << "b = " << b;

(C) (D)

double a = 3.1; double a = 3.1;

double c = 5.5; double c = 5.5;

double b = sqrt(pow(c,2)-pow(a,2)); double b = sqrt(pow(c,2))-sqrt(pow(a,2));

cout << "b = " << b; cout << "b = " << b;

Question 5)

Which of the header files are required for the snippets in Question 4) above.

(A) #include <iostream>


#include <math>

(B) #include <iostream>


#include <cmath>

(C) #include <iostream>


#include <cstdlib>

(D) #include <iostream>


#include <stdlib>

Page 3 of 18
Question 6)

Consider the following snippet:

int main (){

const int SIZE = 5;

double list[SIZE] = {10,20,6,25,31};

double result = list[0];

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

if (list[i] < result){

result = list[i];

cout << result << ", ";

return 0;

What will this program print?

(A) 10, 20, 6, 25, 31,


(B) 10, 20, 25, 31,
(C) 10, 20, 6, 25, 31,
(D) 6,

Page 4 of 18
[For question 7, consider the following code structure]

#include <iostream>
using namespace std;

int operate (int & a, int b)


{
a = a + 1;
return (a * b);
}

double operate (double a, double b)


{
a = a + 1;
return (a/b);
}

int main()
{
int x=5, y=2;
double n=5.0, m=2.0;
cout << operate(x,y) <<"\t";
cout << operate (n,m) <<"\t" ;
cout << x <<"\t";
cout << n;

return 0;
}

Question 7)
What is the output of the code above?

(A) 10 5 5 5
(B) 12 3 5 6
(C) 12 3 6 5
(D) 12 3 6 6

Question 8)
Given that the first line of input data in a file called inputFile.txt is as below:

What value is read into ch2 by the following code?

inFile >> ch1 >> ch2; [(Note: All variables are of type char and inFile is of type ifstream)]

(A) ‘S’
(B) ‘’
(C) ‘C’
(D) ‘n’

Page 5 of 18
Question 9)

What's wrong with the statement below?

while((j < 10) && (j >12))

(A) the logical operator && cannot be used in a test condition


(B) the test condition is always false
(C) the test condition is always true
(D) nothing is wrong.

Question 10)

What is the output for the code snippet below:

int x = 0;
for (x = 3; x < 4; x++)
cout << "x = " << x <<endl;

(A) x = 0

(B) x = 1
x=2
x=3

(C) x = 3
x=4

(D) x = 3

Page 6 of 18
Name: _______________ ID Number: ____________

CS 111 Final Examination, Semester 1, 2017 - Answer Booklet

1) (A) (B) (C) (D)

2) (A) (B) (C) (D)

3) (A) (B) (C) (D)

4) (A) (B) (C) (D)

5) (A) (B) (C) (D)

6) (A) (B) (C) (D)

7) (A) (B) (C) (D)

8) (A) (B) (C) (D)

9) (A) (B) (C) (D)

10) (A) (B) (C) (D)

Page 7 of 18
Section B [Short Answers] [20 Marks]

Question 11 (5 Marks)

1 int main()
2
3 {
4 char choice;
5 double amount;
6 cout << "Please enter choice A, B, C or D";
7 cin >> choice;
8 cout << "Please enter amount: ";
9 cin >> amount;
10 while(!cin.fail() && choice>='A' && choice <='D'){
11 if( /* CONDITION */){
12 cout << "\nInput passes the requirement.\n";
13 }
14 cout << "Please enter choice A, B, C or D";
15 cin >> choice;
16 cout << "Please enter amount: ";
17 cin >> amount;
18 }
19 return 0;
}

The program should print “Input passes the requirement” if the choice is A or B and the amount is less
than 1000. Give a Boolean expression to replace /*CONDITION*/ in line 11, that achieves this.
Please provide the expression in the space below:

Page 8 of 18
Question 12 (10 Marks)

Consider the code below where the main function uses a function max, to compute the maximum value of
all elements in an array and uses a function ave, to compute the average value of all elements in an array:

double max(double array[], int length){


/*
MISSING STATEMENTS
*/
}

double ave(double array[], int length){


/*
MISSING STATEMENTS
*/
}

int main(){
const int SIZE = 8;
double list[SIZE] = {12, 4, 16, 8, 10, 2, 14, 15};
double maximum;

maximum = max(list, SIZE);


average = ave(list, SIZE);

cout << "The Maximum value is " << maximum << endl;
cout << "The Average value is " << average << endl;

return 0;
}

In the space provided below, write down the function statements for the function max. It takes the array
and the size as input parameters. (5 marks)

double max(double array[], int length){

Page 9 of 18
In the space provided below, write down the function statements for the function ave. It takes the array
and the size as input parameters (5 marks).

double ave(double array[], int length){

Page 10 of 18
Question 13 (5 Marks)

Consider the following code snippet.

0 int main() {
1 int a = 2;
2 int b = 1;
3 int c = 0;
4 while((a + 2 )>= b )
5 {c = a + b;
6 cout << c << ",";
7 a = b;
8 b = c; }
9 cout << a + b;
10 return 0;
11 }

The numbers on the left are line numbers.


Please provide the next 6 lines of the hand trace in the space below:

line a b c Comments
1 2 Declare a and initialize to 2
2 “ 1 Declare b and initialize to 1
3 “ “ 0 Declare c and initialize to 0
4 “ “ “ Condition is true
5 “ “ 3 Assign 3 to c

Page 11 of 18
Section C [Writing codes] [20 Marks]

Question 14 (6 Marks)

Consider the code below. It provides the user a linear equation and asks the user to input an answer.
Then it checks the provided answer against the correct answer and provides feedback. Please read the
code carefully.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

const int MAXINT = 10;


const int MININT = -10;

char signchar(int x)
{
if(x >= 0)
return '+';
else
return '-';
}

void random_pair(int first, int second)


{
first = rand() % (MAXINT - MININT + 1) + MININT;
second = rand() % (MAXINT - MININT + 1) + MININT;
}

bool check_linear(int guess, int answer)


{
/*[MISSING STATEMENTS]*/
}

Page 12 of 18
int main()
{
int a = 0;
int b = 0;
int x;
int score = 0;
int tries = 0;
char reply;
srand(time(0));
cout << "***********************************\n";
cout << "* CS111 math tutor 2 *\n";
cout << "***********************************\n\n";
cout << "Enter 'L' to try a linear equation.\nAny other key to quit.";
cin >> reply;

while(reply == 'L')
{
tries++;// Update the number of tries
random_pair(a, b); //Get random number here.

int solution = -b; //Compute the solution


int coeff_1 = a; //Compute the coefficients in the equation
int coeff_2 = a * b;
cout << "\nWhat is the solution to\n";
cout << coeff_1 << " x " << signchar(coeff_2) << " " << fabs(coeff_2) << " = 0\n";
cin >> x;

if(check_linear(x, solution))
{
score++;
}

cout << "Enter 'L' to try a linear equation.\nAny other key to quit.";
cin >> reply;
}

cout << "\nYour score is " << score << " correct out of " << tries << ".\n";

return 0;
}

Page 13 of 18
The function random_pair should assign to two reference parameters first and second, a random
number between MININT and MAXINT. MININT and MAXINT are global constants. This function has one
mistake. This means it currently does not work correctly. Rewrite the function correctly in the space
provided

The second function check_linear should return true if the guess is equal to the answer, and it should
return false otherwise. Write the missing function statements in the space provided below:

Corrected random_pair function:

(2 marks)

check_linear function:

(4 marks)

Page 14 of 18
Question 15 (7 Marks)

Write a complete C++ program to write numbers 1 to 100 (vertically) in a data file NOTES.TXT

Page 15 of 18
Question 16 (7 Marks)

A program is needed to enter five (5) sets of personal data: last name, first name and age.

In the space provided, write a complete C++ program that:

a) initially asks the user to enter five sets of first name, last name and age;

b) extracts the first name initial from the entered first name;

c) uses arrays to store the entered information; and

d) displays on the output monitor all the five sets of entered information in a table form as
shown in sample output below.

[Note: You can assume when prompted, the user enters correct input, hence there’s no need to validate
input.]

Sample output:

Page 16 of 18
Page 17 of 18
The End

Page 18 of 18

You might also like