Final Exam COMSC-165

¡Supera tus tareas y exámenes ahora con Quizwiz!

Suppose a program has the following class and main function: NOTE: Access specifiers have been omitted here because there is another question on this quiz about them. class College { string name; string get_name() { return name; } void set_name(string n) { name = n; } }; int main() { College college_obj; return 0; } On which line in the above program would it be appropriate to insert the word "const" ? void set_name(string n) string name; College college_obj; Correct! string get_name()

string get_name()

#include <iostream> using namespace std; int number = 2; void functionOne(); void functionTwo(); int main() { functionOne(); functionTwo(); functionOne(); functionTwo(); functionOne(); functionTwo(); system("PAUSE"); return 0; } void functionOne() { static int number; cout<<number<<endl; --number; } void functionTwo() { int number=0; cout<<number<<endl; ::number++; } What is the output of this program? 0 0 0 0 0 0 Correct! 0 0 -1 0 -2 0 0 2 0 3 0 4 0 2 -1 3 -2 4

0 0 -1 0 -2 0

Suppose a binary file is storing the following 56 bits: 10101010 11110000 01010101 00001111 00110011 11001100 00011000 NOTE: To make it easier to read there are spaces in-between each group of 8 bits. In the actual file, there would not be any spaces -- remember that binary only consists of two symbols: 0's and 1's. int main() { int num; int record_number; cout<<"Enter a record number: "; cin>>record_number; } Suppose the user enters 2 for the record_number, so the program seeks to record 2 (not shown in the code) and then calls the read function with the int variable passed as an argument (not shown in the code). The goal of the program (since the user entered 2) is to read the integer number being stored in the binary file into the num variable. The integer number in the binary file starts at record 2. Which bits will be read from the binary file?* *HINT: Remember that bytes within a binary file are treated like array indexes. 01010101 11110000 11110000 01010101 00001111 00110011 Correct! 01010101 00001111 00110011 11001100

01010101 00001111 00110011 11001100

int main() { fstream f_o("file1.txt", ios::in | ios::out); 1 ______________________________________ f_o.close(); } In the code shown above the file "file1.txt" is open for both reading and writing. The current contents of the file "file1.txt" is as follows: XVQWJRU Which of the options below contains the correct code to insert at line 1 in the program shown above to seek to the position of the "V" character in the file? 1 f_o.seekp(-2, ios::cur); Correct! 1 f_o.seekp(1, ios::cur); 1 f_o.seekp(-1, ios::cur); 1 f_o.seekp(2, ios::cur);

1 f_o.seekp(1, ios::cur);

An insertion algorithm can be used to insert a new node anywhere into an existing linked list. NOTE: Node is the name of the structure that is being used to create the linked list. It has a field named letter of type char that can store a letter. NOTE: Assume the head pointer has been properly set. NOTE: The index variable stores the index number of where the new node will be inserted within the list. For example if index is 0, then this means that the new node will be inserted at the beginning of the list. NOTE: The variable named value is a variable of type char that contains the letter that we want to store in the new node. Part of the code is shown below: Node* prev = head; Node* ptr = new Node; ptr->letter = value; if(index == 0 || head == nullptr) { 1 __________ 2 __________ } else { for(int i=1; i<index; i++) prev = prev->next; 3 ___________ 4 ___________ } Which of the options below contains the correct code that could be inserted at lines 1-4 in the code above? --------------------- Correct! 1 ptr->next = head; 2 head = ptr; 3 ptr->next = prev->next; 4 prev->next = ptr; --------------------- 1 head = ptr; 2 ptr->next = head; 3 ptr->next = prev->next; 4 prev->next = ptr; --------------------- 1 head = ptr; 2 ptr->next = head; 3 prev->next = ptr; 4 ptr->next = prev->next; --------------------- 1 ptr->next = head; 2 head = ptr; 3 prev->next = ptr; 4 ptr->next = prev->next;

1 ptr->next = head; 2 head = ptr; 3 ptr->next = prev->next; 4 prev->next = ptr;

The program below has a structure named Dept (short for department). Each department has two fields: a name, and an array of courses for each department. Part of the program is shown here: const int_NUM_OF_DEPTS = 5; const int NUM_OF_COURSES = 3; struct Dept { string name; 1 _________ }; int main() { Dept d = new Dept[NUM_OF_DEPTS]; 2 _____________________________ 3 _____________________________ 4 _____________________________ } Which of the options below contains the correct code to insert at lines 1-4 in the above program? 1 string course[NUM_OF_COURSES]; 2 d[0]->name = "Computer Science"; 3 d[0]->course[0] = "COMSC 165"; 4 d[0]->course[1] = "COMSC 260"; 1 Dept course[NUM_OF_COURSES]; 2 d[0]->name = "Computer Science"; 3 d[0]->course[0] = "COMSC 165"; 4 d[0]->course[1] = "COMSC 260"; 1 Dept course[NUM_OF_COURSES]; 2 d[0].name = "Computer Science"; 3 d[0].course[0] = "COMSC 165"; 4 d[0].course[1] = "COMSC 260"; Correct! 1 string course[NUM_OF_COURSES]; 2 d[0].name = "Computer Science"; 3 d[0].course[0] = "COMSC 165"; 4 d[0].course[1] = "COMSC 260";

1 string course[NUM_OF_COURSES]; 2 d[0].name = "Computer Science"; 3 d[0].course[0] = "COMSC 165"; 4 d[0].course[1] = "COMSC 260";

A search_node function is supposed to return true if a search value is found in a linked list. False is returned if the search value is NOT found in the linked list. For example, for the linked list: Red->Blue->Green->Purple->Black->White If the search value is "Purple", true is returned. If the search value is "Pink", false is returned. Part of the search_node function is as follows: bool search_node(Node *head, const string &s) { Node* p = head; 1 _____________ p=p->next; 2 ______________ return true; return false; } Which of the options below contains the correct code that can be inserted at lines 1-2 in the code above? 1 while(p>color != s && p != nullptr) 2 if(p == nullptr) 1 while(p->color != s && p != nullptr) 2 if(p != nullptr) 1 while(p != nullptr && p->color != s) 2 if(p == nullptr) Correct! 1 while(p != nullptr && p->color != s) 2 if(p != nullptr)

1 while(p != nullptr && p->color != s) 2 if(p != nullptr)

Suppose we wanted to add a new functionality to the Rectangle class: to be able to calculate the perimeter of a Rectangle object. The perimeter of a Rectangle is calculated as follows: perimeter = 2*(length + width) For example, if length = 2.0 and width = 5.0, then: perimeter = 2*(2.0 + 5.0) perimeter = 2 * 7.0 perimeter = 14.0 The implementation of the Rectangle class is omitted, but you can assume that length and width are member variables in the Rectangle class (and that there are corresponding member functions for set_length(), set_width, get_length(), and get_width()). 1. There should be a member function named set_perimeter() in the Rectangle class that can be used to directly set the perimeter for a Rectangle object. 2. The return type of the get_perimeter() member function should be Rectangle -- meaning that this function should return an entire Rectangle object. 1. True 2. True 1. False 2. True Correct! 1. False 2. False 1. True 2. False

1. False 2. False

