instructions for multiple choice questions · 2019-11-14 · name: student number: instructions for...

51
Student Name: ____________________ Student number:___________ CS111: Introduction to Computing Science FSTE/SCIMS Final Examination Semester 1 2015 F2F Mode Duration of Exam: 3 hours + 10 minutes Reading Time: 10 minutes Writing Time: 3 hours Instructions: This exam has three sections: Section A: 30 marks, 10 multiple choice questions Section B: 30 marks, 6 short answer questions Section C: 40 marks, 2 short answer questions, 3 programming exercises. Answer all questions in sections A, B and C. There are no choices. Write your answers in the space provided in this booklet. Answer Section A on the answer sheet on the next page. You can use calculators. This exam is worth 50% of your overall mark. The minimum exam mark is 40/100. The last page of this exam paper states “The End Of This Exam” This exam paper has a cover page, one page with instructions and answer sheet for section A, and 24 pages of questions. The total number of pages is 26.

Upload: others

Post on 11-Jan-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Student Name: ____________________ Student number:___________

CS111: Introduction to Computing ScienceFSTE/SCIMS

Final ExaminationSemester 1 2015

F2F Mode

Duration of Exam: 3 hours + 10 minutes

Reading Time: 10 minutes

Writing Time: 3 hours

Instructions:

This exam has three sections:

Section A: 30 marks, 10 multiple choice questions

Section B: 30 marks, 6 short answer questions

Section C: 40 marks, 2 short answer questions, 3 programming exercises.

Answer all questions in sections A, B and C. There are no choices.

Write your answers in the space provided in this booklet.

Answer Section A on the answer sheet on the next page.

You can use calculators.

This exam is worth 50% of your overall mark. The minimum exam mark is 40/100.

The last page of this exam paper states “The End Of This Exam”

This exam paper has a cover page, one page with instructions and answer sheet forsection A, and 24 pages of questions. The total number of pages is 26.

Name:

Student Number:

Instructions for multiple choice questions

• Please mark all your answers on this answer sheet.

• Put your name and student number onto this page.

• Select an answer by filling in the � completely. An answer will be filled �

• Correct mistakes by putting an X through the filled � or erasing it.

• If you put an X through a �, it will be considered unmarked.

• Ambiguous marks will be counted as incorrect.

• Each question in this multiple choice section will be marked as follows:

– Correct answer: 3 points

– No answer: 0 points

– Incorrect answer: 0 points

• There are 10 multiple choice questions.

• The maximum number of points is 30.

Answer Section A multiple choice questions here.

Question 1 Question 2 Question 3 Question 4 Question 5

A �B �C �D �

A �B �C �D �

A �B �C �D �

A �B �C �D �

A �B �C �D �

Question 6 Question 7 Question 8 Question 9 Question 10

A �B �C �D �

A �B �C �D �

A �B �C �D �

A �B �C �D �

A �B �C �D �

One answer per question.

CS111 Introduction to Computing Science Page 1

Section A (3 points each)

1. Which of the following statements is true?

(A) Bjarne Stroustrup invented the Universal Stroustrup Machine.

(B) Alan Turing invented the C++ programming language.

(C) C++ is a high-level programming language.

(D) Dev-C++ is a machine language.

2. (A)

cout << "It is cold.\t" << "It is exam.\t" << "Must be June.\t";

cout << "Outside.\n" << "Inside.\n" << "Now.\n";

(B)

cout << "It is cold.\t Outside.\n"

<< "It is exam.\t Inside.\n"

<< "Must be June.\t Now.\n";

(C)

cout << "It is cold.\n Outside.\t"

<< "It is exam.\n Inside.\t"

<< "Must be June.\n Now.\t";

Which of these code snippets has as output:

It is cold. Outside.

It is exam. Inside.

Must be June. Now.

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (C)

(D) None of the above

3. What does it mean if a compiler issues an Error?

(A) It means it encountered a syntax error, and that it could not compile the code.

(B) It means that the program compiles, but has a logic mistake.

(C) It means that you can safely ignore it because the code will compile despite of the error.

(D) It means that you there might be a problem that will show when you run the program.

CS111 Introduction to Computing Science Page 2

4. Heron’s formula can be used to compute the area for an tri-angle with lengths a, b, c. The first step is to compute thesemiperimeter:

s =a + b + c

2

The area is then

A =√s(s− a)(s− b)(s− c)

Let double a, b, c be variables for the length of the triangle, double s a variable for the semi-perimeter, and double area a variable for the area. Which of the following snippets is syntacticallycorrect and implements Heron’s formula correctly:

(A)s = a/2 + b/2 + c/2;

area = root(s (s-a) (s-b) (s-c))

(B)s = a+b+c / 2;

area = root(s*(s-a)*(s-b)*(s-c));

(C)

a + b + c

s = -----------

2

________________

area = \/s(s-a)(s-b)(s-c)

(D)s = (a + b + c)/2;

area = sqrt(s*(s-a)*(s-b)*(s-c));

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (C)

(D) Snippet (D)

CS111 Introduction to Computing Science Page 3

5. Consider the following snippet:

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

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

cout << " * ";

}

cout << endl;

}

What is its output?

(A)

* * *

* * *

* * *

(B)

*

* *

* * *

(C)

* * *

* *

*

(D)

* *

* *

* *

(A) Output (A)

(B) Output (B)

(C) Output (C)

(D) Output (D)

CS111 Introduction to Computing Science Page 4

6. (A)

