Download Exam 2: Part 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
University of Michigan
EECS 183: Elem. Programming Concepts
Fall 2010
Exam 2: Part 1
Professors: ML Dorf, Elliot Soloway
Wed 10 November 2010
24 questions * 3 pts each = 72 pts
Instructions
Bubble in your name, UMID on scantron sheet. 'section' is not needed.
This course operates under the rules of the college of Engineering Honor Code. Your signature
endorses the pledge below. After you finish your exam, sign the scantron sheet to indicate you
have adhered to the Honor Pledge.
The exam is closed book. No books, notes or the like may be used.
This exam is closed electronic devices. No computers, calculators, PDAs, cell phones or other
electronic devices may be used. I know texting is a good thing – just not during an exam.
The exam is closed neighbor. No 'partner' on this -- only on projects
Some questions are not simple, therefore, read carefully
Assume all code and code fragments compile, unless otherwise specified.
Assume/use only the standard ISO/ANSI C++.
Assume "valid" means compiles and runs in standard ISO/ANSI C++
Honor Pledge:
"I have neither given nor received aid on this exam,
nor have I concealed any violations of the Honor Code."
Page 1 of 10
1. What is the output of the following code fragment?
char ch = 'C';
switch (ch) {
case 'A': cout << "good choice, I like A’s" << endl;
break;
case 'B': cout << "still good" << endl;
case 'C': cout << "this one is borderline" << endl;
default: cout << "YIKES" << endl;
}
A) good choice, I like A’s
B) still good
C) this one is borderline
D) YIKES
* E) none of the above
2. What is the loop condition ( ??? ) for the following loop in order to produce the following
output: 2 4 6 8 10
n = 0;
do
{
n = n + 2;
cout << n << ' ';
} while ( ??? );
A)
*
C)
D)
E)
n <= 10
B) n < 10
n < 8
n >= 2
n > 8
3. What is the output of the following code fragment? (loopCount is of type int.)
for (loopCount = 1; loopCount > 3; loopCount++)
cout << loopCount << ' ';
cout << "Done" << endl;
*
B)
C)
D)
E)
A) Done
1 Done
1 2 Done
1 2 3 Done
1 2 3 4 Done
Page 2 of 10
4. Consider the following code fragment:
for ( int i = 10; i < 40; i = i + 10
for ( int j = 2; j >= 0; j-- )
cout << i+ j << " ";
)
What does the above code fragment print?
A)
*
C)
D)
10 11
B) 12
12 11
none
12 20 21 22 30 31 32
11 10 22 21 20 32 31 30
22 21 32 31
of the above
5. What is the output of the following code fragment, given the input data:
int number, sum = 0;
cin >> number;
while (number != -1) {
cin >> number;
sum = sum + number;
}
cout << sum << endl;
*
B)
C)
D)
E)
99
50
7
-1
A) 56
57
155
156
infinite loop
6. Consider the following C++ function:
int
{
triple(int
x)
x = x * 3;
return x;
}
If variable y of type int is initialized to 1, what does the C++ expression
triple(y) + triple(y);
evaluate to?
*
B)
C)
D)
A) 6
9
12
none
of
the
above
Page 3 of 10
7. Consider the following C++ function
int
{
triple(int&
x)
x = x * 3;
return x;
}
If variable y of type int is initialized to 1, what does the C++ expression
triple(y) + triple(y);
evaluate to?
A) 6
B) 9
*
C) 12
D) none of
the
above
8. In which of the following cases would it be a good idea to pass arguments into a function by
value?
A) The function has two values it wants handed back to the called
routine – that is, the function sets or resets the values of the
variables
B) The function requires a stream argument
C) The function requires an array argument
D) All of the above
* E) None of the above
9. Which of these functions passes two variables by reference?
I. int willysChocolate(int boring[ ], int& willy);
II. int timsCarmels(bool stormy, bool& rainy, string& stink);
III. void jensCoconuts(bool billy, double willy);
IV. void cindysPeanutButterCookies(int& they, int are, bool yummy);
A) I
B) II
C) III
* D) Both I and II
E) Both III and IV
Page 4 of 10
10. The following function must return true if and only if the argument x is a character between
'a' - 'z' or 'A' - 'Z' inclusive.
bool myIsAlpha(char x)
{
if ( condition )
return true;
else
return false;
}
What should condition be so that the above code behaves as specified above? Select all the
choices that apply.
A) 'a' <=
B) 'a' <=
C) 'a' <=
D) 'a' <=
* E) none
x <= 'z' || 'A' <= x <= 'Z'
x <= 'z' &&
'A' <= x <= 'Z'
x ||
x <= 'z' || 'A' <= x ||
x && x <= 'z' &&
'A' <= x &&
of the above
x
x
<=
<=
'Z'
'Z'
11. What happens when a C++ input stream enters the fail state?
A) The system displays an error message, and program execution is
terminated.
B) The system displays an error message, the program continues
running, and further input operation with that stream are
ignored.
C) The system does not display an error message and program execution
is terminated.
** D) The system does not display an error message, the program
continues running, and further input operations with that stream
are ignored until cleared.
E) none of the above.
Page 5 of 10
The following code applies to the next 2 questions
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inStream;
int first, second, third;
first = second = third = 0; inStream.open("infile.txt");
inStream >> first;
inStream.get(ch);
inStream >> second;
inStream.get(ch);
inStream >> third;
cout << (first + second + third) << endl;
return 0;
}
12. If infile.txt holds:
10
20.5
30
What prints?
A) 60
B) 60.5
C) 30
* D) 35
E) none of the above
13. If infile.txt holds
10 a 20.5 30
What prints?
* A) 10
B) 30
C) 35
D) 60
E) none of the above
Page 6 of 10
14. What does the following print? Assume all the appropriate header files
ifstream infile;
infile.txt
infile.open("infile.txt");
int num = 0, sum = 0;
1 2 3 4
while (infile) {
infile >> num;
sum += num;
}
cout << sum;
A) 6
B) 10
* C) 14
D) none of the above
15. If infile.txt holds
1 2 3 4
What does the following print? Assume all the appropriate header files
ifstream infile;
infile.txt
infile.open("infile.txt");
int num = 0, sum = 0;
1 2 3 4
while (infile >> num) {
sum += num;
}
cout << sum;
A) 6
* B) 10
C) 14
D) none of the above
Page 7 of 10
16. Which of the following correctly declares and initializes an array of three ints and fills it with
zeros?
I.
int a[3] = {0};
II.
int a[] = {0, 0, 0};
III.
int a[3];
for (int i = 0; i <= 3; i++)
{
a[i] = 0;
}
A) I and III only
* B) I and II only
C) III only
D) I, II, and III
17. Is the following array declaration valid?
int numbers[10] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1};
* A) yes
B) no
18. Is the following array declaration valid?
int numbers[10] = {10};
A) yes
* B) no
Page 8 of 10
19. Consider the following code fragment:
int
int
a[6]= {0};
b = 6;
for
( int
i =
1;
i <=
5;
i++
)
{
a[i]
cout
}
cout
<<
= i;
<< a[i];
b;
What does the above code fragment print?
A) 012346
B) 0123456
* C) 123456
D) the result is unpredictable because the
out of bounds array access
E) none of the above
above
code makes
an
20. Assume you have the following code:
struct Card {
char rank;
string suit;
};
Card card;
How would you access the "suit" member?
A) card->suit
* B) card.suit
C) Card.suit
D) card[1]
E) Card[1]
Page 9 of 10
21. Which of the following is true of a struct
A)
B)
C)
*
Values are always the same type
Values are all constant
Values are always initialized to 0
D) Values can be of different types
22. Given the following code:
struct Employee {
double salary;
string personalDays[4];
char status;
};
Employee tom, jerry;
Which of the following is FALSE?
A) tom.salary is of type double
* B) jerry.personalDays is of type string
C) tom.personalDays[0] is of type string
D) tom.personalDays[0][1] is of type char
E) more than one of the above
23. What is the proper way to declare an array that holds the amount of money made at a
7-11 every hour for 24 hours, 7 days a week?
A) double moneyMade[24, 7];
B) double [24][7] moneyMade;
* C) double moneyMade [24][7];
D) double moneyMade[24/7];
24. Given the following declaration:
int arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
The element at arr[2][1] is:
A) 2
B) 4
C) 6
* D) 8
E) none of the above
Page 10 of 10