C++2 Final
Given an STL vector v containing the integers 1, 2, 3, 4, 5, 6, 7, 8 and 9 in this order, where 1 is the first element in the vector, what is the resulting vector after the following code segment is executed? rotate(++v.begin(), v.begin() + 3, v.begin() + 4);
1 4 2 3 5 6 7 8 9
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Are p2 and p3 pointing to the same node?
False (They are not pointing to the same node. Although the stored in the nodes is the same, the addresses of the nodes are different, which means that the pointers are pointing to two different nodes that store the same values.)
A word processor application has a printing selection to print documents. Which key factor in designing software will consider the possibility of new printers in the market?
Flexibility
Which of the following scenarios would best suit using a map?
Storing restaurant names and the cities in which the restaurant is found
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Which node has pointer prev pointing to a location in memory that is not valid any longer?
The node to which p3 is pointing.
The operation push removes the bottom element from the stack. True
False
Predict the output? #include <iostream> using namespace std; class Test { int x; Test() { x = 5;] }; int main() { Test *t = new Test; cout << t->x; }
(?)
Given an STL list named aList containing the integers 1, 2, 3, 4, 5, 6, 7, 8 and 9, what is the resulting list after the following code segment is executed? reverse(++aList.begin(), aList.end());
1 9 8 7 6 5 4 3 2
An STL vector v the integers 1, 2, 3, 4, 5, 6, 7, 8 and 9 in this order, where 1 is the first element in the vector. What is the output after running the following code snippet? auto iter = remove_if(v.begin(), v.end(), [](int num) { return num % 2 == 1; }); v.resize(iter - v.begin());printVector(v);
2 4 6 8
Which of the following structures is a multiset?
23 23 56 78 78 83 91 -must be in ascending order and can allow duplicates
Assume you have two queues, queue1 and queue2, both empty and of type int. What is the output after tracing the code below? push 23 into queue1 push 76 into queue1 push 64 into queue1 copy queue1 into queue2 while (queue2 is not empty) { print the front element of queue2 pop an element from queue2 }
23 76 64
An STL vector v contains the integers 23, 67, 82, 67, 67, 46 and 91 in this order, where 23 is the first element in the vector. What is the output after running the following code snippet? remove(v.begin(), v.end(), 67); for (const auto & i : v) cout << i << " ";
23 82 46 91 67 46 91
Suppose you have an empty set. You then insert the following numbers, in this order: 6, 6, 5, 5, 4, 4, 6. What is the second element of the set after inserting the numbers?
5 - duplicates removed, sorted
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question:
3
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Starting from pointer last, how many nodes are connected through pointers prev?
3
An STL vector v contains the integers 10, 11, 12, 13, 14 and 15 in this order, where 10 is the first element in the vector. What is needed to complete the statement below so that the vector becomes 10, 11, 12, 5, 5, 15?
5
An STL list named aList contains the integers 6, 8, 6, 6, 8, 6, 8, 8 and 8 in this order, where 6 is the first element in the list. What is the output after running the following code snippet? replace(aList.begin()++, aList.end(), 6, 8); print(aList);
8 8 8 8 8 8 8 8 8
If a Binary Search is performed on the array shown below, what will be the mid values in the first and second iterations if searching for 90? Array: 35, 67, 79, 80, 84, 89, 90
80 and 89
FIFO closely resembles which of the following? A. The order in which print jobs of the same priority are executed by a printer. B. The order in which customers are serviced in a bank. C. The order in which a browser stores visited pages.
A and B only
Two STL stacks are given, stack1 and stack2, both empty and both of type int. What are output 1 and output 2 after running the code below? push 18 into stack1 push 21 into stack1 push 25 into stack 1 stack2 = stack1 while (stack2 is not empty) { print the top element of stack2 //output 1 pop the top element of stack2 } print the top element of stack1 //output 2 output 1: 25 21 18 output 2: 25
Answer 1: 25 21 18 Answer 2: 25
Given the array below, how many comparisons will be performed by an iterative binary search algorithm (as seen on the slides) if searching for 60 and if searching for 100? Number of comparisons when searching for 60: 4 Number of comparisons when searching for 100: 5
Answer 1: 4 Answer 2: 5
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Are pointers first and last pointing to the same node?
False
AssllJme you have an iterator as defined below: auto iter = c. begin(); Assume c is a container that supports a bidirectional iterator. Which of the following statements is a valid way to access the second element of the container?
C -- not 100%, - NOT A, that is random access
A queue is a last in first out data structure.
False
In a linked list implementation of a stack, only a fixed number of elements can be pushed onto the stack.
False
The elements at the top of the stack have been in the stack the longest.
False
Which of the following code snippets can you use to traverse an STL queue?
Iterators cannot be used on an STL queue. Only front and back elements can be retrieved.
Consider a class declared as template <typename T> class MyClass { ... }; How would you declare ain object of the class using int as the template type? [T] myObj;
MyClass<int> myObj; (?)
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Which node will be deleted?
No nodes are deleted, because the prev pointers stored in the node pointed by p2 is a nullptr.
What is the running time of the destructor implemented in the DArray class?
O(1)
What is the worst case running time of Binary Search?
O(log n)
What is the running time of the destroyList function implemented in the AnyList class?
O(n)
What is the running time of the code segment shown below? for (int i = length - 1; i >= 0; --i) { for (int j = 0; j < length / 2; ++j) { int k = 8; while (k > 0) { cout << (length / 2); cout << a[k] << " "; k /= 2; } } }
O(n^2)
An STL list named aList contains the integers 10, 11, 12, 13, 14 and 15 in this order, where 10 is the first element in the list. What is the resulting list after running the statement below? fill(aList.begin(), aList.begin() + 2, 100);
The statement will produce an error.
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: After executing the entire function, which pointers are dangling pointers? You may pick more than one answer.
There are no dangling pointers.
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: What is the value stored in the node that is next to the first node?
There is no node; pointer next of the first node is a nullptr.
An array is a random access data structure, a stack is not.
True
LIFO stands for Last In First Out.
True
The function front returns the first element in the queue.
True
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Are p1 and p4 pointing to the same node?
True (Don't rely on the value stored in the node. Make sure you compare the addresses stored in the pointers.)
When coding, writing comments helps you and any programmer who might need to modify your code. This is an example of software ___.
User-friendliness
list<string>::const_iterator iter = mylist.cbegin(); Given the iterator declared above, how would you use the iterator to change the second element of the list to "aardvark"? You may assume the list has at least two elements in it.
You can't use a constant iterator to change the va I ue of an element.
An STL list named aList contains integers 1, 5, 6, 9, 2, 8, 4, 7 and 3 in this order, where 1 is the first element in the list. Which statement will sort the list correctly and efficiently? sort(aList.begin()++, aList.end());
aList.sort();
An STL vector v contains several integers. Which statement will correctly check whether number 62 is found? if( missing statement ) cout << "Found." else cout << "Not found."
find(v.begin(), v.end(), 62) != v.end()
Template functions allow you to write a single function that can be called
with different types of parameters (?)
Select all answers that apply. Which of the following statements would successfully insert a pair into an empty map? Assume m is a map.
m["first"] = ''Blue'' m.insert(make_pair("first", "Blue"))
Place a breakpoint in the createList function where indicated for this question. Run the program until it stops at the breakpoint. Once the program halted, answer the following question: Select all the pointers that point to the same node to which pointer last is pointing.
p2
The O-notation of an algorithm is O(n^2), which means that the running time is
quadratic
Given the predicate function below, choose the correct syntax needed to replace all negative integers in a vector v with 0. bool pred(int x) { return (x >= 0); }
replace_if(v.begin(), v.end(), pred, 0);
Given the sequence of operations below, determine the value of variables x, y and z. Assume all variables have been already declared. stack<int> s push 3 into s push 7 into s assign to var x the top element of s pop the top element of s assign to var y the top element of s push 5 into s assign to var z the top element of s
value of x: 7 value of y: 3 value of z: 5