float dist(double& x, double& y){

float z;

if(x > y) {

return x - return y;

}

else{

return y - return x;

}

return z;

}

(B)

void dist(double& x, double& y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return;

}

(C)

bool dist(double x, double y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return 0;

}

(D)

double dist(double x, double y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return z;

}

Which of these functions is syntactically correct, and returns |x− y|?

(A) Function (A)

(B) Function (B)

(C) Function (C)

(D) Function (D)

CS111 Introduction to Computing Science Page 5

7. A local shop offers two types of coffee in three different sizes:

(R)egular (L)arge e(X)tra large(L)ong Black $1.90 $2.10 $2.30(F)lat White $2.00 $2.30 $2.50

Assume we defined the following variables and constants:

const double LONGBLACK R = 1.90;

const double LONGBLACK L = 2.10;

const double LONGBLACK X = 2.30;

const double FLATWHITE R = 2.00;

const double FLATWHITE L = 2.30;

const double FLATWHITE X = 2.50;

double price;

char coffee;

char size;

Which of the following snippets computes the correct price given the type of coffee and the size:

(A) if(coffee ==’L’)

{

if(size == ’R’) {

price = LONGBLACK_R;

}

else if(size == ’L’) {

price = LONGBLACK_L;

}

else {

price = LONGBLACK_X;

}

}

else

{

if(size == ’R’) {

price = LONGBLACK_R;

}

else if(size == ’L’) {

price = LONGBLACK_L;

}

else {

price = LONGBLACK_X;

}

}

