Final Exam Prep COMSC-165

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

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

/

Any global that you create should be a what? Constant Reference Variable Parameter

Constant

Which of the options below is equivalent to s->age = 53? (*s).(*age) = 53; *s.age = 53; Correct! (*s).age = 53; *s.*age = 53;

(*s).age = 53;

#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 2 -1 3 -2 4 0 0 0 0 0 0 0 2 0 3 0 4 0 0 -1 0 -2 0

0 0 -1 0 -2 0

#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 = 0; cout<<number<<endl; number--; } void functionTwo() { cout<<number<<endl; ++number; } What is the output of this program? 0 2 0 3 0 4 0 0 0 0 0 0 0 2 -1 3 -2 4 0 0 -1 0 -2 0

0 2 -1 3 -2 4

Suppose a program starts by defining the following: const short int SIZE = 4; short int arr[SIZE] = {15, 30, 45, 60}; short int *ptr = &arr[0]; NOTE: A short int takes up two (2) bytes of memory. And suppose the array is stored in RAM (memory) as follows: Address Value 00000000 15 00000002 30 00000004 45 00000006 60 Then the program continues: for(short int i=0; i<SIZE: i++) cout<<ptr+i<<endl; What is the output of the program? Correct Answer 00000000 00000002 00000004 00000006 00000000 00000001 00000002 00000003 15 30 45 60 You Answered 15 16 17 18

00000000 00000002 00000004 00000006

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. 11110000 01010101 00001111 00110011 Correct Answer 01010101 00001111 00110011 11001100 11110000 01010101

01010101 00001111 00110011 11001100

Suppose a program has the following 2D array named arr: 1 2 3 4 5 6 7 8 9 And the contents of arr are displayed with the following nested for loops: For(int i=0; i<X; i++) { For(int j=0; j<Y; j++) cout<<arr[j][i]<<" "; Cout<<endl; } Where X=3 and Y=3. What is the output of this program? 1 2 3 4 5 6 7 8 9 3 6 9 2 5 8 1 4 7 7 8 9 4 5 6 1 2 3 1 4 7 2 5 8 3 6 9

1 4 7 2 5 8 3 6 9

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? 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 head = ptr; 2 ptr->next = head; 3 ptr->next = prev->next; 4 prev->next = ptr; Correct! 1 ptr->next = head; 2 head = ptr; 3 ptr->next = prev->next; 4 prev->next = ptr;

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 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"; --------------------------------------------- 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"; --------------------------------------------- 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";

In the vector class: 1. The function begin() refers to one position BEFORE the first element in the vector. 2. The function end() refers to one position AFTER the last element in the vector. 1. True 2. False You Answered 1. False 2. False Correct Answer 1. False 2. True 1. True 2. True

1. False 2. True

A delete_node function will attempt to delete a particular node from the linked list. If the linked list is empty, or if the node does not exist in the list, then false will be returned. Otherwise if the node can be successfully deleted, true will be returned. Part of the delete_node function is as follows: NOTE: The head pointer is passed by reference, which is why it's written as *&head bool delete_node(Node *&head, char letter) { Node *ptr = head, *temp = nullptr; if (head == nullptr) return false; if (head->letter == letter) { head = head->next; delete ptr; ptr = nullptr; return true; } 1 ___________________ ptr = ptr->next; if (ptr->next == nullptr) return false; 2 __________________ 3 __________________ delete temp; temp = nullptr; return true; } Which of the options below contains the correct code that could be inserted at lines 1-3 in the code above? -------------------------------- 1 while (ptr->next->letter != letter && ptr->next != nullptr) 2 temp = ptr->next; 3 ptr->next = temp->next; -------------------------------- Correct! 1 while (ptr->next != nullptr && ptr->next->letter != letter) 2 temp = ptr->next; 3 ptr->next = temp->next; -------------------------------- 1 while (ptr->next->letter != letter && ptr->next != nullptr) 2 ptr->next = temp->next; 3 temp = ptr->next; -------------------------------- 1 while (ptr->next != nullptr && ptr->next->letter != letter) 2 ptr->next = temp->next; 3 temp = ptr->next;

1 while (ptr->next != nullptr && ptr->next->letter != letter) 2 temp = ptr->next; 3 ptr->next = temp->next;

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: A->B->C->D->E->F If the search value is "E", true is returned. If the search value is "Z", false is returned. Part of the search_node function is as follows: bool search_node(Node *head, char search_val) { Node * ptr = head; 1 _____________ ptr=ptr->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? Correct! 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr != nullptr) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr != nullptr) 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr == nullptr) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr == nullptr)

1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr != nullptr)

Suppose a program is supposed to ask the user for the name of a file to read from - the file contains numbers that will be read into the program. If the user enters an invalid filename (meaning a file that does NOT exist), the program should repeatedly keep prompting the user for the filename until they enter something valid. Most of the program is shown here: 1 __________ string filename; do { cout<<"Enter input filename: "; getline(cin, filename); file_obj.open(filename); } 2 ____________ ... (code to read from the file - omitted) file_obj.close(); Which of the options below contains the correct code to insert at lines 1 and 2 in the program shown above? 1-. ofstream file_obj; 2-. while(file_obj); Correct Answer 1-. ifstream file_obj; 2-. while(!file_obj); 1-. ofstream file_obj; 2-. while(!file_obj); 1-. ifstream file_obj; 2-. while(file_obj);

1-. ifstream file_obj; 2-. while(!file_obj);

int size = 6; int* ptr = new int[size]; 1. The pointer ptr is stored on the heap. 2. The array of six integers is stored on the heap. 1. True 2. False 1. False 2. False Correct! 1. False 2. True 1. True 2. True

1. False 2. True

Which of the following can use a member initializer list? Correct! Constructor Object Get function Set function

Constructor

Which of the options below shows the correct way to setup an include guard in the Rectangle class? NOTE: The code for the Rectangle class is not shown here. This question is about the correct order of the three statements to make the include guard. 1. #define RECTANGLE_H 2. #ifndef RECTANGLE_H 3. #endif 1. #ifndef RECTANGLE_H 2. #endif 3. #define RECTANGLE_H 1. #define RECTANGLE_H 2. #endif 3. #ifndef RECTANGLE_H Correct! 1. #ifndef RECTANGLE_H 2. #define RECTANGLE_H 3. #endif

1. #ifndef RECTANGLE_H 2. #define RECTANGLE_H 3. #endif

1. C++ performs bounds checking on arrays. 2. If the value inside the subscripts (brackets) of an array is beyond the bounds of that array, then the program will not compile (for example an array named arr has a size of 10, and the program attempts to access arr[50]). 1. True 2. True 1. False 2. True 1. False 2. False 1. True 2. False

1. False 2. False

1. C++ performs bounds checking on arrays. 2. If the value inside the subscripts (brackets) of an array is beyond the bounds of that array, then the program will not compile (for example an array named arr has a size of 10, and the program attempts to access arr[50]). -------------------------------- 1. True 2. True -------------------------------- 1. True 2. False -------------------------------- 1. False 2. False -------------------------------- 1. False 2. True

1. False 2. False

1. It is possible to output the contents of all members of a structure variable using a cout << statement followed by the name of the structure variable (e.g., cout<<car1; where car1 is a Car structure object). 2. A function cannot modify the members of a structure. 1. True 2. False 1. True 2. True 1. False 2. True Correct! 1. False 2. False

1. False 2. False

1. When opening an output file (i.e., a file that will be written to) where the user is asked to enter the filename, there should be a validation loop to make sure that the filename entered by the user already exists. 2. When opening an input file (i.e., a file that will be read from) where the user is asked to enter the filename, if the filename entered by the user does not already exist, then a new file with that name will be created. 1. True 2. True 1. False 2. True 1. True 2. False Correct Answer 1. False 2. False

1. False 2. False

Suppose a pointer named ptr is pointing at a variable of type int. 1. The statement *ptr will print out an address. 2. The statement &ptr will print out a value. Correct! 1. False 2. False 1. True 2. False 1. True 2. True 1. False 2. True

1. False 2. False

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. Correct! 1. False 2. False 1. True 2. False 1. False 2. True 1. True 2. True

1. False 2. False

The * operator has higher precedence than the . operator. The -> operator does not automatically dereference the pointer on its left. Correct! 1. False 2. False 1. False 2. True 1. True 2. False 1. True 2. True

1. False 2. False

int* test_func(); int main() { int* ptr = test_func(); return 0; } int* test_func() { int x=5; return &x; } 1. Since the address of x is returned by the test_func function, x's value (5) will NOT be removed from memory when the test_func function ends. 2. The return &x; statement will lead to a compiler error -- we need to define a local pointer inside of the test_func function, make it point at x, and then return that pointer. Correct! 1. False 2. False 1. True 2. False 1. True 2. True 1. False 2. True

1. False 2. False

1. The declaration vector<int> vec(10, 5); creates a vector of size 5, and initializes all 5 elements of the vector to the value 10. 2. Assuming a vector named numbers1 has already been created and initialized, the declaration vector<int> numbers2(numbers1); can be used to create a new vector named numbers2 that is a copy of the numbers1 vector. Correct! 1. False 2. True 1. True 2. False 1. True 2. True 1. False 2. False

1. False 2. True

1. Vectors are automatically passed by reference. 2. When a vector has reached it's capacity and we want to add another element to the vector, the vector has to be re-created somewhere else in memory with a larger capacity. 1. False 2. False 1. True 2. True 1. True 2. False Correct! 1. False 2. True

1. False 2. True

1. Vectors are automatically passed by reference. 2. When a vector has reached it's capacity and we want to add another element to the vector, the vector has to be re-created somewhere else in memory with a larger capacity. 1. True 2. True 1. False 2. False 1. True 2. False 1. False 2. True

1. False 2. True

1. Vectors are efficient at inserting/removing elements to/from the front of the vector. 2. Vectors are efficient at inserting/removing elements to/from the back of the vector. 1. True 2. False 1. True 2. True 1. False 2. True 1. False 2. False

1. False 2. True

1. Vectors are efficient at inserting/removing elements to/from the front of the vector. 2. Vectors are efficient at inserting/removing elements to/from the back of the vector. 1. True 2. False 1. True 2. True 1. False 2. False Correct! 1. False 2. True

1. False 2. True

Suppose a program is supposed to remove the second and second-to-last elements in a vector. For example, if we have the following vector: [1, 2, 3, 4, 5, 6, 7] Then the new resulting vector will be: [1, 3, 4, 5, 7] When the program is finished. Which of the algorithms below correctly describes the steps that can be taken to do this? 1. Reverse the first two elements of the vector. 2. Call the pop_back() function. 3. Reverse the entire vector. 4. Reverse the first two elements of the vector. 5. Call the pop_back() function. 6. Reverse the entire vector. Correct! 1. Reverse the last two elements of the vector. 2. Call the pop_back() function. 3. Reverse the entire vector. 4. Reverse the last two elements of the vector. 5. Call the pop_back() function. 6. Reverse the entire vector. 1. Reverse the last two elements of the vector. 2. Call the pop_back() function. 3. Reverse the entire vector. 4. Reverse the first two elements of the vector. 5. Call the pop_back() function. 6. Reverse the entire vector. 1. Reverse the first two elements of the vector. 2. Call the pop_back() function. 3. Reverse the entire vector. 4. Reverse the last two elements of the vector. 5. Call the pop_back() function. 6. Reverse the entire vector.

1. Reverse the last two elements of the vector. 2. Call the pop_back() function. 3. Reverse the entire vector. 4. Reverse the last two elements of the vector. 5. Call the pop_back() function. 6. Reverse the entire vector.

1. If an array is partially initialized, the uninitialized elements will be set to zero. 2. If you leave an element uninitialized in an array, you do NOT have to leave all the ones that follow it uninitialized. 1. False 2. True ------------------ 1. True 2. False ------------------ 1. False 2. False ------------------ 1. True 2. True

1. True 2. Fals

1. Calling the push_back() function will always increase the size of the vector (i.e., the value returned by the capacity() vector function). 1. Calling the push_back() function will always increase the capacity of the vector (i.e., the value returned by the capacity() vector function). 1. False 2. False Correct Answer 1. True 2. False 1. False 2. True 1. True 2. True

1. True 2. False

1. nullptr represents address 0. 2. When a pointer is set to point at nullptr, we can dereference the pointer and change the value that is stored at nullptr's location in memory. 1. False 2. True 1. False 2. False Correct! 1. True 2. False 1. True 2. True

1. True 2. False

Suppose the following 2D array has been declared in a program: int arr[4][5]; One of the diagonals that we have with a 2D array goes from the top-left to the bottom-right. Another diagonal goes from the top-right to the bottom-left. 1. With the 2D array declared above, there are 4 cells in the 2D array that are part of the diagonal that goes from the top-left to the bottom-right. 2. With the 2D array declared above, there are 5 cells in the 2D array that are part of the diagonal that goes from the top-right to the bottom-left. 1. True 2. True Correct! 1. True 2. False 1. False 2. False 1. False 2. True

1. True 2. False

1. If a program needs to work with the same file across different functions, then the file object can be passed by reference to each function that works with the file. 2. A file should ideally be closed as soon as the program is done using it. 1. True 2. False 1. False 2. False Correct! 1. True 2. True 1. False 2. True

1. True 2. True

1. It's possible to print out all of the elements in a 2D array just using a single for loop (i.e., no nested looping). 2. A 2D array declared with size int arr[4][3]; takes up the same amount of memory as a 1D array declared with size int arr[12];. Correct Answer 1. True 2. True 1. False 2. False 1. False 2. True 1. True 2. False

1. True 2. True

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. You Answered 1. False 2. False 1. True 2. False 1. False 2. True Correct Answer 1. True 2. True

1. True 2. True

To read from a binary file, the following needs to be specified: 1. Which _ to start reading from in the binary file. 2. How many _ to read from the binary file. 1. bit 2. bytes 1. byte 2. bits Correct! 1. byte 2. bytes 1. bit 2. bits

1. byte 2. bytes

A reference variable is what? An alias for another variable A pointer to another variable A copy of another variable An address of another variable

An alias for another variable

Suppose a program is supposed to swap the first two nodes and the last two nodes of a linked list that has eight nodes. For example, if the linked list is initially: A->B->C->D->E->F->G->H Then after swapping the linked list will be: *G->H*->C->D->E->F->*A->B* NOTE: Here it is the values themselves that are being swapped (e.g., the characters "A" and "G" 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++) 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++)

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++) 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++)

