ICS 45C Final Study Guide

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

To specify a value of type char in C++, what set of symbols must be used? ' ' or " " ' ' It depends on the implementation. It depends on whether iostream.h was #included " " None of the other choices

' '

What does Line 14 output? (Refer to Picture 9 in Google Docs) *px is [Memory Address] *px is 42 N/A - Compilation Error *px is 7 None of the other choices

*px is 42

SELECT ALL the statements that are true. In general, what are some good principles to live by when coding your first program? - Programs are done after they generate correct output because correct output is all that matters. - You have to write a program once to know how you should have written it the first time. - Write as much of the program as possible all at once, because many pieces interact. - The first way of coding a solution is almost never the best solution. - Adding one piece at a time makes it easier to interpret compiler error messages. - It is best to copy/paste code when you are learning C++, so you minimize typos.

- You have to write a program once to know how you should have written it the first time. - The first way of coding a solution is almost never the best solution. - Adding one piece at a time makes it easier to interpret compiler error messages.

What is the output of the following C++ program? #include <iostream> using namespace std; int main() { for (int i=0; i<10; ++i) cout << i; cout << " "; } 1 2 3 4 5 6 7 8 9 [With a space at the end] None of the other choices 0123456789 [With a space at the end] 0123456789 [Without a space at the end] 0 1 2 3 4 5 6 7 8 9 10 [With a space at the end] 0 1 2 3 4 5 6 7 8 9 [With a space at the end] 123456789 [With a space at the end]

0123456789 [With a space at the end]

What is the output of the following C++ code? #include <iostream> using namespace std; int main(){ int aNumber = 9; aNumber = aNumber + 1; bool aBool = true; aBool = 9; cout << aBool << endl; return 0; } true None of the other choices 0 1 N/A - Runtime Error N/A - Compilation Error 9 false

1

What is the output of the program below? (Refer to Picture 2 in Google Docs) A5B4.3CR~CR~B4.3~A5 ABC54.3R~CR~B4.3~A5 CBA54.3R~CR~B4.3~A5 A5B4.3CR None of the other choices

A5B4.3CR~CR~B4.3~A5

What is the output of Line 8? (Refer to Picture 24 in Google Docs) N/A - Compilation Error Alex Alex0a0000 Alexa None of the other choices

Alex

List the entire lifetime of an object in C++, in order. Construction, Allocation, Lives, Deallocation, Deconstruction Construction, Allocation, Lives, Deallocation, Destruction Allocation, Construction, Lives, Deconstruction, Deallocation Allocation, Construction, Lives, Destruction, Deallocation Construction, Lives, Destruction

Allocation, Construction, Lives, Destruction, Deallocation

What is the primary reason you would convert a function to be a template? Improve readability Improve performance None of the other choices Allow the function to work with different data types

Allow the function to work with different data types

What is a lambda in C++? A built-in data type for storing character sequences A function that takes no arguments and returns no value None of the other choices An anonymous function object that can be defined inline

An anonymous function object that can be defined inline

Which type of container does NOT work with range-based for-loops? C-style fixed-arrays std::vector Dynamic C-style arrays None of the other choices std::array

Dynamic C-style arrays

SELECT ALL the statements which are true. Iterators do not need to know how a class is implemented. When a searching algorithm doesn't find what it was looking for, false is returned. Each iterable container class must have an iterator defined specifically for it. std::sort() works for all container classes.

Each iterable container class must have an iterator defined specifically for it.

What is the key insight regarding list initialization and matching constructors? List initialization prefers list constructors over non-list constructors. List initialization prefers non-list constructors over list constructors List initialization is not recommended for class objects List initialization can only be used with built-in types None of the other choices

List initialization prefers list constructors over non-list constructors.

Consider the following code fragment that traverses a singly-linked list pointed to by the variable head. SELECT ALL of the statements that would apply if we directly used head instead of creating a temporary p to traverse the linked list. (Refer to Picture 4 in Google Docs) Memory leak It will work just fine Segfault We lose the head of our list.

Memory leak We lose the head of our list.

SELECT ALL of the statements that apply when using recursion to write a function: Must make progress toward the base case. Must use a conditional. Must use a loop. Must have at least one base case where there is no recursive call. Must have at least one recursive case where the function calls itself.