Suppose a doubly linked list maintains three pointers: (1) head, (2) tail, and (3) middle. Each node is storing a capital letter (i.e., between 'A' - 'Z') -- assume the name of this field is called value and it has type char. For example: nullptr <- A <-> B <-> C <-> D <-> E -> nullptr In the above doubly linked list, the "<->" represents the fact that the links go in both directions. For example, "A <-> B" means that A's next pointer connects to B, and B's prev pointer connects to A. A's prev pointer points at nullptr. E's next pointer points at nullptr. In this case, head points at A, middle points at C, and tail points at E. This is just one example of a doubly linked list that maintains head/tail/middle pointers, but the concept could be applied to a doubly linked list of any size. Determine whether the following statements are true or false in general for a doubly linked list that maintains head/tail/middle pointers. Each node has a field named value of type char that stores a capital letter (i.e., between 'A' - Z'). Furthermore, assume that the list is sorted in ascending order (i.e., 'A', 'B', 'C', etc.). Assume that search_value is a variable of type char that is storing a capital letter (i.e., between 'A' - 'Z') that we want to search for in the list. We can start by comparing middle->value against search_value. 1. If search_value > middle->value, then we only need to check the second half of the list by following the next pointers starting from the middle node. 2. If the search_value < middle->value, then we only need to check the first half of the list by following the prev pointers starting from the middle node. Correct! 1. True 2. True 1. True 2. False 1. False 2. True 1. False 2. False

1. True 2. True

Suppose there is a Class named Car, which has a member function named set_make that can be used to set the make of a car object to a particular value (e.g. "Toyota", Ford", "Hyundai", etc.): NOTE: Access specifiers have been omitted here. class Car { string make; void set_make(string m) { make = m; } string get_make() { return make; } }; Then in main we have the following code: int main { string value = "Toyota"; Car car_obj; 1. _________________________ return 0; } Which of the options below contains the correct line of code to insert at line 1 in the code shown above to call the set_make function for the car_obj object with the argument "Toyota"? Correct! 1. car_obj.set_make(value); 1. set_make(car_obj->value); 1. set_make(car_obj.value); 1. car_obj->set_make(value);

1. car_obj.set_make(value);

Suppose the goal of a program is to print out the following two lines of output: A E I C E G Most of the code for the program is shown below: #include <iostream> using namespace std; void print_diag1(char*, int); void print_diag2(char*, int); const int COLS = 3; int main() { const int ROWS = 3; char letters[ROWS][COLS] = { {'A', 'B', 'C', { 'D', 'E', 'F', { 'G', 'H', 'I' }; char* ptr = &letters[0][0]; print_diag1(ptr, ROWS); print_diag2(ptr, ROWS); return 0; } void print_diag1(char* ptr, int rows) { for(int i=0; i<rows; i++) { 1. ____________________ 2. ____________________ } cout<<endl; } void print_diag2(char* ptr, int rows) { for(int i=0; i<rows; i++) { 3. __________________ 4. __________________ } cout<<endl; } Question: What is the correct code to insert at lines 1 - 4 in the program shown above? 1. ptr+=4; 2. cout<<*ptr<<" "; 3. ptr+=2; 4. cout<<*ptr<<" "; 1. ptr+=4; 2. cout<<*ptr<<" "; 3. cout<<*ptr<<" "; 4. ptr+=2; 1. cout<<*ptr<<" "; 2. ptr+=4; 3. cout<<*ptr<<" "; 4. ptr+=2; Correct Answer 1. cout<<*ptr<<" "; 2. ptr+=4; 3. ptr+=2; 4. cout<<*ptr<<" ";

1. cout<<*ptr<<" "; 2. ptr+=4; 3. ptr+=2; 4. cout<<*ptr<<" ";

Suppose a program is supposed to do the following: 1) Ask the user for the size of the array, and then dynamically create an array of this size. The array is of type int and stores numbers. 2) Ask the user to enter the individual numbers that will be stored in the array. For example, if the user entered four for the size of the array, then they will be asked to enter four numbers. 3) At the end of the program, the contents of the dynamic array are displayed on the console screen. int main() { int size; int* ptr = nullptr; cout<<"Enter the number of numbers: "; cin>>size; ptr = new int[size]; for(int i=0; i<size; i++) { cout<<"Enter a number: "; cin>>*(ptr+i); } cout<<"The contents of the array are: " for(int i=0; i<size; i++) cout<<*ptr<<" "; 1. _____________________ 2. ______________________ } *Question: What is the correct code to insert at lines 1 and 2 in the program shown above to remove the dynamic array from memory?* ---------------------------------- 1. for(int i=0; i<size; i++) delete ptr[i]; 2. ptr = nullptr; ---------------------------------- Correct! 1. delete[] ptr; 2. ptr = nullptr; ---------------------------------- 1. ptr = nullptr; 2. for(int i=0; i<size; i++) delete ptr[i]; ---------------------------------- 1. ptr = nullptr; 2. delete[] ptr;

1. delete[] ptr; 2. ptr = nullptr;

Suppose a program is supposed to (1) swap the first and third nodes and (2) swap the second and fourth nodes in a singly linked list that has eight nodes. For example, if the linked list is initially: A->B->C->D->E->F->G->H (where the head pointer is pointing at node A) Then after swapping the linked list will be: C->D->A->B->E->F->G->H NOTE: Here it is the values themselves that are being swapped (e.g., the characters "A" and "C" were swapped). The pointers connecting the nodes in the linked list are NOT being changed at all. Part of the program is shown below. The constant LIST_SIZE is storing the size of the linked list (this has to be known for the program to work): char temp; ptr1 = ptr2 = head; 1 _____________________ ptr2=ptr2->next; 2 _____________________ { temp = ptr1->data; ptr1->data = ptr2->data; ptr2->data = temp; ptr1=ptr1->next; ptr2=ptr2->next; } Which of the options below is correct? 1. for(int i=0; i<(LIST_SIZE/2+2); i++) 2. for(int i=0; i<(LIST_SIZE/2-2); i++) 1. for(int i=0; i<(LIST_SIZE/2+2); i++) 2. for(int i=0; i<(LIST_SIZE/2+2); i++) Correct! 1. for(int i=0; i<(LIST_SIZE/2-2); i++) 2. for(int i=0; i<(LIST_SIZE/2-2); i++) 1. for(int i=0; i<(LIST_SIZE/2-2); i++) 2. for(int i=0; i<(LIST_SIZE/2+2); i++)

1. for(int i=0; i<(LIST_SIZE/2-2); i++) 2. for(int i=0; i<(LIST_SIZE/2-2); i++)

