Types, Input/Output and Expressions · Predict the output · 2 marks
Write the output of the program exactly as it appears. If it does not compile, write COMPILE ERROR and give the reason; if its behavior is not defined, write UNDEFINED.
int main() {
int i = 3;
int a = i++;
int b = ++i;
int c = i--;
cout << a << " " << b << " " << c << " " << i << endl;
int d = a * b - c % 2;
i += d;
i /= 4;
cout << d << " " << i << endl;
}3 5 5 4
14 4i++ gives the old value (a is 3, then i is 4); ++i gives the new value (b is 5 and i is 5); i-- gives the old value 5 and leaves i at 4. Then d is 15 - 1 = 14, i becomes 18 and the integer division gives 4.
Conditionals and Loops · Short answer · 2 marks
A while loop and a do-while loop have exactly the same condition and the same body. In what situation do they behave differently? Say what each one does there.
They differ when the condition is false the first time it is reached. The while loop tests before the body, so the body runs zero times; the do-while loop tests after the body, so the body runs once. (If the condition is true at the start, the two behave identically.)
Arrays and Vectors · Short answer · 2 marks
Each fragment below is placed inside main of its own program. Write COMPILE ERROR, UNDEFINED, or the value that is printed.
| Fragment | Result | |
| (1) | int a[3] = {1, 2, 3, 4}; cout << a[0]; | |
| (2) | vector<int> v; v[0] = 7; cout << v[0]; | |
| (3) | vector<int> v(3); v.push_back(9); cout << v[3]; | |
| (4) | int a[4] = {1, 2}; cout << a[3]; |
Answer: COMPILE ERROR; UNDEFINED; 9; 0
(1) More initializers than items is a compile error. (2) An empty vector has no item at index 0, so the assignment is undefined; only push_back or a size at construction creates items. (3) v has 3 zeros and push_back adds a fourth item, 9, at index 3. (4) Items without an initializer are set to 0.
Pointers and Dynamic Memory · Write code · 2 marks
Complete the body of the function according to its header comment.
// Exchanges the values of the two ints that a and b point to.
// Requires: a and b are not null.
// Examples: if x is 1 and y is 2, swap_ints(&x, &y) makes x 2 and y 1.
void swap_ints(int* a, int* b);void swap_ints(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}Classes and Objects · Multiple choice · 1 mark
Consider this class.
class Seat {
public:
Seat(int row) : m_row(row) {}
int row() const { return m_row; }
int sold() const { return m_sold; }
private:
int m_row;
int m_sold;
};After Seat s(4); which statement about s.sold() is correct?
It returns 0, because an int member starts at 0.
Its result is not defined, because m_sold was never given a value.
It returns 4, the same value as s.row().
The class does not compile, because every member must appear in the initializer list.
Answer: (B)
(B). The initializer list sets only m_row. A member of type int that is not initialized holds an unpredictable value (only global and static variables start at 0), so reading it is a bug. The fix is m_sold(0) in the initializer list.
Fundamentals of Programming · University of Waterloo (Waterloo, UWaterloo, UW) · also written ECE150
Make a free, printable ECE 150 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 320 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Waterloo library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ECE 150 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 Waterloo. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.