Must make progress toward the base case. Must use a conditional. Must have at least one base case where there is no recursive call. Must have at least one recursive case where the function calls itself.

What happens if you try to call a lambda without capturing any variables? The lambda is guaranteed to not compile. The compiler is guaranteed to generate a warning message. None of the other choices

None of the other choices

SELECT ALL the code fragments that result in memory leaks. (Refer to Picture 10 in Google Docs) (Refer to Picture 11 in Google Docs) (Refer to Picture 12 in Google Docs)

Picture 10 Picture 11

Consider the following code: 1 #include <iostream> 2 using namespace std; 3 4 int main( ) { 5 int courseCode = 35670; 6 int *p = &courseCode; 7 8 while (p != nullptr) { 9 cout << "Pointer p points to " << p << endl; 10 p = nullptr; 11 } 12 cout << "Pointer p points to " << p << endl; 13 } What is the output of Line 12? - Pointer p points to 0 - Pointer p points to [memory address] - N/A - This program segfaults - Pointer p points to courseCode - Pointer p points to 35670 - None of the other choices

Pointer p points to 0

Consider the following code: 1 #include <iostream> 2 using namespace std; 3 4 int main( ) { 5 int courseCode = 35670; 6 int *p = &courseCode; 7 8 while (p != nullptr) { 9 cout << "Pointer p points to " << p << endl; 10 p = nullptr; 11 } 12 cout << "Pointer p points to " << p << endl; 13 } What is the output of Line 9? None of the other choices Pointer p points to 35670 Pointer p points to 0 Pointer p points to [memory address] N/A - This program segfaults Pointer p points to courseCode

Pointer p points to [memory address]

Which operation can invalidate iterators in a container? Accessing an element with indirection through the iterator. Resizing the container to increase its capacity. Modifying the value of an element. Reassigning the iterator to a different container. None of the other choices

Resizing the container to increase its capacity.

SELECT ALL the statements that are true: Sequence containers maintain the order of elements. Sequence containers allow you to choose the position where to insert an element. Inserting elements into an array can be done without having to maintain their order. Arrays are sequence containers. Doubly-linked lists are sequence containers.

Sequence containers maintain the order of elements. Sequence containers allow you to choose the position where to insert an element. Arrays are sequence containers. Doubly-linked lists are sequence containers.

SELECT ALL that are true of the following code: (Refer to Picture 21 in Google Docs) The compiler keeps track of whether a const variable is a runtime or compile-time constant. b is a compile time constant. a is a compile-time constant.

The compiler keeps track of whether a const variable is a runtime or compile-time constant. a is a compile-time constant.

What happens if you try to call a function template without instantiating it with specific template arguments? Unpredictable result Default template arguments will be used None of the other choices The compiler will try to instantiate the template using the types of the arguments. If this cannot be done, the code will not compile.

The compiler will try to instantiate the template using the types of the arguments. If this cannot be done, the code will not compile.

Generally, if a searching algorithm doesn't find the value it was looking for, it returns: 0 The begin iterator The end iterator -1 nullptr false It depends on the chosen parameter None of the other choices

The end iterator

What happens when an exception occurs within the function or a try block? The program is guaranteed to terminate immediately. The exception is guaranteed to be handled within the same function or block. If it is not, the program terminates. None of the other choices The exception is propagated up the call stack until a matching catch block is found. If one cannot be found, the program terminates.

The exception is propagated up the call stack until a matching catch block is found. If one cannot be found, the program terminates.

Where is the most efficient locator to add a new member to a singly-linked list with only a head pointer? It does not matter. None of the other choices The tail The element just after head The head

The head

How do you capture variables by value in a lambda expression? [] [&] [=] None of the other choices

[=]

Which of the following is a valid lambda expression in C++? - {} - ()[]{} - (){[]} - [](){} - None of the other choices

[](){}

SELECT ALL the statements which are true. begin() returns an iterator that represents the first element in the container. cbegin() returns a read-only iterator that represents the first element in the container. cend() returns a read-only iterator that represents the last element in the container. end() returns an iterator that represents the last element in the container.

