Test 3 COMSC-165

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

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;

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

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

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

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)

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

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

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)

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

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

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

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

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)

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;

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

8

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'

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

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

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

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

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

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

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

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


Conjuntos de estudio relacionados

Chapter 4: Building Construction

View Set

Unit 5: The Development of English

View Set

Earth Science - Chapter 5 Plate Tectonics

View Set

日本語総まとめN2漢字第2週

View Set

Targeted Medical-Surgical: Fluid, Electrolyte, and Acid-Base

View Set

Unit 6.1 Between Europe & China (1500-1700), Unit 5.2 The Atlantic System & Africa (1500-1800), Unit 5.1 Transformations in Europe (1500-1700), Unit 4 - Spanish America, Unit 3.2, Unit 3.1 Latin Europe, Unit 2.2 Mongol Eurasia & Aftermath (1200-1500)...

View Set