A function is_unique is supposed to return true if the array passed to it has no duplicates. Otherwise, if the array has any duplicates, then the function should return false. For example: const int SIZE = 7; bool result; const char arr[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'A' }; const char arr2[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; result = is_unique(arr, SIZE); // is_unique would return false here result = is_unique(arr2, SIZE); // is_unique would return true here Here is most of the implementation for the is_unique function: bool is_unique(const char * ptr, int size) { const char* ptr2 = nullptr; for (int i = 0; i < (size - 1); i++) { ptr2 = ptr + 1; for (int j = i + 1; j < size; j++) { 1 _______________________ return false; 2 ________________________ } 3 ________________________ } return true; } Which of the options below contains the correct instructions to add at lines 1 -3 in the code shown above? 1. if (*ptr == *ptr2) 2. ptr++; 3. ptr2++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr++; 3. ptr2++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr2++; 3. ptr++; Correct Answer 1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++;

1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++;

A function is_unique is supposed to return true if the array passed to it has no duplicates. Otherwise, if the array has any duplicates, then the function should return false. For example: const int SIZE = 7; bool result; const char arr[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'A' }; const char arr2[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; result = is_unique(arr, SIZE); // is_unique would return false here result = is_unique(arr2, SIZE); // is_unique would return true here Here is most of the implementation for the is_unique function: bool is_unique(const char * ptr, int size) { const char* ptr2 = nullptr; for (int i = 0; i < (size - 1); i++) { ptr2 = ptr + 1; for (int j = i + 1; j < size; j++) { 1 _______________________ return false; 2 ________________________ } 3 ________________________ } return true; } Which of the options below contains the correct instructions to add at lines 1 -3 in the code shown above? 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr++; 3. ptr2++; Correct! 1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr2++; 3. ptr++; 1. if (*ptr == *ptr2) 2. ptr++; 3. ptr2++;

1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++;

A function is_unique is supposed to return true if the array passed to it has no duplicates. Otherwise, if the array has any duplicates, then the function should return false. For example: const int SIZE = 7; bool result; const char arr[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'A' }; const char arr2[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' }; result = is_unique(arr, SIZE); // is_unique would return false here result = is_unique(arr2, SIZE); // is_unique would return true here Here is most of the implementation for the is_unique function: bool is_unique(const char * ptr, int size) { const char* ptr2 = nullptr; for (int i = 0; i < (size - 1); i++) { ptr2 = ptr + 1; for (int j = i + 1; j < size; j++) { 1 _______________________ return false; 2 ________________________ } 3 ________________________ } return true; } Which of the options below contains the correct instructions to add at lines 1 -3 in the code shown above? 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr2++; 3. ptr++; 1. if (*ptr == *ptr2) 2. ptr++; 3. ptr2++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr++; 3. ptr2++; Correct! 1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++;

1. if (*ptr == *ptr2) 2. ptr2++; 3. ptr++;

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; 1. new_node->next = ptr; 2. if (i == (num_of_nodes-1)) 3. ptr->prev = new_node; 1. new_node->prev = ptr; 2. if (i == 0) 3. ptr->next = new_node; Correct! 1. new_node->next = ptr; 2. if (i == 0) 3. ptr->prev = new_node;

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

For which of the options below are BOTH statements true? 1. By default, the ifstream object is used to open files for _ access 2. By default, the ofstream object is used to open files for _ access 1. read 2. write or append Correct! 1. read 2. write 1. write or append 2. read 1. write 2. read

1. read 2. write

Suppose a program has a function showarray, which is supposed to be passed an array, and print out the contents of the array. The correct function prototype is _ The correct function call is _ 1. void showarray(int &arr[], int size); 2. showarray(arr[], 5); 1. void showarray(int &arr[], int size); 2. showarray(arr, 5); 1. void showarray(int arr[], int size); 2. showarray(arr[], 5); Correct! 1. void showarray(int arr[], int size); 2. showarray(arr, 5);

1. void showarray(int arr[], int size); 2. showarray(arr, 5);

A program is supposed to prompt the user for the number of inputs, and then dynamically create an array of integers of that size. For example, if the user responds to the input prompt with the number 5, then an integer array of size 5 should be created dynamically. To help create the array, the program has a function create_array, which has the following prototype: int* create_array(int size); Part of the program is as follows (most of the code is omitted): Int main() { int *iptr = nullptr; int size; ....... (omitted) iptr = create_array(size); ........ (omitted) } int* create_array(int size) { ........ (omitted) } For which of the options below are BOTH statements about this program correct? 1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in create_array (end of the function) 1.. The new statement should be in main (before the call to create_array) 2.. The delete statement should be in create_array (end of the function) 1.. The new statement should be in main (before the call to create_array) 2.. The delete statement should be in main (after the call to create_array) Correct! 1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in main (after the call to create_array)

1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in main (after the call to create_array)

1. Dynamic memory allocation requires the usage of a pointer. 2.Forgetting to delete dynamically allocated memory causes a dangling pointer. Correct Answer 1.. True 2.. False 1.. False 2.. True 1.. True 2.. True 1.. False 2.. False

1.. True 2.. False

1. Memory leaks can cause the system to run out of memory. 2. It's possible to have both a dangling pointer and a memory leak to the same block of dynamically allocated memory. 1.. False 2.. True Correct! 1.. True 2.. False 1.. True 2.. True 1.. False 2.. False

1.. True 2.. False

struct node { int data; 1______________ 2_____________ }; int main() { 3_____________ 4_____________ } Which of the options below correctly shows where the next and head pointers should be declared: Note: the node struct is supposed to be implemented as a singly linked list. 1.. node* next; (in struct node) 2.. node* head; (in struct node) 1.. node* head; (in struct node) 3.. node* next; (in main) 3.. node* head; (in main) 4..node* next; (in main) 1.. node* next; (in struct node) 3.. node* head; (in main)

1.. node* next; (in struct node) 3.. node* head; (in main)

struct node { int data; 1______________ 2_____________ }; int main() { 3_____________ 4_____________ } Which of the options below correctly shows where the next and head pointers should be declared: Note: the node struct is supposed to be implemented as a singly linked list. 1.. node* next; (in struct node) 2.. node* head; (in struct node) 1.. node* head; (in struct node) 3.. node* next; (in main) 3.. node* head; (in main) 4..node* next; (in main) 1.. node* next; (in struct node) 3.. node* head; (in main)

1.. node* next; (in struct node) 3.. node* head; (in main)

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 maintain 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 next pointer (but NOT a prey pointer). A pointer to the head of the list is maintained as well. Suppose a pointer ptr is initialized to point at the head of the list. 1. a while(ptr !=nullptr) loop could be used to iterate through all of the nodes in the list one time. 2. a circular singly linked list with N nodes (where each node stores an integer) takes up more memory than a "regular" singly linked list with N nodes (where each node stores an integer). 1. True 2. True 1.False 2. True 1. True 2. False 1.False 2. False

1.False 2. False

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? 10 2.0 8 2.0 10 3.0 8 3.0

10 3.0

Suppose a binary file is storing the following 48 bits: 10101010 11110000 01010101 00001111 00110011 11001100 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() { char ch; int record_number; cout<<"Enter a record number: "; cin>>record_number; } Suppose the user enters 1 for the record_number, so the program seeks to record 1 (not shown in the code) and then calls the read function with the ch variable passed as an argument (not shown in the code). Which bits will be read from the binary file?* *HINT: Remember that bytes within a binary file are treated like array indexes. Correct! 11110000 10101010 11110000 01010101 00001111 10101010 11110000 01010101 00001111 00110011

11110000

#include <iostream> using namespace std; int number = 10; void functionOne(); void functionTwo(); int main() { functionOne(); functionTwo(); functionOne(); functionTwo(); functionOne(); functionTwo(); system("PAUSE"); return 0; } void functionOne() { int number = 20; cout<<number<<endl; number++; } void functionTwo() { int number = 30; cout<<::number<<endl; ::number++; } What is the output of this program? 10 11 12 13 14 15 20 30 20 30 20 30 20 10 20 11 20 12 10 30 11 30 12 30

20 10 20 11 20 12

for(int i=3; i<9; i+=2) for(int j=-3; j>=-9; j-=2) cout<<i<<": "<<j<<endl; What is the output of the above program? ---------------------------- 3 : -3 3 : -5 3 : -7 3 : -9 5 : -3 5 : -5 5 : -7 5 : -9 7 : -3 7 : -5 7 : -7 7 : -9 9 : -3 9 : -5 9 : -7 9 : -9 ---------------------------- 3 : -3 3 : -5 3 : -7 3 : -9 5 : -3 5 : -5 5 : -7 5 : -9 7 : -3 7 : -5 7 : -7 7 : -9 ---------------------------- 3 : -3 3 : -5 3 : -7 5 : -3 5 : -5 5 : -7 7 : -3 7 : -5 7 : -7 ---------------------------- 3 : -3 3 : -5 3 : -7 5 : -3 5 : -5 5 : -7 7 : -3 7 : -5 7 : -7 9 : -3 9 : -5 9 : -7

3 : -3 3 : -5 3 : -7 3 : -9 5 : -3 5 : -5 5 : -7 5 : -9 7 : -3 7 : -5 7 : -7 7 : -9

Suppose a program is supposed to swap the first half and second half of a linked list. For example, if the linked list is initially: A->B->C->D->E->F After the program has finished the linked list will be: D->E->F->A->B->C NOTE: Here it is the values themselves that are being swapped (e.g., the characters "A" and "D" 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; for(int i=0; i<LIST_SIZE/2; i++) ptr2=ptr2->next; for(int i=0; i<LIST_SIZE/2; i++) { 1 ______________ 2 _______________ temp = ptr1->data; ptr1->data = ptr2->data; ptr2->data = temp; 3 ________________ 4 ________________ } Which of the options below is correct? 1.. ptr1=ptr1->next; 3.. ptr2=ptr2->next; Correct! 3.. ptr1=ptr1->next; 4.. ptr2=ptr2->next; 1.. ptr1=ptr1->next; 2.. ptr2=ptr2->next; 1.. ptr2=ptr2->next; 3.. ptr1=ptr1->next;

3.. ptr1=ptr1->next; 4.. ptr2=ptr2->next;

Suppose a program has the following 2D array named arr: 1 2 3 4 5 6 7 8 9 And the contents of arr are displayed with the following nested for loops: for(int i=X-1; i>=0; i--) { for(int j=0; j<Y; j++) cout<<arr[i][j]<<" "; cout<<endl; } Where X=3 and Y=3. What is the output of this program? 1 4 7 2 5 8 3 6 9 Correct! 7 8 9 4 5 6 1 2 3 1 2 3 4 5 6 7 8 9 3 6 9 2 5 8 1 4 7

7 8 9 4 5 6 1 2 3

How many bits are in a byte? Correct! 8 32 16 24

8

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

8 2.0

Suppose a program has the following array: [1, 2, 3] The goal of the program is to print the following: The contents of the array in reverse order AND The contents of the array in forward order So the output of the program would be: 3 2 1 1 2 3 Which of the options below is the correct implementation of the program? C-) int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr--; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl; Correct! A-) int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr++; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl; D-) int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<size; i++) cout<<*(ptr-i)<<endl; ptr++; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; } B-) int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<size; i++) cout<<*(ptr-i)<<endl; ptr--; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; }

A-) int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr++; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl;

Suppose the current state of the heap in memory is as follows: Initial state of the heap: Address Value 00000000 'A' 00000001 00000002 00000003 For each of the options below the initial state of the heap is changed to a different final state. For one of the options below, the transition from the initial state (shown above) to the final state causes a memory leak. Which of the options below describes a memory leak? C-) Address Value -> PTR2 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003 Correct! A-) Address Value 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003 B-) Address Value -> PTR 00000000 00000001 00000002 00000003 D-) Address Value 00000000 00000001 -> PTR 00000002 'B' 00000003

A-) Address Value 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003

All of the following statements are true about function calls EXCEPT: Each parameter has a corresponding argument Arguments are copied into parameters when the function is called Arguments passed by value can change the original variable's value A parameter's scope is the function which uses it

Arguments passed by value can change the original variable's value

Suppose the current state of the heap in memory is as follows: Initial state of the heap: Address Value 00000000 'A' 00000001 00000002 00000003 For each of the options below the initial state of the heap is changed to a different final state. For one of the options below, the transition from the initial state (shown above) to the final state causes a memory leak. Which of the options below describes a memory leak? C-) Address Value -> PTR2 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003 Correct! A-) Address Value 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003 B-) Address Value -> PTR 00000000 00000001 00000002 00000003 D-) Address Value 00000000 00000001 -> PTR 00000002 'B' 00000003

A-) Address Value 00000000 'A' 00000001 -> PTR 00000002 'B' 00000003

Suppose ptr1 and ptr2 are both pointers to integers. if(&ptr1 == *ptr2) cout<<"A"<<endl; Is it ever possible for the above if statement to be true? D-) Yes, if ptr1 and ptr2 are pointing at different variables, but the two variables have the same value. For example, ptr1 is pointing at x and ptr2 is pointing at y, and both x and y have the value 5. B-) No, because &ptr1 is the address of what ptr1 is pointing at, and *ptr2 is the value of what ptr2 is pointing at. (type mismatch) C-) Yes, if ptr1 and ptr2 are both pointing at the same variable. For example, both are pointing at x. Correct! A-) No, because &ptr1 is the address of ptr1 itself, and *ptr2 is the value of what ptr2 is pointing at. (type mismatch)

A-) No, because &ptr1 is the address of ptr1 itself, and *ptr2 is the value of what ptr2 is pointing at. (type mismatch)

struct Person { int age; char gender; double weight; string name; } Person p {35, 'M', 175.5, "Steve"}; Which of the options below correctly represents how the structure object p is stored in memory? D-) RAM (Memory) Address Value Address 0 'S' Address 1 't' Address 2 'e' Address 3 'v' Address 4 'e' ... (gap in memory) Address 24-31 175.5 ... (gap in memory) Address 44 'M' ... (gap in memory) Address 88-91 35 Correct! A-) RAM (Memory) Address Value Address 0-3 35 Address 4 'M' Address 5-12 175.5 Address 13 'S' Address 14 't' Address 15 'e' Address 16 'v' Address 17 'e' C-) RAM (Memory) Address Value Address 0-3 35 ... (gap in memory) Address 20 'M' ... (gap in memory) Address 32-39 175.5 ... (gap in memory) Address 72 'S' Address 73 't' Address 74 'e' Address 75 'v' Address 76 'e' B-) RAM (Memory) Address Value Address 0 'S' Address 1 't' Address 2 'e' Address 3 'v' Address 4 'e' Address 5-12 175.5 Address 13 'M' Address 14-17 35

A-) RAM (Memory) Address Value Address 0-3 35 Address 4 'M' Address 5-12 175.5 Address 13 'S' Address 14 't' Address 15 'e' Address 16 'v' Address 17 'e'

struct Person { int age; char gender; double weight; string name; } Person p {35, 'M', 175.5, "Steve"}; Which of the options below correctly represents how the structure object p is stored in memory? D-) RAM (Memory) Address Value Address 0 'S' Address 1 't' Address 2 'e' Address 3 'v' Address 4 'e' ... (gap in memory) Address 24-31 175.5 ... (gap in memory) Address 44 'M' ... (gap in memory) Address 88-91 35 Correct! A-) RAM (Memory) Address Value Address 0-3 35 Address 4 'M' Address 5-12 175.5 Address 13 'S' Address 14 't' Address 15 'e' Address 16 'v' Address 17 'e' C-) RAM (Memory) Address Value Address 0-3 35 ... (gap in memory) Address 20 'M' ... (gap in memory) Address 32-39 175.5 ... (gap in memory) Address 72 'S' Address 73 't' Address 74 'e' Address 75 'v' Address 76 'e' B-) RAM (Memory) Address Value Address 0 'S' Address 1 't' Address 2 'e' Address 3 'v' Address 4 'e' Address 5-12 175.5 Address 13 'M' Address 14-17 35

A-) RAM (Memory) Address Value Address 0-3 35 Address 4 'M' Address 5-12 175.5 Address 13 'S' Address 14 't' Address 15 'e' Address 16 'v' Address 17 'e'

Suppose a program has a function named checkSolSum, which checks to see if all of the columns in a 3X3 2D array have the same sum or not. The function takes in the 2D array as an argument, and returns true if all of the columns have the same sum; otherwise it returns false. Which of the options below is the correct body of the CheckColSum function? -------------------------------------------------------- B-) bool status = false; int sumColA = values[0][0] + values[1][0] + values[2][0]; int sumColB = values[0][1] + values[1][1] + values[2][1]; int sumColC = values[0][2] + values[1][2] + values[2][2]; if ( (sumColA == sumColB) || (sumColA == sumColC) || (sumColB == sumColC) ) status = true; return status; -------------------------------------------------------- D-) bool status = false; int sumColA = values[0][0] + values[0][1] + values[0][2]; int sumColB = values[1][0] + values[1][1] + values[1][2]; int sumColC = values[2][0] + values[2][1] + values[2][2]; if ( (sumColA == sumColB) || (sumColA == sumColC) || (sumColB == sumColC) ) status = true; return status; -------------------------------------------------------- Correct! A-) bool status = true; int sumColA = values[0][0] + values[1][0] + values[2][0]; int sumColB = values[0][1] + values[1][1] + values[2][1]; int sumColC = values[0][2] + values[1][2] + values[2][2]; if ( (sumColA != sumColB) || (sumColA != sumColC) || (sumColB != sumColC) ) status = false; return status; -------------------------------------------------------- C-) bool status = true; int sumColA = values[0][0] + values[0][1] + values[0][2]; int sumColB = values[1][0] + values[1][1] + values[1][2]; int sumColC = values[2][0] + values[2][1] + values[2][2]; if ( (sumColA != sumColB) || (sumColA != sumColC) || (sumColB != sumColC) ) status = false; return status;

A-) bool status = true; int sumColA = values[0][0] + values[1][0] + values[2][0]; int sumColB = values[0][1] + values[1][1] + values[2][1]; int sumColC = values[0][2] + values[1][2] + values[2][2]; if ( (sumColA != sumColB) || (sumColA != sumColC) || (sumColB != sumColC) ) status = false; return status;

double val = 55.2; double * ptr = &val; For which of the options below will BOTH cout statements print out the address of val? A-) cout<<ptr<<endl; cout<<&val<<endl; D-) cout<<*ptr<<endl; cout<<*val<<endl; C-) cout<<*ptr<<endl; cout<<&val<<endl; B-) cout<<ptr<<endl; cout<<*val<<endl;

A-) cout<<ptr<<endl; cout<<&val<<endl;

double val = 55.2; double * ptr = &val; For which of the options below will BOTH cout statements print out the address of val? D-) cout<<*ptr<<endl; cout<<*val<<endl; Correct! A-) cout<<ptr<<endl; cout<<&val<<endl; B-) cout<<ptr<<endl; cout<<*val<<endl; C-) cout<<*ptr<<endl; cout<<&val<<endl;

A-) cout<<ptr<<endl; cout<<&val<<endl;

Suppose a program is supposed to display 10 rows of '#' characters. Each row should have 15 '#' characters. Which of the options below is the correct implementation of this program? Correct! A-) for(int i=1; i<=10; i++) { for(int j=1; j<=15; j++) cout<<"#"; cout<<endl; } B-) for(int i=1; i<=15; i++) { for(int j=1; j<=10; j++) cout<<"#"; cout<<endl; } C-) for(int i=0; i<=10; i++) { for(int j=0; j<=15; j++) cout<<"#"; cout<<endl; } D-) for(int i=0; i<=15; i++) { for(int j=0; j<=10; j++) cout<<"#"; cout<<endl; }

A-) for(int i=1; i<=10; i++) { for(int j=1; j<=15; j++) cout<<"#"; cout<<endl; }

Which of the following options below is the correct way to declare ptr as a Constant pointer to non-constant data? C-) const int * const ptr = (omitted) B-) const int * ptr = (omitted) D-) const int const * ptr = (omitted) Correct! A-) int * const ptr = (omitted)

A-) int * const ptr = (omitted)

Which of the options below will print out all of the values in the array (99, 101, 250, 399, 415, and 512)? const int SIZE = 6; int arr [SIZE] = {99, 101, 250, 399, 415, 512};\ B-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; (*ptr)++; } A-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; ptr++; } C-) int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; } D-) int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; (*ptr)++; }

A-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; ptr++; }

Which of the options below will print out all of the values in the array (99, 101, 250, 399, 415, and 512)? const int SIZE = 6; int arr [SIZE] = {99, 101, 250, 399, 415, 512}; --------------------------------------------------------------------- B-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; (*ptr)++; } --------------------------------------------------------------------- Correct! A-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; ptr++; } --------------------------------------------------------------------- D-) int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; (*ptr)++; } --------------------------------------------------------------------- C-) int * ptr = &arr; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; }

