Types, Input/Output and Expressions · Multiple choice · 1 mark
What is the value of the expression 2 + 3 * 4 % 5?
4
0
14
2
Answer: (A)
(A). * and % have the same precedence and group from left to right, so 3 * 4 % 5 is 12 % 5, which is 2, and then 2 + 2 is 4. Choice (B) adds first, (2 + 3) * 4 % 5; (C) does 4 % 5 first, 2 + 3 * (4 % 5); (D) forgets the 2 +.
Conditionals and Loops · Write code · 2 marks
Complete the body of the function according to its header comment.
// Requires: k >= 1 and lo <= hi
// Returns how many integers n with lo <= n <= hi are divisible by k.
// Examples: count_multiples(1, 10, 3) returns 3
// count_multiples(5, 5, 5) returns 1
// count_multiples(-4, 4, 2) returns 5
int count_multiples(int lo, int hi, int k);int count_multiples(int lo, int hi, int k) {
int count = 0;
for (int n = lo; n <= hi; n++) {
if (n % k == 0) {
count++;
}
}
return count;
}The test n % k == 0 also works for negative n, because % gives 0 exactly when k divides n.
Strings and Characters · Write code · 3 marks
Write the definition of the function.
// Returns true if and only if s reads the same forwards and backwards.
// Case and spaces matter, so "Abba" is not a palindrome.
// Examples: is_palindrome("level") returns true
// is_palindrome("abca") returns false
// is_palindrome("") returns true
bool is_palindrome(const string& s);bool is_palindrome(const string& s) {
for (size_t i = 0; i < s.size() / 2; i++) {
if (s[i] != s[s.size() - 1 - i]) {
return false;
}
}
return true;
}Arrays and Vectors · Multiple choice · 2 marks
What does v contain after this code runs?
vector<int> v = {2, 2, 3};
for (size_t i = 0; i < v.size(); i++) {
if (v[i] == 2) {
v.erase(v.begin() + i);
}
}{2, 3}
{3}
{2, 2, 3}
Undefined: the loop reads past the end
Answer: (A)
(A). When i is 0 the first 2 is erased and the second 2 slides into index 0, but the loop then moves on to i = 1 and never looks at it again. Index 1 holds 3, so nothing more is erased and the loop stops.
Pointers and Dynamic Memory · Find the bug · 3 marks
The program below should print 6 and release all of its heap memory exactly once, but it contains three memory-management errors.
int main() {
int* a = new int[3];
int* b = new int(5);
int* c = b;
a[0] = 1;
a[1] = 2;
a[2] = 3;
delete a;
delete b;
delete c;
b = new int(6);
cout << *b << endl;
}For each error, give the line number(s) and say what is wrong. [2]
Write the corrected main. [1]
(a) Line 8: the array from new[] must be released with delete[], not delete. Lines 9 and 10: b and c hold the same address, so the same int is deleted twice. Line 11: the new int is never deleted, so it leaks.
(b)
int main() {
int* a = new int[3];
int* b = new int(5);
a[0] = 1;
a[1] = 2;
a[2] = 3;
delete[] a;
delete b;
b = new int(6);
cout << *b << endl;
delete b;
}Introduction to Computer Science I · University of California, Los Angeles (UCLA, UC Los Angeles) · also written COMSCI31
Make a free, printable COM SCI 31 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 UCLA library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COM SCI 31 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 California, Los Angeles. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.