begin() returns an iterator that represents the first element in the container. cbegin() returns a read-only iterator that represents the first element in the container.

Which iterator is used to iterate over the elements of a map in C++ STL? bidirectional_iterator forward_iterator associative_iterator None of the other choices random_access_iterator

bidirectional_iterator

Suppose you are writing functions that each require one parameter v. v holds the address of an integer. v can be pointed to a different address within the function. The value that v refers to may be read, but not modified, within the function. What is the correct type for v ? int &v const int* v None of the other choices int* v const int* const v const int& v

const int* v

Which line should replace Line 6 to properly return A's storage to the heap? (Refer to Picture 14 in Google Docs) delete[] &A; delete A; free(A); return A; delete &A; free(&A); delete[] A;

delete[] A;

SELECT ALL the statements which are true of end(). end() points to the last element in the container. end() points to the first element in the container. end() points to a null value indicating the end of the container end() points to the element just past the last of the elements. end() helps simplify the syntax for iterating over the elements in a container

end() points to the element just past the last of the elements. end() helps simplify the syntax for iterating over the elements in a container

Which line should replace Line 5 to properly print out the values in A---each on a separate line? (Refer to Picture 26 in Google Docs) for (auto e : A) cout << e << endl; for (int e : A) cout << e << endl; for (int i=0; i<N; ++i) cout << A[i] << endl;

for (int i=0; i<N; ++i) cout << A[i] << endl;

Which of these C++ statements is necessary to distinguish between base cases and recursive cases when writing a recursive function? while if for assignment None of the other choices

if

Which of the following is NOT an array of three integers? int e[3] = {0}; int b[] = {2,6,8}; int d[6] = {2,6,8}; int c[3] = {2,6,8}; int a[3];

int d[6] = {2,6,8};

How do you initialize a dynamically allocated array with values using an initializer list? int* array = new int[5] { 5, 4, 3, 2, 1 }; int* array = new int[5] {{ 5, 4, 3, 2, 1 }}; int* array = new int[] { 5, 4, 3, 2, 1 }; int* array = new int[5] ({ 5, 4, 3, 2, 1 }); None of the other choices

int* array = new int[5] { 5, 4, 3, 2, 1 };

SELECT ALL Which of the statements in the constructor for Pub will generate a compile-time error? (Refer to Picture 6 in Google Docs) m_private = 3; m_public = 1; m_protected = 2;

m_private = 3;

What is the output of Lines 10-11? (Refer to Picture 28 in Google Docs) name has 8 characters in the array. name has 50 characters in the array. None of the other choices. name has 9 characters in the array.

name has 50 characters in the array.

What is the output of Line 9? (Refer to Picture 13 in Google Docs) name has 8 characters. name has 50 characters. None of the other choices. name has 9 characters.

name has 8 characters.

SELECT ALL Which of the statements in main() will generate a compile-time error? (Refer to Picture 3 in Google Docs) pub.m_private = 3; pub.m_protected = 2; pub.m_public = 1;

pub.m_private = 3; pub.m_protected = 2;

Which method for a singly-linked list requires two temporary pointers? remove() add() None of the other choices isEmpty() size() search()

remove()

What containers are examples of C++ STL associative containers? - set and map - string and char - vector and list - None of the other choices

set and map

SELECT ALL the statements that are true. setw is an I/O manipulator There is a different version of getline() for std::string It is harmless to overflow an input buffer array you are reading data into

setw is an I/O manipulator There is a different version of getline() for std::string

Which line of code correctly uses iterators to sort a vector of int? std::vector<int> v{11, -1, 8, 4, -7, 0, 5 }; std::sort(v.begin(), v.end()); sort(v); std::sort(v.cbegin(), v.cend()); None of the other choices v.sort();

std::sort(v.begin(), v.end());

How do you define a template class in C++? class keyword, followed by the template parameter list, then the class name typename keyword, then class name template keyword, followed by the template parameter list, then class keyword, followed by the class name None of the other choices

template keyword, followed by the template parameter list, then class keyword, followed by the class name

What is the syntax for throwing an exception of type std::string in C++? throw "error"; throw string("error"); try string("error"); catch string("error"); None of the other choices

throw string("error");