A-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; 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). Which of the options below is the correct implementation of the program? ----------------------------------------------------------- B-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col+=COLS-1) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; ----------------------------------------------------------- C-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<ROWS; curr_row++) for(int curr_col=0; curr_col<COLS; curr_col+=COLS-1) sum+=arr[curr_row][curr_col]; ----------------------------------------------------------- D-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr+col+=COLS-1) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<ROWS; curr_row++) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; ----------------------------------------------------------- Correct! A-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr+col+=COLS-1) sum+=arr[curr_row][curr_col];

A-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr+col+=COLS-1) sum+=arr[curr_row][curr_col];

A swap algorithm works as follows: There are two pointers, x and y. x points at num1 and y points at num2. The values of num1 and num2 need to be swapped using the pointers x and y. For example if num1 is initially 5 and num2 is initially 10, then num1=10 and num2=5 after the swap algorithm has finished executing. Which of the options below is the correct implementation of the swap algorithm? Correct! A-) int temp = *x; *x = *y; *y = temp; C-) int temp = &x; *x = &y; *y = temp; B-) int* temp = *x; *x = *y; *y = *temp; D-) int* temp = &x; *x = &y; *y = &temp;

A-) int temp = *x; *x = *y; *y = temp;

A swap algorithm works as follows: There are two pointers, x and y. x points at num1 and y points at num2. The values of the two pointers are swapped. For example if num1 is initially 5 and num2 is initially 10, then num1=10 and num2=5 after the swap algorithm has finished executing. Which of the options below is the correct implementation of the swap algorithm? D-) int *temp = nullptr; *temp = &x; *x = &y; *y = &temp; (' = *) B-) int *temp = nullptr; 'temp = 'x; 'x = 'y; 'y = 'temp; C-) int temp; temp = &x; *x = &y; *y = temp; (' = *) A-) int temp; temp = 'x; 'x = 'y; 'y = temp;

A-) int temp; temp = 'x; 'x = 'y; 'y = temp; (' = *)

While there is a built-in push_back() method for vectors, there is no built-in push_front method. Suppose a program needs a push_front() method that will add an item to the beginning of the vector. For example if the original vector is [2, 3, 4, 5], then after passing in this vector and the value 1 to push_front() the new resulting vector will be [1, 2, 3, 4, 5]. Which of the options below is the correct implementation of push_front()? B-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>0; i--) v[i-1] = v[i]; v[0] = value; } A-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>0; i--) v[i] = v[i-1]; v[0] = value; } C-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>0; i--) v[i] = v[i-1]; v[0] = value; } D-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>0; i--) v[i-1] = v[i]; v[0] = value; }

A-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>0; i--) v[i] = v[i-1]; v[0] = value; }

Suppose two players are playing a modified version of the game Battleship on a 10 X 10 game board. In this modified version of the game, there is only ONE (1) battleship that takes up FIVE (5) spaces on the game board. NOTE: In Battleship, ships can be placed only horizontally or vertically. They CANNOT be placed diagonally. The current state of the game board for player 1 is shown below. Note the following: An empty cell means player 1 has not tried to hit that cell on the game board yet. A cell with an "*" means player 1 hit that cell, but they missed (i.e., player 2 did not have part of their ship on that cell) A cell with an "X" means player 1 hit that cell and successfully hit part of player 2's ship Player 1's game board is represented by a 10X10 2D array with the name player1. Based on the game board shown above, all of the following options below would be logical moves for player 1 to make on their next turn EXCEPT: D-) player1[6][3] B-) player1[5][2] A-) player1[4][3] C-) player1[5][4]

A-) player1[4][3]

#include <iostream> #include <string> using namespace std; void functionOne(string, string); int main() { string value1 = "A", value2 = "B"; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; system("PAUSE"); return 0; } void functionOne(string param1, string param2) { param1+= "1"; param2+= "2"; cout<<param1<<" "<<param2<<endl; } What is the output of this program? HINT: Think about how the string arguments are being passed to the functionOne function. A1 B2 A1 B2 A11 B22 A11 B22 A111 B222 A111 B222 A1 B2 A B A1 B2 A B A1 B2 A B A1 B2 A B2 A1 B22 A B22 A1 B222 A B222 A1 B2 A1 B A11 B2 A11 B A111 B2 A111 B

A1 B2 A B A1 B2 A B A1 B2 A B

#include <iostream> #include <string> using namespace std; void functionOne(string &, string); int main() { string value1 = "A", value2 = "B"; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; system("PAUSE"); return 0; } void functionOne(string &param1, string param2) { param1+= "1"; param2+= "2"; cout<<param1<<" "<<param2<<endl; } What is the output of this program? A1 B2 A B A1 B2 A B A1 B2 A B A1 B2 A1 B A11 B2 A11 B A111 B2 A111 B A1 B2 A1 B2 A11 B22 A11 B22 A111 B222 A111 B222 A1 B2 A B2 A1 B22 A B22 A1 B222 A B222

A1 B2 A1 B A11 B2 A11 B A111 B2 A111 B

A reference variable is what? An alias for another variable A copy of another variable An address of another variable A pointer to another variable

An alias for another variable

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 O X X X O Then player X has won because they got three X's in a row. 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 row? C) 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 row"; D) 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 row"; Correct Answer B) 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 row"; A) 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 row";

B) 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 row";

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! B-) 1 ptr->next = head; 2 head = ptr; 3 ptr->next = prev->next; 4 prev->next = ptr; A-) 1 ptr->next = head; 2 head = ptr; 3 prev->next = ptr; 4 ptr->next = prev->next; D-) 1 head = ptr; 2 ptr->next = head; 3 ptr->next = prev->next; 4 prev->next = ptr; C-) 1 head = ptr; 2 ptr->next = head; 3 prev->next = ptr; 4 ptr->next = prev->next;

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

1. The name of an array cannot be dereferenced. 2. You can change the address that an array name points to. A-) True True C-) True False D-) False True Correct! B-) False False

B-) 1. False 2. False

uppose 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? A-) 1.. for(int i=0 i<COLS; i++) 2.. for(int j=ROWS-1; j>=0; j--) Correct Answer B-) 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=0; j<ROWS; j++) D-) 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=ROWS-1; j>=0; j--) C-) 1.. for(int i=0 i<COLS; i++) 2.. for(int j=0; j<ROWS; j-++)

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

Suppose there are two pointers, ptr1 and ptr2. Both are pointers to integers. if(ptr1 == ptr2) cout<<"Same A"<<endl; else cout<<"Different A"<<endl; ' = * if('ptr1 == 'ptr2) cout<<"Same B"<<endl; else cout<<"Different B"<<endl; All of the following combinations of output messages are possible EXCEPT: C-) Different A Same B D-) Different A Different B A-) Same A Same B B-) Same A Different B

B-) Same A Different B

Suppose a program has the following 4X4 2D array with 16 (sixteen) cells: *1* 2 3 4 5 *6* 7 8 9 10 *11* 12 13 14 15 *16* The goal of the program is to print out the four bolded diagonal values in the following order: 16, 11, 6, 1 Which of the options below is the correct implementation? A-) for(int curr_row=0, curr_col=0; curr_row<ROWS && curr_col<COLS; curr_row++, curr_col++) cout<<arr[curr_row][curr_col]<<endl; C-) for(int curr_row=0, curr_col=COLS-1; curr_row<ROWS && curr_col>=0; curr_row++, curr_col--) cout<<arr[curr_row][curr_col]<<endl; D-) for(int curr_row=ROWS-1, curr_col=0; curr_row>=0 && curr_col<COLS; curr_row--, curr_col++) cout<<arr[curr_row][curr_col]<<endl; B-) for(int curr_row=ROWS-1, curr_col=COLS-1; curr_row>=0 && curr_col>=0; curr_row--, curr_col--) cout<<arr[curr_row][curr_col]<<endl;

B-) for(int curr_row=ROWS-1, curr_col=COLS-1; curr_row>=0 && curr_col>=0; curr_row--, curr_col--) cout<<arr[curr_row][curr_col]<<endl;

A program is supposed to swap the first and last columns of a 2Darray. For example if we have the following initial 2D array: *1* 2 3 *4* *5* 6 7 *8* *9* 10 11 *12* Then after swapping the first and last columns the 2D array will look like this: *4* 2 3 *1* *8* 6 7 *5* *12* 10 11 *9* Which of the options below is the correct implementation of this program? A-) for(int i=0; i<(ROWS-1); i++) { temp = arr[i][0]; arr[i][0] = arr[i][COLS-1]; arr[i][COLS-1] = temp; } C-) for(int i=0; i<(COLS-1); i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; } D-) for(int i=0; i<COLS; i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; } B-) for(int i=0; i<ROWS; i++) { temp = arr[i][0]; arr[i][0] = arr[i][COLS-1]; arr[i][COLS-1] = temp; }

B-) for(int i=0; i<ROWS; i++) { temp = arr[i][0]; arr[i][0] = arr[i][COLS-1]; arr[i][COLS-1] = temp; }

Which of the following options below will print out the addresses of all of the elements of an array? Correct! B-) for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl; D-) for(int i=0; i<SIZE; i++) cout<<&(arr+i)<<endl; C-) for(int i=0; i<SIZE; i++) cout<<*(arr+i)<<endl; A-) for(int i=0; i<SIZE; i++) cout<<*arr[i]<<endl;

B-) for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl;

Suppose a program is supposed to display 15 rows of '#' characters. Each row should have 10 '#' characters. Which of the options below is the correct implementation of this program? A-) for(int i=1; i<=10; i++) { for(int j=1; j<=15; j++) cout<<"#"; cout<<endl; } B-) for(int i=1; i<=15; i++) { for(int j=1; j<=10; j++) cout<<"#"; cout<<endl; } C-) for(int i=0; i<=10; i++) { for(int j=0; j<=15; j++) cout<<"#"; cout<<endl; } D-) for(int i=0; i<=15; i++) { for(int j=0; j<=10; j++) cout<<"#"; cout<<endl; }

B-) for(int i=1; i<=15; i++) { for(int j=1; j<=10; j++) cout<<"#"; cout<<endl; }

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 red 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 program should calculate the sum of the 19 bolded red 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). Which of the options below is the correct implementation of the program? A-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr+col+=COLS-1) sum+=arr[curr_row][curr_col]; Correct! B-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col+=COLS-1) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; D-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr+col+=COLS-1) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<ROWS; curr_row++) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; C-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<ROWS; curr_row++) for(int curr_col=0; curr_col<COLS; curr_col+=COLS-1) sum+=arr[curr_row][curr_col];

B-) int sum = 0; for(int curr_row=0; curr_row<ROWS; curr_row+=ROWS-1) for(int curr_col=0; curr_col<COLS; curr_col+=COLS-1) sum+=arr[curr_row][curr_col]; for(int curr_row=1; curr_row<(ROWS-1); curr_row++) for(int curr_col=0; curr_col<COLS; curr_col++) sum+=arr[curr_row][curr_col];

Suppose a program has a 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 second node.in the linked list. For example for the following linked list: A->B->D->C->E->F The output of the program would be: B D F Assume ptr is initially pointing at the head of the linked list. Which of the options below is the correct implementation of the program? Correct! B-) while(ptr!= nullptr) { ptr=ptr->next; if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } } D-) while(ptr->next!= nullptr) { ptr=ptr->next; if(ptr->next != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } } A-) while(ptr!= nullptr) { if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; } C-) while(ptr->next!= nullptr) { if(ptr->next != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; }

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

Suppose a program has a function showarray, which is supposed to be passed an array, and print out the contents of the array. 1. The correct function prototype is _ 2. The correct function call is _ C-) 1. void showarray(int &arr[], int size); 2. showarray(arr[], 5); A-) 1. void showarray(int arr[], int size); 2. showarray(arr[], 5); D-) 1. void showarray(int &arr[], int size); 2. showarray(arr, 5); B-) 1. void showarray(int arr[], int size); 2. showarray(arr, 5);

B-) 1. void showarray(int arr[], int size); 2. showarray(arr, 5);

Suppose a program has the following structure: struct Student { string name; char letter_grade; double test_score; bool has_graduated; }; All of the options below contain initializations that are legal EXCEPT: C-) Student s = {"Bruce Wayne", A}; A-) Student s = {"James Bond"}; D-) Student s = {"Luke Skywalker", A, 97.2}; Correct! B-) Student s = {true};

B-) Student s = {true};

Suppose a program has the following code: const int X = 3, Y = 4; int sum=0; int values[X][Y] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; for(int i=0, j=0; i<X; i++, j++) sum+=values[i][j]; cout<<sum<<endl; sum=0; for(int i=0; i<X; i++) sum+=values[i][Y-i-1]; cout<<sum<<endl; What is this program getting the sum of? A-) All of the elements of the 2D array B-) The diagonals of the 2D array C-) The rows of the 2D array D-) The columns of the 2D array

B-) The diagonals of the 2D array

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: [99, 12, 33, 57, 68] The function would return the number 12, which is to the left of the middle element (33).. 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<int> &v) { 1 ______________ v.pop_back(); return v.at(v.size()-1); } Which of the options below contains the correct loop header to insert at line 1 in the code shown above? D-) for(int i=0; i<=(v.size()+1); i++) Correct! B-) for(int i=0; i<(v.size()+1); i++) C-) for(int i=0; i<=(v.size()+2); i++) A-) for(int i=0; i<(v.size()+2); i++)

B-) for(int i=0; i<(v.size()+1); i++)

A function find_middle is supposed to find and return the middle element in a vector. For example, if the vector has the following elements: [99, 12, 33, 57, 68] The function would return the number 33. NOTE: Assume the vector passed to the function always has an odd number of elements (1, 3, 5, etc.) int find_middle(vector<int> &v) { 1 ______________ v.pop_back(); return v.at(v.size()-1); } Which of the options below contains the correct loop header to insert at line 1 in the code shown above? A-) for(int i=0; i<v.size(); i++) C-) for(int i=0; i<=v.size(); i++) D-) for(int i=0; i<=(v.size()-1); i++) B-) for(int i=0; i<(v.size()-1); i++)

B-) for(int i=0; i<(v.size()-1); i++)

Which of the options below is the correct way to declare a 2D array in a function header/prototype? B-) void display(int arr[][COLS], int ROWS); A-) void display(int arr[ROWS][], int COLS); C-) void display(int arr[ROWS][COLS]); D-) void display(int arr[][], int ROWS, int COLS);

B-) void display(int arr[][COLS], int ROWS);

Which of the options below is the correct way to declare a 2D array in a function header/prototype? C-) void display(int arr[ROWS][COLS]); A-) void display(int arr[ROWS][], int COLS); D-) void display(int arr[][], int ROWS, int COLS); B-) void display(int arr[][COLS], int ROWS);

B-) void display(int arr[][COLS], int ROWS);

Suppose a program has a structure named Computer. There is a function print_computer which is passed a Computer structure instance, and prints out the information for it. The function is supposed to follow the following criteria: The function should have read-only access to the Computer structure instance that is passed to it. The function should not waste any space in memory. Which of the options below is the correct prototype for the print_computer function? Correct! B-) void print_computer(const Computer& c); D-) void print_computer(Computer& c); A-) void print_computer(const Computer c); C-) void print_computer(Computer c);

B-) void print_computer(const Computer& c);

Suppose the following code is in main: int main() { const int SIZE = 5; int arr[SIZE] = {10, 20, 30, 40, 50}; int* ptr = arr; } And the goal is to print out the values 10 and 30 from the array. Which of the options below is correct? Correct! C-) for(int i=0; i<(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; } term-1 A-) for(int i=0; i<(SIZE/2+1); i++) { cout<<*ptr<<endl; ptr+=2; } B-) for(int i=0; i<=(SIZE/2+1); i++) { cout<<*ptr<<endl; ptr+=2; } D-) for(int i=0; i<=(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; }

C-) for(int i=0; i<(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; }

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 allow the function to still be able to complete its task, but at the same time would violate the principle of least privilege? Build_array: Make the array parameter constant Build_array: Make the array parameter NON-constant Print_array: Make the array parameter NON-constant Print_array: Make the array parameter constant

Build_array: Make the array parameter constant

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? Print_array: Make the array parameter NON-constant Build_array: Make the array parameter NON-constant Print_array: Make the array parameter constant Correct! Build_array: Make the array parameter constant

Build_array: Make the array parameter constant

A palindrome is a word that is spelled the same both backwards and forwards. For example, "bob" is a palindrome because it is spelled the same both backwards and forwards. "racecar" is also a palindrome for the same reason. "COMSC 165 is not a palindrome because it is not spelled the same forwards and backwards. Suppose there is a function is_NOT_palin, which takes in a vector of type char as an argument, and returns true if the vector does NOT contain a palindrome; otherwise false is returned if the vector IS a palindrome. NOTE: This function calls a pop_front function that will pop the front of the vector. bool is_NOT_palin(vector<char> &v) { while(!v.empty()) { 1 _________________ 2 ____________ pop_front(v); if(!v.empty()) v.pop_back(); } 3 ________________ } Which of the options below contains the correct code to insert at lines 1, 2, and 3 in the code shown above? Correct Answer C-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return true 3.. return false D-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return false 3.. return true B-) 1.. if(v.at(0) == v.at(v.size()-1)) 2.. return false 3.. return true A-) 1.. if(v.at(0) == v.at(v.size()-1)) 2.. return true 3.. return false

C-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return true 3.. return false

Suppose a program starts by defining the following: const short int SIZE = 4; short int arr[SIZE] = {15, 30, 45, 60}; short int *ptr = &arr[0]; And suppose the array is stored in RAM (memory) as follows: Address Value 00000000 15 00000002 30 00000004 45 00000006 60 Then the program continues: for(short int i=0; i<SIZE: i++) cout<<*ptr+i<<endl; What is the output of the program? A-) 00000000 00000001 00000002 00000003 D-) 15 30 45 60 B-) 00000000 00000002 00000004 00000006 Correct! C-) 15 16 17 18

