Professional Documents
Culture Documents
Exam 2013 S 2 Guide
Exam 2013 S 2 Guide
Serving the Cook Islands, Fiji, Kiribati, Marshall Islands, Nauru, Niue, Samoa, Solomon Islands, Tokelau, Tonga, Tuvalu, and Vanuatu.
1
Name: Student Number: Page 1
2. (A)
cout << "It is almost summer\nToo nice a day for an exam\nJust three more hours\n";
(B) (C)
cout << "It is almost summer"; cout << "It is almost summer"
cout << "Too nice a day for an exam"; << "Too nice a day for an exam"
cout << "Just three more hours"; << "Just three more hours" << endl;
a2
A = b2 −
2
Suppose you want to compute the area for a =
1.0 m and b = 2.5 m. The result should be in m2 .
Which of these code snippets is syntactically cor-
rect and computes the area correctly: b
(A) (B)
(C) (D)
6. (A) (B)
void swap(int &num1, int &num2){ bool swap(int &num1, int &num2){
int tmp = num1; int *tmp = &num1;
num1 = num2; num1 = &num2;
num2 = tmp; return num2 = tmp;
} }
(C) (D)
void swap(int num1, int num2){ int swap(int num1, int num2){
tmp = num1; num1 = num2;
num1 = num2; num2 = num1;
num2 = tmp; return num1,num2;
} }
Which of these functions is syntactically correct and swaps the values of parameters num1 and
num2?
(A) Function (A)
(B) Function (B)
(C) Function (C)
(D) Function (D)
Name: Student Number: Page 4
7. Assume that traffic fines are computed according to the following table:
drunk sober
speed ≥ 50 $100 $50
speed < 50 $75 $0
Let drunk be a char and speed an int. Which of the following snippets computes the correct fine?
(A) (B)
(C) (D)
(A) Result 3
(B) Result 4
(C) Result 5
(D) The address of array a
9. Consider the following program:
int main(){
const int SIZE = 6;
double list[SIZE] = {3,5,2,1,4};
for(int i = 1; i<=SIZE;i++){
cout << list[SIZE-i] << ",";
}
return 0;
}
10. data.txt
12.3 11.4 23.6 24.2 9.6 10.0 8.4 14.4 21.5 18.9
process data.cpp
# include <iostream>
# include <fstream>
using namespace std;
int main(){
ofstream logfile;
logfile.open("data.log");
ifstream datafile;
datafile.open("data.txt");
double total = 0;
double dataitem;
int item = 0;
logfile << "Processed " << item << " data items.\n";
logfile << "The sum of all items is " << total << ".\n";
logfile.close();
datafile.close();
return 0;
}
The program process data.cpp reads the file data.txt and writes to file data.log. What will it
print to standard output?
(A) Processing item 12.3 11.4 23.6 24.2 9.6 10.0 8.4 14.4 21.5 18.9
(B) Processing item 1 2 3 4 5 6 7 8 9 10
(C) Processed 10 data items.
The sum of all items is 154.3.
(D) Processed 10 data items.
The sum of all items is 154.3.
Press any key to continue . . .
Name: Student Number: Page 7
11. Students for an introductory maths course are using a math tutor to test their math skills. Some are
complaining that the program doesn’t work correctly, and send the following screen shot:
**************************************************
* CS111 Math tutor *
**************************************************
FINAL TALLY
You answered 1 of 1 multiplication questions correctly.
You answered 2 of 3 linear equation questions correctly.
You answered 3 of 4 questions correctly.
Answer the following two questions in the space provided below: (1) What is the problem with this
program; what are mistakes the program makes? (2) Are these problems syntax errors (also called
compile-time errors) or are they logic errors (also called run-time errors or semantic errors)? Explain.
(1) The program sometimes marks a correct answer as wrong (3 is the solution to 2 x − 6 = 0),
and a wrong answer as correct (3 is not the correct solution to 8 x − 32 = 0). Also it counted
correct answers for multiplication questions, but there was no multiplication question. (2) This
problem is a semantic error. The program compiles fine, but when you run the program it turns
out that 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. 1 mark if it correctly
says that it is a logic error. 1 mark is the explanation is correct.
Name: Student Number: Page 8
Give a boolean expression to replace /*condition*/ , such the program prints the values in array temp
(temperature), but strikes out any value that is below 0 or above 100. If the latter is the case it will
just print - - - .
Please provide the expression in the space below:
temp[i]>=0 && temp[i]<=100 or !(temp[i]<0 || temp[i]>100)
Marking: 4 marks if correct except for the negation.
1 int result = 1;
2 int factor = 1;
do{
3 factor++;
4 result = result * factor;
5 }while(result<=10);
1 int score_mult = 0;
2 int score_div = 0;
3 double average, counter=0;
4 int* score = NULL;
5 int a=0, b=0, answer=0, expected=0;
6 char reply;
7
8 while(reply ==’*’ || reply == ’/’){
9 counter ++;
10 a = rand()%10 + 1;
11 b = rand()%10 +1;
12 if(reply == ’*’){
13 cout << "What is " << a << " multiplied by " << b << ": ";
14 expected = a * b;
15 //remember which of the scores to use
16 score = &score_mult;
17 }
18 else{
19 cout << "What is " << a << " divided by " << b << ": ";
20 expected = a / b;
21 //remember which of the scores to use
22 score = &score_div;
23 }
24 cin >> answer;
25
26 if(answer==expected){
27 cout << "Well done\n";
28 //increment the value of where pointer ’score’ points to by one
29 score = score + 1;
30 }
31 else{
32 cout << "The answer should have been " << expected << endl;
33 }
34 cout << "\nEnter ’*’ for multiplication, ’/’ for division, ’x’ to quit.";
35 cin >> reply;
36 }
37 average = (score_mult + score_div)/counter;
38 cout << "Your average is " << average << endl;
Each of these errors occurs exactly once. Describe where in the program each of these errors occurs,
which variables are involved, and what the effect of the errors on the output for the cost is.
Name: Student Number: Page 10
(a) The first error occurs in line 8. Variable reply has no value. [1 point] (b) A possible division
by zero occurs in line 37. Variable counter was initialized to zero, and is used here. The while
loop could never be executed, and if that is the case, then a division by zero will occur. [1
point] (c) This error occurs in line 29. Pointer score is incremented, while the value should be
incremented. The correct version would be *score =*score+1; [1 point] The effect of the first
error will be that it is very likely that the loop will never execute. [1 point] The second error
means sometimes the result for the average can be a nonsense result. [1 point] The problem with
the confused pointer is that the score will not be kept. [1 point] (Maximal 5 points).
Name: Student Number: Page 11
1 int main(){
2 //This program prints the weekly schedule as a table.
3
4 const int SHIFTS = 3;//Number of shifts per day
5 const int DAYS = 5; //Number of working days
6 int bs[SHIFTS][DAYS] = {{2,4,2,3,1},{3,2,4,1,2},{1,0,2,1,1}};
7 int number = 0;
8
9 cout << "The schedule for this week\n\n";
10
11 cout << "\t";
12 for(int i = 0; i< DAYS; i++){
13 cout << "\tDay " <<i+1;
14 }
15 cout << endl;
16
17 //This is a nested for-loop
18 for(int i = 0; i< SHIFTS;i++){
19 cout << "Shift " << i+1 << "\t";
20 for(int j = 0; j< DAYS;j++)
21 cout << "\t" << bs[i][j]*12;
22 cout <<endl;
23 }
24 cout << endl;
25
26 system("pause");
27 return 0;
28 }
(a) A magic constants is used in line 21. The meaning of the factor ’12’ is unclear. (b) The line
17 is a poor comment. It doesn’t say anything about the purpose of the loop. (c) Variable bs in
line 6 is a cryptic name. It should be changed to a meaningful name like schedule. Variable i
and j are common names for counters, and thus not cryptic. (d) Variable number in line 7 isn’t
used at all. (e) The inner for-loop in line 20 and 21 should use brackets. It seems like line 22 is
intended to be part of the loop, but that is not the case. [1 point each]
Name: Student Number: Page 13
16. Consider to the following reference report for the position of junior developer.
I’m happy to act as reference for Mr. John Doe. As I understand he applied
for a position as junior developer at your company. I hope that the information
in this email will be confidential, as it is very critical of Mr Doe. The problem
is not a lack of technical skills - he is actually very talented - but his conduct
as a programmer.
A main concern is his conduct as a team member. John seemed to take a de-
light to write code that is intentionally difficult to understand, just to
impress or frustrate the person who was assigned to review his code. Colleagues
have also reported that he replaced their name in the comment section by his
own, and thus effectively took credit for their work. When he was
warned to stop this behavior, he deliberately introduced a bug in a al-
most completed product out of spite. It took us two days identify and correct
this bug, and it delayed the release of our product. This was the very reason
why we asked him to leave our company.
A recent review of his work revealed that he reused copyrighted code with-
out informing us or the license holder. We had to redo the work to avoid the
risk of legal challenges. Furthermore, we were told by a competitor that Joe
shared confidential knowledge about our product with one of their de-
velopers who knew John since high school. This competitor warned us with an
assurance that they would of course not use the leaked information.
*
* *
* *
* * *
* * * *
* * * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
1 2 3 4 5 6 7 8 9 10 11 12
The program has to include two functions, plus the main function. The first function readdays(int
month) should read for month month the number of rainy days. It should validate that the number is
between 0 and 31. Its output/input might look like this:
The second function void printrow(int data[],int length,int cutoff) will print one line of the
histogram. It should go through the elements of array data, from 0 to length, excluding length, and print
" * " if the value is larger than or equal to cutoff, and " " otherwise. Its output might look like this:
* * * * *
The main function should use these functions to create the histogram according to the following speci-
fication:
It should first read the number of rainy days for all 12 months, then print one line of the histogram
for 31, 30, . . . , 0 rainy days, and finally print in one line the numbers 1 to 12.
17. (4 marks) Which variables and constants do you need for the main function of this program, and what
are their types?
You need at least one variable, an array rainfall of size 12. The type of the array should be
12. In addition you need a counter i to count the number of days and months. Additionally
there could be constants const in DAYS=31 and const int MONTHS=12. Marking: 1 points for
identifying the main variables and constants, 1 if no unnecessary constant or variables, 1 point
for a proper name, and 1 points for the correct type.
18. (1 mark) Which type of loop is most suitable to validate that the number of rainy days entered is
between 0 and 31?
While-loop or do-loop
19. (1 mark) Which type of loop is most suitable to read in the number of rainy days for 12 months?
for-loop
20. (1 mark) Which type of loop is most suitable to print one line of the histogram for 31, 30, . . . , 0 rainy
days?
for-loop
21. (1 mark) Which type of loop is most suitable to print in one line for each of the 12 months either " "
or " * ", depending on whether the number of rainy days is below the cutoff or not?
for-loop
Name: Student Number: Page 16
22. (6 marks) Write a design for the main function in pseudo-code. Your solution should consider the
specification in the last paragraph of the introduction, and the fact that some steps should be per-
formed by the functions printrow and readdays.
For month 1 to 12
Read the number of rainy days
For day 31 to 0
Print one line of the histogram
For month 1 to 12
Print the number of the month
Marking: 1 point if it is proper pseudo-code. 2 points if it uses the correct type of loop. 2 points
if it is correct. 1 point if it has the right level of detail.
Name: Student Number: Page 17
23. (10 marks) Write C++ code for two functions. First, provide readdays(int month), which should
read for month month the number of rainy days and validate that the number is between 0 and 31.
Please provide the function in the space below:
Marking: 1 point if the parameter definition and use of return is correct. 2 points if it correctly
validates the input. 1 point if it correctly reads the input. 1 point if there are no other serious
syntax errors.
Second, write function void printrow(int data[],int length,int cutoff). It should go through
the elements of array data, from 0 to length, excluding length, and print " * " if the value is larger
than or equal to cutoff, and " " otherwise. Please provide the function in the space below:
Marking: 1 point if the parameter definition and use of return is correct. 2 points if it correctly
uses the array. 1 point if it correctly uses an if condition. 1 point if there are no other serious
syntax errors.
Name: Student Number: Page 18
24. (10 marks) Write a the main function int main(). Take into consideration your pseudo-code, and use,
if possible, the functions readdays and printrow.
int main(){
for(int i = 0; i<MONTHS;i++){
rainfall[i] =readdays(i);
}
cout << endl;
for(int i = 0; i<MONTHS;i++){
cout << setw(3) << i+1 ;
}
cout << endl << endl;
system("pause");
return 0;
}
25. (6 marks) Present two test cases, i.e. specify input, and describe briefly the excepted output. For each
test case describe why you select that input for a test case.
Please provide the first test input with an explanation:
The first test is to enter 12 times the number 12. The program should print 12 bars each contains
12 stars. This test case tests the normal behavior of the program. Marking: 1 point if the input
is correctly chosen, 1 point if the output is correctly described, and one point if the motivation
for the test case makes sense.