What is the syntax for a try/catch block used to catch an exception in C++? try {...} catch (...) {...} {...} catch (...) {...} catch (...) {...} try {...} None of the other choices

try {...} catch (...) {...}

What is the output of the following code? (Refer to Picture 17 in Google Docs) v = { 11, 19, 3, 7, 13, 5, }; v = { 11, 19, 5, 13, 7, 3, }; We don't know / Undefined Behavior N/A - Compilation Error None of the other choices v = { 3, 7, 13, 5, 19, 11, }; v = { 3, 7, 13, 11, };

v = { 3, 7, 13, 5, 19, 11, };

How do you capture variables by reference in a lambda expression? [] [&] [=] None of the other choices

[&]

What is the output of the program below? (Refer to Picture 18 in Google Docs) DB7DB5 DBDB5 D7B5 DB7B5 None of the other choices

DB7B5

What is the output of the program below? (Refer to Picture 8 in Google Docs) 4 8 12 10 None of the other choices 6

10

Given the following variable declarations: int X = 19; double Y = 9.0; int Z = 2; What is the value of the following expression? X / Z + Y / Z 13 13.0 13.5 14.5 14

13.5

What is the result of 5/2 in C++? 2 2.5 N/A - Runtime Error 2.50 2.0 3 None of the other choices

2

In the following program fragment, what are the values of i and result that are written to cout? int result = 15; int i = 15; for (i=1; i<=5; ++i) { result -= i; } cout << i << " " << result; 0 5 5 6 6 5 5 5 6 0

6 0

What is the (logical) length of the array name below, which will be returned by strlen(name)? char name[]{ "klefstad" }; 8 9 Can vary at different points of execution. 7 None of the other choices.

8

What should replace BLANK2 below to properly construct this Student object? (Refer to Picture 25 in Google Docs) A blank line : m_name{ name }, m_age{ age }, M_major{ major } : Person{ name, age }, m_major{ major } None of the other choices : m_major{ major } : m_major{ major }, Person{ major, age }

: Person{ name, age }, m_major{ major }