Suppose a program is supposed to print out the following for a doubly linked list: 1) Starting from the middle node in the list, print out all of the nodes from the middle node to the first node. 2) Starting from the middle node in the list, print out all of the nodes from the middle node to the last node. So, as an example, if we have the following doubly linked list with 11 nodes: nullptr <-A <-> B <-> C <-> D <-> E <-> F <-> G <-> H <-> I <-> J <-> K -> nullptr Where A is the first node (head node), F is the middle node, and K is the last node (tail node). The <-> between two nodes, such as A <-> B, means that A's next pointer points at B, and B's prev pointer points at A. Note that A's prev pointer points at nullptr, and K's next pointer points at nullptr. The output of the program would be the following: F E D C B A F G H I J K Most of the code for the program is shown below. You can assume the following about the program: The doubly linked list has an odd number of nodes (e.g., 3, 5, 7, 9, etc.) The doubly linked list has at least 3 nodes The num_of_nodes variable stores the number of nodes in the doubly linked list The head pointer points at the first node in the doubly linked list The tail pointer points at the last node in the doubly linked list The name of the structure for the doubly linked list is named "LinkedList" It is not shown in the code, but assume the doubly linked list is built with values at the beginning of the program Each LinkedList node has a field named value of type char that stores a letter (such as 'A', 'B', 'C', etc.). int main() { // As mentioned above, several lines of code are omitted at the beginning of the program and are not shown here. // You can assume that the doubly linked list has been built with values, that the head/tail/prev/next pointers have been setup, etc. LinkedList* ptr1 = head; LinkedList *ptr2 = tail; 1. _____________________________________________ { 2. _________________________________________ { ptr1 = ptr1->next; ptr2 = ptr2->prev; } else if(i<num_of_nodes) { cout<<ptr2->value<<" "; ptr2 = ptr2->prev; if(i == (num_of_nodes-1)) cout<<endl; } else { cout<<ptr1->value<<" "; ptr1 = ptr1->next; } return 0; } Question: What is the correct code to insert at lines 1 and 2 in the program shown above? 1. for(int i=0; i<(num_of_nodes+(num_of_nodes/2)); i++) 2. if(i<(num_of_nodes/2)) 1. for(int i=0; i<(num_of_nodes+(num_of_nodes/2)+1); i++) 2. if(i<((num_of_nodes/2)+1)) Correct Answer 1. for(int i=0; i<(num_of_nodes+(num_of_nodes/2)+1); i++) 2. if(i<(num_of_nodes/2)) 1. for(int i=0; i<(num_of_nodes+(num_of_nodes/2)); i++) 2. if(i<((num_of_nodes/2)+1))

1. for(int i=0; i<(num_of_nodes+(num_of_nodes/2)+1); i++) 2. if(i<(num_of_nodes/2))

Suppose a program is supposed to do the following: 1) Ask the user for the size of the array, and then dynamically create an array of this size. The array is of type int and stores numbers. 2) Ask the user to enter the individual numbers that will be stored in the array. For example, if the user entered four for the size of the array, then they will be asked to enter four numbers. 3) At a later point in the program, the user can add more numbers to the end of the array to make it larger. A second dynamic array is used to make this possible. For example, if the user entered four for the original size of the array and then wants to add two more additional elements, then the second dynamic array will have size six. 4) At the end of the program, the contents of the dynamic array (the original numbers + the new numbers) are displayed on the console screen. You can assume that both dynamic arrays are removed from memory when they are no longer being used. The code for this is not shown here as there is another question on the final exam about this. Here is a sample run of the program with sample user inputs. User inputs are shown in green, and the output of the numbers in the array is shown in red. Enter the number of numbers in the array: 4 Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 How many additional numbers would you like to add to the end of the array?: 2 Enter a number: 5 Enter a number: 6 The contents of the array are: 1 2 3 4 5 6 Most of the code for the program is shown below: int main() { int size1, size2; int* ptr1 = nullptr; int* ptr2 = nullptr; cout<<"Enter the number of numbers in the array: "; cin>>size1; ptr1 = new int[size1]; for(int i=0; i<size1; i++) { cout<<"Enter a number: "; cin>>*(ptr1+i); } cout<<"How many additional numbers would you like to add to the end of the array?: "; cin>>size2; size2 += size1; ptr2 = new int[size2]; 1. ___________________________ *(ptr2+i) = *(ptr1+i); 2. __________________________________ { cout<<"Enter a number: "; cin>>*(ptr2+i); } cout<<"The contents of the array are: " for(int i=0; i<size2; i++) cout<<*ptr2<<" "; return 0; } Question: What is the correct code to insert at lines 1 and 2 in the program shown above? Correct Answer 1. for(int i=0; i<size1; i++) 2. for(int i=size1; i<size2; i++) 1. for(int i=0; i<size2; i++) 2. for(int i=size1; i<size2; i++) 1. for(int i=0; i<size2; i++) 2. for(int i=size1; i<(size1+size2); i++) 1. for(int i=0; i<size1; i++) 2. for(int i=size1; i<(size1+size2); i++)

1. for(int i=0; i<size1; i++) 2. for(int i=size1; i<size2; i++)

Suppose a program is supposed to do the following: 1) Ask the user for the size of the array, and then dynamically create an array of this size. The array is of type int and stores numbers. 2) Ask the user to enter the individual numbers that will be stored in the array. For example, if the user entered six for the size of the array, then they will be asked to enter six numbers. 3) At a later point in the program, the user can remove numbers from the beginning of the array to make it smaller. A second dynamic array is used to make this possible. For example, if the user entered six for the original size of the array and then wants to remove two numbers from the beginning of the array, then the second dynamic array will have size four. 4) At the end of the program, the contents of the dynamic array (the numbers NOT including the ones that were removed) are displayed on the console screen. You can assume that both dynamic arrays are removed from memory when they are no longer being used. The code for this is not shown here as there is another question on the final exam about this. Here is a sample run of the program with sample user inputs. User inputs are shown in green, and the output of the numbers in the array is shown in red. Enter the number of numbers in the array: 6 Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: 5 Enter a number: 6 How many numbers would you like to remove from the beginning of the array?: 2 The contents of the array are: 3 4 5 6 Most of the code for the program is shown below: int main() { int size1, size2; int* ptr1 = nullptr; int* ptr2 = nullptr; cout<<"Enter the number of numbers in the array: "; cin>>size1; ptr1 = new int[size1]; for(int i=0; i<size1; i++) { cout<<"Enter a number: "; cin>>*(ptr1+i); } cout<<"How many numbers would you like to remove from the beginning of the array?: "; cin>>size2; size2 = size1 - size2; ptr2 = new int[size2]; 1. ___________________________ 2. _________________________________ cout<<"The contents of the array are: " for(int i=0; i<size2; i++) cout<<*ptr2<<" "; return 0; } Question: What is the correct code to insert at lines 1 and 2 in the program shown above? 1. for(int i=size2; i<size1; i++) 2. *(ptr2+i-(size1-size2)) = *(ptr1+i); 1. for(int i=size1-size2; i<size1; i++) 2. *(ptr2+i) = *(ptr1+i-(size1-size2)); 1. for(int i=size2; i<size1; i++) 2. *(ptr2+i) = *(ptr1+i-(size1-size2)); Correct! 1. for(int i=size1-size2; i<size1; i++) 2. *(ptr2+i-(size1-size2)) = *(ptr1+i);

1. for(int i=size1-size2; i<size1; i++) 2. *(ptr2+i-(size1-size2)) = *(ptr1+i);

Suppose a program is supposed to do the following with a doubly linked list: - If the linked list has an odd (i.e., 3, 5, 7, 9, etc.) number of nodes, then the value of the middle node should be displayed. - If the linked list has an even (i.e., 2, 4, 6, 8, etc.) number of nodes, then nothing should be displayed. For example, with the following doubly linked list that has 5 nodes: nullptr<-A<->B<->C<->D<->E->nullptr "C" would be displayed on the console screen. And with the following doubly linked list that has 6 nodes: nullptr<-A<->B<->C<->D<->E<->F->nullptr Nothing would be displayed on the console screen. It's also possible that multiple nodes will store the same letter. For example: nullptr<-A<->B<->C<->D<->B<->A->nullptr Nothing would be displayed on the console screen since there are 6 nodes in this case. NOTE: The first node's prev pointer always points at nullptr, and the last node's next pointer always points at ptr. So for example, with the 6 node list, this is what is meant by nullptr<-A and F->nullptr. NOTE: Since this is a doubly linked list, the links between two nodes that are next to each other always go in both directions. So for example, A<->B means that A's next pointer points at B, and B's prev (previous) pointer points at A. NOTE: The name of the structure used is LinkedList. It has a field named value (of type char) that can store a letter (i.e. "A", "B", "C", etc.). NOTE: A pointer named head is already pointing at the first node in the linked list. NOTE: A pointer named tail is already pointing at the last node in the linked list. NOTE: The doubly linked list always has at least two (2) nodes. Most of the program is shown here: LinkedList* ptr1 = head; LinkedList* ptr2 = tail; while(ptr1 != nullptr) { 1. __________________________ cout<<ptr1->value; ptr1=ptr1->next; ptr2=ptr2->prev; } Which of the options below contains the correct if statement to insert at line 1 in the code shown above? 1. if(ptr1->next == ptr2->prev) 1. if(*(ptr1->next) == *(ptr2->prev)) 1. if(*ptr1 == *ptr2) Correct Answer 1. if(ptr1 == ptr2)

