Quiz 5 COMSC 165

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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

1. False 2. False

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

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

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

Build_array: Make the array parameter constant

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

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

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

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

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

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

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

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

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

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

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

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


Kaugnay na mga set ng pag-aaral

Biology chapter 7 photosynthesis

View Set

Biology Of The Cell - prac test 1

View Set

Lab Simulation 7-2: Practice Communication between a Virtual Machine and a Host: Network +

View Set

NURS 2050 Maternal Child Health EXAM 1 Practice questions

View Set

Ch 9 Communications and Networks

View Set

Ap Computer Science A Midterm Review

View Set