C-) 15 16 17 18

Suppose a program starts by defining the following: const short int SIZE = 4; short int arr[SIZE] = {15, 30, 45, 60}; short int *ptr = &arr[0]; And suppose the array is stored in RAM (memory) as follows: Address Value 00000000 15 00000002 30 00000004 45 00000006 60 Then the program continues: for(short int i=0; i<SIZE: i++) cout<<*ptr+i<<endl; What is the output of the program? D-) 15 30 45 60 A-) 00000000 00000001 00000002 00000003 C-) 15 16 17 18 B-) 00000000 00000002 00000004 00000006

C-) 15 16 17 18

Suppose a program is supposed to swap the first half and second half of a linked list. For example, if the linked list is initially: A->B->C->D->E->F After the program has finished the linked list will be: D->E->F->A->B->C NOTE: Here it is the values themselves that are being swapped (e.g., the characters "A" and "D" 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; for(int i=0; i<LIST_SIZE/2; i++) ptr2=ptr2->next; for(int i=0; i<LIST_SIZE/2; i++) { 1 ______________ 2 _______________ temp = ptr1->data; ptr1->data = ptr2->data; ptr2->data = temp; 3 ________________ 4 ________________ } Which of the options below is correct? A-) 1.. ptr1=ptr1->next; 2.. ptr2=ptr2->next; D-) 1.. ptr2=ptr2->next; 3.. ptr1=ptr1->next; B-) 1.. ptr1=ptr1->next; 3.. ptr2=ptr2->next; Correct! C-) 3.. ptr1=ptr1->next; 4.. ptr2=ptr2->next;

C-) 3.. ptr1=ptr1->next; 4.. ptr2=ptr2->next;

Suppose a program has the following 4X4 2D array with 16 (sixteen) cells: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The goal of the program is to print out the four bolded diagonal values in the following order: 4, 7, 10, 13 Which of the options below is the correct implementation? D-) for(int curr_row=ROWS-1, curr_col=0; curr_row>=0 && curr_col<COLS; curr_row--, curr_col++) cout<<arr[curr_row][curr_col]<<endl; Correct Answer C-) for(int curr_row=0, curr_col=COLS-1; curr_row<ROWS && curr_col>=0; curr_row++, curr_col--) cout<<arr[curr_row][curr_col]<<endl; B-) for(int curr_row=ROWS-1, curr_col=COLS-1; curr_row>=0 && curr_col>=0; curr_row--, curr_col--) cout<<arr[curr_row][curr_col]<<endl; A-) for(int curr_row=0, curr_col=0; curr_row<ROWS && curr_col<COLS; curr_row++, curr_col++) cout<<arr[curr_row][curr_col]<<endl;

C-) for(int curr_row=0, curr_col=COLS-1; curr_row<ROWS && curr_col>=0; curr_row++, curr_col--) cout<<arr[curr_row][curr_col]<<endl;

Suppose a function is supposed to swap the first half and second half of a vector. Assume the vector passed to the function always has an even number of elements (0, 2, 4, etc.) For example: The initial vector [1, 2, 3, 4, 5, 6] Would become [4, 5, 6, 1, 2, 3] int temp; 1 ______________ { temp = v.at(i); v.at(i)= v.at(j); v.at(j) = temp; } Which of the options below contains the correct loop header to insert at line 1 in the code shown above? C-) for(int i=0, j=v.size()/2; j<v.size(); i++, j++) B-) for(int i=0, j=v.size()/2+1; j<v.size(); i++, j++) A-) for(int i=0, j=v.size()/2; j<(v.size()-1); i++, j++) D-) for(int i=0, j=v.size()/2+1; j<(v.size()-1); i++, j++)

C-) for(int i=0, j=v.size()/2; j<v.size(); i++, j++)

Any global that you create should be a what? Parameter Reference Variable Constant

Constant

A program is supposed to print out the contents of an array in reverse order. For example if the array has the values [1, 2, 3, 4, 5] it will print out 5 4 3 2 1 const int SIZE = 5; Int arr[SIZE] = {1, 2, 3, 4, 5}; Int *ptr = &arr[SIZE-1]; Which of the following options below will print out the contents of an array in reverse order? A-) for(int i=0; i<SIZE; i++) cout<<*(ptr+i)<<" "; D-) for(int i=SIZE-1; i>=0; i--) cout<<*(ptr-i)<<" "; Correct! C-) for(int i=0; i<SIZE; i++) cout<<*(ptr-i)<<" "; B-) for(int i=SIZE-1; i>=0; i--) cout<<*(ptr+i)<<" ";

C-) for(int i=0; i<SIZE; i++) cout<<*(ptr-i)<<" ";

A program is supposed to print out the contents of an array in reverse order. For example if the array has the values [1, 2, 3, 4, 5] it will print out 5 4 3 2 1 const int SIZE = 5; Int arr[SIZE] = {1, 2, 3, 4, 5}; Int *ptr = &arr[SIZE-1]; Which of the following options below will print out the contents of an array in reverse order? You Answered D-) for(int i=SIZE-1; i>=0; i--) cout<<*(ptr-i)<<" "; Correct Answer C-) for(int i=0; i<SIZE; i++) cout<<*(ptr-i)<<" "; A-) for(int i=0; i<SIZE; i++) cout<<*(ptr+i)<<" "; B-) for(int i=SIZE-1; i>=0; i--) cout<<*(ptr+i)<<" ";

C-) for(int i=0; i<SIZE; i++) cout<<*(ptr-i)<<" ";

Suppose a program has a function named find_lowest_in_row, which is supposed to find and return the lowest value in the specified row of a 2D array. The function is passed two arguments: a 2D array named arr, and an integer named search which stores the row number to search for the lowest value in. For example, if search = 2, then this means the function will find and return the lowest value in row 2 of the 2D array. ROWS is a global constant that stores the number of rows in the 2D array. COLS is a global constant that stores the number of columns in the 2D array. NOTE: In general the number of rows and columns in a 2D array can be different. Which of the options below is the correct implementation of the find_lowest_in_row function? A-) int lowest = arr[0][search]; for(int i=0; i<COLS; i++) if(arr[i][search] < lowest) lowest = arr[i][search] return lowest; B-) int lowest = arr[0][search]; for(int i=0; i<ROWS; i++) if(arr[i][search] < lowest) lowest = arr[i][search] return lowest; D-) int lowest = arr[search][0]; for(int i=0; i<ROWS; i++) if(arr[search][i] < lowest) lowest = arr[search][i] return lowest; Correct! C-) int lowest = arr[search][0]; for(int i=0; i<COLS; i++) if(arr[search][i] < lowest) lowest = arr[search][i] return lowest;

C-) int lowest = arr[search][0]; for(int i=0; i<COLS; i++) if(arr[search][i] < lowest) lowest = arr[search][i] return lowest;

Which of the following options below will cause an infinite loop? B-) int num = 10; while(num > 10) { cout<<num<<endl; num++; } D-) int num = 10; while(num <= 10) { cout<<num<<endl; num++; } A-) int num = 10; while(num < 10) { cout<<num<<endl; num++; } Correct! C-) int num = 10; while(num >= 10) { cout<<num<<endl; num++; }

C-) int num = 10; while(num >= 10) { cout<<num<<endl; num++; }

Which of the following options below will cause an infinite loop? D-) int num = 10; while(num <= 10) { cout<<num<<endl; num++; } A-) int num = 10; while(num < 10) { cout<<num<<endl; num++; } C-) int num = 10; while(num >= 10) { cout<<num<<endl; num++; } B-) int num = 10; while(num > 10) { cout<<num<<endl; num++; }

C-) int num = 10; while(num >= 10) { cout<<num<<endl; num++; }

Which of the options below is equivalent to s->age = 53? B-) *s.*age = 53; D-) *s.age = 53; Correct! C-) (*s).age = 53; A-) (*s).(*age) = 53;

C-) (*s).age = 53;

To dereference a structure pointer, the appropriate operator is D-) <- Correct! C-) -> B-) * A-) &

C-) ->

Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3}, {4, 5, 6}}; for(int i=0; i<X; i++) { sum=0; for(int j=0; j<Y; j++) sum+=values[i][j]; cout<<sum<<endl; } What is this program getting the sum of? C-) The rows of the 2D array A-) All of the elements of the 2D array B-) The diagonals of the 2D array D-) The columns of the 2D array

C-) The rows of the 2D array

Suppose a program has a function named print_values. One way this function can be used is to print out all of the elements in an array. This function has two parameter variables: the first is a pointer of type int, and the second is an integer (which represents the number of elements to be printed out). Assume the following has been defined in main: const int SIZE = 5; int arr[SIZE] = {10, 20, 30, 40, 50}; All of the options below contain a valid function call to the print_values function EXCEPT: A-) print_values(arr, SIZE); D-) print_values(&arr[0], SIZE); B-) print_values(arr+0, SIZE); C-) print_values(&arr, SIZE);

C-) print_values(&arr, SIZE);

Suppose a program has a function named print_values. One way this function can be used is to print out all of the elements in an array. This function has two parameter variables: the first is a pointer of type int, and the second is an integer (which represents the number of elements to be printed out). Assume the following has been defined in main: const int SIZE = 5; int arr[SIZE] = {10, 20, 30, 40, 50}; All of the options below contain a valid function call to the print_values function EXCEPT: A-) print_values(arr, SIZE); B-) print_values(arr+0, SIZE); Correct! C-) print_values(&arr, SIZE); D-) print_values(&arr[0], SIZE);

C-) print_values(&arr, SIZE);

HINT: Remember for this question (and every other question on this test), the constant ROWS stores the total number of rows in the 2D array, and the constant COLS stores the total number of columns in the 2D array. Suppose a program has a 3X3 2D array with the following 9 (nine) values: 1 2 3 4 5 6 7 8 9 10 11 *12* 13 14 15 16 And we want to change the bolded value (12) to 99 So after making the change, the updated 2D array is: 1 2 3 4 5 6 7 8 9 10 11 *99* 13 14 15 16 Which of the options below correctly updates the 2D array? D-) arr[ROWS-2][COLS-2] = 99; C-)arr[ROWS-2][COLS-1] = 99; A-) arr[ROWS-1][COLS-1] = 99; B-) arr[ROWS-1][COLS-2] = 99;

C-)arr[ROWS-2][COLS-1] = 99;

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? D) 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"; B) 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"; A) 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"; C) 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";

D) 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";

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? A) 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"; C) 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"; D) 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"; B) 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";

D) 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 create the following 2D array: 93 42 77 68 103 23 Which of the options below contains the correct declaration of this 2D array? B-) int arr[2][3] = { {93, 68}, {42, 103}, {77, 23} }; A-) int arr[3][2] = { {93, 68}, {42, 103}, {77, 23} }; D-) int arr[2][3] = { {93, 42, 77}, {68, 103, 23} }; C-) int arr[3][2] = { {93, 42, 77}, {68, 103, 23} };

D-) int arr[2][3] = { {93, 42, 77}, {68, 103, 23} };

Which of the options below will result in a dangling pointer? A-) int main { int* ptr = new int[5]; ....... ptr = nullptr; delete[] ptr; } Correct! D-) int main { int* ptr = new int[5]; ....... delete[] ptr; } B-) int main { int* ptr = new int[5]; ....... delete[] ptr; ptr = nullptr; } C-) int main { int* ptr = new int[5]; ....... ptr = nullptr; }

D-) int main { int* ptr = new int[5]; ....... delete[] ptr; }

Suppose x and y are both pointers to integers. The goal of the program is to swap the addresses of the two pointers. For example, if x is initially storing the hexadecimal address X0001, and y is initially storing the hexadecimal address X0002, after the program is finished x should be storing X0002 and y should be storing X0001. Which of the options below is the correct implementation of the program? A-) int* temp = &x; x = &y; y = &temp; C-) int* temp = *x; *x = *y; *y = *temp; Correct! D-) int* temp = x; x = y; y = temp; B-) int* temp = &x; *x = &y; *y = &temp;

D-) int* temp = x; x = y; y = temp;

Suppose x and y are both pointers to integers. The goal of the program is to swap the addresses of the two pointers. For example, if x is initially storing the hexadecimal address X0001, and y is initially storing the hexadecimal address X0002, after the program is finished x should be storing X0002 and y should be storing X0001. Which of the options below is the correct implementation of the program? A-) int* temp = &x; x = &y; y = &temp; C-) int* temp = *x; *x = *y; *y = *temp; B-) int* temp = &x; *x = &y; *y = &temp; Correct! D-) int* temp = x; x = y; y = temp;

D-) int* temp = x; x = y; y = temp;

Suppose we have the following stored in RAM (memory): RAM MEMORY Address Value 0X1234ABCD 55 <- X 0XFABC7890 0X1234ABCD <- PTR Where for each row in the table, the address column lists a hexadecimal memory address, and the value column lists the value that is stored at that memory location. For example, the value 55 is stored at memory location 0X1234ABCD. What is the output of the following cout statements? cout<<&ptr<<endl; cout<<ptr<<endl; B-) 55 0X1234ABCD A-) 0XFABC7890 55 C-) 0X1234ABCD 0XFABC7890 Correct! D-) 0XFABC7890 0X1234ABCD

D-) 0XFABC7890 0X1234ABCD

Suppose we have the following stored in RAM (memory): Where for each row in the table, the address column lists a hexadecimal memory address, and the value column lists the value that is stored at that memory location. For example, the value 55 is stored at memory location 0X1234ABCD. What is the output of the following cout statements? cout<<&ptr<<endl; cout<<ptr<<endl; C-) 0X1234ABCD 0XFABC7890 A-) 0XFABC7890 55 D-) 0XFABC7890 0X1234ABCD B-) 55 0X1234ABCD

D-) 0XFABC7890 0X1234ABCD

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? B-) 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"; A-) 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! D-) 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"; C-) 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";

D-) 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: A->B->C->D->E->F If the search value is "E", true is returned. If the search value is "Z", false is returned. Part of the search_node function is as follows: bool search_node(Node *head, char search_val) { Node * ptr = head; 1 _____________ ptr=ptr->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? C-) 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr == nullptr) B-) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr != nullptr) A-) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr == nullptr) Correct! D-) 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr != nullptr)

D-) 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr != nullptr)

1. It is possible to output the contents of all members of a structure variable using a cout << statement followed by the name of the structure variable (e.g., cout<<car1; where car1 is a Car structure object). 2. A function cannot modify the members of a structure. B-) 1. True 2. False A-) 1. True 2. True C-) 1. False 2. True Correct! D-) 1. False 2. False

D-) 1. False 2. False

1. The * operator has higher precedence than the . operator. 2.. The -> operator does not automatically dereference the pointer on its left. A-) 1. True 2. False C-) 1. True 2. True B-) 1. False 2. True Correct! D-) 1. False 2. False

D-) 1. False 2. False

A program is supposed to prompt the user for the number of inputs, and then dynamically create an array of integers of that size. For example, if the user responds to the input prompt with the number 5, then an integer array of size 5 should be created dynamically. To help create the array, the program has a function create_array, which has the following prototype: int* create_array(int size); Part of the program is as follows (most of the code is omitted): Int main() { int *iptr = nullptr; int size; ....... (omitted) iptr = create_array(size); ........ (omitted) } int* create_array(int size) { ........ (omitted) } For which of the options below are BOTH statements about this program correct? A-) 1.. The new statement should be in main (before the call to create_array) 2.. The delete statement should be in main (after the call to create_array) C-) 1.. The new statement should be in main (before the call to create_array) 2.. The delete statement should be in create_array (end of the function) B-) 1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in create_array (end of the function) Correct! D-) 1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in main (after the call to create_array)

D-) 1.. The new statement should in create_array (beginning of the function) 2.. The delete statement should be in main (after the call to create_array

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: Ninth Sixth Third Eighth Fifth Second Seventh Fourth First 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? A-) 1.. for(int i=0 i<COLS; i++) 2.. for(int j=ROWS-1; j>=0; j--) D-) 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=ROWS-1; j>=0; j--) C-) 1.. for(int i=0 i<COLS; i++) 2.. for(int j=0; j<ROWS; j++) B-) 1.. for(int i=COLS-1; i>=0; i--) 2.. for(int j=0; j<ROWS; j++)

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

A palindrome is a word that is spelled the same both backwards and forwards. For example, "bob" is a palindrome because it is spelled the same both backwards and forwards. "racecar" is also a palindrome for the same reason. "COMSC 165 is not a palindrome because it is not spelled the same forwards and backwards. Suppose there is a function is_palin, which takes in a vector of type char as an argument, and returns true if the vector contains a palindrome; otherwise false is returned. NOTE: This function calls a pop_front function that will pop the front of the vector. bool is_palin(vector<char> &v) { while(!v.empty()) { 1 _________________ 2 ____________ pop_front(v); if(!v.empty()) v.pop_back(); } 3 ________________ } Which of the options below contains the correct code to insert at lines 1, 2, and 3 in the code shown above? D-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return false 3.. return true C-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return true 3.. return false B-) 1.. if(v.at(0) == v.at(v.size()-1)) 2.. return false 3.. return true A-) 1.. if(v.at(0) == v.at(v.size()-1)) 2.. return true 3.. return false

