Midterm 3
Explicitly initializing an array like this: int a[3] = {1, 2, 3}; requires the size to be the same or larger than the number of elements supplied.
True
When inserting a value into a partially-filled array, in ascending order, the insertion position is the index of the first value smaller than the value.
False
vector subscripts begin at 1 and go up to the vector size.
False
What is the size of data, after this runs? vector<int> data; data.push_back(3);
1
What prints? void f(vector<int> v) { v.at(0) = 42; } int main() { vector<int> x{1, 2, 3}; f(x); cout << x.at(0) << endl; }
1
Which line prints 3? int main() { vector<int> v{1, 2, 3}; auto size = v.size(); cout << v.back() << endl; // 1. cout << v.front() << endl; // 2. cout << v.at(0) << endl; // 3. cout << v.at(size) << endl; // 4. cout << v.pop_back() << endl; // 5. }
1 OR cout << v.back() << endl;
[1837] What prints? int x = 0; int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; for (const auto& r : a) for (const auto& c : r) x++; cout << x << endl;
6
[1812] What prints when this runs? int a[2][3] = {1, 2, 3, 4, 5, 6}; cout << a[0][2] + a[1][2] << endl;
9
If img is a pointer to the first byte in an image loaded into memory, Pixel is a structure as defined in your textbook, you can create a Pixel pointer pointing to the image by writing: Pixel *p = reinterpret_cast<Pixel *>(img);
True
If p points to the first element in [1, 3, 5] then cout << *++p prints 3.
True
In C++ assigning one array to another is illegal.
True
In C++ using == to compare one array to another is permitted (if meaningless).
True
The clear() member function removes all the elements from a vector.
True
The declaration: vector<int> v(10); creates a vector object containing ten elements initialized to 0.
True
The declaration: vector<string> v{"bill", "bob", "sally"}; creates a vector containing three string objects.
True
The effective size of the C-string char * s1 = "hello"; is 5 characters, but 6 characters are used for storage.
True
The elements of a C++ array created outside of a function are allocated in the static-storage area.
True
The reinterpret_cast instruction changes way that a pointer's indirect value is interpreted.
True
The size of the array is not stored along with its elements.
True
The statement v.insert(v.end() + 1, 3) is undefined because end() + 1 points past the last element in the vector.
True
The subscripts of a C++ array range from 0 to the array size - 1.
True
When passing a 2D array to a function, the array parameter must explicitly list the size for all dimensions except for the first, like: void f(int a[][3], size_t n);
True
strcmp(s1, s2) returns a negative number if s1 is lexicographically "less than" s2.
True
vector subscripts begin at 0 and go up to the vector size - 1.
True
[1535] What does this function do? double mystery(const double a[], size_t len) { double x = 0; for (size_t i = 0; i < len; i++) if (a[i] > x) x = a[i]; return x; }
Undefined. Depends on input
[1328] What is a common pointer error?
Using a pointer without first initializing it
[1725] Which library function performs an equivalent operation on C-strings? string s1 = "Hello"; string s2 = "World"; s1 = s1 + s2;
strcat()
[1347] Examine this version of the swap() function, which is different than the two versions appearing in your text. How do you call it? void swap(int * x, int & y) { . . . } . . . int a = 3, b = 7; // What goes here ?
swap(&a, b);
C-string assignment uses the strcpy() function.
True
Elements in a vector are accessed using a subscript.
True
The declaration: vector<int> v; creates a vector variable but no vector object.
False
[1324] What is true about this code? int n{500}; int *p = &n;
*p is the value of n
The elements of a C++ int array with no explicit initialization, created in a function will be set to zero.
False
The function mystery(const int*, const int*) likely employs a counter-controlled loop.
False
[1350] Here is the pseudocode for the greenScreen() function in H12. What single statement sets the red, green and blue components to 0? Let p point the beginning of the image Set end to point just past the end While p != end If *(p + 3) is 0 (transparent) Clear all of the fields Increment p by 4
*(p) = *(p = 1) = *(p + 2) = 0;
[1424] What prints? int a[] = {1, 3, 5, 7, 9}; int *p = a; cout << *p++; cout << *p << endl;
13
[1833] What prints? int a[5][3] = { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}, {13, 14, 15} }; int *p = &a[0][0]; cout << (p + 5) << endl;
14
[1804] What prints? Assume 4 bytes per int. int a[][2] = {{0},{0}}; cout << sizeof(a) << endl;
16
[1330] What is printed when you run this code? int n{}; int *p = &n; *p = 10; n = 20; cout << *p << endl;
20
[1426] What prints? int a[] = {1, 3, 5, 7, 9}; int *p = a; cout << ++*p; cout << *p << endl;
22
[1425] What prints? int a[] = {1, 3, 5, 7, 9}; int *p = a; cout << *++p; cout << *p << endl;
33
[1506] Below is a cumulative algorithm using an array and an iterator-based loop. What is printed? (Assume all includes have been added, etc.) double average(const int *beg, const int *end) { double sum = 0; size_t count = end - beg; while (beg != end) sum += *beg++; return sum / count; } int main() { int a[] = {2, 4, 6, 8}; cout << average(begin(a), end(a) - 1) << endl; }
4
The library function begin(a) returns a pointer to the element right before the first in the array a.
False
The parameter declarations int p* and int[] p mean the same thing.
False
The following code is logically correct. What is the semantically correct prototype for mystery()? vector<double>v{1,2,3}; mystery(v);
Either mystery(const vector<int>&); OR mystery(vector<int>&); are correct
The statement v.insert(v.begin(), 3) inserts the element 3 into the vector v, overwriting the exiting element at index 0.
False
The static_cast instruction changes way that a pointer's indirect value is interpreted.
False
When deleting an element from a partially-filled array, it is an error if the index of the element to be removed is < size.
False
When deleting elements from a partially-filled array, the array should be declared const.
False
An array passed to a function is passed by reference.
False
Assume that v contains [1, 2, 3]. The result of writing cout << v.at(4); is a compiler error.
False
Assume the vector v contains [1, 2, 3]. v.erase(0); changes v to [2, 3].
False
Assume vector<int> v; Writing cout << v.front(); throws a runtime exception.
False
C++ arrays offer built-in member functions for inserting and deleting.
False
In a partially-filled array capacity represents the number of elements that are in use.
False
In a partially-filled array, the size represents the allocated size of the array.
False
In the declaration: vector<int> v; the word vector represents the object's base type.
False
Physically, a 2D array is stored as a rectangular grid of columns and rows.
False
The declaration: vector<int> v = new vector<>(); creates a vector object with no elements.
False
[1610] Below is a mystery() function with no types for its parameter. What does the function do? void mystery(a, b&, c, d, e) { for (i = b; i > d; i--) a[i] = a[i - 1]; a[d] = e; b++; }
Inserts input into a partially-filled array
[1406] Which line throws and out_of_range exception? double speed[5] = {. . .};
NONE OF THESE: cout << speed[1] << endl; cout << speed[4] << endl; cout << speed[0] << endl; cout << speed[5] << endl;
Assume vector<double> speed(5); Which line throws a runtime error?
NONE OF THESE: speed[0] = speed.back(); speed.erase(speed.begin()); cout << speed[5]; speed.front() = 12;
[1335] What is printed when you run this code? int *p = &0; cout << *p << endl;
No output; compiler error
Which of these are true? int main() { vector<int> v{1, 2, 3}; for (auto e : v) e = 0; cout << v.at(0) << endl; }
Prints 1 Code runs but has no effect on v False: Code will not compile Prints 3 Code compiles but gives a warning Prints 0
Which of these are true? int main() { vector<int> v{1, 2, 3}; for (int i = v.size() - 1; i >= 0; i--) cout << v.at(i) << " "; cout << endl; }
Prints 3 2 1
Which of these are true? int main() { vector<int> v{1, 2, 3}; for (auto i = v.size() - 1; i >= 0; i--) cout << v.at(i) << " "; cout << endl; }
Prints 3 2 1 Issues a compiler warning, but no error Crashes when run False: Compiler error Endless loop
[1523] What does this function do? int mystery(const int a[], size_t n) { int x = a[n - 1]; while (n > 0) { n--; if (a[n] < a[x]) x = a[n]; } return x; }
Return the smallest number in the array
[1341] Which area of memory are local variables stored in?
Stack
[1342] Which area of memory are global variables stored in?
Static storage area
[1332] What is printed when you run this code? int *n{nullptr}; cout << n << endl;
The address value 0
[1612] Below is pop(), a template function that works with a partially-filled array. The function copies the last element in the array into the output parameter e and returns true if successful; it returns false otherwise. What is the error? template <typename T> bool pop(T* a, size_t& size, T& e) { if (size) { e = a[size]; size--; return true; } return false; }
The wrong value is assigned to e
A forward reference can be used when you want to use a pointer to a structure as a data member without first defining the entire structure.
True
A vector represents a linear homogeneous collection of data.
True
An array passed to a function is passed by address.
True
Arrays generally have higher performance than a vector.
True
Assume the vector v contains [1, 2, 3]. v.erase(0); is a syntax error. Correct!
True
Assume the vector v contains [1, 2, 3]. v.erase(v.begin() + 2); changes v to [1, 2].
True
Assume the vector v contains [1, 2, 3]. v.pop_back(); changes v to [1, 2].
True
What is stored in data after this runs? vector<int> data{1, 2, 3}; data.front();
[1,2,3]
[1432] Which array definition contains undefined values? int SIZE = 3; int a1[SIZE]; int a2[3]; int a3[3]{}; int a4[] = {1, 2, 3}; int a5[3] = {1, 2};
a2
[1833] What prints? int a[5][3] = { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}, {13, 14, 15} }; int *p = &a[0][0]; cout << (p + 5) << endl;
an address
[1605] Below is a declaration for a partially-filled array. What is the correct prototype for a function add() that appends a new element to the end of the array and returns true if successful? const size_t MAX = 100; double nums[MAX]; size_t size = 0;
bool add(double a[], size_t& size, size_t MAX, double e);
[1606] Below is a declaration for a partially-filled array. What is the correct prototype for a function insert() that inserts a new element at position pos in the array, shifts the remaining elements right, and returns true if successful? const size_t MAX = 100; double nums[MAX]; size_t size = 0;
bool insert(double a[], size_t& size, size_t MAX, double e, size_t pos);
[1402] Which of these lines correctly prints 2.5? struct S { int a = 3; double b = 2.5; }; S obj, *p = &obj;
cout << p->b << endl;
[1421] What is the equivalent array notation? int dates[10]; cout << *dates + 2 << endl;
dates[0] + 2
[1309] The value for the variable d is stored: int a = 1; void f(int b) { int c = 3; static int d = 4; }
in the static storage area
[1322] In C++, global variables are stored:
in the static storage area
[1320] All of these are legal C++ statements; which of them uses the C++ dereferencing operator? int a = 3, b = 4;
int x = *p;
[1321] All of these are legal C++ statements; which of them uses indirection? int a = 3, b = 4;
int x = *p;
[1318] All of these are legal C++ statements; which of them uses the C++ reference declarator? int a = 3, b = 4;
int&x = a;
[1307] The value for the variable b is stored: int a = 1; void f(int b) { int c = 3; static int d = 4; }
on the stack
[1308] The value for the variable c is stored: int a = 1; void f(int b) { int c = 3; static int d = 4; }
on the stack
[1414] What is the address of the first pixel in the last row of this image? Pixel *p; // address of pixel data int w, h; // width and height of image
p + w * (h - 1)
[1351] Here is a fragment of pseudocode for the negative() function in H12. What statement represents the underlined portion of code? Let p point to beginning of the image Let end be pixel one past the end of the image While p != end Invert the red component Move p to next component
p++;
[1311] The variable buf is a pointer to a region of memory storing contiguous int values. (This is similar to your homework, where you had a region of memory storing unsigned char values.) The four lines shown here are legal. Which operation is illegal? int *p1 = buf; const int *p2 = buf; int * const p3 = buf; const int * p4 const = buf;
p3++; legal: p1++; p2++; *p1 = 3; *p3 = 7;
[1338] What is the term used to describe a variable with stores a memory address?
pointer
In C++ the parameterized collection classes are called __________?
templates
Match each item with the correct term below. Used to access the data inside a variable pointer Determines the amount of memory required and the operations permitted on a variable The meaning assigned to a set of bits stored at a memory location An object whose value is an address in memory Expression using the address operator Expression using the reference declarator Expression using the dereferencing operator Expression using the pointer declarator Expression returning the number of allocated bytes used by an object Address value 0
variable name variable type variable value pointer p = a; int x = 3; y = *a; double * v; sizeof(Star) nullptr
Match each item with the correct definition below. Elements always allocated on the heap How arrays are passed to functions What happens to an array when passed to a function const int *array int * const array const int * const array sizeof(a) / sizeof(a[0]) end(a) - begin(a) auto e : a) . . x = 0; for (auto e : a) x += e; x = a[0]; for (auto e: a) if (e > x) x = e; auto p = a; while (p != end(a)) p++; cout << a[0]; while (i < len) cout << ", " << a[i++];
vector by address decays Elements may not be modified; pointer may be Elements in may be modified; pointer may not be Neither pointer nor elements in may be modified Elements in array using arithmetic Elements in array using pointer difference A range-based loop Cumulative algorithm Extreme values algorithm Iterator-based loop Fencepost algorithm