CS Melvin Final

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

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

0 2 -1 3 -2 4

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

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

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

B-) False False

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? B-) for(int i=SIZE-1; i>=0; i--) cout<<*(ptr+i)<<" "; 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)<<" ";

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

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

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

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

Constant

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

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

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

D-)1. False2. False

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

Send the value returned to a cin object

Suppose a program is supposed to print out the inside elements (non-edge elements) of a 2D array. For example, for the following 2D array: The four (4) bolded red values shown above would be printed out. And for the following 2D array: The nine (9) bolded red values shown above would be printed out. Which of the options below is the correct implementation of the program? NOTE: The constant ROWS stores the number of rows, and the constant COLS stores the number of columns. for(curr_row=1; curr_row<=(ROWS-1); curr_row++) for(curr_col=1; curr_col<=(COLS-1); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<=(ROWS-2); curr_row++) for(curr_col=2; curr_col<=(COLS-2); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=2; curr_row<(ROWS-2); curr_row++) for(curr_col=2; curr_col<(COLS-2); curr_col++) cout<<arr[curr_row][curr_col]; for(curr_row=1; curr_row<(ROWS-1); curr_row++) for(curr_col=1; curr_col<(COLS-1); curr_col++) cout<<arr[curr_row][curr_col];

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

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

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

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

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

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: for(int i=0; i<ARR_SIZE; i++) inputfile >> letters[i]; while(inputfile >> letters[count] && count < ARR_SIZE) count++; while(count < ARR_SIZE && inputfile >> letters[count]) 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++;

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

# # # # #

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

20 10 20 11 20 12

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

Parameter

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

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;

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

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

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

Which of the following options below is the correct way to declare ptr as a Constant pointer to non-constant data? B-) const int * ptr = (omitted) C-) const int * const 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}; D-) 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++; } B-) 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++; } B-) int * ptr = arr; for(int i=0; i<SIZE; i++) { cout<<*ptr <<endl; (*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? B-) int *temp = nullptr; *temp = *x;*x = *y;*y = *temp; C-) int temp; temp = &x;*x = &y;*y = temp; D-) int *temp = nullptr; *temp = &x;*x = &y;*y = &temp; Correct Answer A-) int temp; temp = *x;*x = *y;*y = temp;

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

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

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

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

C-) ->

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

C-) The rows of the 2D array

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

Constructor

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

Value

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? file_obj.seekp(-2, ios::beg); file_obj.seekp(2, ios::beg); Correct Answer file_obj.seekp(1, ios::beg); file_obj.seekp(-1, 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)); 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));

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

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

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

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

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

string get_name()

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

Overloaded functions

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

Prototype

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

01010101 00001111 00110011 11001100

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

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

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

1. False 2. True

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. ptr2++; 3. ptr++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr2++; 3. ptr++; 1. if (*(ptr+i) == *(ptr2+j)) 2. ptr++; 3. ptr2++; 1. if (*ptr == *ptr2) 2. ptr++; 3. ptr2++;

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

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. 11110000 01010101 00001111 00110011 10101010 Correct! 11110000 10101010 11110000 01010101 00001111

11110000

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

8 2.0

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

A V shape with #s

Suppose the current state of the heap in memory is as follows: 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? B-) D-) C-) Correct! A-)

A-)

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

An alias for another variable

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

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

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

Global

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

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

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

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

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

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

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

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

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

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

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. Correct Answer invFile.open("invtry.dat", ios::in | ios::binary); invFile.open("invtry.dat", ios::in | ios::out | ios::binary); invFile.open("invtry.dat", ios::in | ios::out); invFile.open("invtry.dat", ios::out | ios::binary);

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

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

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

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

zero (0)

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

1. False 2. False

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

1. False 2. True

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

1. False2. False

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

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

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

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()? C-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-2; i>0; i--)v[i] = v[i-1]; v[0] = value; } D-) void push_front(vector<int> &v, int value){ v.push_back(-1); for(int i=v.size()-2; i>0; i--)v[i-1] = v[i]; v[0] = value; } Correct! A-) void push_front(vector<int> &v, int value){ v.push_back(-1); for(int i=v.size()-1; i>0; i--)v[i] = v[i-1]; v[0] = value; } B-) void push_front(vector<int> &v, int value) { v.push_back(-1); for(int i=v.size()-1; i>0; i--)v[i-1] = v[i]; v[0] = value; }

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

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