1. if(ptr1 == ptr2)

Suppose a program needs to build a doubly linked list in reverse order. This means that, for example, if the user enters the following names in this order: Wonder Woman Superman Spider-man Batman Then the following doubly linked list would be created: Doubly_LL.PNG So the first name entered by the user (Wonder Woman) is the last node (tail node), and the last name entered by the user (Batman) is the first node (head node). Most of the code is shown below: Student* head = nullptr; Student* tail = nullptr; Student* ptr = nullptr; Student* new_node = nullptr; int num_of_nodes; cout << "Enter # nodes: "; cin >> num_of_nodes; cin.ignore(); for (int i = 0; i<num_of_nodes; i++) { new_node = new Student; 1 _____ cout << "Enter name: "; getline(cin, new_node->name); 2 _____ tail = new_node; else 3 _____ ptr = new_node; } head = ptr; if (head != nullptr) head->prev = nullptr; Which of the options below contains the correct code to insert at lines 1, 2, and 3 in the code shown above? 1. new_node->prev = ptr; 2. if (i == (num_of_nodes-1)) 3. ptr->next = new_node; Correct! 1. new_node->next = ptr; 2. if (i == 0) 3. ptr->prev = new_node; 1. new_node->prev = ptr; 2. if (i == 0) 3. ptr->next = new_node; 1. new_node->next = ptr; 2. if (i == (num_of_nodes-1)) 3. ptr->prev = new_node;

1. new_node->next = ptr; 2. if (i == 0) 3. ptr->prev = new_node;

Suppose a program is supposed to split a doubly linked list into two doubly linked lists of equal length. For example, if we have the following initial linked list: nullptr <-*A* <-> B <-> C <-> D <-> E <-> F <-> G <-> *H* -> nullptr Where A is the first node (head node) and H is the last node (tail node). The head1 pointer is pointing at A and the tail1 pointer is pointing at H (see the program below). The <-> between two nodes, such as A <-> B, means that A's next pointer points at B, and B's prev pointer points at A. Note that A's prev pointer points at nullptr, and H's next pointer points at nullptr. After splitting the list, we now have the following TWO (2) doubly linked lists: nullptr <-*A* <-> B <-> C <-> *D* -> nullptr nullptr <- *E* <-> F <-> G <-> *H* -> nullptr At this point, head1 is pointing at A, tail1 is pointing at D, head2 is pointing at E, and tail2 is pointing at H (see the program below). Most of the code for the program is shown below. You can assume the following about the program: - The doubly linked list has an even number of nodes (e.g., 4, 6, 8, 10, etc.) - The doubly linked list has at least 4 nodes - The num_of_nodes variable stores the number of nodes in the doubly linked list - Both before and after splitting the lists, the head1 pointer points at the first node in the first doubly linked list (this node is the first node in the original "big" list before splitting, and also the first node in the first list after splitting) - Before splitting the lists, the tail1 pointer points at the last node in the original "big" list. After splitting it points at the last node in the new first list. - Before splitting the lists, the head2 pointer points at nullptr. After splitting it points at the first node in the second list. - Before splitting the lists, the tail2 pointer points at nullptr. After splitting it points at the last node in the second list. - It is not shown in the code, but assume the doubly linked list is built with values at the beginning of the program - The structure for the doubly linked list is named "LinkedList" - Each LinkedList node has a field named value of type char that stores a letter (such as 'A', 'B', 'C', etc.). int main() { // As mentioned above, several lines of code are omitted at the beginning of the program and are not shown here. // You can assume that the linked list has been built with values, that the head/tail/prev/next pointers have been setup, etc. LinkedList* ptr = head1; tail2 = tail1; for(int i=0; i<(num_of_nodes/2-1); i++) ptr = ptr->next; 1. _________________ 2. _________________ 3. __________________ 4. __________________ return 0; } Question: What is the correct code to insert at lines 1 - 4 in the program shown above? 1. tail1 = ptr; 2. head2 = tail1->next; 3. ptr->prev->next = nullptr; 4. ptr->prev = nullptr; 1. head2 = ptr; 2. tail1 = head2->next; 3. ptr->prev->next = nullptr; 4. ptr->prev = nullptr; 1. head2 = ptr; 2. tail1 = head2->next; 3. ptr->next->prev = nullptr; 4. ptr->next = nullptr; Correct! 1. tail1 = ptr; 2. head2 = tail1->next; 3. ptr->next->prev = nullptr; 4. ptr->next = nullptr;

1. tail1 = ptr; 2. head2 = tail1->next; 3. ptr->next->prev = nullptr; 4. ptr->next = nullptr;

Suppose a program has a 3X3 2D array. The goal of the program is to search the 2D array for a search_value in the following order: Seventh Fourth First Eighth Fifth Second Ninth Sixth Third So this means the cell in the diagram above with the word "first" would be checked for the search_value first, and the cell with the word "Ninth" would be checked for the search_value last. NOTE: The 2D array is an array of integers. It does NOT store words such as "First". The diagram above was to show you the order in which the 2D array should be searched. 1 _______________________ 2 _________________________ if(arr[j][i] == search_value) return true; return false; Which of the options below contains the correct loops to insert at lines 1 and 2 in the code shown above? 1.. for(int i=0 i<COLS; i++) 2.. for(int j=0; j<ROWS; j-++) 1.. for(int i=0 i<COLS; i++) 2.. for(int j=ROWS-1; j>=0; j--) Correct Answer 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=0; j<ROWS; j++) 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=ROWS-1; j>=0; j--)

1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=0; j<ROWS; j++)

int func(int, int); int func(double, int, int=1); double func(double, double, double=0.5); double func(int, double); int main() { cout<<func(2+0, 5)<<endl); cout<<func(13/2.0, 3.0)<<endl; } int func(int num1, int num2) { return num1*num2; } int func(double num1, int num2, int num3) { return num1+num2+num3; } double func(double num1, double num2, double num3) { return num1-num2-num3; } double func(int num1, double num2) { return num1/num2; } What is the output of this program? 8 2.0 Correct! 10 3.0 8 3.0 10 2.0

10 3.0

Suppose a program has two functions that work with an array: Print_array: This function takes in two parameters, an array and the size of the array, and is supposed to print out all of the elements of the array. Build:array: This function takes in two parameters, an array and the size of the array, and is supposed to build the array from user inputs. So both functions receive (1) an array and (2) the size of that array. However, we also talked about the principle of least privilege and setting up the array parameter as a constant parameter vs non-constant parameter. Which of the following options below would make it impossible for the function to be able to complete its task? Correct! Build_array: Make the array parameter constant Print_array: Make the array parameter constant Print_array: Make the array parameter NON-constant Build_array: Make the array parameter NON-constant

Build_array: Make the array parameter constant