D-) 1.. if(v.at(0) != v.at(v.size()-1)) 2.. return false 3.. return true

1. You can use the [] operator to insert a value into a vector that is empty. 2. You can use the at() member function operator to insert a value into a vector that is empty. Correct! D-) False False C-) True True A-) True False B-) False True

D-) False False

1. int x=5, *ptr = nullptr; 2. ptr = &x; 3. ptr = 100; // Store 100 in x 4. cout << x << endl; What is/are the error(s) in the code shown above? B-) Line 2 should be *ptr = *x;Line 3 should be *ptr = 100; C-) Line 2 should be *ptr = &x; Correct! D-) Line 3 should be *ptr = 100; A-) Line 2 should be *ptr = &x;Line 3 should be *ptr = 100;

D-) Line 3 should be *ptr = 100;

int x=5, *ptr = nullptr; ptr = &x; ptr = 100; // Store 100 in x cout << x << endl; What is/are the error(s) in the code shown above? C-) Line 2 should be *ptr = &x; D-) Line 3 should be *ptr = 100; A-) Line 2 should be *ptr = &x; Line 3 should be *ptr = 100; ' = * B-) Line 2 should be *ptr = 'x; Line 3 should be *ptr = 100;

D-) Line 3 should be *ptr = 100;

A program is supposed to swap the first and last rows of a 2Darray. For example if we have the following initial 2D array: *1 2 3 4* 5 6 7 8 *9 10 11 12* Then after swapping the first and last rows the 2D array will look like this: *9 10 11 12* 5 6 7 8 *1 2 3 4* Which of the options below is the correct implementation of this program? Correct! D-) for(int i=0; i<COLS; i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; } A-) for(int i=0; i<(ROWS-1); i++) { temp = arr[i][0]; arr[i][0] = arr[i][COLS-1]; arr[i][COLS-1] = temp; } B-) for(int i=0; i<ROWS; i++) { temp = arr[i][0]; arr[i][0] = arr[i][COLS-1]; arr[i][COLS-1] = temp; } C-) for(int i=0; i<(COLS-1); i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; }

D-) for(int i=0; i<COLS; i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; }

In the rock paper scissors game, both players are supposed to enter either 'p' or 'P' for paper, 'r' or 'R' for rock, or 's' or 'S' for scissors. Which of the while loops below will make sure that player p1 has entered a valid input before continuing with the rest of the program? NOTE: the variable p1 is storing the input entered by player 1 ---------------------------------------------------- D-) while(p1 != 'p' && p1 != 'P' && p1 != 'r' && p1 != 'R' && p1 != 's' && p1 != 'S') { cout<<"ERROR: enter P/p, R/r, or S/s: "; cin>>p1; } ---------------------------------------------------- A-) while((p1 != 'p' || 'P') || (p1 != 'r' || 'R') || (p1 != 's' || 'S')) { cout<<"ERROR: enter P/p, R/r, or S/s: "; cin>>p1; } ---------------------------------------------------- C-) while((p1 != 'p' && 'P') && (p1 != 'r' && 'R') && (p1 != 's' && 'S')) { cout<<"ERROR: enter P/p, R/r, or S/s: "; cin>>p1; } ---------------------------------------------------- B-) while(p1 != 'p' || p1 != 'P' || p1 != 'r' || p1 != 'R' || p1 != 's' || p1 != 'S') { cout<<"ERROR: enter P/p, R/r, or S/s: "; cin>>p1; }

D-) while(p1 != 'p' && p1 != 'P' && p1 != 'r' && p1 != 'R' && p1 != 's' && p1 != 'S') { cout<<"ERROR: enter P/p, R/r, or S/s: "; cin>>p1; }

Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3}, {4, 5, 6}}; for(int i=0; i<Y; i++) { sum=0; for(int j=0; j<X j++) sum+=values[j][i]; cout<<sum<<endl; } What is this program getting the sum of? B-) The diagonals of the 2D array C-) The rows of the 2D array Correct! D-) The columns of the 2D array A-) All of the elements of the 2D array

D-) The columns of the 2D array

There is a close correspondence between the indexes in a 1D array and 2D array of the same size. For example, suppose a program has the following two array declarations: const int SIZE = 4, X=2, Y=2; int arr1[SIZE]; int arr2[X][Y]; Both the 1D array and the 2D array have four indexes (subscripts). For example, suppose the numbers 11, 22, 33, and 44 are being stored. The 1D array would look like this: 11 22 33 44 And the 2D array would look like this: 11 22 33 44 Assume the variable index is storing an index number from the 1D array. Which of the options below will correctly convert an index from the 1D array into the equivalent index in the 2D array? D-) arr2[index/X][index%Y] A-) arr2[index/X][index/Y] B-) arr2[index%X][index%Y] C-) arr2[index%X][index/Y]

D-) arr2[index/X][index%Y]

A 4X4 2D array consists of 16 (sixteen) cells. On this grid, there are two different ways to make four X's diagonally. One of them is as follows: X - - - - X - - - - X - - - - X Think about the second way to make the four x's diagonally. All of the options below are cells that are part of the second diagonal EXCEPT: C-) arr[2][1] D-) arr[2][2] A-) arr[0][3] B-) arr[3][0]

D-) arr[2][2]

Suppose a program has the following structure declaration and main function: struct Student { string name; char letter_grade; double GPA; int units; }; int main() { const int SIZE=3; Student arr[SIZE]; system("PAUSE"); return 0; } All of the options below will correctly print out the starting address of the array arr EXCEPT: Correct Answer D-) cout<<arr.name<<endl; C-) cout<<&arr[0].name<<endl; A-) cout<<arr<<endl; B-) cout<<&arr[0]<<endl;

D-) cout<<arr.name<<endl;

struct Computer { string CPU; }; Suppose there is a function named print_computer, which is supposed to receive a pointer to a computer structure instance, and print the CPU field. Which of the options below is the correct implementation of the print_computer function? B-) void print_computer(Computer c->) { cout << *c.cpu <<endl; } Correct! D-) void print_computer(Computer* c) { cout << c->cpu <<endl; } A-) void print_computer(Computer* c) { cout << *c.cpu <<endl; } C-) void print_computer(Computer c->) { cout << c->cpu <<endl; }

D-)void print_computer(Computer* c){ cout << c->cpu <<endl; }

int sum(int, int, int=0); What is shown in the line of code above? Default reference Default constant Default parameter Default argument

Default argument

display_output(num1, num2, num3); get_menu_choice(); What describes the two lines of code shown above? Function prototypes Function headers Function overloading Function calling

Function calling

int sum1(int, int); int sum2(int, int, int); What describes the two lines of code shown above? Function headers Function calling Function prototypes Function overloading

Function prototypes

int sum1(int, int); int sum2(int, int, int); What describes the two lines of code shown above? Function prototypes Function calling Function overloading Function headers

Function prototypes

Which type of variables should be avoided (i.e., not used)? Parameter Reference Static Global

Global

Which type of variables should be avoided (i.e., not used)? Static Parameter Reference Global

Global

_ are initialized to 0 Local variables Global constants Global variables Local constants

Global

____ are initialized to 0 Local constants Global constants Global variables Local variables

Global variables

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

Suppose a program has the following linked list: A->B->C->D And the goal is delete all of the four nodes in the linked list. Which of the options below is the correct implementation of the program? --------------------------------------------- Node *ptr=head->next->next->next; delete ptr; ptr = head->next->next; delete ptr; ptr=head; delete ptr; delete head; ptr=head=nullptr; --------------------------------------------- Node *ptr=head->next->next->next; delete ptr; ptr = head->next->next; delete ptr; ptr = head->next; delete ptr; ptr=head; delete ptr; ptr=head=nullptr; --------------------------------------------- Node *ptr=head; delete ptr; ptr = head->next; delete ptr; ptr = head->next->next; delete ptr; ptr=head->next->next->next; delete ptr; delete head; ptr=head=nullptr; --------------------------------------------- Node *ptr=head; delete ptr; ptr = head->next; delete ptr; ptr = head->next->next; delete ptr; ptr=head->next->next->next; delete ptr; ptr=head=nullptr;

Node *ptr=head->next->next->next; delete ptr; ptr = head->next->next; delete ptr; ptr = head->next; delete ptr; ptr=head; delete ptr; ptr=head=nullptr;

_ have the same name but different parameters Static variables Default arguments Overloaded functions Function prototypes

Overloaded functions

__ have the same name but different parameters Default arguments Overloaded functions Function prototypes Static variables

Overloaded functions

head | A-> B-> C->D->r->F --------x --------- \ ----------P Suppose a program has the singly linked list shown above. When the program ends there will be two separate linked lists: A->B->C->D->E->F (where head still points at A) and X->D->E->F (where P still points at X) Which of the options below will cause the program to end with the two linked lists shown above? head->next->next->next = P->next; P->next = head->next->next->next; head->next->next->next = P; P->next = head->next->next->next; Correct! P->next = head->next->next->next; head->next->next->next = P->next; P->next = head->next->next->next; head->next->next->next = P;

P->next = head->next->next->next; head->next->next->next = P->next;

head | A-> B-> C->D->r->F --------x --------- \ ----------P Suppose a program has the linked list shown above. The goal of the program is to insert node "X" in-between nodes "C" and "D". Node "A" is the head of the linked-list, and a pointer named P points at node "X". Which of the options below is the correct implementation of the program? --------------------------------------------- head->next->next->next = P->next; P->next = head->next->next->next; --------------------------------------------- P->next = head->next->next->next; head->next->next->next = P->next; --------------------------------------------- head->next->next->next = P; P->next = head->next->next->next; --------------------------------------------- Correct! P->next = head->next->next->next; head->next->next->next = P;

P->next = head->next->next->next; head->next->next->next = P;

head | A-> B-> C->D->r->F --------x --------- \ ----------P Suppose a program has the linked list shown above. The goal of the program is to insert node "X" in-between nodes "C" and "D". Node "A" is the head of the linked-list, and a pointer named P points at node "X". Which of the options below is the correct implementation of the program? P->next = head->next->next->next; head->next->next->next = P->next; head->next->next->next = P; P->next = head->next->next->next; head->next->next->next = P->next; P->next = head->next->next->next; Correct! P->next = head->next->next->next; head->next->next->next = P;

P->next = head->next->next->next; head->next->next->next = P;

Suppose a program has the following linked list: head---- p1 p2 | ---------|--| A-> --------B--> C->D>E->F Where the head of the linked list is A, and two pointers named P1 and P2 both point at node B. The goal of the program is to swap nodes C and D in the linked list, such that the linked list will be: A->B->D->C->E->F Which of the options below is the correct implementation of this program? P2 = P1->next; P1=P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1; P2 = P1->next; P1=P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1; P1=P1->next; P2 = P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1; Correct! P1=P1->next; P2 = P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1;

P1=P1->next; P2 = P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1;

Suppose a program has the following linked list: head---- p1 p2 | ---------|--| A-> --------B--> C->D>E->F Where the head of the linked list is A, and two pointers named P1 and P2 both point at node B. The goal of the program is to swap nodes C and D in the linked list, such that the linked list will be: A->B->D->C->E->F Which of the options below is the correct implementation of this program? P2 = P1->next; P1=P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1; P2 = P1->next; P1=P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1; Correct Answer P1=P1->next; P2 = P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1; You Answered P1=P1->next; P2 = P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1;

P1=P1->next; P2 = P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1;