(B) if(size == ’R’) {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

else if (size == ’L’) {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

else {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (A) and (B)

(D) Neither (A) nor (B)

CS111 Introduction to Computing Science Page 6

8. What is entailed by the array/pointer duality law?

(A) The name of an array is actually a pointer to the first element of the array.

(B) For every array you should have a pointer as companion variable for the size.

(C) Every pointer refers to twice as much memory as an array does?

(D) That you cannot use both arrays and pointers inside of the same function.

9. Consider the following program:

int main() {

const int SIZE = 5;

double list[SIZE] = {3,2,4,5,1};

double newelement = 6;

for(int i = SIZE-1; i>0; i--) {

list[i] = list[i-1];

}

list[0] = newelement;

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

cout << list[i] << ", ";

}

return 0;

}

What will this program print?

(A) 3, 2, 4, 5, 1,

(B) 6, 3, 2, 4, 5,

(C) 6, 2, 4, 5, 1,

(D) 6, 3, 2, 4, 5, 1,

CS111 Introduction to Computing Science Page 7

10. process data.cpp

0123456789101112131415161718192021222324

# include <iostream>

# include <fstream>

using namespace std;

int main(){

Enter necessary declarationsand file handling here.

double dataitem;

double total = 0;

cout << "Enter your data (Q to quit): ";

cin >> dataitem;

while(!cin.fail()) {

total = total + dataitem;

cin >> dataitem;

datafile << dataitem << "\t";

}

cout << "The total of all items is " << total << endl;

datafile.close();

return 0;

}

The program reads values from standard input and writes to data.txt. What are the two missing linesin line 6 and 7?

(A) ifstream datafile;

datafile.open("data.txt");

(B) fstream datafile;

datafile.open("data.txt", ios::in | ios::binary);

(C) ofstream datafile;

datafile.open("data.txt");

(D) stream datafile;

datafile.fail();

CS111 Introduction to Computing Science Page 8

Section B (5 points each)

11. A programmer has written a program for a coffee shop that computes the price of an order. Staff andcustomers at the coffee shop are complaining that the program does not work correctly, and a memberof staff sends the following screen shot:

************* Welcome to Cafe 42 *************

(R)egular (L)arge

(L)ong Black $2.00 $2.50

(F)lat White $2.20 $2.70

(T)ea $1.50 $2.00

What drink [L/F/T]?: L

What size [R/L]?: L

How many: 2

This drink costs: $4.00

----------------------

Do you want another drink[Y/N]?: Y

What drink [L/F/T]?: T

What size [R/L]?: L

How many: 3

This drink costs: $4.50

----------------------

Do you want another drink[Y/N]?: N

Thanks. Your total is $8.00.

Please collect your drinks at the counter.

Answer the following two questions in the space provided below: (1) What is the problem with thisprogram; what are mistakes the program makes? (2)The compiler did not find the mistake. Why?Could the programmer have found this problem by testing? Explain.

CS111 Introduction to Computing Science Page 9

12. Consider the following code snippet:

0123456789101112131415161718

int main() {

double temp;

cout << "Please enter water temperature (Celcius):";

cin >> temp;

while(/*CONDITION*/) {

if(cin.fail()) {

cin.clear();

string bin;

cin >> bin;

}

cout << "Enter a number between 0 and 100 Celsius: ";

cin >> temp;

}

cout << temp << " Celsius is " << 1.8*temp + 32 << " Fahrenheit\n";

return 0;

}

Give a boolean expression to replace /*CONDITION*/ in line 6, such the program asks the user tore-enter the answer if the user enters something that is not a number (thus the input fails), or if thetemperature is below 0, or if the temperature is above 100.

Please provide the expression in the space below:

CS111 Introduction to Computing Science Page 10

13. Consider the following code snippet:12345

67

8

910

int number = 3;

cout << "Hailstone sequence starting at "<< number << endl;

while(number > 1) {if(number % 2 == 0) {

number = number/2;

}else {

number = 3*number+1;

}cout << number << endl;

}cout << number << endl;

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 number comment1 3 initialising number to 32 ” print Hailstone sequence starting at 3

3 ” condition is true

4 ” condition is false

6 ” enter else branch7 10 assign 10 to number

8 ” print number3 ” condition is true...

......

CS111 Introduction to Computing Science Page 11

14. Consider the following program:

1234567891011121314151617181920212223242526272829303132333435363738

# include <iostream>

using namespace std;

void add_element(double newelement, double array[], int used, int capacity) {

//Move all elements to the end

for(int i=capacity-1; i>0; i--) {

array[i]= array[i-1];

}

//Add new element at the beginning

array[0] = newelement;

if(used<capacity) {

used++;

}

return;

}

int main() {

int SIZE = 10;

int length = 0;

double datalist[SIZE];

double dataitem;

cout<< "Enter data item: ";

cin >> dataitem;

//Stop when input fails OR the length exceeds SIZE

while(!cin.fail() || length < SIZE) {

add_element(dataitem,datalist,length,SIZE);

cout<< "Enter next item: ";

cin >> dataitem;

}

//Print the last element of datalist

cout << "The last element is in the list is: " << datalist[SIZE] << endl;

return 0;

}

The numbers on the left are line numbers.

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

(a) Using a value parameter when a reference parameter should be used.

(b) An array bounds violation.

(c) Confusing || with &&.

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 12

Answer question 14 here

CS111 Introduction to Computing Science Page 13

15. Consider the following program:

123456789101112131415161718192021222324252627282930313233343536373839

#include <iostream>

using namespace std;

const double MOVIEA = 5.00;

const double MOVIEB = 4.00;

char input;

char ask_two_options(string question, char optionA, char optionB) {

//This function has return type char

cout << question;

cin >> input;

while(input!=optionA && input!=optionB) {

cout << "Wrong input. Enter "<< optionA << " or " << optionB << ": ";

cin >> input;

}

return input;

}

double movie_price(char input,char is_member) {

//Computes movie price

const double REDUCTION = 0.1; //10% for movie club member

double px;

if(input ==’A’)

px = MOVIEA;

else

px = MOVIEB;

if(is_member == ’Y’) {

px = px * 0.9;

}

return px;

}

int main() {

char movie = ask_two_options("Which movie ?: ",’A’, ’B’);

char is_member = ask_two_options("Are you movie club member: ",’Y’, ’N’);

cout << "The movie will cost " << movie_price(movie,is_member) <<endl;

return 0;

}

The numbers on the left are line numbers.

This program compiles and the executable runs and computes everything correctly. However, thereare five problems with programming style.

(a) Poor comment.

(b) Cryptic variable name.

(c) Lack of brackets.

(d) Use of a global variable.

(e) Use of a magic number.

Describe exactly where each of these problems occurs, and explain the issue.

CS111 Introduction to Computing Science Page 14

Answer question 15 here

CS111 Introduction to Computing Science Page 15

16. Please match the behavior to the right with the value of the ACS Code of Ethics to the left that itviolates. Fill into the blanks one of the letters A to E.

Primacy of the Public Interest

Competency

Enhancement of the Quality of Life

Professional development

Honesty

(A) They did not have the knowledge or expertise toproduce the system as specified.

(B) Their staff did not attend any training on inter-net security in the last five years.

(C) The company told us they would meet the dead-line, even though they knew they would not beable to make meet it.

(D) They did place their own business interests abovethose of the society at large.

(E) They showed complete disregard for how theirwork could negatively affect the life of theirclients.

CS111 Introduction to Computing Science Page 16

Section C (A total of 40 points.)

This program exercise asks you to provide part of a program that computes the price of a drink in acoffee shop. It should ask the use for the choice of drink and the number of drinks and prints the price. Itshould also writes details of the order to a file called order.csv. The coffee shop has only 3 different typesof drinks: coffee, tea and smoothies.

A screen shot may look like this:

**** Welcome to CAFE 42 ****

(A) Coffee $2.00

(B) Tea $1.50

(C) Smoothie $3.00

Which drink: A,B, or C? Any other capital letter to quit: A

How many drinks: 2

Price $4.00

Which drink: A,B, or C? Any other capital letter to quit: B

How many drinks: 3

Price $4.50

Which drink: A,B, or C? Any other capital letter to quit: 1

Enter an upper case letter: C

How many drinks: 1

Price $3.00

Which drink: A,B, or C? Any other capital letter to quit: Q

The file order.csv that will be written for the orders in this screenshot will look as follows:

Number Option Price

2 A 4

3 B 4.5

1 C 3

This is a table containing the number of drinks, the drink (’A’, ’B’ or ’C’), and the price.

CS111 Introduction to Computing Science Page 17

You are given a skeleton code. It contains already one function get positive number. You have toprovide two additional functions and a main function.

12345678910111213141516171819202122232425262728293031323334353637

# include <iostream>

# include <fstream>

# include <iomanip>

using namespace std;

int get_positive_number(string question);

char get_upper(string question);

double get_price(char choice);

int main() {

//Open log file

//Print first line of log file

//Print welcome

//Ask which drink and read and validate answer.

//While the user does not want to quit

//Ask and read how many drinks and validate answer

//Compute the price

//Print the price

//Print one line to the log file

//Ask which drink and read and validate answer.

//Close logfile

}

int get_positive_number(string question) {

cout << question;

int input;

cin >> input;

while(cin.fail() || input < 0) {

if(cin.fail()) {

cin.clear();

string bin;

cin >> bin;

}

cout << "Enter a number: ";

cin >> input;

}

return input;

}

The two function that you have to provide are get upper and get price.

• The pseudo code for the main function is given as comment in line 11 to 21.

• The function get positive number will read a number. If the input fails – because the user entersomething that is not an integer – or if the number if negative, then the function will ask the user tore-enter the number. If the validation succeeds, the function returns the validated input value.

CS111 Introduction to Computing Science Page 18

Please answer the following questions

17. (2 marks) Consider the following screenshot:

**** Welcome to CAFE 42 ****

(A) Coffee $2.00

(B) Tea $1.50

(C) Smoothie $3.00

Which drink: A,B, or C? Any other capital letter to quit: B

How many drinks: 2

Price $3.00

Which drink: A,B, or C? Any other capital letter to quit: A

How many drinks: 1

Price $2.00

Which drink: A,B, or C? Any other capital letter to quit: C

How many drinks: 3

Price $9.00

Which drink: A,B, or C? Any other capital letter to quit: Q

What will be the content of the file order.csv

(2 marks) Given the following content of the file order.csv:

Number Option Price

3 B 4.5

2 C 6

1 A 2

Which and how many drinks did the customers order?

CS111 Introduction to Computing Science Page 19

18. The function get upper should do the following:

Read a character. Ask to re-enter , as long as the answer is not between ’A’ and ’Z’. Oncea valid answer has been given return that answer.

(1 mark) In which line on page 17 do you find the prototype for function get upper?

(1 mark) What are the parameters of function get upper, and if they exist, what is their type?

(1 mark) What type of loop is the best suited to ask to re-enter the character if the validation fails?

(5 marks) What is the code for function get upper?

CS111 Introduction to Computing Science Page 20

19. The function get price should do the following:

Given a choice from ’A’ to ’C’ it should return the correct price.

(1 mark) What is the return type of function get price?

(1 mark) What are the parameters of function get price, and if they exist, what is their type?

(1 mark) What are the constants that you need for prices of the different drinks? Give a declarationof the necessary constants.

(5 marks) What is the code for function get price?

CS111 Introduction to Computing Science Page 21

20. You can find the pseudo code for the function main in line 11 to 21 on page 17. Answer the followingquestions:

(1 mark) Give two lines of code that will declare a file handle for an output file, and then open outputfile order.csv?

(1 mark) Give one line of code that writes the first line of order.csv to file?

(1 mark) Give one line of code that, given the number of drinks, the choice, and the price prints oneline of the log file order.csv?

(1 mark) Give one line of code that sets the number of digits that will be printed to standard outputto 2?

(2 marks) Give one line of code that uses the function get positive number to get a positive integer,and assigns its return value to a variable number. It should pass the question "How many drinks: ".

(2 marks) Give one line of code that uses the function get upper to get an upper case letter, andassigns its return value to a variable choice. It should pass the question "Which drink: A,B, or

C? Any other capital letter to quit: ".

(2 marks) Give one line of code that computes the price, given a choice of drink, and the number ofdrinks. Use the function get price.

CS111 Introduction to Computing Science Page 22

21. (10 marks) What is the code for the main function?The pseudo code is given on page 17, line 11 to 21.

CS111 Introduction to Computing Science Page 23

Use this page if you need extra space. State clearly which exercise you answer on this page.

CS111 Introduction to Computing Science Page 24

Use this page if you need extra space. State clearly which exercise you answer on this page.

The End Of This Exam.

Student Name: ____________________ Student number:___________

CS111: Introduction to Computing ScienceFSTE/SCIMS

Final ExaminationSemester 1 2015

F2F Mode

Duration of Exam: 3 hours + 10 minutes

Reading Time: 10 minutes

Writing Time: 3 hours

Instructions:

This exam has three sections:

Section A: 30 marks, 10 multiple choice questions

Section B: 30 marks, 6 short answer questions

Section C: 40 marks, 2 short answer questions, 3 programming exercises.

Answer all questions in sections A, B and C. There are no choices.

Write your answers in the space provided in this booklet.

Answer Section A on the answer sheet on the next page.

You can use calculators.

This exam is worth 50% of your overall mark. The minimum exam mark is 40/100.

The last page of this exam paper states “The End Of This Exam”

This exam paper has a cover page, one page with instructions and answer sheet forsection A, and 24 pages of questions. The total number of pages is 26.

CS111 Introduction to Computing Science Page 1

Answer Key for Exam A

Section A (3 points each)

1. Which of the following statements is true?

(A) Bjarne Stroustrup invented the Universal Stroustrup Machine.

(B) Alan Turing invented the C++ programming language.

(C) C++ is a high-level programming language.

(D) Dev-C++ is a machine language.

2. (A)

cout << "It is cold.\t" << "It is exam.\t" << "Must be June.\t";

cout << "Outside.\n" << "Inside.\n" << "Now.\n";

(B)

cout << "It is cold.\t Outside.\n"

<< "It is exam.\t Inside.\n"

<< "Must be June.\t Now.\n";

(C)

cout << "It is cold.\n Outside.\t"

<< "It is exam.\n Inside.\t"

<< "Must be June.\n Now.\t";

Which of these code snippets has as output:

It is cold. Outside.

It is exam. Inside.

Must be June. Now.

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (C)

(D) None of the above

3. What does it mean if a compiler issues an Error?

(A) It means it encountered a syntax error, and that it could not compile the code.

(B) It means that the program compiles, but has a logic mistake.

(C) It means that you can safely ignore it because the code will compile despite of the error.

(D) It means that you there might be a problem that will show when you run the program.

CS111 Introduction to Computing Science Page 2

4. Heron’s formula can be used to compute the area for an tri-angle with lengths a, b, c. The first step is to compute thesemiperimeter:

s =a + b + c

2

The area is then

A =√s(s− a)(s− b)(s− c)

Let double a, b, c be variables for the length of the triangle, double s a variable for the semi-perimeter, and double area a variable for the area. Which of the following snippets is syntacticallycorrect and implements Heron’s formula correctly:

(A)s = a/2 + b/2 + c/2;

area = root(s (s-a) (s-b) (s-c))

(B)s = a+b+c / 2;

area = root(s*(s-a)*(s-b)*(s-c));

(C)

a + b + c

s = -----------

2

________________

area = \/s(s-a)(s-b)(s-c)

(D)s = (a + b + c)/2;

area = sqrt(s*(s-a)*(s-b)*(s-c));

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (C)

(D) Snippet (D)

CS111 Introduction to Computing Science Page 3

5. Consider the following snippet:

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

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

cout << " * ";

}

cout << endl;

}

