final
What prints? Assume 4 bytes per int. int a[][2] = {{0},{0}}; cout << sizeof(a) << endl; Illegal declaration. Does not compile. 8 16 12 4
16
What is printed here? #include <iostream> using namespace std; int main() { int x = 20, y = 10; y = x--; cout << x << ", " << y << endl; }
19, 20
Assume that the input is 4 4 3 2 5. What will print? int i = 1; int n; cin >> n; do { i++; cin >> n; } while (n % 2); cout << i << endl; 2 4 infinite loop 3 Does not compile
2
The following code: #include <string> class Xynoid { double a; int b; std::string c; }; int main() { Xynoid x; } Does not compile. Compiles and links. Two members uninitialized Compiles but does not link. Compiles and links. All members initialized. Compiles and links. All members uninitialized
Compiles and links. Two members uninitialized
What prints? void fn(int, double, double&) { cout << "A" << endl; } void fn(int, int, double&) { cout << "B" << endl; } void fn(int, int, double) { cout << "C" << endl; } void fn(int, int, int) { cout << "D" << endl; } int main() { fn(2.5, 1.5, 7); } D B Syntax error: no candidates Syntax error: ambiguous A
D
What prints here? auto a = 1; switch (a) { case 1: cout << "1"; case 2: cout << "2"; case 3: } cout << endl; Prints nothing 2 Does not compile 12 1
Does not compile
Below is a cumulative algorithm using an array and a range-based loop. What is printed? (Assume this is inside main() with all includes, etc.) int a[] = {2, 4, 6, 8}; int sum = 0; for (auto e : a) sum += e; cout << "sum->" << e << endl; sum->8 sum->20 Does not compile; e is undefined. Does not compile. Cannot use range-loop on arrays. Compiles and runs, but results are undefined.
Does not compile; e is undefined.
Below is insert(), a template function that works with a partially-filled array. The function inserts the argument e into the array, in sorted order. The function returns true if it succeeds, false otherwise. The function contains an error; what is the error? template <typename T> bool insert(T* a, size_t& size, size_t MAX, T e) { if (size >= MAX) return false; size_t i = 0; while (i < size) { if (a[i] > e) break; i++; } for (j = size; j > i; j--) a[j] = a[j - 1]; a[i] = e; return true; } The function writes over memory outside the array when it should not The value is inserted into the wrong position Every time the function is called, an array element is "lost" The second loop should start at i and go up to size
Every time the function is called, an array element is "lost"
Default arguments appear only in the function implementation. True False
False
What prints? int x = 0; int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; for (auto r : a) for (auto c : r) x++; cout << x << endl; Illegal; will not compile 6 3 2 Undefined (out of bounds)
Illegal; will not compile
What happens with this code? #include <string> #include <iostream> using namespace std; class Cat { string name_; public: Cat() = default; explicit Cat(const string& n); string get() const; }; Cat::Cat(const string& n): name_(n) {} string Cat::get() const { return name_; } int main() { string s = "Bill"; Cat b; b = s; cout << b.get() << endl; } Line Cat b; does not compile. No suitable constructor. Line beginning with: Cat::Cat should not have empty body The does compile; it prints "Bill". Line b = s; does not compile. Type mismatch Line Cat b; does not link. No suitable implementation.
Line b = s; does not compile. Type mismatch
This code: void f() { int *p = new int[3]{rand(), rand(), rand()}; if (p[1] != 0 && p[2] != 0) cout << p[0] / p[1] / p[2] << endl; delete[] p; } has a dangling pointer None of these has a memory leak has a double delete has a syntax error
None of these
What kind of error is this? ~/workspace/ $ ./ex1 The Patriots won the 2018 Super Bowl Operating system signal or trap None of these Syntax error (mistake in grammar) Runtime error (throws exception when running) Linker error (something is missing when linking) Type error (wrong initialization or assignment) Compiler error (something is missing when compiling)
None of these
Which statement is false? The elements in a vector are: homogeneous all of the same type None of these stored next to each other in memory accessed by using an index or subscript
None of these
Below is remove(), a template function that works with a partially-filled array. The function removes all copies of the argument e, returning the number of copies removed. The function contains an error; what is the error? template <typename T> int remove(T* a, size_t& size, T e) { int removed = 0; size_t i = 0; while (i < size) { if (a[i] == e) { removed++; size--; for (size_t j = i; j < size; j++) a[j] = a[j + 1]; } i++; } return removed; } Not all copies of e are necessarily removed None of these a should be a const T* size should not be passed by reference The condition should go to while (i <= size)
Not all copies of e are necessarily removed
What prints? vector<int> v{1, 2, 3, 4, 5}; cout << v.pop_back() << endl; 4 5 Nothing; compile-time error. 1 Nothing; run-time error.
Nothing; compile-time error.
What does this code do? int x = 0; vector<int> v{1, 3, 2}; for (auto e : v) x = e; cout << x << endl; Prints 0 Prints 6 Finds the largest element in v Prints 2 Finds the last element in v Sums the elements in v
Prints 2 Finds the last element in v
What does this function do? double mystery(const double a[], size_t len) { double x = a[0]; for (size_t i = 1; i < len; i++) if (a[i] < x) x = a[i]; return x; } Returns the largest number in the array Does not compile Returns the smallest number in the array Undefined. Depends on the input.
Returns the smallest number in the array
What is the value of the magic_powers variable after executing the following code snippet? string magic_powers = ""; int experience_level = 9; if (experience_level > 10) { magic_powers = magic_powers + "Golden sword "; } if (experience_level > 8) { magic_powers = magic_powers + "Shining lantern "; } if (experience_level > 2) { magic_powers = magic_powers + "Magic beans "; } Magic beans Golden sword Shining lantern Magic beans An empty string
Shining lantern Magic beans
What is printed when you run this code? int *n{nullptr}; cout << n << endl; The word "nullptr" No compilation errors, but undefined behavior The address value 0 No output; compiler error.
The address value 0
What is printed when you run this code? int *n{nullptr}; cout << &n << endl; No compilation errors, but undefined behavior The word "nullptr" The address value 0 No output; compiler error. The address value where n is stored
The address value where n is stored
A loop that reads data until the input stream signals that it is done is called a data loop. True False
True
Implementation files may use the statement using namespace std; True False
True
Which of these are appropriate uses of the C++ assert facility? Validate function arguments under the programmer's control Validate the postcondition of a calculation Validate assumptions about your code Debugging checks Error conditions (such as file not found) Validate input received by your program
Validate function arguments under the programmer's control Validate the postcondition of a calculation Validate assumptions about your code Debugging checks
What is stored in data after this runs? vector<int> data{1, 2, 3}; data.clear(); [1, 2] [] [2, 3] [1, 2, 3, 0] None of these [1, 2, 3]
[]
What must I change in the test to go to the next iteration What must I do to enter the loop? Has my loop reached its goal? Can my loop be entered at all? options: bounds precondition, loop guards, advance the loop, loop postcondition
advance the loop bounds precondition loop postcondition loop guards
Given the following structure and variable definitions, which data members are default initialized? struct Employee { long empID; std::string lastName; double salary; int age; }; Employee bob{777, "Zimmerman"}; lastName None of these empID age salary
age salary
The structure and variable definitions are fine. Which statements are legal? struct R { int a, b; } a, b; struct Q { int a, b; } c, d; .b = c; c = d; b = c; a = d; None of these are correct
c = d;
In the classic for loop, which portion of code is analogous to an if statement? assignment statement first statement following the loop initialization statement None of these condition expression
condition expression
Which of these is a legal assignment? string name = "Houdini"; const char *cstr = name.c_str(); const char *cstr = c_str(name); string str = c_str(name); char* cstr = name.c_str(); string* strp = name.c_str();
const char *cstr = name.c_str();
Which of these lines correctly prints 3? struct S { int a = 3; double b = 2.5; }; S obj, *p = &obj; cout << *(p.a) << endl; cout << (*p).a << endl; cout << *(p).a << endl; cout << *p.a << endl; cout << p.a << endl;
cout << (*p).a << endl;
Examine the following definition. empID is a _____________. struct Employee { long empID; std::string lastName; double salary; }; instance variable type-id None of these data member structure tag field
data member
In the reading you learned about five concepts related to variables. Line 1 is ______________. More than one answer may be required. int a; // 1 extern int b; // 2 double c = 3.15; // 3 a = 4; // 4 input initialization assignment definition declaration
definition declaration
Which loop is used when inserting an element into an array? for (j = size; j > pos; j--) a[j] = a[j - 1]; for (j = MAX; j > size; j--) a[j - 1] = a[j]; for (j = pos; j < size; j++) a[j] = a[j + 1]; for (j = size; j < MAX; j++) a[j - 1] = a[j]; None of these
for (j = size; j > pos; j--) a[j] = a[j - 1];
This code illustrates the ____________ idiom. if( n % 2 == 1) cout << "Odd" << endl; guarded action alternative action None of these are correct multiple selection
guarded action
Which statement ensures that r() terminates for all values of n? int mr(int n) { // code goes here return r(n - 1) + n * n; } if (n == 1) { return 1; } if (n < 1) { return 1; } if (n == 1) { return 1; } if (n == 0) { return 0; } if (n == 0) { return 0; }
if (n < 1) { return 1; }
Which line opens the file input.txt for reading? ifstream in("input.txt"); istream in = "input.txt"; istream in("input.txt"); iostream in = "input.txt"; fstream in("input.txt");
ifstream in("input.txt");
This compiles, runs and prints 12. What is the correct parameter declaration for x? int x = 6; multiply(x, 2); cout << x << endl; const int& x int x int& x None of these
int& x
In the C++ stream hierarchy, the base class of the ostream class is:
ios
Assume the user types "brown cow" when this code runs. What type is ch2? char ch1; auto ch2 = cin.get(ch1); int bool char ostream& istream&
istream&
What command hands in hw04 for course credit?
make submit
Below is a partially-filled array. When appending elements to this array in a loop, what statement(s) correctly updates the array with value? const size_t MAX = 100; double nums[MAX]; size_t size = 0; double value; nums[++size] = value; nums[size] = value; size++; nums[size] = value; None of these nums[size++] = value;
nums[size++] = value;
Assume that p1 is a pointer to an integer and p2 is a pointer to a second integer. Both integers appear inside a large contiguous sequence in memory, with p2 storing a larger address. How many total integers are there in the slice between p1 and p2? p2 - p1; p2 - p1 - 1; p1 - p2; None of these p1 - p2 + 1;
p2 - p1;
Which line below points ppi to pi? int main() { double pi = 3.14159; double *ppi; // code goes here // code goes here } *ppi = π *ppi = pi; None of these ppi = π ppi& = pi;
ppi = π
What happens when this code fragment compiles and runs? #define N #ifndef N cout << "Hello"; #else cout << "Goodbye"; #endif It does not compile. prints nothing prints "HelloGoodbye" prints "Goodbye" prints "Hello"
prints "Goodbye"
In C++, the statement string s; produces an empty string object produces an uninitialized variable produces a string variable containing null produces a string reference, not a string object. None of these are correct
produces an empty string object
Which library function performs an equivalent operation on C-strings? string s1 = "Hello"; string s2 = "World"; s1 = s1 + s2; strcmp() strlen() None of these strcpy() strcat()
strcat()
What statement is used to signal other parts for your program that a particular error has occurred? throw try None of these return catch raise
throw
Which of the following statements throws a valid exception in C++? 4 throw; throws str; throw.function(); throw 2;
throw 2;
The function ____ returns a string containing an appropriate message. what where when log
what