Suppose a program has the following singly linked list: head---- p1 p2 | ---------|--| A-> --------B--> C->D>E->F Where the head of the linked list is A, and two pointers named P1 and P2 both point at node B. When the program is finished, there will actually be two lists that are formed: A->B->C->E->F and D->C->E->F NOTE: What this means is that node D is removed from the "main list", meaning the list that starts with node A, which is the head node. However, at the same time the program also ends up forming a smaller "sub list" in which the D node also connects with the C node -- in other words, in this sub list node D is the first node, and it connects to the C, E, and F nodes which are also part of the main list (so node B connects to node C and node D also connects to node C, but again node D 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 A. Which of the options below will cause the original list to be split into the two lists shown above? ----------------------------------- P2 = P1->next; P1=P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1; ----------------------------------- Correct! P1=P1->next; P2 = P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1; ----------------------------------- P1=P1->next; P2 = P1->next; head->next->next = P2; P1->next = P2->next; P2->next = P1; ----------------------------------- P2 = P1->next; P1=P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1;

P1=P1->next; P2 = P1->next; head->next->next->next = P2; P1->next = P2->next; P2->next = P1;

---p1 p2 ---- | | A -> B -> C->D>E->F Suppose a program has the following linked list shown above. The goal of the program is to delete node "D". Two pointers named P1 and P2 are both pointing at node "B". Which of the options below will correctly remove node "D" from the linked list? --------------------------------------------- P1->next->next=P1->next->next->next P2=P1->next->next; delete p1; p1=nullptr; --------------------------------------------- P2=P1->next->next; P1->next->next=P1->next->next->next; delete p2; p2=nullptr; ------------------------------------------- P2=P1->next->next; P1->next->next=P1->next->next->next; delete p1; p1=nullptr; --------------------------------------------- P1->next->next=P1->next->next->next; P2=P1->next->next; delete p2; p2=nullptr;

P2=P1->next->next; P1->next->next=P1->next->next->next; delete p2; p2=nullptr;

---p1 p2 ---- | | A -> B -> C->D>E->F Suppose a program has the following linked list shown above. The goal of the program is to delete node "D". Two pointers named P1 and P2 are both pointing at node "B". Which of the options below will correctly remove node "D" from the linked list? --------------------------------------------- P1->next->next=P1->next->next->next P2=P1->next->next; delete p1; p1=nullptr; --------------------------------------------- P2=P1->next->next; P1->next->next=P1->next->next->next; delete p2; p2=nullptr; ------------------------------------------- P2=P1->next->next; P1->next->next=P1->next->next->next; delete p1; p1=nullptr; --------------------------------------------- P1->next->next=P1->next->next->next; P2=P1->next->next; delete p2; p2=nullptr;

P2=P1->next->next; P1->next->next=P1->next->next->next; delete p2; p2=nullptr;

void display_num(int num1) { cout<<num1<<endl; } num1 is a _ variable Static Parameter Local Reference

Parameter

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 allow the function to still be able to complete its task, but at the same time would violate the principle of least privilege? Build_array: Make the array parameter NON-constant Print_array: Make the array parameter constant Print_array: Make the array parameter NON-constant Build_array: Make the array parameter constant

Print_array: Make the array parameter NON-constant

A function definition includes all of the options below EXCEPT: Prototype Body Name Parameter list

Prototype

All of the following are valid ways a function can use a value returned to it EXCEPT: Send the value returned to a cout object Assign the value returned to a variable Use the value returned in an expression Send the value returned to a cin object

Send the value returned to a cin object

All of the following are valid ways a function can use a value returned to it EXCEPT: Assign the value returned to a variable Use the value returned in an expression Send the value returned to a cin object Send the value returned to a cout object

Send the value returned to a cin object

Which type of variable retains its value in-between function calls? Static Reference Global Parameter

Static

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

Upside down V

int rows = 5, curr_row, curr_col; int cols = 2*rows-1, mid_col = cols/2; for (curr_row = 0; curr_row<(rows - 1); curr_row++) { for (curr_col = 0; curr_col<curr_row; curr_col++) cout << " "; cout << "#"; for (curr_col = curr_row + 1; curr_col<(cols - curr_row - 1); curr_col++) cout << " "; cout << "#\n"; } for (curr_col = 0; curr_col<mid_col; curr_col++) cout << " "; cout << "#\n"; What pattern does the program above draw?

V

Suppose a program has a function named prod: int prod(int num1, int num2) { return num1*num2; } num1 is passed by what? Value Parameter Reference Argument

Value

#include <iostream> #include <string> using namespace std; void functionOne(string, string &); int main() { string value1 = "X", value2 = "Y"; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; functionOne(value1, value2); cout<<value1<<" "<<value2<<endl; system("PAUSE"); return 0; } void functionOne(string param1, string &param2) { param1+= "1"; param2+= "2"; cout<<param1<<" "<<param2<<endl; } What is the output of this program? X1 Y2 X1 Y2 X11 Y22 X11 Y22 X111 Y222 X111 Y222 X1 Y2 X Y2 X1 Y22 X Y22 X1 Y222 X Y222 X1 Y2 X Y X1 Y2 X Y X1 Y2 X Y X1 Y2 X1 Y X11 Y2 X11 Y X111 Y2 X111 Y

X1 Y2 X Y2 X1 Y22 X Y22 X1 Y222 X Y222

Suppose a program has a 3X3 2D array with 9 (nine) cells. The middle cell of the 2D array has an 'X' in it: 0 0 0 0 x 0 0 0 0 int ROWS = 3, COLS = 3; char arr[ROWS][COLS]; Which of the options below will store the character 'X' in the middle of the 2D array? arr[ROWS/2-1][COLS/2-1] = 'X'; arr[(ROWS+1)/2][(COLS+1)/2] = 'X'; arr[(ROWS-1)/2][(COLS-1)/2] = 'X'; arr[ROWS/2+1][COLS/2+1] = 'X';

arr[(ROWS-1)/2][(COLS-1)/2] = 'X';

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(Rectangle& r) bool isEqualTo(const Rectangle& r) Correct! bool isEqualTo(const Rectangle& r) const bool isEqualTo(Rectangle& r) const

bool isEqualTo(const Rectangle& r) const

Suppose a program has the following structure declaration and main function: struct Student { string name; char letter_grade; double GPA; int units; }; int main() { const int SIZE=3; Student arr[SIZE]; system("PAUSE"); return 0; } All of the options below will correctly print out the starting address of the array arr EXCEPT: cout<<arr<<endl; cout<<&arr[0]<<endl; cout<<&arr[0].name<<endl; Correct! cout<<arr.name<<endl;

cout<<arr.name<<endl;

Suppose there is an array named numbers. What is the correct syntax for storing numbers entered by the user into the array using a range-based for loop? for(int &val: &numbers) { cout<<"Enter a number: "; cin>>val; } for(int val: numbers) { cout<<"Enter a number: "; cin>>val; } for(int &val: numbers) { cout<<"Enter a number: "; cin>>val; } for(int val: &numbers) { cout<<"Enter a number: "; cin>>val; }

for(int &val: numbers) { cout<<"Enter a number: "; cin>>val; }

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? ----------------------------------- Correct Answer do { cout<<ptr->letter<<endl; ptr=ptr->next; } while(ptr != head); ----------------------------------- while(ptr->next != head) { cout<<ptr->letter<<endl; ptr=ptr->next; } ----------------------------------- do { cout<<ptr->letter<<endl; ptr=ptr->next; } while(ptr->next != head); ----------------------------------- while(ptr != head) { cout<<ptr->letter<<endl; ptr=ptr->next; }

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

int main() { 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 would seek to the position of the "B" character in the file? Correct! file_obj.seekp(1, ios::beg); file_obj.seekp(-1, ios::beg); file_obj.seekp(-2, ios::beg); file_obj.seekp(2, ios::beg);

file_obj.seekp(1, ios::beg);

Suppose a program has the following structure: int MAX = 31; struct Student { int IDnumber[MAX]; char name[MAX]; int age; double GPA; double test_score; }; int main() { Student s; ........... (omitted) 1 ________ } In the omitted portion of main, the user is prompted for input for each of the structure fields (e.g. IDnumber, name, age, etc.). On line 1 in the code above, the program is supposed to write the user's input to a binary file. File_obj 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? file_obj.write(reinterpret_cast<char&>(*s), sizeof(s)); file_obj.write(reinterpret_cast<char&>(&s), sizeof(s)); Correct! file_obj.write(reinterpret_cast<char*>(&s), sizeof(s)); file_obj.write(reinterpret_cast<char*>(*s), sizeof(s));

file_obj.write(reinterpret_cast<char*>(&s), sizeof(s));

Suppose a program is supposed to print out the inside elements (non-edge elements) of a 2D array. For example, for the following 2D array: 1 2 3 4 5* 6 7 *8 9* 10 11 *12 13 14 15 16 The four (4) bolded red values shown above would be printed out. And for the following 2D array: 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 The nine (9) bolded red values shown above would be printed out. Which of the options below is the correct implementation of the program? NOTE: The constant ROWS stores the number of rows, and the constant COLS stores the number of columns. for(curr_row=1; curr_row<=(ROWS-1); curr_row++) for(curr_col=1; curr_col<=(COLS-1); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<(ROWS-2); curr_row++) for(curr_col=2; curr_col<(COLS-2); curr_col++) cout<<arr[curr_row][curr_col]; Correct! for(curr_row=1; curr_row<(ROWS-1); curr_row++) for(curr_col=1; curr_col<(COLS-1); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<=(ROWS-2); curr_row++) for(curr_col=2; curr_col<=(COLS-2); curr_col++) cout<<arr[curr_row][curr_col];

for(curr_row=1; curr_row<(ROWS-1); curr_row++) for(curr_col=1; curr_col<(COLS-1); curr_col++) cout<<arr[curr_row][curr_col];

Suppose a program is supposed to print out the inside elements (non-edge elements) of a 2D array. For example, for the following 2D array: 1 2 3 4 5* 6 7 *8 9* 10 11 *12 13 14 15 16 The four (4) bolded red values shown above would be printed out. And for the following 2D array: 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 The nine (9) bolded red values shown above would be printed out. Which of the options below is the correct implementation of the program? NOTE: The constant ROWS stores the number of rows, and the constant COLS stores the number of columns. for(curr_row=1; curr_row<=(ROWS-1); curr_row++) for(curr_col=1; curr_col<=(COLS-1); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<(ROWS-2); curr_row++) for(curr_col=2; curr_col<(COLS-2); curr_col++) cout<<arr[curr_row][curr_col]; Correct! for(curr_row=1; curr_row<(ROWS-1); curr_row++) for(curr_col=1; curr_col<(COLS-1); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<=(ROWS-2); curr_row++) for(curr_col=2; curr_col<=(COLS-2); curr_col++) cout<<arr[curr_row][curr_col];

for(curr_row=1; curr_row<(ROWS-1); curr_row++) for(curr_col=1; curr_col<(COLS-1); curr_col++) cout<<arr[curr_row][curr_col];

Suppose there is an array named numbers. What is the correct syntax for storing numbers entered by the user into the array using a range-based for loop? for(int val: &numbers) { cout<<"Enter a number: "; cin>>val; } --------------------------------------------- for(int &val: &numbers) { cout<<"Enter a number: "; cin>>val; } --------------------------------------------- for(int val: numbers) { cout<<"Enter a number: "; cin>>val; } --------------------------------------------- for(int &val: numbers) { cout<<"Enter a number: "; cin>>val; }

for(int &val: numbers) { cout<<"Enter a number: "; cin>>val; }

Suppose a function is supposed to swap the first half and second half of a vector, EXCEPT for the middle element which never changes. Assume the vector passed to the function always has an odd number of elements (1, 3, 5, etc.). For example: The initial vector [1, 2, 3, 4, 5, 6, 7] Would become [5, 6, 7, 4, 1, 2, 3] int temp; 1 ______________ { temp = v.at(i); v.at(i)= v.at(j); v.at(j) = temp; } Which of the options below contains the correct loop header to insert at line 1 in the code shown above? for(int i=0, j=v.size()/2+1; j<(v.size()-1); i++, j++) Correct! for(int i=0, j=v.size()/2+1; j<v.size(); i++, j++) for(int i=0, j=v.size()/2; j<v.size(); i++, j++) for(int i=0, j=v.size()/2; j<(v.size()-1); i++, j++)

for(int i=0, j=v.size()/2+1; j<v.size(); i++, j++)

Suppose the following code is in main: int main() { const int SIZE = 5; int arr[SIZE] = {10, 20, 30, 40, 50}; int* ptr = arr; } And the goal is to print out the values 10 and 30 from the array. Which of the options below is correct? Correct! for(int i=0; i<(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; } for(int i=0; i<=(SIZE/2+1); i++) { cout<<*ptr<<endl; ptr+=2; } for(int i=0; i<=(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; } for(int i=0; i<(SIZE/2+1); i++) { cout<<*ptr<<endl; ptr+=2; }

for(int i=0; i<(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; }

Suppose a program is supposed to reverse an array. For example, if we have the array arr = {1, 2, 3, 4, 5}, after reversing the array we would have arr = {5, 4, 3, 2, 1}. Most of the code for the program is shown here: Const int SIZE = 5; int arr[SIZE] = {1, 2, 3, 4, 5}; int firstindex =0, lastindex = SIZE-1, temp; 1 _______________________________ { temp = arr[firstindex]; arr[firstindex] = arr[lastindex]; arr[lastindex] = temp; firstindex++; lastindex--; } All of the following options below could be correctly inserted at line 1 in the code shown above EXCEPT: for(int i=0; i<(SIZE/2-1); i++) while(firstindex <= lastindex) for(int i=0; i<(SIZE/2); i++) while(firstindex < lastindex)

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

Suppose a program is supposed to reverse an array. For example, if we have the array arr = {1, 2, 3, 4, 5}, after reversing the array we would have arr = {5, 4, 3, 2, 1}. Most of the code for the program is shown here: Const int SIZE = 5; int arr[SIZE] = {1, 2, 3, 4, 5}; int firstindex =0, lastindex = SIZE-1, temp; 1 _______________________________ { temp = arr[firstindex]; arr[firstindex] = arr[lastindex]; arr[lastindex] = temp; firstindex++; lastindex--; } All of the following options below could be correctly inserted at line 1 in the code shown above EXCEPT: while(firstindex <= lastindex) for(int i=0; i<(SIZE/2); i++) while(firstindex < lastindex) for(int i=0; i<(SIZE/2-1); i++)

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

Suppose a program is supposed to remove all of the elements in the except, EXCEPT for the first element. For example, if the original vector is: [1, 2, 3, 4, 5] Then after the program is finished the resulting vector will be [1]. Which of the options below is the correct implementation of this program? ----------------------------------------- for(int i=v.size(); i>=0; i--) { v.pop_back(); i++; } ----------------------------------------- for(int i=0; i<v.size(); i++) { v.pop_back(); i--; } ----------------------------------------- for(int i=v.size()-1; i>=0; i--) { v.pop_back(); i++; } ----------------------------------------- Correct! for(int i=0; i<(v.size()-1); i++) { v.pop_back(); i--; }

for(int i=0; i<(v.size()-1); i++) { v.pop_back(); i--; }

Suppose a program has the following vector: [99, 23, 55, 71, 87, 64, 35, 42] The goal is to remove the second, fourth, sixth, eighth, etc. elements from the vector So after the program has finished, the vector should be: [99, 55, 87, 35] Which of the options below is the correct implementation of the program? HINT: the first loop is being used to move the elements in the vector that we want to keep. The second loop is then being used to remove the elements that we want to get rid of. ----------------------------------- for(int i=0; i<(v.size()/2-1); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2); i++) v.pop_back(); ----------------------------------- for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i+1]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back(); ----------------------------------- Correct! for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back(); ----------------------------------- for(int i=0; i<(v.size()/2-1); i++) v[i] = v[2*i+1]; for(int i=0; i<=(v.size()/2); i++) v.pop_back();

for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back();

Suppose a program has the following vector: [99, 23, 55, 71, 87, 64, 35, 42] The goal is to remove the second, fourth, sixth, eighth, etc. elements from the vector So after the program has finished, the vector should be: [99, 55, 87, 35] Which of the options below is the correct implementation of the program? HINT: the first loop is being used to move the elements in the vector that we want to keep. The second loop is then being used to remove the elements that we want to get rid of. ----------------------------------------- for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i+1]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back(); ----------------------------------------- for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back(); ----------------------------------------- for(int i=0; i<(v.size()/2-1); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2); i++) v.pop_back(); ----------------------------------------- for(int i=0; i<(v.size()/2-1); i++) v[i] = v[2*i+1]; for(int i=0; i<=(v.size()/2); i++) v.pop_back();

for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back();

Suppose a program is supposed to display the following numbers: 1 4 7 10 13 All of the options below will correctly display the numbers shown above EXCEPT: ----------------------------------------------- for(int i=0; i<14; i++) if((i % 3) == 1) cout<<i<<endl ----------------------------------------------- for(int i=1; i<15; i++) if((i % 3) == 1) cout<<i<<endl; ----------------------------------------------- for(int i=0; i<14; i+=3) if((i % 3) == 1) cout<<i<<endl; ----------------------------------------------- for(int i=1; i<15; i+=3) if((i % 3) == 1) cout<<i<<endl;

for(int i=0; i<14; i+=3) if((i % 3) == 1) cout<<i<<endl;

Suppose a program is supposed to display the following numbers: 1 4 7 10 13 All of the options below will correctly display the numbers shown above EXCEPT: for(int i=1; i<15; i++) if((i % 3) == 1) cout<<i<<endl; -------------------------------- for(int i=0; i<14; i++) if((i % 3) == 1) cout<<i<<endl -------------------------------- for(int i=1; i<15; i+=3) if((i % 3) == 1) cout<<i<<endl; -------------------------------- for(int i=0; i<14; i+=3) if((i % 3) == 1) cout<<i<<endl;

for(int i=0; i<14; i+=3) if((i % 3) == 1) cout<<i<<endl;

Suppose a program is supposed to do the following: Write the first line of an input file (file1.txt) to an output file (file2.txt) NUM_OF_FILE_LINES times, where NUM_OF_FILE_LINES is the number of lines in the file1.txt file. For example, if the contents of file1.txt are: 12 55 78 99 127 150 Then when the program finishes the contents of file2.txt will be: 12 12 12 12 12 12 NOTE: Some of the code is omitted, but you can assume the following: The infile object is associated with the file file1.txt. This file is opened at the very beginning of the program. The outfile object is associated with the file file2.txt. This file is opened at the very beginning of the program. The NUM_OF_FILE_LINES variable stores the number of lines in the file file1.txt. Both the file1.txt and file2.txt files are properly closed at the very end of the program, after all of the looping is finished. Also a HINT: If a file is closed and then reopened, the current position in the file resets back to the beginning of the file. Which of the options below is the correct implementation of the program? ------------------------------------------ for(int i=0; i<NUM_OF_FILE_LINES; i++) { for(int j=0; j<(NUM_OF_FILE_LINES-i); j++) { infile>>number; outfile<<number<<endl; infile.close(); infile.open("file1.txt"); } } ------------------------------------------ for(int i=0; i<NUM_OF_FILE_LINES; i++) { for(int j=0; j<(NUM_OF_FILE_LINES-i); j++) { infile>>number; outfile<<number<<endl; } infile.close(); infile.open("file1.txt"); } ------------------------------------------ for(int i=0; i<NUM_OF_FILE_LINES; i++) { for(int j=0; j<(NUM_OF_FILE_LINES-i); j++) infile>>number; infile.close(); infile.open("file1.txt"); outfile<<number<<endl; } ------------------------------------------ Correct Answer for(int i=0; i<NUM_OF_FILE_LINES; i++) { for(int j=0; j<(NUM_OF_FILE_LINES-i); j++) { infile>>number; infile.close(); infile.open("file1.txt"); } outfile<<number<<endl; }

for(int i=0; i<NUM_OF_FILE_LINES; i++) { for(int j=0; j<(NUM_OF_FILE_LINES-i); j++) { infile>>number; infile.close(); infile.open("file1.txt"); } outfile<<number<<endl; }

Which of the following options below will print out the addresses of all of the elements of an array? ---------------------------- Correct! for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl; ---------------------------- for(int i=0; i<SIZE; i++) cout<<*arr[i]<<endl; ---------------------------- for(int i=0; i<SIZE; i++) cout<<&(arr+i)<<endl; ---------------------------- for(int i=0; i<SIZE; i++) cout<<*(arr+i)<<endl;

for(int i=0; i<SIZE; i++) cout<<&arr[i]<<endl;

A program is supposed to print out the contents of an array in reverse order. For example if the array has the values [1, 2, 3, 4, 5] it will print out 5 4 3 2 1 const int SIZE = 5; Int arr[SIZE] = {1, 2, 3, 4, 5}; Int *ptr = &arr[SIZE-1]; Which of the following options below will print out the contents of an array in reverse order? ------------------------ for(int i=SIZE-1; i>=0; i--) cout<<*(ptr+i)<<" "; ------------------------ for(int i=SIZE-1; i>=0; i--) cout<<*(ptr-i)<<" "; ------------------------ Correct! for(int i=0; i<SIZE; i++) cout<<*(ptr-i)<<" "; ------------------------ for(int i=0; i<SIZE; i++) cout<<*(ptr+i)<<" ";

for(int i=0; i<SIZE; i++) cout<<*(ptr-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 AAA BBB CCC DDD EEE FFF GGG Then the resulting output file will be: File2.txt AAA BBB CCC EEE FFF GGG 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; 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-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; }

Which of the options below is the correct implementation of the vector clear member function? NOTE: The clear member function will cause the vector to become empty. ---------------------------------- for(int i=v.size()-1; i>=0; i--) { v.pop_back(); i++; } ---------------------------------- for(int i=v.size(); i>=0; i--) { v.pop_back(); i++; } ---------------------------------- for(int i=0; i<v.size(); i++) { v.pop_back(); i--; } ---------------------------------- for(int i=0; i<(v.size()-1); i++) { v.pop_back(); i--; }

for(int i=0; i<v.size(); i++) { v.pop_back(); i--; }

Suppose a program has the following singly linked list: A->B->C->D (where head is pointing at node A, and node D 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 = head->next; head->next->next->next->next = nullptr; head->next->next->next->next = head head->next->next->next->next = head; head = head->next; 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 = head head->next->next->next->next = nullptr;

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

Suppose a program has the following singly linked list: A->B->C->D (where head is pointing at node A, and node D 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 = head->next; head->next->next->next->next = nullptr; head->next->next->next->next = head 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->next->next->next->next = head; head = head->next; head->next->next->next->next = nullptr;

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

Suppose a program has the following linked list: head | A-> B-> C->D And the goal of the program is to make node "B" the new head of the linked list, and move node "A" to the end of the list. The resulting linked list should be : B->C->D->A which of the options below is the correct implementation of the program? --------------------------------------------- head=head->next; head->next->next->next->next=nullptr; head->next->next->next->next=head; --------------------------------------------- head->next->next->next->next=head; head=head->next; head->next->next->next->next=nullptr; --------------------------------------------- head->next->next->next->next=nullptr; head=head->next; head->next->next->next->next=head; --------------------------------------------- head=head->next; head->next->next->next->next=head; head->next->next->next->next=nullptr;

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

Suppose a program has the following linked list: head | A-> B-> C->D And the goal of the program is to make node "B" the new head of the linked list, and move node "A" to the end of the list. The resulting linked list should be : B->C->D->A which of the options below is the correct implementation of the program? --------------------------------------------- head=head->next; head->next->next->next->next=nullptr; head->next->next->next->next=head; --------------------------------------------- head->next->next->next->next=head; head=head->next; head->next->next->next->next=nullptr; --------------------------------------------- head->next->next->next->next=nullptr; head=head->next; head->next->next->next->next=head; --------------------------------------------- head=head->next; head->next->next->next->next=head; head->next->next->next->next=nullptr;

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

Suppose a program has the following linked list: head | A-> B-> C->D And the goal of the program is to make node "B" the new head of the linked list, and move node "A" to the end of the list. The resulting linked list should be : B->C->D->A which of the options below is the correct implementation of the program? --------------------------------------------- head=head->next; head->next->next->next->next=nullptr; head->next->next->next->next=head; --------------------------------------------- head->next->next->next->next=head; head=head->next; head->next->next->next->next=nullptr; --------------------------------------------- head->next->next->next->next=nullptr; head=head->next; head->next->next->next->next=head; --------------------------------------------- head=head->next; head->next->next->next->next=head; head->next->next->next->next=nullptr;

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

Suppose a program has a doubly linked list. A doubly linked list is a list where each node has both a prev and a next pointer, and pointers to both the first node (head) and last node (tail) are maintained. We want to convert an existing doubly linked list into a circular doubly linked list. A circular doubly linked list is a list in which the first node (head) connects to the last node (tail), and the last node (tail) connects to the first node (head). Which of the options below will correctly convert an existing doubly linked list to a circular doubly linked list? Correct! head->prev=tail; tail->next=head; head->next->prev=tail; tail->prev->next=head; head->next=tail; tail->prev=head; head->prev->next=tail; tail->next->prev=head;

head->prev=tail; tail->next=head;

Suppose there was no built-in OR (||) operator in C++. If this were the case, it would still be possible to represent OR (||) in terms of AND (&&) as well as NOT (!). Which of the options below is logically equivalent to: If(P || Q) cout<<"TRUE"; else cout<<"FALSE"; NOTE: P and Q are both variables of type bool --------------------------------------- if (!(!(!P && !Q))) cout<<"TRUE"; else cout<<"FALSE"; --------------------------------------- if (!(!(P && Q))) cout<<"TRUE"; else cout<<"FALSE"; --------------------------------------- if (!(!P && !Q)) cout<<"TRUE"; else cout<<"FALSE"; --------------------------------------- if (!(P && Q)) cout<<"TRUE"; else cout<<"FALSE";

if (!(!P && !Q)) cout<<"TRUE"; else cout<<"FALSE";

Suppose there was no built-in AND (&&) operator in C++. If this were the case, it would still be possible to represent AND (&&) in terms of OR (||) as well as NOT (!). Which of the options below is logically equivalent to: If(P && Q) cout<<"TRUE"; else cout<<"FALSE"; NOTE: P and Q are both variables of type bool ------------------------------- if (!(!(P || Q))) cout<<"TRUE"; else cout<<"FALSE"; ------------------------------- if (!(!P || !Q)) cout<<"TRUE"; else cout<<"FALSE"; ------------------------------- if (!(!(!P || !Q))) cout<<"TRUE"; else cout<<"FALSE"; ------------------------------- if (!(P || Q)) cout<<"TRUE"; else cout<<"FALSE";

if (!(!P || !Q)) cout<<"TRUE"; else cout<<"FALSE";

Suppose a program is supposed to find the second largest element in an array, and store it in a variable named second_largest. Part of the code is shown here: const int SIZE = 10; int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int largest, second_largest; if(arr[0] > arr[1] { largest = arr[0]; second_largest = arr[1]; } else { largest = arr[1]; second_largest = arr[0]; } for(int i=2; i<SIZE; i++) { .......(omitted) } So in this particular example, when the code finishes second_largest would store 9 (and largest would store 10). Which of the options below contains the correct code to place into the body of the for loop shown above? ----------------------------------------- if(arr[i] > second_largest) { second_largest = largest; largest = arr[i]; } else if(arr[i] > largest) largest = arr[i]; ----------------------------------------- if(arr[i] > second_largest) second_largest = arr[i]; else if(arr[i] > largest) { second_largest = largest; largest = arr[i]; } ----------------------------------------- if(arr[i] > largest) largest = arr[i]; else if(arr[i] > second_largest) { second_largest = largest; largest = arr[i]; } ----------------------------------------- if(arr[i] > largest) { second_largest = largest; largest = arr[i]; } else if(arr[i] > second_largest) second_largest = arr[i];

if(arr[i] > largest) { second_largest = largest; largest = arr[i]; } else if(arr[i] > second_largest) second_largest = arr[i];

Suppose there are four integer variables, num1, num2, num3, and num4 And the goal is to write a program that will display "YES" if num 3 is the second largest of the four numbers. Otherwise, the program will display "NO". NOTE: The four numbers are always distinct (i.e., no two numbers are the same) --------------------------------------------------- if(num3 < num1 || (num3 > num2 && num3 > num4)) cout<<"YES"<<endl; else if(num3 < num2 || (num3 > num1 && num3 > num4)) cout<<"YES"<<endl; else if(num3 < num4 || (num3 > num1 && num3 > num2)) cout<<"YES"<<endl; else cout<<"NO"<<endl; --------------------------------------------------- if(num3 < num1 || num3 > num2 || num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 || num3 > num1 || num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 || num3 > num1 || num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl; --------------------------------------------------- if(num3 < num1 && (num3 > num2 || num3 > num4)) cout<<"YES"<<endl; else if(num3 < num2 && (num3 > num1 || num3 > num4)) cout<<"YES"<<endl; else if(num3 < num4 && (num3 > num1 || num3 > num2)) cout<<"YES"<<endl; else cout<<"NO"<<endl; --------------------------------------------------- if(num3 < num1 && num3 > num2 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 && num3 > num1 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 && num3 > num1 && num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl;

if(num3 < num1 && num3 > num2 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 && num3 > num1 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 && num3 > num1 && num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl;

Suppose there are four integer variables, num1, num2, num3, and num4 And the goal is to write a program that will display "YES" if num 3 is the second largest of the four numbers. Otherwise, the program will display "NO". NOTE: The four numbers are always distinct (i.e., no two numbers are the same) if(num3 < num1 && num3 > num2 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 && num3 > num1 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 && num3 > num1 && num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl; if(num3 < num1 || (num3 > num2 && num3 > num4)) cout<<"YES"<<endl; else if(num3 < num2 || (num3 > num1 && num3 > num4)) cout<<"YES"<<endl; else if(num3 < num4 || (num3 > num1 && num3 > num2)) cout<<"YES"<<endl; else cout<<"NO"<<endl; if(num3 < num1 && (num3 > num2 || num3 > num4)) cout<<"YES"<<endl; else if(num3 < num2 && (num3 > num1 || num3 > num4)) cout<<"YES"<<endl; else if(num3 < num4 && (num3 > num1 || num3 > num2)) cout<<"YES"<<endl; else cout<<"NO"<<endl; if(num3 < num1 || num3 > num2 || num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 || num3 > num1 || num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 || num3 > num1 || num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl;

if(num3 < num1 && num3 > num2 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num2 && num3 > num1 && num3 > num4) cout<<"YES"<<endl; else if(num3 < num4 && num3 > num1 && num3 > num2) cout<<"YES"<<endl; else cout<<"NO"<<endl;

Suppose a program has the following code, where vec is a vector: if(vec.empty()) cout<<"A"; else cout<<"B"; Which of the options below contains code that is equivalent to the code shown above? HINT: Make sure to consider all possible scenarios, where the vector may have any number of elements (including no elements). For any possible vector, the program needs to correctly display either "A" or "B" based upon how many elements are in the vector. --------------------------------------------------------------------------- if(vec.size() != 0) cout<<"A"; else cout<<"B"; --------------------------------------------------------------------------- if(vec.capacity() != 0) cout<<"A"; else cout<<"B"; --------------------------------------------------------------------------- if(vec.capacity() == 0) cout<<"A"; else cout<<"B"; --------------------------------------------------------------------------- Correct! if(vec.size() == 0) cout<<"A"; else cout<<"B";

if(vec.size() == 0) cout<<"A"; else cout<<"B";

Which of the following options below is the correct way to declare ptr as a Constant pointer to non-constant data? const int const * ptr = (omitted) Correct! int * const ptr = (omitted) const int * ptr = (omitted) const int * const ptr = (omitted)

int * const ptr = (omitted)

All of the following are legal array declarations EXCEPT: int arr [] = {}; int arr [5] = {}; int arr [5] = {2, 7}; int arr [] = {2, 7};

int arr [] = {};

Suppose a program has the following array: [1, 2, 3] The goal of the program is to print the following: The contents of the array in reverse order AND The contents of the array in forward order So the output of the program would be: 3 2 1 1 2 3 Which of the options below is the correct implementation of the program? -------------------------------- int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<size; i++) cout<<*(ptr-i)<<endl; ptr--; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; } -------------------------------- int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<size; i++) cout<<*(ptr-i)<<endl; ptr++; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr++; } -------------------------------- int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr--; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl; -------------------------------- Correct! int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr++; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl;

int arr[SIZE] = {1, 2, 3}; int *ptr = &arr[SIZE-1]; for(int i=0; i<SIZE; i++) { cout<<*ptr<<endl; ptr--; } ptr++; for(int i=0; i<size; i++) cout<<*(ptr+i)<<endl;

Which of the following options below is legal in C++? int calc(int num1 = 10, int num2 = 20, int num3, 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); int calc(int num1 = 10, int num2, int num3 = 20, int num4);

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

Which of the following options below is legal in C++? int calc(int num1 = 10, int num2, int num3 = 20, int num4); int calc(int num1 = 10, int num2 = 20, int num3, 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);

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

Suppose a program is supposed to read every other line of a file, starting from the second line, and display it on the console screen. For example if a file has the lines: 1111 2222 3333 4444 5555 6666 Then the output on the console screen would be: 2222 4444 6666 Which of the options below is a correct implementation of the program? ------------------------------------------------ int count = 0; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; } ------------------------------------------------ int num_of_lines = count_lines(filename); for(int i=0; i<num_of_lines; i+=2) { getline(file_obj, file_line); cout<<file_line; } ------------------------------------------------ int num_of_lines = count_lines(filename); for(int i=0; i<(num_of_lines/2); i++) { getline(file_obj, file_line); cout<<file_line; } ------------------------------------------------ int count = 1; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; }

int count = 0; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; }

Suppose a program is supposed to read every other line of a file, starting from the second line, and display it on the console screen. For example if a file has the lines: 1111 2222 3333 4444 5555 6666 Then the output on the console screen would be: 2222 4444 6666 Which of the options below is a correct implementation of the program? ---------------------------------------------- int num_of_lines = count_lines(filename); for(int i=0; i<num_of_lines; i+=2) { getline(file_obj, file_line); cout<<file_line; } ---------------------------------------------- Correct! int count = 0; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; } ---------------------------------------------- int num_of_lines = count_lines(filename); for(int i=0; i<(num_of_lines/2); i++) { getline(file_obj, file_line); cout<<file_line; } ---------------------------------------------- int count = 1; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; }