What is its output?

(A)

* * *

* * *

* * *

(B)

*

* *

* * *

(C)

* * *

* *

*

(D)

* *

* *

* *

(A) Output (A)

(B) Output (B)

(C) Output (C)

(D) Output (D)

CS111 Introduction to Computing Science Page 4

6. (A)

float dist(double& x, double& y){

float z;

if(x > y) {

return x - return y;

}

else{

return y - return x;

}

return z;

}

(B)

void dist(double& x, double& y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return;

}

(C)

bool dist(double x, double y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return 0;

}

(D)

double dist(double x, double y){

double z;

if(x > y) {

z = x - y;

}

else{

z = y - x;

}

return z;

}

Which of these functions is syntactically correct, and returns |x− y|?

(A) Function (A)

(B) Function (B)

(C) Function (C)

(D) Function (D)

CS111 Introduction to Computing Science Page 5

7. A local shop offers two types of coffee in three different sizes:

(R)egular (L)arge e(X)tra large(L)ong Black $1.90 $2.10 $2.30(F)lat White $2.00 $2.30 $2.50

Assume we defined the following variables and constants:

const double LONGBLACK R = 1.90;

const double LONGBLACK L = 2.10;

const double LONGBLACK X = 2.30;

const double FLATWHITE R = 2.00;