A-) player1[4][3]

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

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

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

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

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

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

A program is supposed to swap the first and last columns of a 2Darray. For example if we have the following initial 2D array: Then after swapping the first and last columns the 2D array will look like this: Which of the options below is the correct implementation of this program? D-) for(int i=0; i<COLS; i++) { temp = arr[0][i]; arr[0][i] = arr[ROWS-1][i]; arr[ROWS-1][i] = temp; } Correct! 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; } 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; }

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

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

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

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

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

B-) Student s = {true};

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

B-) The diagonals of the 2D array

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

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

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

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

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

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

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+1]; for(int i=0; i<=(v.size()/2); i++) v.pop_back(); Correct Answer for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back(); for(int i=0; i<(v.size()/2-1); i++) v[i] = v[2*i]; for(int i=0; i<=(v.size()/2); i++) v.pop_back(); for(int i=0; i<(v.size()/2); i++) v[i] = v[2*i+1]; for(int i=0; i<=(v.size()/2+1); i++) v.pop_back();

Correct Answer 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 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 A-) Same A Same B D-) Different A Different B Correct! B-) Same A Different B

Correct! B-) Same A Different B

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? B-) 00000000 00000002 00000004 00000006 D-) 15 30 45 60 Correct! C-) 15 16 17 18 A-) 00000000 00000001 00000002 00000003

Correct! C-) 15 16 17 18

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? A-) 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; } Correct! C-) for(int i=0; i<(SIZE/2); i++) { cout<<*ptr<<endl; ptr+=2; } B-) for(int i=0; i<=(SIZE/2+1); i++) { cout<<*ptr<<endl; ptr+=2; }

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

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! D-) int* temp = x; x = y; y = temp; B-) int* temp = &x; *x = &y; *y = &temp; C-) int* temp = *x; *x = *y; *y = *temp; A-) int* temp = &x; x = &y; y = &temp;

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

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) Correct! head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head head = head->next; head->next->next->next->next = nullptr; head->next->next->next->next = head head->next->next->next->next = head; head = head->next; head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head head->next->next->next->next = nullptr;

Correct! head->next->next->next->next = nullptr; head = head->next; head->next->next->next->next = head

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? head->next=tail; tail->prev=head; head->next->prev=tail; tail->prev->next=head; Correct! head->prev=tail; tail->next=head; head->prev->next=tail; tail->next->prev=head;

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

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

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

Suppose 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; B-) 550X1234ABCD A-) 0XFABC789055 Correct! D-) 0XFABC78900X1234ABCD C-) 0X1234ABCD0XFABC7890

D-) 0XFABC78900X1234ABCD

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? 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"; 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"; 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? A-) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr == nullptr) You Answered B-) 1 while(ptr->letter != search_val && ptr != nullptr) 2 if(ptr != nullptr) Correct Answer D-) 1 while(ptr != nullptr && ptr->letter != search_val) 2 if(ptr != nullptr) C-) 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. A-) 1. True 2. True C-) 1. False 2. True Correct! D-) 1. False 2. False B-) 1. True 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) 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) 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)

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

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

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

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

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

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; A-) Line 2 should be *ptr = &x;Line 3 should be *ptr = 100; Correct! D-) 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;

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

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

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: A-) cout<<arr<<endl; C-) cout<<&arr[0].name<<endl; Correct! D-) cout<<arr.name<<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? Correct! D-)void print_computer(Computer* c){ cout << c->cpu <<endl; } B-)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; }

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

Function calling

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

Function prototypes

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

Global variables

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

Print_array: Make the array parameter NON-constant

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

bool isEqualTo(const Rectangle& r) const

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

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

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

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

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

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

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

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

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

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

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

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

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

reference

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

reference

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; } Correct! void doubleVal(int *ptr) { *ptr *= 2; } void doubleVal(int *ptr) { &ptr *= 2; }

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

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

void func(string str1, string str2)

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

void set_num_of_bedrooms(int b)

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

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


Ensembles d'études connexes

Advanced Accounting Ch. 5 (FIFO, LIFO, Inventory)

View Set

Anthropology Exam #2 essay questions

View Set

Adult Gerontology - Endocrine Q&A

View Set

Ch. 41, NURS 205 - Exam 2 - Chapter 21, 22, 41, 43, 44

View Set