0% found this document useful (0 votes)
27 views22 pages

Exam 2014 S 1 Guide

The document is an answer key for an exam in CS111 Introduction to Computing Science. It provides the correct answers to 10 multiple choice questions that test concepts related to C++ programming language, compiler errors, arrays, loops, functions, and file input/output. The questions cover a range of fundamental computing topics and the correct answer is identified for each question based on the given options.

Uploaded by

Khuresh Shah
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)
27 views22 pages

Exam 2014 S 1 Guide

The document is an answer key for an exam in CS111 Introduction to Computing Science. It provides the correct answers to 10 multiple choice questions that test concepts related to C++ programming language, compiler errors, arrays, loops, functions, and file input/output. The questions cover a range of fundamental computing topics and the correct answer is identified for each question based on the given options.

Uploaded by

Khuresh Shah
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/ 22

CS111 Introduction to Computing Science Page 1

Answer Key for Exam A


Section A (3 points each)

1. Which statement about the C++ programming language is true?


(A) The C++ language was developed by Bjarne Stroustrup in the 1980s.
(B) The C++ is a machine language for Universal Turing Machines.
(C) The C++ was used in the late 1800s by human clerks for scientific calculations.
(D) The C++ does not allow for the use of pointers and arrays.

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;

Which of these code snippets has as output:


The summer has passed.
This exam has not.
But it will. Soon.

(A) Snippet (A)


(B) Snippet (B)
(C) Snippet (C)
(D) All of the above
3. What will the errors that the compiler issues tell you?
(A) Whether you understand the problem correctly.
(B) Which run time errors exist in your pseudo code.
(C) Whether the program will compute the expected outcome.
(D) Why the compiler cannot produce machine code from the source code.
CS111 Introduction to Computing Science Page 2

4. Consider the following problem:


You are organizing a picnic for a group of 7 people (including yourself ). Each of them
contributes $15 for food. You do some smart shopping and pay only $84.35 for food. At the
end of the picnic you split the difference between the total of contributions and the money
you’ve payed equally among the seven of you.
Which of these code snippets is syntactically correct, and computes the amount of money each of you
gets back:

(A) (B)

float people = 7; int people = 7.0;


float contribution = 15.00; float contribution = 15.00;
float payed = 84.35; float payed = 84.35;
float difference = total - payed; float total = people / contribution;
float total = people * contribution; float difference = total - payed;
int cashback = difference / people; float cashback = difference % people;
cout << "Each gets $" << cashback << ".\n"; cout << "Each gets $" << cashback << ".\n";

(C) (D)

int people = 7; int contribution = 15;


double contribution = 15.00; int people = 7;
double payed = 84.35; double payed = 84.35;
int total = people * contribution; double total = people * contribution;
int difference = total - payed; double difference = total - payed;
int cashback = difference - people; double cashback = difference/people;
cout << "Each gets $" << cashback << ".\n"; cout << "Each gets $" << cashback << ".\n";

(A) Snippet (A)


(B) Snippet (B)
(C) Snippet (C)
(D) Snippet (D)
CS111 Introduction to Computing Science Page 3

5. Consider the following snippet:

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;

What is its output?


(A) Total: 3
(B) Total: 6
(C) Total: 9
(D) It will print nothing because total is out of scope in the last line of this snippet.

6. (A) (B)

char is_capital(bool letter) { bool is_capital(char letter) {


if(letter<=’A’ || letter >=’Z’) { if(letter>=’A’ && letter <=’Z’)
return false; {
} return true;
else{ }
return true;
} return false;
} }

(C) (D)

void is_capital(char &letter) { bool is_capital() {


if(letter<=’Z’ && letter >=’A’) char letter;
{ if(letter>=’A’ && letter <=’Z’)
letter = true; {
} return letter;
else{ }
letter = false; system("pause");
} return 0;
} }

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:

days <= 7 days > 7


(C)ompact car $70 $60
(M)id-size car $90 $80

Assume we defined the following variables and constants:

const double RATE C1 = 70;


const double RATE C2 = 60;
const double RATE M1 = 90;
const double RATE M2 = 80;
double rate=0;
int days;
char size;
Which of the following snippets computes the correct rate given size and days:

(A) if(days<=7) { (B) if(size == ’C’) {


if(size == ’C’) { if(days <= 7) {
rate = RATE_C1; rate = RATE_C1;
} else { } else {
rate = RATE_C2; rate = RATE_C2;
} }
} else { } else {
if(size == ’C’) { if(days <= 7) {
rate = RATE_M1; rate = RATE_M1;
} else { } else {
rate = RATE_M2; rate = RATE_M2;
} }
} }

(C) if(size == ’C’) { (D) if(days<=7) {


if(days <= 7) { if(size == ’C’) {
rate = RATE_C1; rate = RATE_C1;
} else { } else if (days >7) {
rate = RATE_C2; rate = RATE_C2;
} }
} else { } else if (size ==’M’) {
if(days > 7) { if(size == ’C’) {
rate = RATE_M1; rate = RATE_M1;
} else { } else if (days >7) {
rate = RATE_M2; rate = RATE_M2;
} }
} }

(A) Snippet (A)