const double FLATWHITE L = 2.30;

const double FLATWHITE X = 2.50;

double price;

char coffee;

char size;

Which of the following snippets computes the correct price given the type of coffee and the size:

(A) if(coffee ==’L’)

{

if(size == ’R’) {

price = LONGBLACK_R;

}

else if(size == ’L’) {

price = LONGBLACK_L;

}

else {

price = LONGBLACK_X;

}

}

else

{

if(size == ’R’) {

price = LONGBLACK_R;

}

else if(size == ’L’) {

price = LONGBLACK_L;

}

else {

price = LONGBLACK_X;

}

}

(B) if(size == ’R’) {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

else if (size == ’L’) {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

else {

if(coffee == ’L’) {

price = LONGBLACK_R;

}

else {

price = FLATWHITE_R;

}

}

(A) Snippet (A)

(B) Snippet (B)

(C) Snippet (A) and (B)

(D) Neither (A) nor (B)

CS111 Introduction to Computing Science Page 6

8. What is entailed by the array/pointer duality law?

(A) The name of an array is actually a pointer to the first element of the array.

(B) For every array you should have a pointer as companion variable for the size.

(C) Every pointer refers to twice as much memory as an array does?

(D) That you cannot use both arrays and pointers inside of the same function.

9. Consider the following program:

int main() {

const int SIZE = 5;

double list[SIZE] = {3,2,4,5,1};

double newelement = 6;

for(int i = SIZE-1; i>0; i--) {

list[i] = list[i-1];

}

list[0] = newelement;

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

cout << list[i] << ", ";

}

return 0;

}

What will this program print?

(A) 3, 2, 4, 5, 1,

(B) 6, 3, 2, 4, 5,

(C) 6, 2, 4, 5, 1,

(D) 6, 3, 2, 4, 5, 1,

CS111 Introduction to Computing Science Page 7

10. process data.cpp

0123456789101112131415161718192021222324

# include <iostream>

# include <fstream>

using namespace std;

int main(){

Enter necessary declarationsand file handling here.

double dataitem;

double total = 0;

cout << "Enter your data (Q to quit): ";

cin >> dataitem;

while(!cin.fail()) {

total = total + dataitem;

cin >> dataitem;

datafile << dataitem << "\t";

}

cout << "The total of all items is " << total << endl;

datafile.close();

return 0;

}

The program reads values from standard input and writes to data.txt. What are the two missing linesin line 6 and 7?

(A) ifstream datafile;

datafile.open("data.txt");

(B) fstream datafile;

datafile.open("data.txt", ios::in | ios::binary);

(C) ofstream datafile;

datafile.open("data.txt");

(D) stream datafile;

datafile.fail();

CS111 Introduction to Computing Science Page 8

Section B (5 points each)

11. A programmer has written a program for a coffee shop that computes the price of an order. Staff andcustomers at the coffee shop are complaining that the program does not work correctly, and a memberof staff sends the following screen shot:

************* Welcome to Cafe 42 *************

(R)egular (L)arge

(L)ong Black $2.00 $2.50

(F)lat White $2.20 $2.70

(T)ea $1.50 $2.00

What drink [L/F/T]?: L

What size [R/L]?: L

How many: 2

This drink costs: $4.00

----------------------

Do you want another drink[Y/N]?: Y

What drink [L/F/T]?: T

What size [R/L]?: L

How many: 3

This drink costs: $4.50

----------------------

Do you want another drink[Y/N]?: N

Thanks. Your total is $8.00.

Please collect your drinks at the counter.

Answer the following two questions in the space provided below: (1) What is the problem with thisprogram; what are mistakes the program makes? (2)The compiler did not find the mistake. Why?Could the programmer have found this problem by testing? Explain.

(1) The program does not compute the price of the individual drinks correctly, and does makesa mistake with the total. The first order should have been $5.00, the second $6.00. The totalfor the two amount mentioned in the screenshot should have been $8.50 and not $8.00. (2) Thisproblem is a semantic error, which means that the program compiles fine, but when you run theprogram it, it does not compute what it is supposed to compute. Using proper test cases wouldhave helped finding these mistakes, as the mistakes appear for fairly normal orders.Marking: 2 points if one problem was correctly identified. 3 points if more than one was correctlyidentified. 2 mark if it correctly explains the semantic error, and what role testing could have tofind it.

CS111 Introduction to Computing Science Page 9

12. Consider the following code snippet:

0123456789101112131415161718

int main() {

double temp;

cout << "Please enter water temperature (Celcius):";

cin >> temp;

while(/*CONDITION*/) {

if(cin.fail()) {

cin.clear();

string bin;

cin >> bin;

}

cout << "Enter a number between 0 and 100 Celsius: ";

cin >> temp;

}

cout << temp << " Celsius is " << 1.8*temp + 32 << " Fahrenheit\n";

return 0;

}

Give a boolean expression to replace /*CONDITION*/ in line 6, such the program asks the user tore-enter the answer if the user enters something that is not a number (thus the input fails), or if thetemperature is below 0, or if the temperature is above 100.

Please provide the expression in the space below:

cin.fail() || temp < 0 || temp > 100 or any equivalent solution.Marking: 5 marks if correct. Note that there are a few correct equivalent answers. 4 mark ifcorrect except that the student used temp <= 0 or temp >=100. Three marks if correct except forconfusing && and ||. Two marks if either the temperature part, or the cin.fail() part is correct,and no further errors. Two marks if the answer is the exact opposite of what is required, i.e. itis the negation.

CS111 Introduction to Computing Science Page 10

13. Consider the following code snippet:12345

67

8

910

int number = 3;

cout << "Hailstone sequence starting at "<< number << endl;

while(number > 1) {if(number % 2 == 0) {

number = number/2;

}else {

number = 3*number+1;

}cout << number << endl;

}cout << number << endl;

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 number comment1 3 initialising number to 32 ” print Hailstone sequence starting at 3

3 ” condition is true

4 ” condition is false

6 ” enter else branch7 10 assign 10 to number

8 ” print number3 ” condition is true...

......

line number comment4 ” condition is true

5 ” assign 5 to number

8 ” print number3 ” condition is true

4 ” condition is false

CS111 Introduction to Computing Science Page 11

14. Consider the following program:

1234567891011121314151617181920212223242526272829303132333435363738

# include <iostream>

using namespace std;

void add_element(double newelement, double array[], int used, int capacity) {

//Move all elements to the end

for(int i=capacity-1; i>0; i--) {

array[i]= array[i-1];

}

//Add new element at the beginning

array[0] = newelement;

if(used<capacity) {

used++;

}

return;

}

int main() {

int SIZE = 10;

int length = 0;

double datalist[SIZE];

double dataitem;

cout<< "Enter data item: ";

cin >> dataitem;

//Stop when input fails OR the length exceeds SIZE

while(!cin.fail() || length < SIZE) {

add_element(dataitem,datalist,length,SIZE);

cout<< "Enter next item: ";

cin >> dataitem;

}

//Print the last element of datalist

cout << "The last element is in the list is: " << datalist[SIZE] << endl;

return 0;

}

The numbers on the left are line numbers.

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

(a) Using a value parameter when a reference parameter should be used.

(b) An array bounds violation.

(c) Confusing || with &&.

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 12

Answer question 14 here

(a) This happens in line 4. Parameter used should be a reference variable. [1 point] This meansthat incrementing used in line 13, is pointless, since that will not update the argument. [1 point](b) A potential array bounds violation occurs in line 35. The last element is datalist[SIZE-1][1 point] An array bound error can result in a lot of things, most likely it will cause the functionprint some meaningless number. [1 point] (c) This error occurs in line 26. Given the comment inline 25, this condition should be !cin.fail() || length < SIZE. [1 point] This mistake meansthat the while loop will stop if the array is full AND the user enters non valid input. The commentsays OR. [1 point] (Maximal 5 points).

CS111 Introduction to Computing Science Page 13

15. Consider the following program:

123456789101112131415161718192021222324252627282930313233343536373839

#include <iostream>

using namespace std;

const double MOVIEA = 5.00;

const double MOVIEB = 4.00;

char input;

char ask_two_options(string question, char optionA, char optionB) {

//This function has return type char

cout << question;

cin >> input;

while(input!=optionA && input!=optionB) {

cout << "Wrong input. Enter "<< optionA << " or " << optionB << ": ";

cin >> input;

}

return input;

}

double movie_price(char input,char is_member) {

//Computes movie price

const double REDUCTION = 0.1; //10% for movie club member

double px;

if(input ==’A’)

px = MOVIEA;

else

px = MOVIEB;

if(is_member == ’Y’) {

px = px * 0.9;

}

return px;

}

int main() {

char movie = ask_two_options("Which movie ?: ",’A’, ’B’);

char is_member = ask_two_options("Are you movie club member: ",’Y’, ’N’);

cout << "The movie will cost " << movie_price(movie,is_member) <<endl;

return 0;

}

The numbers on the left are line numbers.

This program compiles and the executable runs and computes everything correctly. However, thereare five problems with programming style.

(a) Poor comment.

(b) Cryptic variable name.

(c) Lack of brackets.

(d) Use of a global variable.

(e) Use of a magic number.

Describe exactly where each of these problems occurs, and explain the issue.

CS111 Introduction to Computing Science Page 14

Answer question 15 here

(a)The poor comment is in line 9. It is obvious that the return type is char. (b) Variable px

is a cryptic variable name. (c) The if in lines 24 to 27 should use brackets. (d) Variable input

declared in line 6 is global . (e) The number 0.9 in line 29 is a magic number.

CS111 Introduction to Computing Science Page 15

16. Please match the behavior to the right with the value of the ACS Code of Ethics to the left that itviolates. Fill into the blanks one of the letters A to E.

(D) Primacy of the Public Interest

(A) Competency

(E) Enhancement of the Quality of Life

(B) Professional development

(C) Honesty

(A) They did not have the knowledge or expertise toproduce the system as specified.

(B) Their staff did not attend any training on inter-net security in the last five years.

(C) The company told us they would meet the dead-line, even though they knew they would not beable to make meet it.

(D) They did place their own business interests abovethose of the society at large.

(E) They showed complete disregard for how theirwork could negatively affect the life of theirclients.

CS111 Introduction to Computing Science Page 16

Section C (A total of 40 points.)

This program exercise asks you to provide part of a program that computes the price of a drink in acoffee shop. It should ask the use for the choice of drink and the number of drinks and prints the price. Itshould also writes details of the order to a file called order.csv. The coffee shop has only 3 different typesof drinks: coffee, tea and smoothies.

A screen shot may look like this:

**** Welcome to CAFE 42 ****

(A) Coffee $2.00

(B) Tea $1.50

(C) Smoothie $3.00

Which drink: A,B, or C? Any other capital letter to quit: A

How many drinks: 2

Price $4.00

Which drink: A,B, or C? Any other capital letter to quit: B

How many drinks: 3

Price $4.50

Which drink: A,B, or C? Any other capital letter to quit: 1

Enter an upper case letter: C

How many drinks: 1

Price $3.00

Which drink: A,B, or C? Any other capital letter to quit: Q

The file order.csv that will be written for the orders in this screenshot will look as follows:

Number Option Price

2 A 4

3 B 4.5

1 C 3

This is a table containing the number of drinks, the drink (’A’, ’B’ or ’C’), and the price.

CS111 Introduction to Computing Science Page 17

You are given a skeleton code. It contains already one function get positive number. You have toprovide two additional functions and a main function.

12345678910111213141516171819202122232425262728293031323334353637

# include <iostream>

# include <fstream>

# include <iomanip>

using namespace std;

int get_positive_number(string question);

char get_upper(string question);

double get_price(char choice);

int main() {

//Open log file

//Print first line of log file

//Print welcome

//Ask which drink and read and validate answer.

//While the user does not want to quit

//Ask and read how many drinks and validate answer

//Compute the price

//Print the price

//Print one line to the log file

//Ask which drink and read and validate answer.

//Close logfile

}

int get_positive_number(string question) {

cout << question;

int input;

cin >> input;

while(cin.fail() || input < 0) {

if(cin.fail()) {

cin.clear();

string bin;

cin >> bin;

}

cout << "Enter a number: ";

cin >> input;

}

return input;

}

The two function that you have to provide are get upper and get price.

• The pseudo code for the main function is given as comment in line 11 to 21.

• The function get positive number will read a number. If the input fails – because the user entersomething that is not an integer – or if the number if negative, then the function will ask the user tore-enter the number. If the validation succeeds, the function returns the validated input value.

CS111 Introduction to Computing Science Page 18

Please answer the following questions

17. (2 marks) Consider the following screenshot:

**** Welcome to CAFE 42 ****

(A) Coffee $2.00

(B) Tea $1.50

(C) Smoothie $3.00

Which drink: A,B, or C? Any other capital letter to quit: B

How many drinks: 2

Price $3.00

Which drink: A,B, or C? Any other capital letter to quit: A

How many drinks: 1

Price $2.00

Which drink: A,B, or C? Any other capital letter to quit: C

How many drinks: 3

Price $9.00

Which drink: A,B, or C? Any other capital letter to quit: Q

What will be the content of the file order.csv

Number Option Price

2 B 3

1 A 2

3 C 9

(2 marks) Given the following content of the file order.csv:

Number Option Price

3 B 4.5

2 C 6

1 A 2

Which and how many drinks did the customers order?

3 tea, 2 smoothies and 1 coffee.

CS111 Introduction to Computing Science Page 19

18. The function get upper should do the following:

Read a character. Ask to re-enter , as long as the answer is not between ’A’ and ’Z’. Oncea valid answer has been given return that answer.

(1 mark) In which line on page 17 do you find the prototype for function get upper?

Line 7.

(1 mark) What are the parameters of function get upper, and if they exist, what is their type?

string question

(1 mark) What type of loop is the best suited to ask to re-enter the character if the validation fails?

While or do

(5 marks) What is the code for function get upper?

char get_upper(string question) {

cout << question;

char input;

cin >> input;

while(input<’A’ || input > ’Z’) {

cout << "Enter an upper case letter: ";

cin >> input;

}

return input;

}

Marking: 1 point if the parameter definition and use of return is correct. 2 points if it correctlyvalidates the input. 1 point if it correctly uses get positive number. 1 point if there are noother serious syntax errors.

CS111 Introduction to Computing Science Page 20

19. The function get price should do the following:

Given a choice from ’A’ to ’C’ it should return the correct price.

(1 mark) What is the return type of function get price?

double

(1 mark) What are the parameters of function get price, and if they exist, what is their type?

int choice

(1 mark) What are the constants that you need for prices of the different drinks? Give a declarationof the necessary constants.

const double COFFEE = 2.00;const double TEA = 1.50;const double SMOOTHIE = 3.00;

(5 marks) What is the code for function get price?

double get_price(char choice) {

const double COFFEE = 2.00;

const double TEA = 1.50;

const double SMOOTHIE = 3.00;

if(choice == ’A’) {

return COFFEE;

}

else if(choice == ’B’) {

return TEA;

}

else {

return SMOOTHIE;

}

}

Marking: 1 mark if the parameter definition and use of return is correct. 2 marks if it correctlycomputes the price. 1 mark if it uses constants, and no magic numbers. 1 mark if there are noother serious syntax errors.

CS111 Introduction to Computing Science Page 21

20. You can find the pseudo code for the function main in line 11 to 21 on page 17. Answer the followingquestions:

(1 mark) Give two lines of code that will declare a file handle for an output file, and then open outputfile order.csv?

ofstream datafile;

datafile.open("order.csv");

(1 mark) Give one line of code that writes the first line of order.csv to file?

datafile << "Number\tOption\tPrice\n";

(1 mark) Give one line of code that, given the number of drinks, the choice, and the price prints oneline of the log file order.csv?

datafile << number << "\t" << choice << "\t"<< price << endl;

Marking: The print statement should use a separator between the numbers, e.g. a tab or space.

(1 mark) Give one line of code that sets the number of digits that will be printed to standard outputto 2?

cout << setprecision(2) << fixed;

(2 marks) Give one line of code that uses the function get positive number to get a positive integer,and assigns its return value to a variable number. It should pass the question "How many drinks: ".

number = get positive number("How many drinks: ");

Marking: 1 mark for correct use of function, 1 mark for correct assignment.

(2 marks) Give one line of code that uses the function get upper to get an upper case letter, andassigns its return value to a variable choice. It should pass the question "Which drink: A,B, or

C? Any other capital letter to quit: ".

choice = get upper("Which drink: A,B, or C? Any other key to quit: ");

Marking: 1 mark for correct use of function, 1 mark for correct assignment.

(2 marks) Give one line of code that computes the price, given a choice of drink, and the number ofdrinks. Use the function get price.

price = get price(choice) * number;

Marking: 1 mark for correct use of function, 1 mark for correct use in assignment and expression.

CS111 Introduction to Computing Science Page 22

21. (10 marks) What is the code for the main function?The pseudo code is given on page 17, line 11 to 21.

int main() {

cout << setprecision(2) << fixed;

//Open logfile

ofstream datafile;

datafile.open("order.csv");

//Print first line of log file

datafile << "Number\tOption\tPrice\n";

//Print welcome

cout << "**** Welcome to CAFE 42 ****\n\n";

cout << "(A) Coffee\t$2.00\n";

cout << "(B) Tea\t\t$1.50\n";

cout << "(C) Smoothie\t$3.00\n\n";

//Ask which drink and read and validate answer.

int choice = get_upper("Which drink: A,B, or C? Any other capital letter to quit: ");

//While the user does not want to quit

while(choice >= ’A’ && choice <=’C’) {

//Ask and read how many drinks and validate answer

int number = get_positive_number("How many drinks: ");

//Compute the price

double price = get_price(choice) * number;

//Print the price

cout << "Price\t$"<< price << endl;

//Print one line to the logfile

datafile << number << "\t" << choice << "\t"<< price << endl;

//Ask which drink and read and validate answer.

choice = get_upper("Which drink: A,B, or C? Any other capital letter to quit: ");

}

//close logfile

datafile.close();

return 0;

}

CS111 Introduction to Computing Science Page 23

Use this page if you need extra space. State clearly which exercise you answer on this page.

Marking rubricCriteria Very good Good Fair Poor CommentSyntax: The programwould compile withoutcompiler error.

2 2 1 0 Examples of errors areforgotten semicolons,illegal variable names,made up keyword. Oneor two minor mistakesshould still give fullmarks.

Semantics: Using con-structs correctly andfor their intended use.

3 2 1 0 Examples of mistakesare confusing = with==, || with &&, con-fusing values with vari-ables, array bound er-rors, using the wrongtype of loop, incorrectuse of else.

Structure: The pro-gram is well organisedand looks designed.

3 2 1 0 Mistakes would be un-necessary duplication ofcode, but also disregard-ing the given pseudocode.

Functionality: The pro-gram should producethe expected outcomes.

2 2 1 0 Some minor mistakesshould still give fullmarks.

CS111 Introduction to Computing Science Page 24

Use this page if you need extra space. State clearly which exercise you answer on this page.

The End Of This Exam.