Introduction to Programming II Final

Ace your homework & exams now with Quizwiz!

What is the result of (3 / 4)? (a) 0 (b) 0.75 (c) 1 (d) Depends on the variable it is assigned to.

(a) 0

What is the output of this program? int a = 9; int &aref = a; a++; aref++; std::cout << a; (a) 11 (b) Run-time error (c) 9 (d) Compile-time error (e) 12 (f) 10 (g) None of the above

(a) 11

Which of the following statements about int and long is true? (a) int is larger than long (b) int is smaller than long (c) int is smaller or equal in size to long (d) int is larger or equal in size to long (e) None of the above

(c) int is smaller or equal in size to long

Which of the following is NOT a legal initialization? (a) int x = 1; (b) int x(1); (c) int x; x = 1; (d) int x = 01; (e) int x = 0xA; (f) int x = {1};

(c) int x; x = 1;

After the following statements, which option changes the value of i to 143? int *p; int i, k; i=142; k=i;p = &i; (a) k = 143; (b) *k = 143; (c) p = 143; (d) *p = 143; (e) Both (a) and (c) (f) Both (a) and (d) (g) None of the above

(d) *p = 143;

What will occur if two header guards use the same variable name? (a) Only one of the two headers will be included. (b) Both headers will be included. (c) Neither header will be included. (d) Either (a), (b), or (c) will randomly occur. (e) None of the above.

(a) Only one of the two headers will be included.

What is the purpose of header guards? (a) To prevent re-declaration by ensuring a header is only included once. (b) To allow for default argument values and function overloading. (c) To allow for optimized compilation speeds. (d) To stop private functions from being accessed publicly. (e) To ensure that every function is declared prior to invocation. (f) To ensure that all headers are included by the needed implementation files.

(a) To prevent re-declaration by ensuring a header is only included once.

What is the type of x in: string str = "hi"; auto x = str.size(); (a) string::size type (b) size t (c) unsigned int (d) int (e) 3 (f) long (g) 2 (h) None of the above

(a) string::size type

void wth(int i, int &k) { i = 1; k = 2; } int main () { int x = 0; wth(x, x); cout << x << endl; return 0; } (a) Compile-time error (b) 2 (c) 0 (d) wth (e) 1 (f) Run-time error (g) None of the above

(b) 2

When should you use string::npos? (a) When you need a very large integer. (b) To indicate that you want to refer to past the end of a string. (c) When every you need the largest positive value an int can hold. (d) It is needed to convert strings to ints. (e) None of the above.

(b) To indicate that you want to refer to past the end of a string.

Which of the following Unix command line commands will copy a file? (a) mv (b) cp (c) cat (d) c (e) copy file (f) copy (g) cp file

(b) cp

