Exam 2014 S 1 Guide
Exam 2014 S 1 Guide
2. (A)
cout << "The summer has passed.\nThis exam has not.\nBut it will.\n\tSoon.\n";
(B) (C)
cout << "The summer has passed.\t"; cout << "The summer has passed.";
cout << "This exam has not.\t"; cout << "\nThis exam has not.";
cout << "But it will.\nSoon.\t"; cout << "\nBut it will."
cout << endl; cout << "\tSoon." << endl;
(A) (B)
(C) (D)
int total=0;
for(int i=0; i<3; i++) {
int rowtotal = 0;
for(int j=1; j<3; j++) {
rowtotal = rowtotal + j;
}
total = total + rowtotal;
}
cout << "Total: " << total << endl;
6. (A) (B)
(C) (D)
Which of these functions is syntactically correct, and returns true if the parameter is a capital letter,
and false otherwise?
(A) Function (A)
(B) Function (B)
(C) Function (C)
(D) Function (D)
CS111 Introduction to Computing Science Page 4
7. The cost of a car rental is based on the size of the car, and the number of days it is rented:
(A) Result 3
(B) Result 5
(C) Result 7
(D) The address of array element a[2]
9. Consider the following program:
int main() {
const int SIZE = 6;
double list[SIZE] = {4,7,9,10,4};
return 0;
}
10. data.txt
1.2 2.4 1.6 4.0 3.6 0.1 5.3 4.7 1.5 2.2
process data.cpp
0 # include <iostream>
1 # include <fstream>
2 using namespace std;
3
4 int main(){
5
6 Enter necessary declarations
7 and file handling here.
8
9 double dataitem;
10 double maxitem = 0;
11
12 datafile >> dataitem;
13 if(!datafile.fail()) {
14 maxitem = dataitem;
15
16 while(datafile >> dataitem) {
17 if(dataitem>maxitem) {
18 maxitem=dataitem;
19 }
20 }
21
22 cout << "The maximum of all items is " << maxitem << ".\n";
23 }
24 datafile.close();
25 system("pause");
26 return 0;
27 }
The program process data.cpp reads the file data.txt and computes the maximum value of all data
items in data.txt. Which two lines are missing in lines 6 and 7.
(A) ofstream datafile;
datafile.open("data.txt");
(B) ifstream datafile;
datafile.open("data.txt");
(C) fstream datafile;
datafile.open("data.txt", ios::out | ios::binary);
(D) fstream datafile;
getline(datafile, line);
CS111 Introduction to Computing Science Page 7
11. A colleague has written a program to compute the cost and rewards for small energy producers.
The rates depend on whether it is weekday or weekend, and are different for energy produced or
consumed. The balance is the difference between the money earned by production and the money
owed for consumption.
A user is complaining that the program doesn’t work correctly, and sends the following screen shot:
Answer the following two questions in the space provided below: (1) What is the problem with this pro-
gram; what are mistakes the program makes? (2) Why did the compiler not find the mistake? Explain.
(1) The program mixes up the rates for production and consumption. It should have computed
$1.00 consumed and $1.80 produced. The total should have been $0.80. Or $0.20, given the
wrong numbers given here. (2) This problem is a semantic error, which means that the program
compiles fine, but when you run the program it, it does not compute what it is supposed to
compute. Marking: 2 points if one problem was correctly identified. 3 points if more than one
was correctly identified. 2 mark if it correctly explains the semantic error.
CS111 Introduction to Computing Science Page 8
while(!cin.fail()) {
if( /*CONDITION*/) {
entries[i] = entry;
i++;
}
else {
cout << "Entry ignored. ";
}
cout << "Next number (Q to quit): ";
cin >> entry;
}
Give a boolean expression to replace /*CONDITION*/ , such the program assigns the value of entry to
entries[i] only if i is smaller than LENGTH and entry is positive.
Please provide the expression in the space below:
Either i <LENGTH && entry>=0 or !(i >=LENGTH || entry < 0)
Marking: 4 marks if correct, or if correct except for the use of entry > 0 (or entry <= 0) to
check is entry is positive. 3 marks if correct except for the negation. 2 marks if i<LENGTH is
incorrect (Serious! Array bounds!). 2 marks if correct except confusing && with ||. 1 marks if it
is syntactically correct and uses the right variables, constants .
CS111 Introduction to Computing Science Page 9
1 int x = 13;
2 int y;
do {
3 y = x % 2;
4 x = (x-y)/2;
5 } while(x!=y);
6 cout << "x: " << x <<endl;
7 cout << "y: " << y <<endl;
8 system("pause");
9 return 0;
line x y comment
1 13 - initialising x to 13
2 ” - declaring y
3 ” 1 13 % 2 is 1
4 6 ” (13-1)/ 2 is 6
5 ” ” condition is true
3 ” 0 6 % 2 is 0
4 3 ” (6-0)/ 2 is 3
.. .. .. ..
. . . .
5 ” ” condition is true
3 ” 1 3 % 2 is 1
4 1 ” (3-1)/ 2 is 1
5 ” ” condition is false
6 ” ” print x: 1
CS111 Introduction to Computing Science Page 10
1 # include <iostream>
2 using namespace std;
3
4 double compute_min(double* array, int size) {
5
6 double current_min;
7
8 for(int i = 0; i<size; i++) {
9 if( current_min > *(array+i)) {
10 current_min = *(array+i);
11 cout << "The value of the current minimum is " << array+i << endl;
12 }
13 }
14
15 return current_min;
16 }
17
18 int main() {
19 const int SIZE = 5;
20 double list[SIZE]= {3.4,2.3,5.6,1.8,2.7};
21 double min_list = compute_min(list,SIZE);
22
23 cout << "The first element of the list is " << list[1] << endl;
24 cout << "The last element of the list is " << list[5] << endl;
25 cout << "The minimum of the list is " << min_list << endl;
26
27 system("pause");
28 return 0;
29 }
(a) The first error occurs in line 9. Variable current min has no value in the first iteration. [1
point] This means sometimes the result for the minimum can be a nonsense result.[1 point] (b)
An array bounds violation occurs in line 24. Array list has only 5 elements, but list[5] is
the sixth element. [1 point] This means sometimes the result printed for the last element will be
a nonsense result.[1 point] (c) This error occurs in line 11. This statement does use the value
of array[i] but its address. [1 point] It will not print any useful information, certainly not the
value. [1 point] (Maximal 5 points).
CS111 Introduction to Computing Science Page 12
1 int main() {
2 double balance,input;
3 int tariff;
4 double total = 0;
5
6 cout << "\nWhich tariff? 1: Weekday, 2: weekend, Q: Quit: ";
7 cin >> tariff;
8 //Check if something other than a number was entered. Quit if this is true.
9 while(!cin.fail()) {
10 //Declare two variables
11 double consumed, produced;
12 cout << "How much did you consume: ";
13 cin >> consumed;
14 cout << "How much did you produce: ";
15 cin >> produced;
16 double balance = 0;
17 //Check which tariff was selected by the user
18 if(tariff==1) {
19 balance = 0.24 * produced - 0.20 * consumed;
20 }
21 else {
22 balance = 0.18 * produced - 0.15 * consumed;
23 }
24 if(balance>=0) {
25 cout << "You earned $" << balance << endl;
26 }
27 else
28 cout << "You owe $" << -balance << endl;
29 total = total + balance;
30 cout << "\nWhich tariff? 1: Weekday, 2: weekend, Q: Quit: ";
31 cin >> tariff;
33 }
34 cout << "you totl balance iz $" << total << ".\n";
35 return 0;
36 }
(a)The poor comment is in 10. It is obvious that this is what the next line does. (b) Line
34. Spelling mistakes are here. (c) Variable input in line 2 is never used. (Variables balance
declared in line 2 is also never used.) (d) The else branch in line 28 has no brackets. Only line
28 is part of the else branch. (e) The variable balance declared in line 11 shadows the variable
balance declared in line 2.
CS111 Introduction to Computing Science Page 14
We are here to celebrate Mrs. Amy Adams, a professional who always put the
interest of the public first. We are here to celebrate also her
contributions to enhance the quality of life through information
technology. The world is different now than it was 30 years ago, and Amy has
a big share in it.
We know Amy best for her frank opinions and honesty . She did
and does not hesitate to admit mistakes, even mistakes made by herself, and
even if is not always appreciated by managers who want any project com-
pleted yesterday. Not that she made many mistakes in her long career. We
know her for her competence and expertise in every area of IT. She
started when phones were still wired to the wall and programs were written
on punch cards. Now she is leading development in mobile apps in program-
ming languages that are younger any of us. She was always committed to
professional development , and is, after 30 years in IT, still 5
years ahead of the rest of us.
Alan Turing, array bounds violation, assembler code, Bjarne Stroustrup, car rental, code of
conduct, coding guideline, competence, compiler, drank coffee, hobbies, honesty, intentionally
difficult, laziness, machine code, mining companies, movie theatre, music preference, object
oriented, pollution, professional development, public, quality of life, shareholder, software
bugs, software license, syntax error, tardiness, throughput, trade union.
CS111 Introduction to Computing Science Page 15
1 int main() {
2 const int DAYS = 7;
3 const double GOV_FACTOR = 0.1; // Produce at least 10% of total on all days
4 const double SUBSIDY = 0.2; // 20% Subsidy
5 const string weekday[DAYS]= {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
6
7 double total = 0;
8 double earnings = 0;
9 double produced[DAYS];
10
11 cout << "****** Renewable energy calculator ******\n\n";
12
13 //Read the produced energy for all 7 days
14 for(int i=0; i<DAYS; i++) {
15 double daily_production;
16 cout << "How much did you produce on "<< weekday[i] << "? ";
17 daily_production = get_energy();
18 produced[i] = daily_production;
19 total = total + daily_production;
20 earnings = earnings + get_earning(daily_production);
21 }
22
23 //Add the subsidy if the production qualifies
24 double threshold = total * GOV_FACTOR;
25 if(get_subsidy(threshold,produced,DAYS)) {
26 earnings = earnings + earnings * SUBSIDY;
27 }
28
29 cout << "You earned $" << earnings << " for the energy you produced.\n";
30 return 0;
31 }
CS111 Introduction to Computing Science Page 16
17. (1 mark) Given the rates that are paid per kWh of energy (see the previous page), and the screen shot
above, what were the earnings for Wednesday’s production?
$1.60
(1 mark) Given the screen shot above, what was the total production for the entire week?
50kW h
(1 mark) The government pays the subsidy, if the producer produces on every day of the week at
least 10% of the weekly total. Given the productions in the screen shot above, do they qualify for the
government subsidy?
Yes.
CS111 Introduction to Computing Science Page 17
18. The function get energy is used on line 17 in the main function. The function get energy should do
the following:
Read a number. Repeat the following if the number is negative. Ask the to enter a new value,
and read that value. Once the number is positive, the function should return that value.
(1 mark) What is the return type of function get energy?
double
(1 mark) What are the parameters of function get energy, and if they exist, what is their type?
there are none
(1 mark) What type of loop is the best suited to ask the user for new numbers as long as they are
negative?
While or do
(2 marks) What is the pseudo code for function get energy?
Read a number.
While number is negative.
Ask for a positive number
Read the number.
Return the number.
Marking: 1 point if it is proper pseudo-code. 1 points if it is correct.
double get_energy() {
double energy;
return energy;
}
Marking: 1 point if the parameter definition and use of return is correct. 3 points if it correctly
validates the input. 2 point if there are no other serious syntax errors.
CS111 Introduction to Computing Science Page 18
19. The function get earnings is used on line 20 in the main function. The function get earnings should
do the following:
Return the production multiplied by $0.25 if the production is more than 100 kWh, and
return production multiplied by $0.20 otherwise.
(1 mark) What is the return type of function get earnings?
double
(1 mark) What are the parameters of function get earnings, and if they exist, what is their type?
double production
Marking: 1 point if the parameter definition and use of return is correct. 3 points if it correctly
computes the earnings. 2 point if there are no other serious syntax errors.
CS111 Introduction to Computing Science Page 19
20. The function get subsidy is used on line 25 in the main function. The function get subsidy should
do the following:
Given an array with productions for every day of the week and a threshold, it should count
how many times the production is below the threshold. If this is more than 0 the function
should return false. Otherwise it should return true.
(1 mark) What is the return type of function get subsidy?
bool
(1 mark) What are the parameters of function get subsidy, and if they exist, what is their type?
double threshold, double array[], int size
(1 mark) What type of loop is the best suited to check if the production of all seven days of the week
is above the threshold?
for-loop
(2 marks) What is the pseudo code for function get subsidy?
if(counter > 0) {
return false;
}
return true;
}
Marking: 1 point if the parameter definition and use of return is correct. 3 points if it correctly
gives the answer. 2 point if there are no other serious syntax errors.
CS111 Introduction to Computing Science Page 20
21. (3 marks) Present a test cases for the complete program, i.e. specify input, and describe the ex-
pected output.
The output given in the screen shot would make a good test case. Marking: 1 mark for correct
input, one marks for correct output.
Use this box if you need extra space. State clearly which exercise you answer on this box.
CS111 Introduction to Computing Science Page 21
Use this page if you need extra space. State clearly which exercise you answer on this page.
CS111 Introduction to Computing Science Page 22
Use this page if you need extra space. State clearly which exercise you answer on this page.