int count = 0; while(getline(file_obj, file_line)) { if((count % 2) == 1) cout<<file_line; count++; }

Suppose a program is supposed to print out the number of odd elements and the number of even elements in an array. For example, if the program has the following array: const int SIZE = 10; int arr[SIZE] = {1, 3, 2, 5, 7, 4, 9, 11, 6, 13}; Then the output of the program would be: Odd: 7 Even: 3 Which of the options below is the correct implementation of the program? --------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<count<<endl; cout<<"Even: "<<SIZE-count<<endl; --------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 2) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl; --------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 2) count++; cout<<"Odd: "<<count<<endl; cout<<"Even: "<<SIZE-count<<endl; Correct Answer --------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl;

int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl;

Suppose a program is supposed to print out the number of odd elements and the number of even elements in an array. For example, if the program has the following array: const int SIZE = 10; int arr[SIZE] = {1, 3, 2, 5, 7, 4, 9, 11, 6, 13}; Then the output of the program would be: Odd: 7 Even: 3 Which of the options below is the correct implementation of the program? int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl; ---------------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<count<<endl; cout<<"Even: "<<SIZE-count<<endl; ---------------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 2) count++; cout<<"Odd: "<<count<<endl; cout<<"Even: "<<SIZE-count<<endl; ---------------------------------------------- int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 2) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl;

int count=0; for(int i=0; i<SIZE; i++) if((arr[i] % 2) != 1) count++; cout<<"Odd: "<<SIZE-count<<endl; cout<<"Even: "<<count<<endl;

Which of the options below will cause a compiler error in a C++ program? int divide(double num1); double divide(int num1); double divide(int num1, int num2, int num3, int num4); int divide(); double divide(int num1); int divide(double num1); int divide(double num1, int num2); double divide(int num1, double num2); int divide(int num1, int num2); int divide(int num2, int num2, int num3); double divide(double num1, double num2); double divide(double num1, double num2, double num3); int divide(int num1, int num2); double divide(int num1, int num2, int num3); int divide(); double divide();

int divide(int num1, int num2); double divide(int num1, int num2, int num3); int divide(); double divide();

Which of the options below will cause a compiler error in a C++ program? int divide(int num1, int num2); int divide(int num2, int num2, int num3); double divide(double num1, double num2); double divide(double num1, double num2, double num3); int divide(double num1); double divide(int num1); double divide(int num1, int num2, int num3, int num4); int divide(); int divide(int num1, int num2); double divide(int num1, int num2, int num3); int divide(); double divide(); double divide(int num1); int divide(double num1); int divide(double num1, int num2); double divide(int num1, double num2);

int divide(int num1, int num2); double divide(int num1, int num2, int num3); int divide(); double divide();

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? int get_menu_choice(int menu_choice) { ....... (omitted) } int get_menu_choice(){ ....... (omitted) } void get_menu_choice() { ....... (omitted) } void get_menu_choice(int menu_choice) { ....... (omitted) }

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

Suppose a program is supposed to print out whether an array is sorted or not. For example, for the array arr = [1, 2, 3, 4, 5], the output would be "Sorted" because the values in the array are in ascending order from left to right. However, for the array arr = [1, 5, 3, 4, 2], the output would be "NOT sorted" because the array is NOT sorted in ascending order from left to right. NOTE: arr is the name of the array, and SIZE is the size of the array Which of the options below is the correct implementation of this program? int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"NOT sorted"<<endl; else cout<<"Sorted"<<endl; ----------------------------------- int i=0; while(i <SIZE && arr[i] < arr[i+1]) i++; if(i == SIZE) cout<<"NOT sorted"<<endl; else cout<<"Sorted"<<endl; ---------------------------------- int i=0; while(i <SIZE && arr[i] < arr[i+1]) i++; if(i == SIZE) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl; ---------------------------------- int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl;

int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl;

Suppose a program is supposed to search an array to see if a value occurs in the array. For example, if the array arr = [1, 2, 3, 4, 5, 6] and the search value search_value = 6, then the program would print out "Search value found", because 6 occurs in the array. On the other hand, if the array arr = [1, 2, 3, 4, 5, 6], and the search value search_value = 7, the program would print out "Search value NOT found", because 7 does NOT occur in the array. NOTE: arr is an array storing numbers, SIZE is the size of the array, and search_value is the value that is being searched for in the array. Which of the options below is the correct implementation of the program? ------------------------------------------------- int i=0; while(i < SIZE && arr[i] != search_value) i++; if(i == SIZE) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl; ------------------------------------------------- int i=0; while(i < SIZE && arr[i] != search_value) i++; if(i == SIZE) cout<<"Search value found"<<endl; else cout<<"Search value NOT found"<<endl; ------------------------------------------------- int i=0; while(i < SIZE && arr[i] == search_value) i++; if(i == SIZE) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl; ------------------------------------------------- int i=0; while(i < SIZE && arr[i] == search_value) i++; if(i == SIZE) cout<<"Search value found"<<endl; else cout<<"Search value NOT found"<<endl;

int i=0; while(i < SIZE && arr[i] != search_value) i++; if(i == SIZE) cout<<"Search value NOT found"<<endl; else cout<<"Search value found"<<endl;

Suppose a program is supposed to print out whether an array is sorted or not. For example, for the array arr = [1, 2, 3, 4, 5], the output would be "Sorted" because the values in the array are in ascending order from left to right. However, for the array arr = [1, 5, 3, 4, 2], the output would be "NOT sorted" because the array is NOT sorted in ascending order from left to right. NOTE: arr is the name of the array, and SIZE is the size of the array Which of the options below is the correct implementation of this program? -------------------------------------------- int i=0; while(i <SIZE && arr[i] < arr[i+1]) i++; if(i == SIZE) cout<<"NOT sorted"<<endl; else cout<<"Sorted"<<endl; -------------------------------------------- int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl; -------------------------------------------- int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"NOT sorted"<<endl; else cout<<"Sorted"<<endl; -------------------------------------------- int i=0; while(i <SIZE && arr[i] < arr[i+1]) i++; if(i == SIZE) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl;

int i=0; while(i <(SIZE-1) && arr[i] < arr[i+1]) i++; if(i == (SIZE-1)) cout<<"Sorted"<<endl; else cout<<"NOT sorted"<<endl;

Which of the options below will result in a dangling pointer? int main { int* ptr = new int[5]; ....... ptr = nullptr; delete[] ptr; } int main { int* ptr = new int[5]; ....... ptr = nullptr; } int main { int* ptr = new int[5]; ....... delete[] ptr; ptr = nullptr; } Correct! int main { int* ptr = new int[5]; ....... delete[] ptr; }

int main { int* ptr = new int[5]; ....... delete[] ptr; }