#include <iostream> #include <string> using namespace std; void f(string, string &); int main() { string s1 = "Hello", s2 = "World"; f(s1, s2); cout<<s1<<" "<<s2<<endl; f(s1, s2); cout<<s1<<" "<<s2<<endl; f(s1, s2); cout<<s1<<" "<<s2<<endl; system("PAUSE"); return 0; } void f(string s1, string &s2) { s1+= "T"; s2+= "R"; cout<<s1<<" "<<s2<<endl; } What is the output of this program? HelloT WorldR HelloT WorldR HelloTT WorldRR HelloTT WorldRR HelloTTT WorldRRR HelloTTT WorldRRR Correct! HelloT WorldR Hello WorldR HelloT WorldRR Hello WorldRR HelloT WorldRRR Hello WorldRRR HelloT WorldR Hello World HelloT WorldR Hello World HelloT WorldR Hello World HelloT WorldR HelloT World HelloTT WorldR HelloTT World HelloTTT WorldR HelloTTT World

HelloT WorldR Hello WorldR HelloT WorldRR Hello WorldRR HelloT WorldRRR Hello WorldRRR

void test_func(int *); int main() { int x=5; int* ptr = &x; test_func(ptr); } void test_func(int *ptr2) { cout<<*ptr2<<endl; } Which of the following options below represents an accurate memory diagram of the program while the program is inside of the test_func function? ---------------------------------------------- MEMORY (RAM) Address Value .... Address 40-43 5 <-x .... Address 62-65 Address 40 <- ptr, ptr2 ---------------------------------------------- Correct! MEMORY (RAM) Address Value .... Address 40-43 5 <-x .... Address 62-65 Address 40 <- ptr .... Address 84-87 Address 40 <- ptr2 ---------------------------------------------- MEMORY (RAM) Address Value .... Address 40-43 5 <-x, ptr2 .... Address 62-65 Address 40 <- ptr ---------------------------------------------- MEMORY (RAM) Address Value .... Address 40-43 5 <-x .... Address 62-65 Address 40 <- ptr .... Address 84-87 Address 62 <- ptr2

MEMORY (RAM) Address Value .... Address 40-43 5 <-x .... Address 62-65 Address 40 <- ptr .... Address 84-87 Address 40 <- ptr2

int ROWS = 5; int COLS = 5; for(int curr_col=0; curr_col<COLS; curr_col++) cout<<"#"; cout<<"\n"; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) { for(int curr_col=0; curr_col<(ROWS - curr_row - 1); curr_col++) cout<<" "; cout<<"#\n"; } for(int curr_col=0; curr_col<COLS; curr_col++) cout<<"#"; cout<<"\n"; What pattern does the program above draw?

Z

Suppose we wanted to add a new member function to the Rectangle class named isEqualTo. The isEqualTo function compares two rectangle objects to see if they are equal to one another. Two Rectangle objects are equal if (1) the lengths of of both objects are equal and (2) the widths of both objects are equal. The function returns true if the two Rectanagle objects are equal to one another, otherwise it returns false. The following is an example of how we could call is the isEqualTo function with two Rectangle objects r1 and r2: int main() { Rectangle r1{2, 5}; Rectangle r2{2, 7}; bool equal; equal = r1.isEqualTo(r2); if(equal) cout<<"The two rectangle objects are equal\n\n"; else cout<<"The two rectangle objects are NOT equal\n\n"; } Here is part of the Rectangle class: class Rectangle { private: double length; double width; public: 1________________________ // header of isEqualTo is omitted { //body of isEqualTo starts here return length == r.length && width == r.width; } //body of isEqualTo ends here // the rest of the functions are omitted } Which of the options below is the correct function header for the isEqualTo function to insert at line 1 in the Rectangle class? HINT: Think about the principle of least privilege and how it applies to the isEqualTo function. bool isEqualTo(const Rectangle& r) Correct! bool isEqualTo(const Rectangle& r) const bool isEqualTo(Rectangle& r) const bool isEqualTo(Rectangle& r)

bool isEqualTo(const Rectangle& r) const

A singly linked list is a list in which each node has a next pointer, and a pointer to the head of the list is maintained as well. Suppose a program has implemented a circular singly linked list. A circular linked list is a list in which all of the nodes form a circle, and the last node's next pointer points to the first node (head node). Each node has a next pointer (but NOT a prev pointer). Assuming the following: Each node in the linked list stores a letter (of type char). The head pointer is pointing at the first node in the list. A pointer named ptr is also pointing at the first node in the list. The linked list always has at least two nodes. Which of the options below will print out the letter for each node in the linked list exactly once? -------------------------------- while(ptr != head) { cout<<ptr->letter<<endl; ptr=ptr->next; } -------------------------------- Correct! do { cout<<ptr->letter<<endl; ptr=ptr->next; } while(ptr != head); -------------------------------- do { cout<<ptr->letter<<endl; ptr=ptr->next; } while(ptr->next != head); -------------------------------- while(ptr->next != head) { cout<<ptr->letter<<endl; ptr=ptr->next; }

do { cout<<ptr->letter<<endl; ptr=ptr->next; } while(ptr != head);

Suppose a program has the following structure: const int SIZE = 25; struct Computer { char CPU_type[SIZE]; char GPU_type[SIZE]; char RAM_type[SIZE]; int amount_of_ram; double CPU_speed; }; int main() { Computer c; ........... (omitted) 1 ________ } In the omitted portion of main, the user is prompted for input for each of the structure fields (e.g. CPU_type, GPU_type, RAM_type, etc.). On line 1 in the code above, the program is supposed to write the user's input to a binary file. f_o is a fstream object associated with the file. Which of the options below contains the correct statement to place at line 1 in the code shown above? f_o.write(reinterpret_cast<char&>(*c), sizeof(c)); f_o.write(reinterpret_cast<char*>(*c), sizeof(c)); Correct! f_o.write(reinterpret_cast<char*>(&c), sizeof(c)); f_o.write(reinterpret_cast<char&>(&c), sizeof(c));

f_o.write(reinterpret_cast<char*>(&c), sizeof(c));

int main() { char letter; fstream file_obj("Letters.txt", ios::in | ios::out); 1 ______________________________________ file_obj.close(); } In the code shown above the file "Letters.txt" is open for both reading and writing. The current contents of the file "Letters.txt" is as follows: ABCDEFG Which of the options below contains the correct call to seekg to insert on line 1 in the program shown above that would seek to the position of the "E" character in the file? Correct! file_obj.seekg(-3, ios::end); file_obj.seekg(-2, ios::end); file_obj.seekg(6, ios::end); file_obj.seekg(5, ios::end);

file_obj.seekg(-3, ios::end);

int main() { char letter; fstream file_obj("Letters.txt", ios::in | ios::out); file_obj.read(&letter, sizeof(letter)); 1 ______________________________________ file_obj.close(); } In the code shown above the file "Letters.txt" is open for both reading and writing. The current contents of the file "Letters.txt" is as follows: ABCDEFG Which of the options below contains the correct call to seekg to insert on line 1 in the program shown above that would seek to the position of the "C" character in the file? file_obj.seekg(-2, ios::cur); file_obj.seekg(-1, ios::cur); file_obj.seekg(2, ios::cur); Correct Answer file_obj.seekg(1, ios::cur);

file_obj.seekg(1, ios::cur);

A function find_left_of_middle is supposed to find and return the element to the left of the middle element in a vector. For example, if the vector has the following elements: ["Red", "Green", "Blue", "Orange", "Purple", "Pink", "Brown"] The function would return the string "Blue", which is to the left of the middle element ("Orange"). NOTE: Assume the vector passed to the function always has an odd number of elements (1, 3, 5, etc.) int find_left_of_middle(vector<string> &colors) { 1 ______________ colors.pop_back(); return colors.at(colors.size()-1); } Which of the options below contains the correct loop header to insert at line 1 in the code shown above? for(int i=0; i<=(colors.size()+1); i++) Correct! for(int i=0; i<(colors.size()+1); i++) for(int i=0; i<(colors.size()+2); i++) for(int i=0; i<=(colors.size()+2); i++)

for(int i=0; i<(colors.size()+1); i++)

Suppose a program is supposed to do the following: write all of the lines from one file EXCEPT for the middle line to another file. For example, if we have the following input file: File1.txt John Amy Adam Sarah Jason Jasmine Tim Amanda David Then the resulting output file will be: File2.txt John Amy Adam Sarah Jasmine Tim Amanda David Which of the options below is the correct implementation of the program? NOTE: - The variable num_of_file_lines stores the number of lines in the input file (file1.txt). - Assume that the input file (file1.txt) always has an odd number of lines. ----------------------------------- for(int i=0; i<(num_of_file_lines/2-1); i++) { infile>>line; outfile<<line<<endl; } if(infile>>line) outfile<<line<<endl; for(int i=0; i<(num_of_file_lines/2-1); i++) { infile>>line; outfile<<line<<endl; } ----------------------------------- for(int i=0; i<(num_of_file_lines/2-1); i++) { infile>>line; outfile<<line<<endl; } if(!(infile>>line)) outfile<<line<<endl; for(int i=0; i<(num_of_file_lines/2-1); i++) { infile>>line; outfile<<line<<endl; } ----------------------------------- Correct! for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; } if(!(infile>>line)) outfile<<line<<endl; for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; } ----------------------------------- for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; } if(infile>>line) outfile<<line<<endl; for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; }

