Types, Input/Output and Expressions · Complete the code · 2 marks
Fill in the blanks so that the program prints Total: $14.00. Do not change any other code.
int main() {
double price = 3.5;
int qty = 4;
cout.setf(ios::___1___);
cout.precision(___2___);
cout << "Total: $" << ___3___ << endl;
}| (1) | |
| (2) | |
| (3) |
Answer: fixed; 2; price * qty
See the answers above. Without ios::fixed, precision(2) would count significant digits and the output would be 14. The expression qty * price is equally correct.
Conditionals and Loops · Multiple choice · 1 mark
How many times is the body of this loop executed?
for (int i = 10; i > 0; i -= 3) {
cout << i << " ";
}3 times
4 times
5 times
It never stops
Answer: (B)
(B). i takes the values 10, 7, 4 and 1; after the update it is and the test i > 0 fails.
Strings and Characters · Find the bug · 3 marks
The function below is supposed to follow its header comment but misbehaves.
// Returns s reversed.
// Examples: reverse_string("abc") returns "cba"
// reverse_string("") returns ""string reverse_string(const string& s) {
string result = "";
for (size_t i = s.size() - 1; i >= 0; i--) {
result += s[i];
}
return result;
}Which line is wrong, and what goes wrong when s is "abc"? [2]
Write the corrected line. [1]
(a) Line 3. i is a size_t, which is never negative, so i >= 0 is always true. After the iteration with i == 0, i-- wraps around to a huge value, and the loop reads far outside the string instead of stopping. (For an empty string, s.size() - 1 already wraps around.)
(b)
for (int i = static_cast<int>(s.size()) - 1; i >= 0; i--) {Arrays and Vectors · Multiple choice · 2 marks
Recall that v.size() has type size_t, an unsigned integer type. What happens when this code runs?
vector<int> v;
for (size_t i = 0; i < v.size() - 1; i++) {
cout << v[i] << endl;
}Nothing is printed, because 0 is not less than .
It does not compile, because a size_t cannot hold .
The subtraction wraps around to a huge number, so the loop reads items that do not exist.
It prints 0 once, because the first item of an empty vector is 0.
Answer: (C)
(C). v.size() - 1 is computed with unsigned arithmetic: 0 - 1 wraps around to the largest size_t value, so the condition i < v.size() - 1 is true and v[0] reads outside the empty vector (undefined behavior). The safe test is i + 1 < v.size().
Classes and Objects · Multiple choice · 1 mark
The class Account is declared as follows.
class Account {
public:
Account(double opening);
double balance() const;
private:
double m_balance;
};Which is a correct definition of balance written outside the class?
double balance() const { return m_balance; }
double Account.balance() const { return m_balance; }
double Account::balance() const { return m_balance; }
double Account::balance() { return m_balance; }
Answer: (C)
(C). The class name and :: tell the compiler which class the function belongs to, and the definition must repeat the const of the declaration. (A) defines an unrelated ordinary function that cannot see m_balance, and (D) does not match the declaration.
Elementary Programming Concepts · University of Michigan (Michigan, UMich, U of M) · also written EECS183
Make a free, printable EECS 183 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 280 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Michigan library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh EECS 183 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of Michigan. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.