SCENARIO: Class String For Homework 4, the same functionality of class String from Homework 3 is implemented using a dynamically allocated array. The data member inside, also named buf, is now a pointer to an array of char which must be terminated with a null character. Suppose we write a helper function, strdup(const char *s), that returns an exact copy of the C-string pointed to by the parameter s by allocating an array with new[] and copying the characters from s into that new[] storage with strcpy(). What should replace Line 6 to write a proper constructor for String from a C-string s? (Refer to Picture 7 in Google Docs) { delete[] buf; buf = strdup(s.buf); s.buf = nullptr; return *this; } : buf(strdup(s.buf)) { } { buf = nullptr; s.buf = nullptr; return *this; } { buf = s.buf; s.buf = nullptr; } : buf(strdup(s)) { } { delete[] buf; buf = strdup(s); s.buf = nullptr; } { delete[] buf; buf = s.buf; s.buf = nullptr

: buf(strdup(s)) { }

SCENARIO: Class String For Homework 4, the same functionality of class String from Homework 3 is implemented using a dynamically allocated array. The data member inside, also named buf, is now a pointer to an array of char which must be terminated with a null character. Suppose we write a helper function, strdup(const char *s), that returns an exact copy of the C-string pointed to by the parameter s by allocating an array with new[] and copying the characters from s into that new[] storage with strcpy(). What should replace Line 6 to write a proper copy constructor for String from another String object s? (Refer to Picture 15 in Google Docs) { delete[] buf; buf = strdup(s.buf); s.buf = nullptr; return *this; } : buf(strdup(s.buf)) { } { buf = nullptr; s.buf = nullptr; return *this; } { buf = s.buf; s.buf = nullptr; } : buf(strdup(s)) { } { delete[] buf; buf = strdup(s); s.buf = nullptr; } { delete[] buf; buf = s.buf

: buf(strdup(s.buf)) { }

What should replace BLANK1 below to derive Student from Person? (Refer to Picture 1 in Google Doc) - : public Person - : protected Person - : private Person - : Person - public Person - None of the other choices

: public Person

What is the output of this program? #include <iostream> using namespace std; int main() { int grade = 95; if (grade < 60) { cout << 'F'; } else if (grade < 70) { cout << 'D'; } else if (grade < 80) { cout << 'C'; } else if (grade < 90) { cout << 'B'; } else cout << 'A'; } FDCBA C None of the other choices B A D

A

SELECT ALL of the following statements which are true. A C++ vector is a dynamic array that can grow at the end Inserting into the end of a vector is slow Each key in a map must be unique Sets allow duplicate elements A C++ vector has traits for magnitude and direction A deque is a dynamic array that can grow at both ends

A C++ vector is a dynamic array that can grow at the end Each key in a map must be unique A deque is a dynamic array that can grow at both ends

Assume String is a class that has only declared and defined a default constructor, copy constructor, copy assignment, and destructor. What happens in the following code? String doSomething() { String s; return String{s}; } Due to the rule of 5 / rule of 0, a compilation error will occur because the move constructor and the move assignment have not been declared and defined. A copy of s is created in the return statement. The contents of s are moved into the new String object that gets returned.

A copy of s is created in the return statement.

What is a function template in C++? A type of pointer that can be passed between functions A function that takes in multiple arguments A generic function definition that can be instantiated to operate on various specific types None of the other choices

A generic function definition that can be instantiated to operate on various specific types

What value does the following code output at line 7? 1 #include <iostream> 2 using namespace std; 3 4 int main(){ 5 int foo = 77; 7 cout << &foo << endl; 8 return 0; 9 } N/A - Compilation error 1 0 77 A memory address None of the other choices

A memory address

SELECT ALL statements which are potentially true of the following code: void doSomething() { ListNode* p = new ListNode(); // ListNode is a struct or class // do stuff with p here } Assuming "delete p" is the last line of the function, and p is not deleted at any other time, if an exception is thrown, p may still be properly deleted. Assuming "delete p" is the last line of the function, and p is not deleted at any other time, if the function returns early, p may not be deleted The programmer may forget to deallocate p. If p is not deallocated, undefined behavior will result. If p is not deallocated, a memory leak will result.

Assuming "delete p" is the last line of the function, and p is not deleted at any other time, if the function returns early, p may not be deleted The programmer may forget to deallocate p. If p is not deallocated, a memory leak will result.

What does the following code output? #include <iostream> using namespace std; class A { public: A() {cout << 'A';} }; class B { public: B() {cout << 'B';} }; class C { public: C() : a(), b() {} private: B b; A a; }; int main() { C c; } N/A - Compilation Error AB BA There is no output. None of the other choices.

BA

What is the output of the program below? (Refer to Picture 27 in Google Docs) DB~B~D BDB~B~D BDB~B~D~B None of the other choices DB~B~B~D

BDB~B~D~B

Assume we have two classes: class Node, which contains data and next, and class List, implemented as a singly linked list, which has one data member named head, which is a pointer to a Node. Which of the following statements are true? Every Node in the list is in various locations in memory, and those memory addresses are stored in an array inside of the List object, which makes accessing each Node possible. Every Node in the list is in various locations in memory, and each Node contains a pointer to the next Node in the list without needing to be contained in the List class. Every Node in the linked list is exactly one space in memory away from the next, making it possible to find the next Node and traverse through the list. Every Node is contained within the List class object, making direct access to every Node of the linked list possible. None of the other choices

Every Node in the list is in various locations in memory, and each Node contains a pointer to the next Node in the list without needing to be contained in the List class.

SELECT ALL the statements that are true. Flags are preferable to manipulators. Flags can change output. Manipulators can change output. Due to the behavior of flag groups, unexpected outcomes may occur when using flags.

Flags can change output. Manipulators can change output. Due to the behavior of flag groups, unexpected outcomes may occur when using flags.

SELECT ALL the statements that are true of const variables. const variables may be later changed through assignment. Function parameters can be made constant via the const keyword. const variables can be initialized from non-const variables. const variables must be initialized when you define them. Proper use of const is important for pass by reference and pass by address. Proper use of const is important when passing by value. A const variable is a compile-time constant if its initializer is a constant expression. Variables in the initializer list are initialized in the order that they are specified in the initializer list.

Function parameters can be made constant via the const keyword. const variables can be initialized from non-const variables. const variables must be initialized when you define them. Proper use of const is important for pass by reference and pass by address. A const variable is a compile-time constant if its initializer is a constant expression.

SELECT ALL statements that are true of the following line of code: int courseCode = 35760; It dereferences a pointer It is an initialization It declares a reference It is a declaration It declares a pointer

It is an initialization It is a declaration

Which statement about adding a list constructor to an existing class is true? It may break existing programs that rely on the previous behavior It requires modifying all existing instances of the class It always improves the behavior of existing programs It guarantees safer memory management None of the other choices

It may break existing programs that rely on the previous behavior

What is the result of the following code? (Refer to Picture 22 in Google Docs) Segfault It might print "the original!" but will have corrupted memory. None of the other choices. An exception is guaranteed to be thrown. It is guaranteed to print "the original!" The compiler will always warn you about a potential buffer overrun.

It might print "the original!" but will have corrupted memory.

What happens if the user enters more than 254 characters? (Refer to Picture 5 in Google Docs) An exception is guaranteed to be thrown. Any character past the 255th is ignored and not written into memory. The array grows to accommodate the length of the user's input. Any character past the 254th is ignored and not written into memory. It overruns the end of the array and writes into memory beyond the array, corrupting memory.

It overruns the end of the array and writes into memory beyond the array, corrupting memory.

SELECT ALL that are true of the following code: (Refer to Picture 19 in Google Docs) It prints 100\n9\n It prints 100\n100\n The n value passed into printNum cannot change within the scope of main(). Line 11 causes a compilation error. The n value passed into printNum cannot change within the scope of printNum().

It prints 100\n9\n The n value passed into printNum cannot change within the scope of printNum().

Which container classes provide their own sort() member function? - Map - Vector - List - Deque - Set

List

SELECT ALL statements that are true of the following program. Consider the following program that declares a global static array named A of type double. Select ALL statements that are true about this program. constexpr int N = 10; double A[N]; void F() { cout << A[1]; } int main() { for (int i=0; i<N; ++i) A[i] = 2.5 * i; cout << A[4]; F(); } A initially contains 10 double elements but may be reallocated to a different size. The initial values in A are unpredictable. A contains 10 double elements over its entire lifetime A can be accessed by function F. A's initial values are all 0.0. A is allocated before main() is called and deallocated when the program exits. A is allocated when main() is called and deallocated when main() returns.

The initial values in A are unpredictable. A contains 10 double elements over its entire lifetime A can be accessed by function F. A is allocated before main() is called and deallocated when the program exits.

SELECT ALL the statements that are true. Consider the following program that declares an array of type double named A inside its body. Select ALL statements that are true about this program. constexpr int N = 10; void F() { // cout << A[1]; ??? } int main() { double A[N]; for (int i=0; i<N; ++i) A[i] = 2.5 * i; cout << A[4]; F(); } A initially contains 10 double elements but may be reallocated to a different size. The initial values in A are unpredictable. A contains 10 double elements over its entire lifetime A can be accessed by function F. A's initial values are all 0.0. A is allocated before main() is called and deallocated when the program exits. A is allocated when main() is called and deallocated when main() returns.

The initial values in A are unpredictable. A contains 10 double elements over its entire lifetime A is allocated when main() is called and deallocated when main() returns.

What does it mean to capture external variables by reference in a lambda expression? The lambda function creates copies of the captured variables The lambda function captures the addresses of the variables None of the other choices The lambda function has references to the original external variables with the same name

The lambda function has references to the original external variables with the same name

SELECT ALL the statements that are true. The new operator requests memory to be allocated by the compiler. Static and automatic memory allocation are done in the heap. The new operator creates the object using heap memory. The new operator returns a pointer to the address of the memory that has been allocated. The heap area of memory is quite small.

The new operator creates the object using heap memory. The new operator returns a pointer to the address of the memory that has been allocated.

SELECT ALL the following statements that are true. The noexcept keyword is used to promise not to throw exceptions visible to the caller noexcept prevents the function from throwing exceptions or calling other functions that potentially throw exceptions It's up to the programmer to make sure that exceptions are not thrown by functions that should not throw them If an unhandled exception would exit a noexcept function, std::terminate will be called void noexcept doSomething() is valid syntax to define a non-throwing function

The noexcept keyword is used to promise not to throw exceptions visible to the caller It's up to the programmer to make sure that exceptions are not thrown by functions that should not throw them If an unhandled exception would exit a noexcept function, std::terminate will be called

What is instantiation in the context of C++ template classes? The process of creating a concrete class from a template class with specific template arguments The process of overriding template parameters in a class None of the other choices The process of defining a template class

The process of creating a concrete class from a template class with specific template arguments

What happens when an assertion evaluates to false? Nothing happens. The program enters recovery mode. The program terminates with an error message. None of the other choices

The program terminates with an error message.

Which of the following is an advantage of using iterators? They automatically resize containers to accommodate new elements. They render for loops and index variables entirely deprecated. They provide direct access to the container elements without indirection. None of the other choices They work with many types of containers regardless of implementation details.

They work with many types of containers regardless of implementation details.

Select all the statements that are true about the following code. Assume that String is being implemented with a linked list and that this function should not result in any resource overlaps between different Strings. String& String::operator+=(const String &s) { return *this = *this + s; } This function relies on String::operator+ having been implemented correctly. This function will not create a copy of the list stored in this object. It is much more efficient to only copy s and attach it to the last Node of this object's linked list. Assuming list::last(head) successfully returns the tail of a linked list, this function can be implemented better by utilizing the line "list::last(head)->next = s.head;"

This function relies on String::operator+ having been implemented correctly. It is much more efficient to only copy s and attach it to the last Node of this object's linked list.

What happens in C++ when you index an array out of bounds? Compilation Error An exception is guaranteed to be thrown. You are guaranteed to receive an error message. The array grows to handle the new index. You access memory outside of the array, getting unexpected values or modifying memory that does not belong to the array.

You access memory outside of the array, getting unexpected values or modifying memory that does not belong to the array.

SELECT ALL statements that are true about the following code: list::Node* list::append(Node* lhs, Node* rhs) { list::Node* copyHead = new Node(); copyHead->data = lhs->data; copyhead->next = nullptr; list::Node* curr = lhs->next; list::Node* current = copyHead; while (curr != nullptr) { list::Node* newNode = new Node(); newNode->data = curr->data; newNode->next = nullptr; current->next = newNode; current = current->next; curr = curr->next; } curr = rhs; while (curr != nullptr) { list::Node* newNode = new Node(); newNode->data = curr->data; newNode->next = nullptr; current->next = newNode; current = current->next; curr = curr->next; } return copyHead; } This program will segfault if lhs is an empty list. Line 3-5 and 12-14: Clunky/Wordy allocation of new Node. Constructor intialization should be done isntead. Line 3: Extra node allocated causes a memory leak. This successfully handles edge ca

This program will segfault if lhs is an empty list. Line 3-5 and 12-14: Clunky/Wordy allocation of new Node. Constructor intialization should be done isntead. Since the function is already defined with the list namespace, you can use Node instead of list::Node within the body of the function. Lines 7-29: The similarity between curr and current variable names presents a readability issue. Lines 11-19 and 22-30 do similar actions that should be refactored into a helper function.

What is the purpose of the member named next within a linked list node? To allow forward and backward traversal. To print out the list. None of the other choices To access nodes after this node.

To access nodes after this node.

Why might we also keep a tail pointer in addition to a head pointer to our singly linked list? To allow reverse traversal. To allow more efficient appending to the middle of the list. None of the other choices To allow more efficient appending to the tail end of the list. To allow more efficient deletion of nodes.

To allow more efficient appending to the tail end of the list.

Which of the following best describes the purpose of assertions in C++? To catch programming errors and identify their location To handle errors gracefully and provide friendly error messages To validate user input and ensure semantic correctness To test performance and efficiency None of the other choices

To catch programming errors and identify their location

What is the purpose of a try/catch block in C++? To terminate the program when an error occurs To improve program performance by reducing error checking None of the other choices To handle and recover from errors

To handle and recover from errors

What is the purpose of C++ STL algorithms? - To optimize memory usage - To facilitate inter-proccess communication - To provide a set of pre-defined math operations - To perform common operations on elements of STL containers - None of the other choices

To perform common operations on elements of STL containers

What is the purpose of using static_assert in C++? - To provide descriptive error messages during runtime. - To perform runtime checks. - To validate function parameters and handle errors gracefully. - To perform compile-time checks on conditions. - None of the other choices

To perform compile-time checks on conditions.

What is the purpose of C++ STL iterators? To simplify memory management To optimize execution time To provide a way to access and manipulate elements of containers in a generic and uniform manner To facilitate inter-thread communication None of the other choices

To provide a way to access and manipulate elements of containers in a generic and uniform manner

What is the purpose of lambda captures in C++? To specify the lambda's return type. To define the parameters. To use external variables in the function body. None of the other choices

To use external variables in the function body.

What is the consequence of using an invalidated iterator? The iterator automatically adjusts. The iterator points to a random memory location. Undefined behavior. The program is guaranteed to terminate with an error message. None of the other choices

Undefined behavior.

SCENARIO: Class String For Homework 4, the same functionality of class String from Homework 3 is implemented using a dynamically allocated array. The data member inside, also named buf, is now a pointer to an array of char which must be terminated with a null character. Suppose we write a helper function, strdup(const char *s), that returns an exact copy of the C-string pointed to by the parameter s by allocating an array with new[] and copying the characters from s into that new[] storage with strcpy(). What should replace Line 6 to write a proper move constructor for String? (Refer to Picture 23 in Google Docs) { delete[] buf; buf = strdup(s.buf); s.buf = nullptr; return *this; } : buf(strdup(s.buf)) { } { buf = nullptr; s.buf = nullptr; return *this; } { buf = s.buf; s.buf = nullptr; } : buf(strdup(s)) { } { delete[] buf; buf = strdup(s); s.buf = nullptr; } { delete[] buf; buf = s.buf; s.buf = nullptr; } { delete

{ buf = s.buf; s.buf = nullptr; }

SCENARIO: Class String For Homework 4, the same functionality of class String from Homework 3 is implemented using a dynamically allocated array. The data member inside, also named buf, is now a pointer to an array of char which must be terminated with a null character. Suppose we write a helper function, strdup(const char *s), that returns an exact copy of the C-string pointed to by the parameter s by allocating an array with new[] and copying the characters from s into that new[] storage with strcpy(). What should replace Line 6 to write a proper move assignment operator for String? (Refer to Picture 20 in Google Docs) { delete[] buf; buf = strdup(s.buf); s.buf = nullptr; return *this; } : buf(strdup(s.buf)) { } { buf = nullptr; s.buf = nullptr; return *this; } { buf = s.buf; s.buf = nullptr; } : buf(strdup(s)) { } { delete[] buf; buf = strdup(s); s.buf = nullptr; return *this; } { delete[] buf; buf = s.buf; s.buf

{ delete[] buf; buf = s.buf; s.buf = nullptr; return *this; }

SCENARIO: Class String For Homework 4, the same functionality of class String from Homework 3 is implemented using a dynamically allocated array. The data member inside, also named buf, is now a pointer to an array of char which must be terminated with a null character. Suppose we write a helper function, strdup(const char *s), that returns an exact copy of the C-string pointed to by the parameter s by allocating an array with new[] and copying the characters from s into that new[] storage with strcpy(). What should replace Line 6 to write a proper copy assignment operator for String? (Refer to Picture 16 in Google Docs) { delete[] buf; buf = strdup(s.buf); s.buf = nullptr; return *this; } : buf(strdup(s.buf)) { } { buf = nullptr; s.buf = nullptr; return *this; } { buf = s.buf; s.buf = nullptr; } : buf(strdup(s)) { } { delete[] buf; buf = strdup(s); s.buf = nullptr; } { delete[] buf; buf = s.buf; s.buf = nullptr; }

{ delete[] buf; buf = strdup(s.buf); return *this; }


Ensembles d'études connexes

Chapter 07 - Utility Maximization_Quiz_sc

View Set

ISNS 2359 Earthquakes and Volcanos Chapter 4 Questions

View Set

Infant and Child Development CH. 7 +8

View Set

Microeconomics - Chapter 1 - Principles of Microeconomics

View Set

A Man for All Seasons Quotes to Know

View Set