CS111 Exam
CS111 Exam
Final Examination
Semester 1 2017
F2F and Online Mode
Duration of Exam: 3 hours + 10 minutes
Instructions:
Question 1)
Select the statement which will EXACTLY reproduce the line of text above.
Question 2)
Question 3)
(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
(A) (B)
cout << "b = " << b; cout << "b = " << b;
(C) (D)
cout << "b = " << b; cout << "b = " << b;
Question 5)
Which of the header files are required for the snippets in Question 4) above.
Page 3 of 18
Question 6)
result = list[i];
return 0;
Page 4 of 18
[For question 7, consider the following code structure]
#include <iostream>
using namespace std;
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:
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)
Question 10)
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: ____________
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:
int main(){
const int SIZE = 8;
double list[SIZE] = {12, 4, 16, 8, 10, 2, 14, 15};
double maximum;
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)
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).
Page 10 of 18
Question 13 (5 Marks)
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 }
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>
char signchar(int x)
{
if(x >= 0)
return '+';
else
return '-';
}
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.
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:
(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.
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;
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