Está en la página 1de 10

Exam 1

Fall 2011 Semester


Engr101: Introduction to Algorithms and Programming
Section 100 / 200 / 300

You are allowed to use the books and your notes.


You may not use electronic devices: including, but not limited to, calculators, laptops, and cell phones.
You may not access the internet in any way.
You may not use any device that can interpret, compile, or run program code in any way.

Write your uniqname on the top of each page of the exam (excluding the first page).
You are responsible for the legibility of your work. Any work that is not clear to the grader due to sloppiness
or poor handwriting will be marked incorrect.
You must read and sign the Honor Code below after you have completed the exam.

Printed Uniqname: ___________________________


Printed Name: ____________________________

Circle your Lab Section below (you will lose 1 point if you dont do this or it is incorrect):
101:
102:
103:
104:
105:
106:
107:
108:
109:

Cat Th, 8:30-10:30a


Rob Th, 10:30a-12:30p
Cat Th, 12:30-2:30p
Cassie Th, 2:30-4:30p
Rob Th, 4:30-6:30p
Cassie Fri, 8:30-10:30a
Cassie Fri, 10:30a-12:30p
Cat Fri, 12:30-2:30p
Rob Fri, 2:30-4:30p

201:
202:
203:
204:
205:
206:
207:
208:
209:

I have neither given nor received aid on


this examination, nor have I concealed
a violation of the Honor Code.

Lisa Tu, 8:30-10:30a


Nader Tu, 10:30a-12:30p
Apoorva Tu, 12:30-2:30p
Apoorva Tu, 2:30p-4:30p
Nader Tu, 4:30p-6:30p
Nader Fri, 8:30-10:30a
Apoorva Fri, 10:30a-12:30p
Lisa Fri, 12:30-2:30p
Lisa Fri, 2:30-4:30p

Number

301:
302:
303:
304:
305:
306:
307:
308:
309:

II

III

Oz Mon, 8:30-10:30a
Oz Mon, 10:30a-12:30p
Jeff Mon, 12:30-2:30p
Sumit Mon, 4:30p-6:30p
Oz Wed, 8:30-10:30a
Jeff Wed, 10:30a-12:30p
Jeff Wed, 12:30-2:30p
Sumit Wed, 2:30-4:30p
Sumit Wed, 4:30p-6:30p

IV

Total

Grade

Signature:_________________________

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 1 of 10

Uniqname: ____________________________
The remainder of this page is intentionally left blank.

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 2 of 10

Uniqname: ____________________________
Question 1 - Matching (10 points)
Match each concept on the left-hand side with the phrase on the right that fits best. Note that there are
more items in the right column than in the left, so some answers will not be used.
_____ #include directives

A. return type of a predicate

_____ {

B. relational operator for equality

_____ int division

C. remainder (modulus) operator

_____ insertion

D. stream output operator

_____ ==

E. evaluates to whole number part

_____ loop invariant

F. terminate procedure with prejudice

_____ bool

G. part of scaffolding

_____ main

H. non-integer numeric type

_____ %

I. delimits compound statement

_____ double

J. cin is an example of this


K. supports reasoning about iteration correctness
L. function called by operating system to invoke
program

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 3 of 10

Uniqname: ____________________________
Question 2 - Multiple Choice (20 points)
Check the single most appropriate answer for each.
a) If the command g++ myprog.cpp -o out completes without errors or warnings it means:
____
____
____
____
____

myprog.cpp is a semantically correct C++ program


myprog.cpp is a syntactically legal C++ program
myprog.cpp contains no side effects
myprog.cpp has no infinite loops
none of the above

b) In the following C++ expression, which operator is applied last?


x = - 23 / (5 + y % 44)
____
____
____
____
____

=
/
+
%

c) Suppose the function mystery has been defined with the signature int mystery(int baz);
What is the value of bar after the following loop executes?
int foo, bar = 0;
while (bar < 30) {
foo = mystery(bar);
bar++;
}
____
____
____
____
____

0
mystery(29)
mystery(30)
30
cannot tell from information given

d) Which of the following captures the formula 5.5x in a C++ expression (assume x is a positive
integer)?
____
____
____
____
____

5.5 * x
(5.5 * x) / 1
(5.5 * x) % 1
(55 * x) / 10
None of the above

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 4 of 10

Uniqname: ____________________________
e) Which of the following prints GoBlue! on a single line of output?
____
____
____
____
____

cout
cout
cout
cout
cout

<<
<<
<<
>>
>>

"Go"; cout << "Blue!" << endl;


"Go" "Blue" << endl;
"Go" << endl << "Blue!" << endl;
"GoBlue!" >> endl;
"GoB" + "lue!" << endl;

f) Which of the following lines of code will not compile?


____
____
____
____
____

if
if
if
if
if

(1+1
(2+2
true
(not
(not

== 2) cout << "duh";


== 3) cout << "who cares?";
cout << "ok";
true) cout << "false"; else cout << "true";
1) cout << "false"; else if (not 0) cout << "true";

g) Which of the following declarations signals a violation of our strict distinction between functions and
procedures?
____
____
____
____
____

void myproc(int x);


void myproc(double & y);
int myfun(int x, double z);
double myfun(int x, double & z);
double myfun();

h) What does the following statement do?


ofstream myfile("myfile.txt");
____
____
____
____
____

(1) declares an output file stream (ofstream) named myfile


(2) opens the ofstream myfile and associates it with the file pathname "myfile.txt"
(3) writes the line "myfile.txt" to the ofstream myfile
(1) and (2)
(1) and (3)

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 5 of 10