for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; } if(!(infile>>line)) outfile<<line<<endl; for(int i=0; i<num_of_file_lines/2; i++) { infile>>line; outfile<<line<<endl; }

Suppose the following code is in main: int main() { const int S = 9; int a[S] = {99, 12, 27, 8, 41, 65, 49, 13, 22}; int* p = a; } Question: Which of the options below will do the following? Print out the value 99 Print out the value 27 Print out the value 41 Print out the value 49 Print out the value 22 Print out a "garbage value" that is beyond the bounds of the array -------------------- for(int j=0; j<(S/2); j++) { cout<<*p<<endl; p+=2; } -------------------- for(int j=0; j<=(S/2); j++) { cout<<*p<<endl; p+=2; } -------------------- Correct Answer for(int j=0; j<=(S/2+1); j++) { cout<<*p<<endl; p+=2; } -------------------- for(int j=0; j<(S/2+1); j++) { cout<<*p<<endl; p+=2; }

for(int j=0; j<=(S/2+1); j++) { cout<<*p<<endl; p+=2; }

Suppose a program has the following singly linked list: W->X->Y->Z (where head is pointing at node W, and node Z is pointing at nullptr) For which of the options below would line THREE (3) cause the program to crash? (a program will crash as soon as an invalid memory access is attempted) head->next->next->next->next = head; head = head->next; head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head head->next->next->next->next = nullptr; Correct! head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head head = head->next; head->next->next->next->next = nullptr; head->next->next->next->next = head

head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head

In the game of tic-tac-toe, two players ('X' and 'O') play against each other on a 3X3 (three by three) grid with 9 (nine) squares. There are three ways to win the game: Get three of your symbol ('X' or 'O') in a row Get three of your symbol ('X' or 'O') in a column Get three of your symbol ('X' or 'O') diagonally For example, if the current state of the game is: O - X - O - - X - - - - X - O Then player X has won because they got three X's in a column. Like many board games, the game of tic-tac-toe can be represented by a 2D array. Suppose the game board is represented by a 2D array named arr of type char (each index stores 'X' or 'O'). To determine if a player has won the game, there are several cases that have to be checked. Which of the options below correctly determines if player X has won the game by getting three X's in a column? ----------------------------------- if((arr[0][0] == 'X' && arr[1][0] == 'X' && arr[2][0] == 'X') || (arr[0][1] == 'X' && arr[1][1] == 'X' && arr[2][1] == 'X') || (arr[0][2] == 'X' && arr[1][2] == 'X' && arr[2][2] == 'X')) cout<<"Player X has won by getting three X's in a column"; ----------------------------------- if((arr[0][0] == 'X' || arr[1][0] == 'X' || arr[2][0] == 'X') && (arr[0][1] == 'X' || arr[1][1] == 'X' || arr[2][1] == 'X') && (arr[0][2] == 'X' || arr[1][2] == 'X' || arr[2][2] == 'X')) cout<<"Player X has won by getting three X's in a column"; ----------------------------------- if((arr[0][0] == 'X' && arr[0][1] == 'X' && arr[0][2] == 'X') || (arr[1][0] == 'X' && arr[1][1] == 'X' && arr[1][2] == 'X') || (arr[2][0] == 'X' && arr[2][1] == 'X' && arr[2][2] == 'X')) cout<<"Player X has won by getting three X's in a column"; ----------------------------------- if((arr[0][0] == 'X' || arr[0][1] == 'X' || arr[0][2] == 'X') && (arr[1][0] == 'X' || arr[1][1] == 'X' || arr[1][2] == 'X') && (arr[2][0] == 'X' || arr[2][1] == 'X' || arr[2][2] == 'X')) cout<<"Player X has won by getting three X's in a column";

if((arr[0][0] == 'X' && arr[1][0] == 'X' && arr[2][0] == 'X') || (arr[0][1] == 'X' && arr[1][1] == 'X' && arr[2][1] == 'X') || (arr[0][2] == 'X' && arr[1][2] == 'X' && arr[2][2] == 'X')) cout<<"Player X has won by getting three X's in a column";

Suppose a program is supposed to find the second largest element in an array, and store it in a variable named Z. Part of the code is shown here: const int M = 12; int X[M] = {25, 32, 46, 8, 5, 75, 88, 11, 47, 63, 71, 6}; int Y, Z; if(X[0] > X[1]) { Y = X[0]; Z = X[1]; } else { Y = X[1]; Z = X[0]; } for(int a=2; a<M; a++) { .......(omitted) } So in this particular example, when the code finishes Z would store 75. Which of the options below contains the correct code to place into the body of the for loop shown above? -------------------------- if(X[a] > Z) Z = X[a]; else if(X[a] > Y) { Z = Y; Y = X[a]; } -------------------------- Correct! if(X[a] > Y) { Z = Y; Y = X[a]; } else if(X[a] > Z) Z = X[a]; -------------------------- if(X[a] > Y) Y = X[a]; else if(X[a] > Z) { Z = Y; Y = X[a]; } -------------------------- if(X[a] > Z) { Z = Y; Y = X[a]; } else if(X[a] > Y) Y = X[a];

if(X[a] > Y) { Z = Y; Y = X[a]; } else if(X[a] > Z) Z = X[a];

Suppose there is a structure named LinkedList, and we have dynamically created a LinkedList node: LinkedList* ptr = new LinkedList; Next, we want to read the contents of one (1) record from a binary file and store the contents of this record in the LinkedList node that ptr is pointing at. Which of the options below contains the correct read statement to do this? NOTE: Assume the binary file has already been opened properly for the infile object. infile.read(reinterpret_cast<char*>(*ptr), sizeof(ptr)); --------------------- Correct! infile.read(reinterpret_cast<char*>(ptr), sizeof(*ptr)); --------------------- infile.read(reinterpret_cast<char*>(*ptr), sizeof(*ptr)); --------------------- infile.read(reinterpret_cast<char*>(ptr), sizeof(ptr));