(B) Snippet (B)
(C) Snippet (C)
(D) Snippet (D)
CS111 Introduction to Computing Science Page 5

8. What will the following code snippet print:

double array[3] = {12,6,7};


double* ptr = array;
*ptr = 3;
cout << "Result " << *(ptr+2);

(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};

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


double diff = list[i]-list[i-1];
cout << setw(2) << diff << ",";
}

return 0;
}

What will this program print?


(A) 3, 2, 1,-6,-4,
(B) 4, 7, 9,10, 4,
(C) 7, 9,10, 4, 0,
(D) This program has array bounds error and will not compile.
CS111 Introduction to Computing Science Page 6

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

Section B (5 points each)

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:

************* Renewable energy calculator *************

| Rate per kWh consumed| Rate per kWh produced


|------------------------|-----------------------
Weekday | 0.20 | 0.24
Weekend | 0.15 | 0.18

Which tariff? Type 1 for weekday, 2 for weekend, Q to quit: 1


How much did you consume: 5
How much did you produce: 0
You owe $1.20

Which tariff? Type 1 for weekday, 2 for weekend, Q to quit: 2


How much did you consume: 0
How much did you produce: 10
You earned $1.50

Which tariff? Type 1 for weekday, 2 for weekend, Q to quit: Q


Your total balance (earned - owed) is $0 earned.

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

12. Consider the following code snippet:

const int LENGTH = 25;


double entries[LENGTH]= {0};
double entry;
int i = 0;

cout << "Enter number (Q to quit): ";


cin >> entry;

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

13. Consider the following code snippet:

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;

The numbers on the left are line numbers.


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

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

14. Consider the following program:

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 }

The numbers on the left are line numbers.


This program compiles and runs, but this program has still three common errors:

(a) Possible use of an uninitialized variable.


(b) Array bounds violation.
(c) Confusing pointers with the data they point to.
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 of the program is.
CS111 Introduction to Computing Science Page 11

Answer question 14 here

(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

15. Consider the following program:

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 }

The numbers on the left are line numbers.


This program compiles and the executable runs and computes everything correctly. However, there
are five problems with programming style.
(a) Poor comment.
(b) Misspelled output.
(c) Unused variable.
(d) Forgetting brackets.
(e) A variable that shadows another.
Describe exactly where each of these problems occurs, and explain the issue.
CS111 Introduction to Computing Science Page 13

Answer question 15 here

(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

16. Consider to the following welcome address. .

Dear Ladies and Gentlemen,

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.

Please give a big hand to a truly outstanding IT professional.


Please fill in the blanks. Use 5 words or phrases from the following list:

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

Section C (A total of 40 points.)


This section asks you to write function to complete a program that computes earnings from renewable energy
production. The program asks for each day of the seven days of the week how much energy was produced.
The producer is rewarded for any kWh produced per day according to the following table:
Daily production below 100 kWh $0.20 per kWh
Daily production above 100 kWh $0.25 per kWh
The government wants to stimulate producers who can produce a steady amount of energy each day. If a
producer produces on every day of the week at least 10% of the total weekly production, the government
will pay a subsidy of 20%.
For this exercise the main is provided. This means you do not have to program the main
function. You have to program three functions used by the main function.
1. get energy (Used in line 17) This function ask the user to enter a value for energy. It will ask
repeatedly if the user enter negative values.
2. get earning (Used in line 20) This function computes the earnings, given the daily production.
3. get subsidy (Used in line 25) This function return trues if the weekly production qualifies for
government subsidy, and false otherwise.
More details about these function will be given later in this section.

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

The output of the program should look like this:

****** Renewable energy calculator ******

How much did you produce on Mon? 6


How much did you produce on Tue? 7
How much did you produce on Wed? 8
How much did you produce on Thu? 9
How much did you produce on Fri? 7
How much did you produce on Sat? 7
How much did you produce on Sun? 6
You earned $12 for the energy you produced.

Please answer the following questions

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.

(6 marks) What is the code for function get energy?

double get_energy() {
double energy;

cin >> energy;


while(energy < 0) {
cout << "Please enter a positive number";
cin >> 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

(2 marks) What is the pseudo code for function get earnings?

If production larger than 100


earning is production * 0.25
else
earning is production * 0.20
Return the earnings.
Marking: 1 point if it is proper pseudo-code. 1 points if it is correct.

(6 marks) What is the code for function get earnings?

double get_earning(double produced) {


const double CUTOFF = 100;
const double LOWRATE = 0.20;
const double HIGHRATE = 0.25;
double earning;

if(produced > CUTOFF) {


earning = HIGHRATE * produced;
}
else {
earning = LOWRATE * produced;
}
return earning;
}

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?

For element 1 to 7 of array


if the element is less than the threshold
increment counter
If counter is bigger than 0
return false
else
return true
.
Marking: 1 point if it is proper pseudo-code. 1 points if it is correct
(6 marks) What is the code for function get subsidy?

bool get_subsidy(double threshold, double array[], int size) {


int counter = 0;

for(int i=0; i<size; i++) {


if(array[i]<threshold) {
counter++;
}
}

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.

The End Of This Exam.

You might also like