Uniqname: ____________________________
Question 3 What does the code do? (35 points)
In the space provided beside each of the following six programs, write one of the following:
The screen output of the program, using underscore to denote any spaces (ie, 6_24_42)
Does not compile if the program as written would not compile
Infinite loop if the program would enter an infinite loop and never finish during runtime
Assume that each program has #include <iostream> and using namespace std; at the
beginning. Do not put any scratch work in the answer box.
int main() {
int year = 1984;
while(year > 0)
{
cout << year % 10;
year = year / 10;
}
return 0;
}

int main() {
double x = 0;
int n = 1;
while (n<5)
{
if (n%2==1)
cout << n << " ";
x = x + n;
n++;
}
cout << x;
return 0;
}

int main() {
int year = 1900;
cout << year;
if (year % 4 == 0)
cout << " is";
if (year % 100 == 0)
cout << " is not";
else if (year % 400 == 0)
cout << " is";
cout << " a leap year";
return 0;
}

ENGR 101: Intro to Computers & Programming - Fall 2011

Answer:

Answer:

Answer:

Page 6 of 10

Uniqname: ____________________________
int main() {
double x = 0;
int n=1;
while(n<5);
{
x = x + 2/n;
n++;
}
cout << x;
return 0;
}

Answer:

void adjust(int & minus, int & plus) {


minus = minus - 1;
plus = plus - minus;
cout << minus << plus;
return;
}

Answer:

int main() {
int evil = 2, good = 0;
while(evil > good)
{
adjust(evil, good);
}
return 0;
}

void cycle(int a) {
while (a != 1) {
cout << a << " ";
switch (a%2) {
case 1:
a = 3*a+1;
break;
default:
a = a/2;
}
}
return;
}

Answer:

int main(){
cycle(6);
return 0;
}

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 7 of 10

Uniqname: ____________________________
Question 4 Write some code (35 points)
For this question, you must write code for the main() function in the space provided on the next page
(you have already been given the scaffolding). Your completed program will help a group of students
decide at which location they should hold a party. The location data where your program will gather its
input is stored in a file called locations.txt. The information that your program calculates will be
output to a pair of files called options.txt and ignores.txt, and also to the screen. Make sure
to open files before they are used and close them when you are finished. Each line in
locations.txt corresponds to a location where the party could be held, formatted as follows:
<locNum> <capacity> <cost> <xCoord> <yCoord>
where:
<locNum>
is the location number expressed as an integer
<capacity>
is the number of people the location can hold expressed as an integer
<cost>
is the total cost of the location expressed as a double
<xCoord> and <yCoord>
are the x and y coordinates of the location expressed as integers
In order for your program to be correct, it must do the following:
1. Prompt the user to enter the number of people that will be attending the party by outputting the
following message to the screen: Number of people:
2. Read the number of people, numPeople, from the standard input stream.
3. Using iteration, continually read data from the input file locations.txt until the end of the file
is reached. Data from the file must be read one line at a time. In other words, your program must
read data from the file in groups of 5 values each time it is read. You can assume that the input file
will be properly formatted and will always contain at least one line of data.
4. For each location in the file read in step 3, check if the locations capacity is large enough to
accommodate the number of people that will be attending the party that was input in step 2. Then,
a. If the location can accommodate the party, output the following on single line to the
options.txt file:
<locNum> <costPerPerson> <distance>
where:
<locNum> is the current location number expressed as an integer
<costPerPerson> is the cost of the current location per person who will attend the
party expressed as a double
<distance> is the distance to the current location expressed as a double calculated using
the following equation: = 2 + 2 where xCoord and yCoord are
the x and y coordinates of the current location that were read from the file in step 3.
b. If the location cannot accommodate the party, output the following on single line to the
ignores.txt file:
<locNum>
where:
<locNum> is the current location number expressed as an integer
5. Once all locations have been read from the file, if no locations were able to accommodate the party
in step 4a, output the following message to the screen:
The party is canceled!
If there were locations that could accommodate the party, output the following to the screen:
There are <numOps> options!
where:
<numOps> is the number of locations that can accommodate the party expressed as an integer.
Note: The last page of the exam contains some examples of how your program should function.
ENGR 101: Intro to Computers & Programming - Fall 2011

Page 8 of 10

Uniqname: ____________________________
Note: This question can be completed using fewer than 30 lines of code.

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 9 of 10

Uniqname: ____________________________
Contents of the
locations.txt
input file

123 50 350.0 3 4
456 200 240.0 7 7
789 100 100.0 2 1

Contents of
options.txt output file

Screen
Output

Number of people:
50
There are 3 options!

Screen
Output

Number of people:
400
There are 2 options!

Screen
Output

Number of people:
1000
The party is canceled!

123 7 5
456 4.8 9.89949
789 2 2.23607

Contents of
ignores.txt output file

Contents of the
locations.txt
input file

111
123
456
789

700
450
200
100

800.0
600.5
240.5
100.0

9
3
7
2

10
4
7
1

Contents of
options.txt output file

111 2 13.4536
123 1.50125 5

Contents of
ignores.txt output file

456
789

Contents of the
locations.txt
input file

123 100 50.25 11 12


456 400 40.45 34 23
789 700 70.75 12 10

Contents of
options.txt output file
Contents of
ignores.txt output file

123
456
789

ENGR 101: Intro to Computers & Programming - Fall 2011

Page 10 of 10

También podría gustarte