infile.read(reinterpret_cast<char*>(ptr), sizeof(*ptr));

Suppose a function is supposed to calculate the sum of all of the edge elements in a 2D array. For example, suppose we have the following 4X4 2D array with sixteen values: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 In this case the function would calculate the sum of the 12 bolded values shown above. Another example with a 5X5 2D array with 25 values: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 In this case the function would calculate the sum of the 16 bolded values shown above. One way to handle this is to have two different sets of nested for loops, since some of the rows must be handled differently from others (HINT: look at the difference between the first and last rows vs the inner rows). NOTE: The name of the array is A The number of rows is stored in X The number of columns is stored in Y Which of the options below is the correct implementation of the program? ----------------------------------- int B = 0; for(int C=0; C<X; C+=X-1) for(int D=0; D<Y; D+=Y-1) B+=A[C][D]; for(int C=1; C<(X-1); C++) for(int D=0; D<Y; D++) B+=A[C][D]; ----------------------------------- int B = 0; for(int C=0; C<X; C+=X-1) for(int D=0; D<Y; D++) B+=A[C][D]; for(int C=1; C<X; C++) for(int D=0; D<Y; D+=Y-1) B+=A[C][D]; ----------------------------------- Correct! int B = 0; for(int C=0; C<X; C+=X-1) for(int D=0; D<Y; D++) B+=A[C][D]; for(int C=1; C<(X-1); C++) for(int D=0; D<Y; D+=Y-1) B+=A[C][D]; ----------------------------------- int B = 0; for(int C=0; C<X; C+=X-1) for(int D=0; D<Y; D+=Y-1) B+=A[C][D]; for(int C=1; C<X; C++) for(int D=0; D<Y; D++) B+=A[C][D];

int B = 0; for(int C=0; C<X; C+=X-1) for(int D=0; D<Y; D++) B+=A[C][D]; for(int C=1; C<(X-1); C++) for(int D=0; D<Y; D+=Y-1) B+=A[C][D];

Which of the following options below is legal in C++? int calc(int num1 = 10, int num2 = 20, int num3, int num4); Correct! int calc(int num1, int num2, int num3 = 10, int num4 = 20); int calc(int num1 = 10, int num2, int num3 = 20, int num4); int calc(int num1, int num2 = 10, int num3, int num4 = 20);

int calc(int num1, int num2, int num3 = 10, int num4 = 20);

In a menu-driven program, the get_menu_choice function has one sole purpose: to prompt the user for their menu choice, and send the user's input back to main. In a menu-driven program where there are three menu choices, the prompt for the menu choice would look like this: Enter your choice (1 - 3): Which of the options below correctly represents the get_menu_choice function? -------------------- Correct! int get_menu_choice() { ....... (omitted) } -------------------- int get_menu_choice(int menu_choice) { ....... (omitted) } -------------------- void get_menu_choice(int menu_choice) { ....... (omitted) } -------------------- void get_menu_choice() { ....... (omitted) }

int get_menu_choice() { ....... (omitted) }

Suppose a program is supposed to search an array to see if a value occurs in the array. For example, if the array a = ["James", "Jane", "Andrew", "Rebecca", "Steve"] and the search value s = "Rebecca", then the program would print out "Search value found", because "Rebecca" occurs in the array. On the other hand, if the array a = ["James", "Jane", "Andrew", "Rebecca", "Steve"] and the search value s = "Thomas", the program would print out "Search value NOT found", because "Thomas" does NOT occur in the array. NOTE: a is an array storing strings, LEN is the length of the array, and s is the value that is being searched for in the array. Which of the options below is the correct implementation of the program? ------------------------------------------------------ int index=0; while(index < LEN && a[index] != s) index++; if(index == LEN) cout<<"Search value found"<<endl; else cout<<"Search value NOT found"<<endl; ------------------------------------------------------ Correct! int index=0; while(index < LEN && a[index] != s) index++; if(index == LEN) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl; ------------------------------------------------------ int index=0; while(index < LEN && a[index] == s) index++; if(index == LEN) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl; ------------------------------------------------------ int index=0; while(index < LEN && a[index] == s) index++; if(index == LEN) cout<<"Search value found"<<endl; else cout<<"Search value NOT found"<<endl;

int index=0; while(index < LEN && a[index] != s) index++; if(index == LEN) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl;

Suppose a program is supposed to merge two files in the following manner: write two lines from the first file, write two lines from the second file, and repeat the process until all of the lines have been written to the new merged file. For example suppose we have the following two input files: File1.txt File2.txt Computer Science English Mathematics Business Engineering Communication Physics History Chemistry Drama Biology Art Then the merged output file would be: File3.txt Computer Science Mathematics English Business Engineering Physics Communication History Chemistry Biology Drama Art Which of the options below is a correct implementation of the program? NOTE: Assume the two input files always have the same number of lines Assume the number of lines is always even in both files Assume both files always have at least two (2) lines The infile1 variable is associated with the File1.txt file The infile2 variable is associated with the File2.txt file The total_lines variable stores the total number of lines in the two files together (8 in the example shown above) (the options are on the next page) Which of the options below is a correct implementation of the program? ------------------------------------- while(infile1>>file_line1 && infile>>file_line2) { outfile<<file_line1<<endl; outfile<<file_line2<<endl; } ------------------------------------- int total_lines = count_lines(filename1) + count_lines(filename2); for(int i=0; i<(total_lines/2); i++) { if((i % 2) == 0) infile1>>file_line; else infile2>>file_line; outfile<<file_line<<endl; } ------------------------------------- Correct! int total_lines = count_lines(filename1) + count_lines(filename2); for(int i=0; i<total_lines; i++) { if((i % 4) <=1) infile1>>file_line; else infile2>>file_line; outfile<<file_line<<endl; } ------------------------------------- infile1>>file_line1; infile2>>file_line2; do { outfile<<file_line1<<endl; outfile<<file_line2<<endl; } while(infile1>>file_line1 && infile>>file_line2);

int total_lines = count_lines(filename1) + count_lines(filename2); for(int i=0; i<total_lines; i++) { if((i % 4) <=1) infile1>>file_line; else infile2>>file_line; outfile<<file_line<<endl; }

Suppose a program is supposed to shift all of the elements in an array one position to the right, and the last element in the array is carried over into the first position. For example, the original array [1, 2, 3, 4] becomes [4, 1, 2, 3] after being shifted to the right by one position. The start of the program is: const int SIZE = 4; int numbers[SIZE] = {1, 2, 3, 4}; int temp, i; Which of the options below is the correct continuation of the program? ------------------------------------ Correct! int value=numbers[0]; int temp, i; for(i=1; i<SIZE; i++) { temp = numbers[i]; numbers[i] = value; value = temp; } numbers[0] = value; ------------------------------------ int value=numbers[0]; int temp, i; for(i=1; i<(SIZE-1); i++) { temp = numbers[i]; numbers[i] = value; value = temp; } numbers[0] = value; ------------------------------------ int value=numbers[0]; int temp, i; for(i=1; i<SIZE; i++) { temp = numbers[i]; numbers[i] = value; value = temp; } numbers[0] = numbers[i]; ------------------------------------ int value=numbers[0]; int temp, i; for(i=1; i<(SIZE-1); i++) { temp = numbers[i]; numbers[i] = value; value = temp; } numbers[0] = numbers[i];

int value=numbers[0]; int temp, i; for(i=1; i<SIZE; i++) { temp = numbers[i]; numbers[i] = value; value = temp; } numbers[0] = value;