What is the output of this program? void func (int *b) { *b = 1; } int main () { int *a; int n; a = &n; *a = 0; func(a); cout << *a << endl; (a) The address of a (b) Run-time error (c) 1 (d) The address of b (e) The address of n (f) Compile-time error (g) 0 (h) None of the above

(c) 1

What is a recursive function? (a) A function that doesn't use loops. (b) An iterable function. (c) A function that calls itself. (d) A function that returns the same value, if called with the same arguments. (e) A function that can be used with different number of arguments. (f) None of the above

(c) A function that calls itself.

At the command line, the Unix command "cd .." does what? (a) Copies the contents of the current working directory into the child's directory (b) Copies all of the children of the current directory into the cd directory (c) Changes the current working directory to be the parent directory. (d) Changes the permissions of the current directory (e) Prints the working directory

(c) Changes the current working directory to be the parent directory.

Can two functions have the same name? (a) Yes, but only if they have a different number of parameters. (b) No, only methods and operators can have the same names, not functions. (c) Yes, but only if they have different types and/or number of parameters. (d) Yes, but only if they have different names of parameters. (e) No, C++ doesn't support function overloading. (f) None of the above

(c) Yes, but only if they have different types and/or number of parameters.

Which of the follow is FALSE about C++ streams? (a) The clear method returns the stream to a good state, but leaves the buffer unchanged. (b) The cin stream has a eof (end-of-file) method. (c) fstreams must be constructed with a file path. (d) std::skipws works on cin and ifstream. (e) Flushing the buffer forces the buffer to be emptied prior to moving to the next action. (f) endl both inserts a newline character and flushes the buffer

(c) fstreams must be constructed with a file path.

Which of the following are true about templated functions? (a) It is itself not a function, but a way to create a function (b) It contains the keyword template (c) It makes use of a template parameter to represent a calling type. (d) All of the above (e) None of the above

(d) All of the above

Which of the following is a NOT good reason to use the unsigned integer type in C++? (a) Because a function returns it. (b) Because you will need to perform bitwise operations. (c) Because you need the expected overflow/underflow behaviour. (d) Because a value can never be negative. (e) All of the above are valid (f) None of the above are valid

(d) Because a value can never be negative.

When will the string's length and size methods return different results? (a) When the string is empty (b) When the string hold UTF8 characters (c) When the string has been passed by reference to a function (d) Never, they always are the same (e) When the string has been initialized (f) When the string has been appended to (g) When the strings capacity is different from its size

(d) Never, they always are the same

When should you use x.at(1) instead of x[1] to access the second character of the string x? (a) When you need the fastest perfor- mance. (b) When you need to be able to assigned to the resulting rvalue. (c) They are the same, so either is always fine. (d) When you need an error if x is shorter than two characters. (e) None of the above.

(d) When you need an error if x is shorter than two characters.

Does C++ support default values for function arguments? (a) No, all parameters must be matched to arguments. (b) Yes, but they must be specified in the implementation file. (c) No, but operator overloading can achieve the same effect. (d) Yes, but they must be specified in the header file.

(d) Yes, but they must be specified in the header file.

What is the output of this program? int a = 9; int *aptr = &a; a++; aptr++; std::cout << a; (a) Run-time error (b) 12 (c) 9 (d) Compile-time error (e) 10 (f) 11 (g) None of the above

(e) 10

Which of the following operators have side effects? (a) The postfix increment operator. (b) The extraction operator. (c) The assignment operator. (d) The insertion operator. (e) All of the above.

(e) All of the above.

Which of the following are NOT integral (integer-like) literals? (a) 0x34a (b) '3' (c) 12 (d) 0 (e) 054 (f) All of the above are integral types

(f) All of the above are integral types

When must a vector's capacity be greater than its size? (a) Never, the size is always greater-than or equal to the capacity. (b) Multiple of the above will must result in a capacity greater than its size. (c) After a call to push back. (d) After a call of the append method. (e) When the default constructor is invoked. (f) When the reserve method has been called with a value greater than its size.

(f) When the reserve method has been called with a value greater than its size.

For the range-based for loop below, what type can xs be? for (auto x : xs) ... (a) vector<int> (b) vector<vector<char>> (c) string (d) int (e) All of the above (f) (a) and (b) (g) (a) and (c) (h) (a), (b) and (c)

(h) (a), (b) and (c)

What will be the output of the following code? void foo(int *p) { cout << *p << endl; } int main() { int i = 10, *p = &i; foo(++p); } (a) 11 (b) Compile time error (c) 10 (d) Some garbage value or error

Correct answers: (d)

If you expect a function your code will call may raise an exception, which keywords are needed to handle the exception? (a) try (b) finally (c) except (d) throw (e) catch (f) None of the above. (g) 2 of the above. (h) 3 of the above. (i) All of the above.

(g) 2 of the above. (try and catch)

Which of the following types is largest? (a) bool (b) char (c) short (d) int (e) long (f) long long (g) Multiple types are tied for largest. (h) Depends on compiler/OS.

(h) Depends on compiler/OS.

What type can x be if x->begin() is not a type error? (a) A pointer to a vector<long>. (b) A pointer to a set<long>. (c) A iterator of a vector<vector<long>>. (d) A iterator of a set<set<long>>. (e) (a) and (b) (f) (a) and (c) (g) (c) and (d) (h) (b) and (d) (i) All of the above. (j) None of the above.

(i) All of the above.

What will be the output of the following code? void foo(int *p) { cout << *p << endl; (*p)++; } int main() { int i = 10; foo(&i); (a) 10 (b) Some garbage value (c) Run time error (d) 11 (e) Compile time error (f) Segmentation fault/code crash

(a) 10

Why is it important to flush a stream? (a) Because it forces the program to wait until the buffer is cleared. (b) Because it aids in debugging by combining cerr with cout. (c) Because without flushing, no output is shown. (d) Because it ensures the stream is open and able to accept data. (e) None of the above.

(a) Because it forces the program to wait until the buffer is cleared.

Where should comments explaining how to use a function be located? (a) Immediately before the function declaration. (b) At the top of the implementation file. (c) Immediately before the function implementation. (d) At the top of the header file. (e) On the same line as complicated code. (f) None of the above.

(a) Immediately before the function declaration.

What does it mean to have an overloaded operator? (a) It means the operator can be used with multiple types. (b) It means the operator is heavily used. (c) It means the operator's behaviour is undefined under certain circumstances. (d) It means the operator is used by the iostream library to output values. (e) None of the above.

(a) It means the operator can be used with multiple types.

What is printed by the following code? int x=3; if (!x) if(x=4) cout << "AAA"; else cout << "BBB"; (a) No output generated (b) AAA (c) BBBAAA (d) Compile-time error (e) BBB (f) AAABBB (g) None of the above

(a) No output generated

What type of iterator is an array's name? (a) Random Access Iterator (b) Bi-directional Iterator (c) Forward Iterator (d) All of the above (e) None of the above

(a) Random Access Iterator

Where are local variables are allocated? (a) Stack (b) Free memory (c) Heap (d) Permanent storage area

(a) Stack

Which of the following types provided by the STL are not templated? (a) cout (b) unordered set (c) multimap (d) vector (e) All of the above. (f) None of the above

(a) cout

Which of the following algorithms is commonly used to output a data structure to an ostream? (a) std::copy (b) std::generate (c) std::for each (d) std::swap (e) std::print all (f) None of the above.

(a) std::copy

Which of the following is NOT a legal infinite loop? (a) while (True) {...} (b) for (;;) {...} (c) while (7) {...} (d) for (;-1;) {...} (e) All of the above are legal infinite loops

(a) while (True) {...}

What does the set named c contain after the following code? set<int> a = {1, 1, 6, 3, 4}; vector<int> b = {2, 3, 4, 5}; set<int> c = {8, 9}; set difference(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.end())); (a) {1, 6,8, 9} (b) {2, 5,8, 9} (c) {3, 4,8, 9} (d) {1, 1,6, 8, 9} (e) {1, 1,2,3,3, 4, 4, 5, 6, 8, (f) {8, 9} (g) None of the above (it won't compile)

(a) {1, 6,8, 9}

What is the result of static cast<long>(10.7)? (a) 11 (b) 10 (c) 10.7 (d) Depends on if the long is signed.

(b) 10

What will be the output of the following code? void foo(int *p) { cout << *p << endl; } int main() { int i = 10, *p = &i; foo(p++); } (a) Segmentation fault/code crash (b) 10 (c) Some garbage value (d) 11 (e) Compile time error (f) Run time error

(b) 10

When does the Google Style Guide NOT recommend having a trailing underscore after a data member name? (a) For data members that don't have accessors (b) For data members of structs (c) For data members that are const (d) For private data members (e) It always recommends the underscore

(b) For data members of structs

What is the output of this program? void func(int &a, int &b) { int c = a; a = b; b = c; cout << "In func " << a << b; } int main() { int a = 5, b = 10; func(a, b); std::cout << " In main " << a << b; return 0; } (a) In func 510 In main 510 (b) In func 105 In main 105 (c) Run-time error (d) In func 105 In main 510 (e) Compile-time error (f) In func 510 In main 105 (g) None of the above

(b) In func 105 In main 105

What happens if you use the address-of (&) operator on an iterator? (a) Undefined behaviour (it may crash your program). (b) It returns the address of the iterator in memory. (c) Syntax error, you can't use & on an iterator. (d) It returns the address of the object pointed at. (e) None of the above.

(b) It returns the address of the iterator in memory.

What is wrong with the following code? vector<int> v = {1, 2, 3} while (v.size()) { cout << *v.begin() << endl; v.pop back(); } (a) It will raise an exception because push back will be called on an empty vector. (b) It will print 1 three times because it is popping from the end, not the beginning. (c) It is an infinite loop because size can never be negative. (d) It will generate a syntax error because *v.begin() you can't dereference a vector.

(b) It will print 1 three times because it is popping from the end, not the beginning.

In the lab where you created a SingleLink list, would a out of range exception need to be raised for a negative indices? (a) No, because no test case had negative indices. (b) No, because negative indices are impossible. (c) Yes, because a negative index would result in a memory leak. (d) Yes, because a negative index would access a memory position prior to the first element. (e) No, because negative indices loop around like in Python. (f) None of the above.

(b) No, because negative indices are impossible.

Which of the following causes memory leaks? (a) Copying pointers and references. (b) Not delete'ing new'ed memory. (c) Accessing uninitialized memory. (d) Allowing variables to fall out of scope. (e) None of the above.

(b) Not delete'ing new'ed memory.

What is the name of the :: operator? (a) The Within Operator (b) The Scope Resolution Operator (c) The Namespace Operator (d) None of the above

(b) The Scope Resolution Operator

Which of the following operators are right associative? (a) The postfix increment operator. (b) The assignment operator. (c) The extraction operator. (d) The insertion operator. (e) None of the above.

(b) The assignment operator.

When is an array degraded? (a) When the scope changes. (b) When it is passed to a function. (c) When it is used in a loop. (d) When the array is subscripted. (e) All of the above.

(b) When it is passed to a function.

Why is self-assignment (i.e. x = x; a concern for types with a non-synthetic operator=? (a) It isn't a concern, as self-assignment doesn't have any effect. (b) Without care, self-assignment could result in unnecessary work being per- formed. (c) Self-assignment is a syntax error, so classes need not be concerned by it. (d) Self-assignment is illegal, and all classes must an raise exception if de- tected.

(b) Without care, self-assignment could result in unnecessary work being per- formed.

Is '9' + 1; legal? (a) Yes, because both operands are digits. (b) Yes, because both operands are integral types. (c) No, because you can't add a char to an int. (d) No, because addition is not an expression. (e) No, because you can't add a letter to a digit.

(b) Yes, because both operands are integral types.

For loops have three parts (here named a,b,c,d in for (a; b; c) {d}.For a loop that iterates twice over its body, what is the order of execution of a, b, c and d? (a) a→b→d→c→b→d (b) a → b → d → c → b → d → c → b (c) a → b → d → c → b → d → c → b →d (d) a→b→d→c→b→d→c→b →d→c (e) a→b→c→d→a→b→c→d (f) None of the above

(b) a → b → d → c → b → d → c → b

What is the result of (cin >> x)? (a) >> (b) cin (c) x (d) Depends on if the insertion was successful.

(b) cin

Which of the following describe allocating new memory of type long and of size size? (a) new long *lptr = long[size]; (b) long *lptr = new long[size]; (c) long lptr = new *long[size]; (d) new long lptr = *long[size]; (e) long *lptr = new *long[size];

(b) long *lptr = new long[size];

What will happen in this code? int a = 100, b = 200; int *p = &a, *q = &b; p = q; (a) a is assigned to b (b) p now points to b (c) Run-time error (d) a and b have been swapped (e) b is assigned to a (f) q now points to a (g) Compile-time error (h) p now points to a (i) None of the above

(b) p now points to b

Which of the following namespace techniques is disallowed by this course? (a) using std::cout; cout << "Hi"; (b) using namespace std; cout << "Hi"; (c) std::cout << "Hi"; (d) None of the above.

(b) using namespace std; cout << "Hi";

What is the result of 3 / 4.0? (a) 1 (b) 0 (c) 0.75 (d) None of the above

(c) 0.75

Given following Circular Buffer: Array Contents: 4, 3, 1, 5, 2 Head: 3; Tail: 0 What is new configuration after a pop front and a push back 10? (a) Invalid (b) 10,3,1,0,2 (c) 10,3,1,5,2 (d) 4, 3, 1, 5, 10

(c) 10,3,1,5,2

What will be the output of the following code? void foo(int **p) { *p = new int(2); cout << **p << ' '; } int main() { int i = 97, *p = &i; foo(&p); cout << *p << endl; } (a) Segmentation fault/code crash (b) Compile time error (c) 2 2 (d) Run time error (e) 2 97

(c) 2 2

What will be the output of the following code? void foo(int *p) { p = new int(2); cout << *p << ' '; } int main() { int i = 97, *p = &i; foo(&i); cout << *p << endl; } (a) Run time error (b) Segmentation fault/code crash (c) 2 97 (d) 2 2 (e) Compile time error

(c) 2 97

How many different kinds of comments are there in C++? (a) There are no comments in C++ (b) 1 kind (c) 2 kinds (d) 3 kinds

(c) 2 kinds

What does the following code output? map<int, string> id to name = { {10, "Josh"},{20, "Emily"},}; id to name.insert({30, "Abdol"}); string x = id to name[40]; cout << id to name.size() << endl; (a) 2 (b) 3 (c) 4 (d) None of the above (it won't compile)

(c) 4

Which is the result of (true + 4)? (a) 4 (b) Undefined (c) 5 (d) Syntax Error (e) None of the above.

(c) 5

What line number is causing the syntax error in: 02.1-numericTypes.cpp:8:39: error: expected ')' (a) 39 (b) 2 (c) 8 (d) 1

(c) 8

Which of the following is the Insertion Operator ? (a) >> (b) += (c) << (d) :: (e) None of the above

(c) <<

What is x in this declaration?: const string * x; (a) A constant pointer to a string (b) A pointer to a string (c) A pointer to a constant string (d) A constant pointer to a constant string (e) Syntax Error (f) None of the above

(c) A pointer to a constant string

In the lab where you created your own vector class in the student namespace. What affect would changing the object returned by the member function front have? (a) Changes to this object are impossible as front is a const function member. (b) Changes to this object would not alter the vector it belongs to. (c) Changes to this object would alter the vector it belongs to. (d) Changes to this object are impossible as front returns a const object. (e) None of the above.

(c) Changes to this object would alter the vector it belongs to.

If you declare a string, what is its initial value? (a) 0 (b) false (c) Empty (d) Undefined (e) None of the above.

(c) Empty

Can you declare a reference? (a) Yes (b) Depends on if the reference is const (c) No (d) Depends on if the reference is for a fundamental type

(c) No

Is the variable name milesPerHour allowed? (a) No, it is an illegal name. (b) Yes, it is legal and correctly styled. (c) No, it violates the style guide. (d) No, variable names shouldn't include full words. (e) None of the above are true.

(c) No, it violates the style guide.

What will be the output of the following code? int main() { int a[3] = {1, 2, 3}; int *p = a; cout << p << '-' <<a; } (a) Compile time error (b) undefined (c) Same address is printed twice (d) Run time error (e) Two different addresses are printed

(c) Same address is printed twice

In order to use the assert function, you need to include the cassert header. What does the c in the name mean? (a) That the function requires a compiler pre-processor. (b) That the function runs at compile time. (c) That the header is from the C language. (d) None of the above.

(c) That the header is from the C language.

What does the "[]" of a "delete[] x;" indicate? (a) That x is a data structure. (b) That x is subscriptable. (c) That x is an array. (d) None of the above.

(c) That x is an array.

What is NOT a benefit of statically typed languages (like C++)? (a) The programmer is explicit about the type of each variable (b) The compiler can check for type correctness (c) The code is more flexible (d) It allows the code to be optimized for speed (e) None of the above

(c) The code is more flexible

What is wrong with the following line? string x = 'c'; (a) There is a syntax error. (b) There is a name error. (c) There is a type error. (d) Strings cannot be initialized. (e) Nothing is wrong.

(c) There is a type error.

Why is it important to frequently compile and test your code? (a) To ensure that your work is being saved (b) To enable the compiler to optimize your code (c) To reduce the time between adding a bug and finding it (d) To prevent compiler errors from becoming runtime errors

(c) To reduce the time between adding a bug and finding it

If you declare a int, what is its initial value? (a) 0 (b) false (c) Undefined (d) Empty (e) None of the above.

(c) Undefined

What does the following code output: vector<int> xs = {1, 2, 3, 4}; for (auto x : xs) { xs.push back(5); cout << xs.size() << endl; (a) 5 (b) Syntax error (c) Undefined behavior (d) 4 (e) 9

(c) Undefined behavior

Can std::sort be used to sort an array of floats? (a) No, sort requires random access iterators. (b) No, sort can only be performed on STL containers. (c) Yes, you just need a pointer to the start and one-past-the-end of the array. (d) It depends on the compiler.

(c) Yes, you just need a pointer to the start and one-past-the-end of the array.

Assuming s is a string, which of the following statements results in an infinite loop ? (a) for(auto i=s.size()-1; i>0; i--) (b) for(auto i=0; i<s.size(); i++) (c) for(auto i=s.size()-1; i>=0; i--) (d) for(auto i=0; i<s.size()-1; i++) (e) All of the above (f) None of the above

(c) for(auto i=s.size()-1; i>=0; i--)

The correct statement for a function declaration that takes a const pointer to a string that needs to be revised (in the body of this function, the pointer will be be used to modify its pointed at string, but cannot be pointed at a differnt object), a pointer to an int, and returns a reference to an int is: (a) & int fun(const * string const, * int) (b) int & fun(const string * const, int *) (c) int & fun(string * const, int *) (d) & int fun(const * string, * int) (e) int & fun(const string *, int *) (f) Multiple answers are correct

(c) int & fun(string * const, int *)

Which of the following is illegal? (a) int *ip; (b) string s, *sp = 0; (c) int i; double* dp = &i; (d) int *pi = 0; (e) All of the above (f) None of the above

(c) int i; double* dp = &i;

When is an object's destructor called? (a) When the block of that local variable ends. (b) When the delete operator is called on that object. (c) When that object is assigned to. (d) (a) and (b) (e) (a) and (c) (f) (b) and (c) (g) All of the above. (h) None of the above.

(d) (a) and (b)

I created a new class, named SafeInt that acts much like an int, but raises exceptions for certain unsafe operations (like division by zero). I've implemented all the needed operators (like operator/), but I want to be able to combine ints and SafeInts. Which functions need to be implemented so that code like the following will work? SafeInt x = SafeInt(2) / 3; (a) SafeInt::SafeInt(const int &) (b) SafeInt SafeInt::operator/(const int&) (c) SafeInt to SafeInt(const int &) (d) (a) or (b) will work. (e) (a) or (c) will work. (f) (b) or (c) will work. (g) All of the above will work. (h) None of the above.

(d) (a) or (b) will work.

How many different instances (copies) of vector are generated by the following code? bool has_1(vector<int> input) { return find( input.begin(), input.end(), 1) != input.end(); } int main() { vector<int> v = {1, 2, 3}; vector<int> v2 = v; cout << has 1(v) << endl; } (a) 6 (b) 5 (c) 4 (d) 3 (e) 2 (f) 1

(d) 3

What is the output from the following code: int x = 3; do cout << x; while (x--); (a) No output generated (b) 3210-1 (c) 3333 (d) 3210 (e) Compile-time error (f) 33333 (g) 333 (h) 321 (i) None of the above

(d) 3210

Why are copies generally bad? (a) Because copying takes additional time. (b) Because they limit the ability to modify the original variable. (c) Because they require more memory. (d) All of the above.

(d) All of the above.

Why is a full namespace merge a bad idea? (a) Because it slows down programmer productivity by requiring more inventive names (b) Because it makes compilation slower (c) Because it forces the compilation of the entire STL (d) Because it makes it difficult to determine where a name came from. (e) All of the above.

(d) Because it makes it difficult to determine where a name came from.

Why did we teach arrays in CSE 232? (a) Because modern C++ makes heavy use of them. (b) Because they are an essential data structure. (c) Because they are necessary to use pointers. (d) Because they represent the simplest compound data structure. (e) None of the above.

(d) Because they represent the simplest compound data structure.

How does the two parameter version of stol indicate that it was unable to process a string in its entirety? (a) By throwing an exception. (b) By returning a sentinel value. (c) By returning an iterator and a bool, where the bool indicates success. (d) By adjusting a value pointed at by its second argument. (e) None of the above.

(d) By adjusting a value pointed at by its second argument.

What is NOT included when initializing a variable? (a) The variable's value (b) The variable's name (c) The variable's type (d) None of the above

(d) None of the above

What will change if struct is changed to class in the following? struct Test { double gpa; public: string name; private: string comments; } (a) The data member gpa will be made private. (b) The data member gpa will be made public. (c) All three data members will become private. (d) Nothing, it still won't compile because of a missing semicolon. (e) All three data members will become public.

(d) Nothing, it still won't compile because of a missing semicolon.

What is the name of the ampersand (&) operator? (a) The dereference operator (b) The pointer operator (c) The and operator (d) The address-of operator (e) None of the above

(d) The address-of operator

When is a pointer better than a reference? (a) When you need many names for the same variable. (b) When a copy needs to be avoided. (c) When you are writing C++17 code. (d) When you need to do pointer arithmetic. (e) None of the above.

(d) When you need to do pointer arithmetic

Which of the following are user-defined datatypes? (a) invalid (b) typedef (c) enum (d) struct (e) All of the above

(d) struct

If a custom destructor is necessary, what other functions may also need to be customized? (a) Assignment operator (b) Default constructor (c) Copy constructor (d) (a) and (b) (e) (a) and (c) (f) (b) and (c) (g) All of the above. (h) None of the above.

(e) (a) and (c)

What will be the output of the following code? int main() { int a[3] = {1, 2, 3}; int *p = a; cout << *(a+1) << '-' << p[1]; } (a) Compile time error (b) Run time error (c) Different addresses are printed (d) 1-1 (e) 2-2

(e) 2-2

Which is x after int y=8; int x=y++;? (a) 9 (b) 7 (c) Undefined (d) y (e) 8 (f) None of the above.

(e) 8

Which of the following is NOT a preprocessor statement? (a) #ifndef SOME NAME (b) #define SOME NAME (c) #pragma once (d) #include <iostream> (e) All of the above are preprocessor statements

(e) All of the above are preprocessor statements

Which of the following ways to indicate an error state was not demonstrated in this class? (a) Returning a bool that indicates success. (b) Returning a sentinel value. (c) Returning a iterator which points at a data structure's end. (d) Throwing an exception. (e) All of the above were taught.

(e) All of the above were taught.

Which of the following are good reasons to use a vector instead of an array? (a) Vectors can grow at runtime to be any needed size. (b) Vectors provide safer methods for access. (c) Vectors provide useful methods for manipulation. (d) Vectors have similar performance to arrays. (e) All of the above. (f) All of the except for one of the options.

(e) All of the above.

How large is an int? (a) 1 byte (b) 2 bytes (c) 4 bytes (d) 8 bytes (e) Depends on the system

(e) Depends on the system

Choose the statement which is FALSE with respect to dynamic memory allocation. (a) Allocated memory can be changed during the run time of the program based on the requirement of the program (b) Memory is allocated in a less structured area of memory, known as heap (c) None of the above (d) Used for unpredictable memory requirements (e) Dynamically allocated memory is automatically deleted when it falls out of scope.

(e) Dynamically allocated memory is automatically deleted when it falls out of scope.

What special privileges does a friend function have over a class? (a) It can alter const objects. (b) It can call deleted functions. (c) It can define the operator<<. (d) It can make public members private. (e) None of the above.

(e) None of the above.

When can you use a catch block without a try block? (a) When you want the code in the block to always run. (b) When the statement immediately before it may raise an exception. (c) When it is unclear where an exception can be thrown. (d) When any exception can be caught. (e) None of the above.

(e) None of the above.

What does marking a function member const indicate? (a) That it can be invoked on const objects. (b) That it doesn't change the object it belongs to. (c) That it returns a const object. (d) All of the above. (e) Only (a) and (b). (f) Only (a) and (c). (g) Only (b) and (c). (h) None of the above.

(e) Only (a) and (b).

Which option describes passing a single- dimensional array (of ints) as an argument in a function? (a) int sum(int ary[], size t size) (b) int sum(int *ary, size t size) (c) int sum(int & ary[size]) (d) All of the above (e) Two of the (a), (b), and (c)

(e) Two of the (a), (b), and (c)

Is the following legal? template <typename T> class Node { public: T data ; private: Node<T> * next ; }; (a) No, a class must have function members. (b) No, it has a syntax error. (c) No, a type cannot refer to itself. (d) No, a data member cannot be a pointer. (e) Yes, it is legal.

(e) Yes, it is legal.

What does this declaration declare? string* x, y, z; (a) x and y are pointers to a string, z is a string (b) x, y and z are pointers to string types (c) Run-time Error (d) Compile-time Error (e) x is a pointer to a string, y is a string, z is a string (f) None of the above

(e) x is a pointer to a string, y is a string, z is a string

Which of the following iterators can be legally derefrenced for a vector named x? (a) x.begin() (b) x.cbegin() (c) x.rbegin() (d) x.crend() (e) (a) and (b) (f) (a), (b) and (c) (g) All of the above. (h) None of the above.

(f) (a), (b) and (c)

Assuming 32-bit ints, how many bytes of memory does the following code leak? for (int i = 0; i <= 3; ++i) { int * ptr = new int[i]; } (a) 0 (b) 3 (c) 6 (d) 9 (e) 12 (f) 24 (g) 48 (h) 72

(f) 24

A pointer to an element in an array has the properties of which type of iterator? (a) Forward iterator (b) Random access iterator (c) Output iterator (d) Input iterator (e) Bi-directional iterator (f) All of the above

(f) All of the above

Which of the following can be allocated dynamically? (a) set (b) array (c) iterator (d) vector (e) map (f) All of the above

(f) All of the above

In the lab where you created a SingleLink list, what are the cases that the "del" function member must account for? (a) If the argument is the first element in the list. (b) If list is empty. (c) If the argument is the last element in the list. (d) If the argument isn't present in the list. (e) If the argument is in the list (but isn't the first or last element). (f) All of the above.

(f) All of the above.

Which of the following tasks benefit from using the sstream library? (a) Converting a string to a long. (b) Converting a long to a string. (c) Concatenating multiple strings together. (d) Parsing text into its components. (e) Formatting lines prior to output. (f) All of the above. (g) All of the above except (b).

(f) All of the above.

Which of the following cause a string to be copied (presuming y is a string)? (a) const string * x = y; (b) const string * const x = y; (c) string & x = y; (d) string * x = y; (e) const string & x = y; (f) None of the above

(f) None of the above

What operations can't be performed on a reverse iterator, but can with a forward iterator? (a) operator++ (b) operator-- (c) operator== (d) operator* (e) All of the above. (f) None of the above.

(f) None of the above.

Which of the functions must be invoked by the following code? Type x; x = y; (a) Type's default constructor (b) Type's assignment operator (c) Type's copy constructor (d) Type's destructor (e) Type's conversion constructor (f) None of (a-e). (g) 2 of (a-e). (h) 3 of (a-e). (i) 4 of (a-e). (j) All of (a-e).

(g) 2 of (a-e).


Related study sets

ATI Chapter 32 Medications Affecting Labor and Delivery

View Set

Clinical Genetics Exam 2 PP 4 (Population Genetics)

View Set

American Revolution Unit 2 US History

View Set

TExES Special Education 161 (Review Set)

View Set

MKTG 3104 Connect Quiz Questions

View Set

Securities Industry Essentials (SIE) Exam

View Set