A swap algorithm works as follows: There are two pointers, x and y. x points at num1 and y points at num2. The values of the two pointers are swapped. For example if num1 is initially 5 and num2 is initially 10, then num1=10 and num2=5 after the swap algorithm has finished executing. Which of the options below is the correct implementation of the swap algorithm? Correct! int temp; temp = *x; *x = *y; *y = temp; int *temp = nullptr; *temp = *x; *x = *y; *y = *temp; int *temp = nullptr; *temp = &x; *x = &y; *y = &temp; int temp; temp = &x; *x = &y; *y = temp;

int temp; temp = *x; *x = *y; *y = temp;

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 AAA 000 BBB 111 CCC 222 DDD 333 Then the merged output file would be: File3.txt AAA BBB 000 111 CCC DDD 222 333 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; } ------------------------------------------------ infile1>>file_line1; infile2>>file_line2; do { outfile<<file_line1<<endl; outfile<<file_line2<<endl; } while(infile1>>file_line1 && infile>>file_line2); ------------------------------------------------ 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; } ------------------------------------------------ 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;

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? ------------------------------------------------ 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-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] = 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 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? -------------------------------------------- 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] = 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] = 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] = 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 a function named dynamic_array, which is supposed to create and return a dynamically allocated array of integers on the heap based on the size that is passed to the function. What does the function prototype for this function look like? int dynamic_array(int*, int); Correct! int* dynamic_array(int); int* dynamic_array(int*, int); int dynamic_array(int*);

int* dynamic_array(int);

Suppose x and y are both pointers to integers. The goal of the program is to swap the addresses of the two pointers. For example, if x is initially storing the hexadecimal address X0001, and y is initially storing the hexadecimal address X0002, after the program is finished x should be storing X0002 and y should be storing X0001. Which of the options below is the correct implementation of the program? Correct! int* temp = x; x = y; y = temp; int* temp = &x; *x = &y; *y = &temp; int* temp = *x; *x = *y; *y = *temp; int* temp = &x; x = &y; y = &temp;

int* temp = x; x = y; y = temp;

Suppose a viewRecord function is supposed to do the following: -Prompt the user for a record number. -Seek to the record (using the user's input from step 1) by calling the seekg function. -Read the record from the file by calling the read function. -Display the contents of the record on the console screen. Which of the options below contains the correct access specifiers that are needed at a minimum for opening the invtry.dat file inside of the viewRecord function? NOTE: Only the access specifiers that are needed should be included. invFile.open("invtry.dat", ios::in | ios::out); invFile.open("invtry.dat", ios::in | ios::out | ios::binary); Correct! invFile.open("invtry.dat", ios::in | ios::binary); invFile.open("invtry.dat", ios::out | ios::binary);

invFile.open("invtry.dat", ios::in | ios::binary);

Suppose a viewRecord function is supposed to do the following: Prompt the user for a record number. Seek to the record (using the user's input from step 1) by calling the seekg function. Read the record from the file by calling the read function. Display the contents of the record on the console screen. Which of the options below contains the correct access specifiers that are needed at a minimum for opening the invtry.dat binary file inside of the viewRecord function? NOTE: Only the access specifiers that are needed should be included. invFile.open("invtry.dat", ios::in | ios::out | ios::binary); invFile.open("invtry.dat", ios::out | ios::binary); invFile.open("invtry.dat", ios::binary); Correct! invFile.open("invtry.dat", ios::in | ios::binary);

invFile.open("invtry.dat", ios::in | ios::binary);

Suppose a program has the following declarations: const int SIZE = 5; char arr[SIZE] = {1, 2, 3, 4, 5}; char *ptr = arr; char *ptr2 = &arr[SIZE-1]; int num_of_elts; Which of the options below will store the number of elements in the array (5) in num_of_elts? HINT: A character (char) takes up one byte of memory. So, for example, if the first element of arr is at address 20, then this means that the last element of arr would be at address 24. num_of_elts = ptr2 - ptr1; num_of_elts = ptr2 + ptr1; Correct! num_of_elts = ptr2 - ptr1 + 1; num_of_elts = ptr2 + ptr1 - 1;

num_of_elts = ptr2 - ptr1 + 1;

Suppose a program has the following initial linked list: head | A-> B-> C->D the goal of the program is to reverse the linked list by reversing the pointers. After the program is finished, the reversed linked list will be: ----------head ------------| A<-B<- C<-D NOTE: The program is designed specifically to reverse a linked list with four nodes. It is NOT a general program that will reverse any linked list. Which of the options below is the correct implementation of this program? ---------------------------------------------- ptr=head->next->next->next->next; head->next->next->next->next = head->next->next; head->next->next->next = head->next; head->next->next = head; head->next=nullptr; head=ptr; ---------------------------------------------- Correct! ptr=head->next->next->next; head->next->next->next->next = head->next->next; head->next->next->next = head->next; head->next->next = head; head->next=nullptr; head=ptr; ---------------------------------------------- ptr=head->next->next->next->next; head->next->next->next->next = head->next->next->next; head->next->next->next = head->next->next; head->next->next = head->next; head->next=head; head=ptr; ---------------------------------------------- ptr=head->next->next->next; head->next->next->next->next = head->next->next->next; head->next->next->next = head->next->next; head->next->next = head->next; head->next=head; head=ptr;

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

Suppose a program has the following initial linked list: head | A-> B-> C->D the goal of the program is to reverse the linked list by reversing the pointers. After the program is finished, the reversed linked list will be: ----------head ------------| A<-B<- C<-D NOTE: The program is designed specifically to reverse a linked list with four nodes. It is NOT a general program that will reverse any linked list. Which of the options below is the correct implementation of this program? Correct! ptr=head->next->next->next; head->next->next->next->next = head->next->next; head->next->next->next = head->next; head->next->next = head; head->next=nullptr; head=ptr; ptr=head->next->next->next->next; head->next->next->next->next = head->next->next->next; head->next->next->next = head->next->next; head->next->next = head->next; head->next=head; head=ptr; ptr=head->next->next->next->next; head->next->next->next->next = head->next->next; head->next->next->next = head->next; head->next->next = head; head->next=nullptr; head=ptr; ptr=head->next->next->next; head->next->next->next->next = head->next->next->next; head->next->next->next = head->next->next; head->next->next = head->next; head->next=head; head=ptr;

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

A function can give another function access to one of its variables by making the variable a _ variable. local reference global static

reference

A function can give another function access to one of its variables by making the variable a _ variable. static local global reference

reference

int sub(int num1, int num2, &num3) { return num1-num2-num3; } num3 is passed by _ value pointer reference parameter

reference

int sub(int num1, int num2, &num3) { return num1-num2-num3; } num3 is passed by _ parameter value reference pointer

reference

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: [1, 2, 3, 4, 5, 6, 7, 8] Would become [4, 3, 2, 1, 8, 7, 6, 5] 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)); reverse(vec.begin()+(vec.size()/2)-1, 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), vec.end());

Dynamically allocated arrays are created during _ time, and they are stored on the _. Correct! run, heap compile, stack compile, heap run, stack

run, heap

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" ? Correct Answer string get_name() string name; College college_obj; void set_name(string n)

string get_name()

Suppose the following has been declared in main in a program that uses the Student structure: Student* stuptr = new Student[2]; All of the options below will correctly set the name field of the second student student object (i.e., the student object at index 1 in the array) to "Adam" EXCEPT: (*(stuptr + 1)).name = "Adam"; (stuptr1 + 1)->name = "Adam"; Correct! stuptr[1]->name = "Adam"; stuptr[1].name = "Adam";

stuptr[1]->name = "Adam";

Suppose a program is supposed to swap adjacent elements in an array. For example, if the array is initially [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], after running the code below it would be [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]. Part of the code is as follows: const int SIZE = 10; int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int temp; for(int i=0; i<SIZE; i+=2) { .......(omitted) } Which of the options below contains the correct code to place in the body of the for loop shown above? arr[i] = arr[i-1]; temp = arr[i]; arr[i-1] = temp; ------------------ temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; ------------------ temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; ------------------ arr[i] = arr[i+1]; temp = arr[i]; arr[i+1] = temp;

temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp

Suppose a program is supposed to swap adjacent elements in an array. For example, if the array is initially [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], after running the code below it would be [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]. Part of the code is as follows: const int SIZE = 10; int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int temp; for(int i=0; i<SIZE; i+=2) { .......(omitted) } Which of the options below contains the correct code to place in the body of the for loop shown above? arr[i] = arr[i+1]; temp = arr[i]; arr[i+1] = temp; ---------------------------- arr[i] = arr[i-1]; temp = arr[i]; arr[i-1] = temp; ----------------------------- temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; ----------------------------- temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp;

temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;

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::College set_college_name(string c) { college_name = c; } void::set_college_name College(string c) { college_name = c; } void set_college_name::College(string c) { college_name = c; } Correct! void College::set_college_name(string c) { college_name = c; }

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

In a menu-driven program, the display_menu_choice function has one sole purpose: to display the menu with the different options to the user. For example, the menu might look like this: 1. Addition 2. Subtraction 3. Multiplication 4. Division (quotient) 5. Division (remainder) 6. Quit Which of the options below correctly represents the display_menu function? --------------------------------------------------- int display_menu() { ....... (omitted) } --------------------------------------------------- void display_menu() { ....... (omitted) } --------------------------------------------------- void display_menu(int menu_choice) { ....... (omitted) } --------------------------------------------------- int display_menu(int menu_choice) { ....... (omitted) }

void display_menu() { ....... (omitted) }

The doubleVal function is supposed to be passed a pointer to an integer, and it doubles the value of the number. Which of the options below is the correct implementation of the doubleVal function? -------------------------------------- void doubleVal(int *ptr) { &ptr *= 2; } -------------------------------------- void doubleVal(int &ptr) { 'ptr '= 2; } (' = *) -------------------------------------- void doubleVal(int *ptr) { 'ptr '= 2; } (' = *) -------------------------------------- void doubleVal(int &ptr) { &ptr *= 2; }

void doubleVal(int *ptr) { 'ptr '= 2; } (' = *)

Suppose a program is supposed to produce the following output: B B Most of the program is shown here: int main() { string str1 = "A" string str2 = "B"; func(str1, str2); cout<<str1<<endl; cout<<str2<<endl; } 1 ________________ { string temp = str1; str1 = str2; str2 = temp; } Which of the options below contains the correct function header to insert at line 1 in the program shown above? void func(string &str1, string str2) void func(string str1, string str2) void func(string str1, string &str2) void func(string &str1, string &str2)

void func(string &str1, string str2)

Suppose a program is supposed to produce the following output: X Y Most of the program is shown here: int main() { string str1 = "X" string str2 = "Y"; func(str1, str2); cout<<str1<<endl; cout<<str2<<endl; } 1 ________________ { string temp = str1; str1 = str2; str2 = temp; } Which of the options below contains the correct function header to insert at line 1 in the program shown above? void func(string &str1, string &str2) void func(string str1, string &str2) void func(string &str1, string str2) void func(string str1, string str2)

void func(string str1, string str2)

While there is a built-in pop_back() method for vectors, there is no built-in pop_front method. Suppose a program needs a pop_front() method that will remove the first element from the vector. For example if the original vector is [1, 2, 3, 4, 5], then after passing in this vector to pop_front() the new resulting vector will be [2, 3, 4, 5]. Which of the options below is the correct implementation of pop_front()? -------------------------------------- B-) void pop_front(vector<int> &v) { if(!v.empty()) { for(int i=1; i<v.size(); i++) v[i] = v[i-1]; v.pop_back(); } } -------------------------------------- D-) void pop_front(vector<int> &v) { if(!v.empty()) { for(int i=1; i<(v.size()-1); i++) v[i] = v[i-1]; v.pop_back(); } } -------------------------------------- C-) void pop_front(vector<int> &v) { if(!v.empty()) { for(int i=1; i<(v.size()-1); i++) v[i-1] = v[i]; v.pop_back(); } } -------------------------------------- Correct Answer void pop_front(vector<int> &v) { if(!v.empty()) { for(int i=1; i<v.size(); i++) v[i-1] = v[i]; v.pop_back(); } }

void pop_front(vector<int> &v) { if(!v.empty()) { for(int i=1; i<v.size(); i++) v[i-1] = v[i]; v.pop_back(); } }

struct Computer { string CPU; }; Suppose there is a function named print_computer, which is supposed to receive a pointer to a computer structure instance, and print the CPU field. Which of the options below is the correct implementation of the print_computer function? Correct! void print_computer(Computer* c) { cout << c->cpu <<endl; } void print_computer(Computer* c) { cout << *c.cpu <<endl; } void print_computer(Computer c->) { cout << *c.cpu <<endl; } void print_computer(Computer c->) { cout << c->cpu <<endl; }

void print_computer(Computer* c) { cout << c->cpu <<endl; }

Suppose a program has a structure named Computer. There is a function print_computer which is passed a Computer structure instance, and prints out the information for it. The function is supposed to follow the following criteria: The function should have read-only access to the Computer structure instance that is passed to it. The function should not waste any space in memory. Which of the options below is the correct prototype for the print_computer function? void print_computer(Computer c); void print_computer(Computer& c); void print_computer(const Computer c); Correct Answer void print_computer(const Computer& c);

void print_computer(const Computer& c);

While there is a built-in push_back() method for vectors, there is no built-in push_front method. Suppose a program needs a push_front() method that will add an item to the beginning of the vector. For example if the original vector is [2, 3, 4, 5], then after passing in this vector and the value 1 to push_front() the new resulting vector will be [1, 2, 3, 4, 5]. Which of the options below is the correct implementation of push_front()? --------------------------------------------------- Correct Answer void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>=0; i--) v[i+1] = v[i]; v[0] = value; } --------------------------------------------------- void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>=0; i--) v[i+1] = v[i]; v[0] = value; } --------------------------------------------------- void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>=0; i--) v[i] = v[i+1]; v[0] = value; } --------------------------------------------------- void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>=0; i--) v[i] = v[i+1]; v[0] = value; }

void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>=0; i--) v[i+1] = v[i]; v[0] = value; }

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 Answer void set_num_of_bedrooms(int b) House(int b) House() int get_num_of_bedrooms()

void set_num_of_bedrooms(int b)

Suppose a program is supposed to display the word "TEST". Which of the below programs is the correct implementation of the program? int main() { test1(); } void test3() { cout<<"TEST"; } void test2() { test3(); } void test1() { test2(); } ----------------------------------------- void test3() { cout<<"TEST"; } void test2() { test3(); } void test1() { test2(); } int main() { test1(); } ----------------------------------------- void test1() { test2(); } void test2() { test3(); } void test3() { cout<<"TEST"; } int main() { test1(); } ----------------------------------------- int main() { test1(); } void test1() { test2(); } void test2() { test3(); } void test3() { cout<<"TEST"; }

void test3() { cout<<"TEST"; } void test2() { test3(); } void test1() { test2(); } int main() { test1(); }

NOTE: ARR_SIZE is the size of the array letters, NUM_OF_FILE_LINES is the number of lines in the file that is associated with the file object inputfile, and count is initialized to zero (0). Assume that NUM_OF_FILE_LINES is greater than or equal to ARRAY_SIZE. If NUM_OF_FILE_LINES is greater than ARRAY_SIZE, then only the first ARRAY_SIZE lines from the file will be read into the array. All of the options below will correctly read the contents of a file into an array EXCEPT: --------------------------------------------------------------------------- while(count < ARR_SIZE && inputfile >> letters[count]) count++; --------------------------------------------------------------------------- for(int i=0; i<ARR_SIZE; i++) inputfile >> letters[i]; --------------------------------------------------------------------------- Correct! while(inputfile >> letters[count] && count < ARR_SIZE) count++; --------------------------------------------------------------------------- for(int i=0; i<NUM_OF_FILE_LINES; i++) { if(i < ARR_SIZE) inputfile >> letters[i]; else break; }

while(inputfile >> letters[count] && count < ARR_SIZE) count++;

NOTE: ARR_SIZE is the size of the array letters, NUM_OF_FILE_LINES is the number of lines in the file that is associated with the file object inputfile, and count is initialized to zero (0). Assume that NUM_OF_FILE_LINES is greater than or equal to ARRAY_SIZE. If NUM_OF_FILE_LINES is greater than ARRAY_SIZE, then only the first ARRAY_SIZE lines from the file will be read into the array. All of the options below will correctly read the contents of a file into an array EXCEPT: ------------------------------------------------- while(count < ARR_SIZE && inputfile >> letters[count]) count++; ------------------------------------------------- for(int i=0; i<ARR_SIZE; i++) inputfile >> letters[i]; ------------------------------------------------- for(int i=0; i<NUM_OF_FILE_LINES; i++) { if(i < ARR_SIZE) inputfile >> letters[i]; else break; } ------------------------------------------------- while(inputfile >> letters[count] && count < ARR_SIZE) count++;

while(inputfile >> letters[count] && count < ARR_SIZE) count++;

Suppose a program has a 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 second node.in the linked list. For example for the following linked list: A->B->D->C->E->F The output of the program would be: B D F 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!= nullptr) { if(ptr != 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->next!= nullptr) { if(ptr->next != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } ptr=ptr->next; } -------------------------------- Correct! while(ptr!= nullptr) { ptr=ptr->next; if(ptr != nullptr) { cout<<ptr->data<<endl; ptr=ptr->next; } }

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

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

The default constructor takes _ argument(s) two (2) one (1) three (3) Correct! zero (0)

zero (0)


Ensembles d'études connexes

Vocab Unit 2 choosing the right word

View Set

Chapter 4 Random Variable and Probability Distribution

View Set

Media Law: FINAL EXAM (Chapters 1-7)

View Set

Midterm Business in the European Union

View Set

Chapter 1.1 The Power of Consumers

View Set

The Legal Environment of Business: Chapter 18

View Set