Suppose a program has the following singly linked list: Green -> Red -> Blue -> Black -> Orange -> Yellow The pointer head points at the Green node. There is also the following stand-alone node that is not initially part of a list: White The pointer ptr points at the White node. When the program ends there should be two separate linked lists: 1) Green -> Red -> Blue -> Black -> Orange -> Yellow (where head still points at Green) AND 2) White->Black -> Orange -> Yellow (where ptr still points at White) Which of the options below will cause the program to end with the two linked lists shown above? ----------------------------- head->next->next->next = ptr->next; ptr->next = head->next->next->next; ----------------------------- Correct Answer ptr->next = head->next->next->next; head->next->next->next = ptr->next; ----------------------------- head->next->next->next = ptr; ptr->next = head->next->next->next; ----------------------------- ptr->next = head->next->next->next; head->next->next->next = ptr;

ptr->next = head->next->next->next; head->next->next->next = ptr->next;

Suppose a program has the following singly linked list: Samuel -> Jessica -> Robert -> Linda -> Martin -> Amber And the following pointers are in the program: head: points at the Samuel node ptr1: points at the Jessica node ptr2: points at the Jessica node When the program is finished, there should be two lists that are formed: 1) Samuel -> Jessica -> Robert -> Martin -> Amber AND 2) Linda -> Robert -> Martin -> Amber NOTE: What this means is that node Linda is removed from the "main list", meaning the list that starts with node Samuel, which is the head node. However, at the same time the program also ends up forming a smaller "sub list" in which the Linda node also connects with the Robert node -- in other words, in this sub list node Linda is the first node, and it connects to the Robert, Martin, and Amber nodes which are also part of the main list (so node Jessica connects to node Robert and node Linda also connects to node Robert, but again node Linda is NOT part of the main list at the end of the program). Note that there is only one head pointer, which points at node Samuel. Which of the options below will cause the original list to be split into the two lists shown above? ------------------------------- ptr2 = ptr1->next; ptr1=ptr1->next; head->next->next->next = ptr2; ptr1->next = ptr2->next; ptr2->next = ptr1; ------------------------------- Correct! ptr1=ptr1->next; ptr2 = ptr1->next; head->next->next->next = ptr2; ptr1->next = ptr2->next; ptr2->next = ptr1; ------------------------------- ptr2 = ptr1->next; ptr1=ptr1->next; head->next->next = ptr2; ptr1->next = ptr2->next; ptr2->next = ptr1; ------------------------------- ptr1=ptr1->next; ptr2 = ptr1->next; head->next->next = ptr2; ptr1->next = ptr2->next; ptr2->next = ptr1;

ptr1=ptr1->next; ptr2 = ptr1->next; head->next->next->next = ptr2; ptr1->next = ptr2->next; ptr2->next = ptr1;

Suppose a program is supposed to do the following: Reverse the first half of a vector, and reverse the second half of a vector. Assume that the number of elements in the vector is always even. For example, a vector with the following elements: [17, 99, 26, 37, 12, 84, 65, 45] Would become [37, 26, 99, 17, 45, 65, 84, 12] Which of the options below is the correct implementation of the program? Correct! reverse(vec.begin(), vec.end()-(vec.size()/2)); reverse(vec.begin()+(vec.size()/2), vec.end()); reverse(vec.begin(), vec.end()-(vec.size()/2)-1); reverse(vec.begin()+(vec.size()/2), vec.end()-1); reverse(vec.begin(), vec.end()-(vec.size()/2)-1); reverse(vec.begin()+(vec.size()/2)-1, vec.end()-1); reverse(vec.begin(), vec.end()-(vec.size()/2)); reverse(vec.begin()+(vec.size()/2)-1, vec.end());

reverse(vec.begin(), vec.end()-(vec.size()/2)); reverse(vec.begin()+(vec.size()/2), vec.end());

Suppose a program has the following College class: class College { std::string college_name; void set_college_name(std::string c); std::string get_college_name(); }; The functions will be implemented in another file outside of the class definition. Which of the options below is the correct way to implement the set_college_name function? void::set_college_name College(string c) { college_name = c; } void::College set_college_name(string c) { college_name = c; } Correct! void College::set_college_name(string c) { college_name = c; } void set_college_name::College(string c) { college_name = c; }

void College::set_college_name(string c) { college_name = c; }

Suppose a program has the following House class (implementation details are omitted): class House { int num_of_bedrooms; House(); House(int b); void set_num_of_bedrooms(int b); int get_num_of_bedrooms(); }; And for each House object that is created, it needs to be guaranteed that the num_of_bedrooms is at least 1. In other words, input validation logic is needed to make sure that num_of_bedrooms is always at least 1. If code in the application program (i.e., in main) attempts to set num_of_bedrooms to less than 1, the input validation logic will detect this and simply set num_of_bedrooms to 1, and display an error message to the user that the number of bedrooms must be at least 1. Where in the House class would it be appropriate to place this input validation logic? Correct! void set_num_of_bedrooms(int b) int get_num_of_bedrooms() House() House(int b)

void set_num_of_bedrooms(int b)

Suppose a program is supposed to display the text "Everyone loves COMSC 165!". Which of the below programs is the correct implementation of the program? ---------------------------------------------- Correct! void test3() { cout<<"Everyone loves COMSC 165!"; } void test2() { test3(); } void test1() { test2(); } int main() { test1(); } ---------------------------------------------- void test1() { test2(); } void test2() { test3(); } void test3() { cout<<"Everyone loves COMSC 165!"; } int main() { test1(); } ---------------------------------------------- int main() { test1(); } void test1() { test2(); } void test2() { test3(); } void test3() { cout<<"Everyone loves COMSC 165!"; } ---------------------------------------------- int main() { test1(); } void test3() { cout<<"Everyone loves COMSC 165!"; } void test2() { test3(); } void test1() { test2(); }

void test3() { cout<<"Everyone loves COMSC 165!"; } void test2() { test3(); } void test1() { test2(); } int main() { test1(); }

Suppose a program has a singly linked list structure where each linked list node is storing a character. The goal of the program is to print out the contents of every other node, starting from the first node in the linked list. Assume that the linked list always has an odd number of nodes. For example, for the following linked list: Z->K->V->R->X (where the head pointer is pointing at node Z) The output of the program would be: Z V X Assume ptr is initially pointing at the head of the linked list. Which of the options below is the correct implementation of the program? --------------------------------------------- while(ptr->next!= nullptr) { if(ptr->next != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; } --------------------------------------------- while(ptr->next!= nullptr) { ptr=ptr->next; if(ptr->next != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } } --------------------------------------------- while(ptr!= nullptr) { ptr=ptr->next; if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } } --------------------------------------------- Correct! while(ptr!= nullptr) { if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; }

while(ptr!= nullptr) { if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; }


Conjuntos de estudio relacionados

Accounting 2302 Chapter 14: The Statement of Cash Flows

View Set

Physiology Chapter 1 - The Study of Body Function

View Set

KIN 3502 FINAL -- ch 11, kin hhhh, KIN 3502 - Sport Skills & Motor Abilities (Chap. 11), KIN 3502 - Final - Chap. 12 Quiz (Psychological Measures), KIN 3502 - Exam 2 - Chap. 9 Quiz, KIN 3502 final -- ch 12, Kin 3502 Final- Psychological Measurements

View Set

World History: SOCIAL CHANGES: POSITIVE CONTRIBUTIONS

View Set

BL-Linux Chapter 12 - Shell Scripting

View Set

MUS 110 CH 15 Listening Study Guide: LG 4 Farmer: Fair Phyllis

View Set