PREP: TEST 3 COMSC-165

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

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

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

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

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

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. 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

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

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

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

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

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

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

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

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'

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;

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; (' = *)

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

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

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

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

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

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

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

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

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

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

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

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;

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;

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;

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

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

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

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

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 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 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 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 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());

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; } (' = *)

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(); } }

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

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


Ensembles d'études connexes

Exam 2 Community Health: Chapter 6

View Set

Medical Terminology by Jane Rice Chapter 1 -Vocabulary

View Set

Biochemistry: Diffusion and Osmosis

View Set