CS 2010 - Final Exam

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

Modifies userVals' elements

The function's only behavior should be to return the sum of array userVals' elements. What common error does the function make? Assume userVals always has exactly 10 elements. int SumArrayElements(int userVals[]) { for (i = 1; i < 10; ++i) { userVals[i] = userVals[i] + userVals[i - 1]; } return userVals[i]; }

D.) 0

The numNegatives variable counts the number of negative values in the array userVals. What should numNegatives be initialized to? int userVals[20]; unsigned int i; numNegatives = XXX; for (i = 0; i < 20; ++i) { if (userValues[i] < 0) { numNegatives = numNegatives + 1; } } A.) No initialization needed. B.) userVals[0] C.) -1 D.) 0

D.) both arrays should be declared to have the same number of elements.

Two arrays, itemsName and itemsPrices, are used to store a list of item names and their corresponding prices. Which is true? A.) both arrays should be of the same data type. B.) the item names should appear in both arrays. C.) using two arrays saves memory versus using one array. D.) both arrays should be declared to have the same number of elements.

C.) if (n <= 1)

Assuming Fact(0) is 1 and Fact(n) returns n*n-1*n-2*..., which XXX is the base condition for the factorial function? int Fact(int n) { XXX return 1; else return n * Fact(n - 1); } A.) if(n == 1) B.) if(n >= 0) C.) if(n <= 1) D.) if(n == 0)

void SortDescending(int inputVals[], ...)

Based on the function names, which function most likely should allow the inputVals array parmeter to be modified? (Not all parameters are shown). A.) void SortDescending(int inputVals[], ...) B.) int FindMaxValue(int inputVals[], ...) C.) double CalculateAverage(int inputVals[], ...) D.) void PrintSmallestVal(int inputVals[], ...)

B.) in the parameters of a function definitiontc

Default parameters may appear ____. A.) in a function call's arguments B.) in the parameters of a function definition C.) in the return type of a function definition D.) in the main() function's local variables

2

Given the following list of sorted elements, how many elements of the list will be checked to find 25 using binary search? {12, 13, 15, 20, 23, 24, 25, 36, 40}

3

How many recursive calls are made to the function NumSeries()? int NumSeries (int num) { if (num == 1) { cout << num << " "; else { NumSeries(num / 2); cout << num << " "; } } int main() { NumSeries(5); return 0; }

B.) quadratic

O(N^2) has a _____ runtime complexity. A.) linear B.) quadratic C.) log-linear D.) logarithmic

D.) swap

Sorting algorithms ____ elements in a list, comparing their values to sort. A.) sort B.) delete C.) insert D.) swap

PrintSum has void return type, so cannot be assigned to a variable.

The following program generates an error. Why? void PrintSum(int num1, int num2) { cout << num1 + num2; } int main() { int y; y = PrintSum(4, 5); return 0; }

The variable min is declared in FindMin(), but is used in main()

The following program results in a compiler error. Why? int FindMin(int a, int b) { int min; if (a < b) { min = a; } else { min = b; } return min; } int main() { int x; int y; cin >> x; cin >> y; min = FindMin(x, y); cout << "The smaller number is: " << min << endl; return 0; }

C.) either calls itself or is in a potential cycle of function calls.

A recursive function _______. A.) combines data members and functions in a single unit. B.) repeatedly calls other member functions of the same class. C.) either calls itself or is in a potential cycle of function calls. D.) repeatedly calls the main function

D.) can modify the array's elements directly because arrays are passed by pointer.

A function defined beginning with void SetNegativesToZeros(int userValues[],...) should modify userValues such that any negative integers are replaced by zeros. The function _____. A.) cannot be written because an array passed to a function is constant so cannot be modified B.) should define userValues as const int& userValues to be able to modify the array's elements C.) should make a local copy of the array and then set the copy's negative values to zeros D.) can modify the array's elements directly because arrays are passed by pointer.

algoritm

A(n) _______ is a sequence of steps for solving a problem.

maxValue = Max(15, max(35, 25));

For the following function, which is a valid function call? Assume maxValue is an integer. int Max(int x, int y) { if (x > y) { return x; } else { return y; } }

2

For the given program, how many cout statements will execute? void PrintShippingCharge(double itemWeight) { if ((itemWeight > 0.0) && (itemWeight <= 10.0)) { cout << (itemWeight * 0.75) << endl; } else if ((itemWeight > 10.0) && (itemWeight <= 15.0)) { cout << (itemWeight * 0.85) << endl; } else if ((itemWeight > 15.0) && (itemWeight <= 20.0)) { cout << (itemWeight * 0.95) << endl; } } int main() { PrintShippingCharge(18); PrintShippingCharge(6); PrintShippingCharge(25); return 0; }

A.) (i < NUM_STATES) && (!foundState)

For the given program, which XXX iterates through the array to find the state that is input (userState) until a match is found? string stateNames[NUM_STATES]; int statePop[NUM_STATES]; string userState; bool foundState = false; unsigned int i; cin >> userState; foundState = false; for (i = 0; XXX; ++i) { if (stateNames[i] == userState) { foundState = true; cout << userState << ", " << statePop[i] << endl; } } A.) (i < NUM_STATES) && (!foundState) B.) (i < NUM_STATES - 1) C.) (i < NUM_STATES - 1) && (!foundState)

7

For the list {12, 15, 13, 20, 23, 27, 25, 36, 40}, how many elements will be compared to find 25 using linear search?

Sandy

For the list {Allen, Barry, Christopher, Daisy, Garry, Sandy, Zac}, what is the second name searched when the list is searched for Garry using binary search?

Line 3

Function CalcSum() was copied and modified to form the new function CalcProduct(). Which line of the new function contains an error? int CalcSum(int a, int b) { int s; s = a + b; return s; } int CalcProduct(int a, int b) { int p; // Line 1 p = a * b; // Line 2 return s; // Line 3 }

B.) for(i = 0; i < arraySize; ++i) where arraySize is a second function parameter

Given a function defined beginning with: void PrintMaxItem(int valsArray[],...) that prints the largest item in any array of integers. How would this function most likely iterate through the array? A.) for(i = 0; i < valsArray; ++i) due to the compiler knowing the size of arrays B.) for(i = 0; i < arraySize; ++i) where arraySize is a second function parameter C.) for(i = 0; i < valsArray.size(); ++i) where size is a built-in function for arrays D.) for(i = 0; i != lastItem; ++i) where lastItem is the value of the array's last element

C.) {3 6 9 17 44}

Given a list of numbers {17 3 44 6 9}, identify the list after sorting in ascending order. A.) {44 17 9 6 3} B.) {17 3 44 6 9} C.) {3 6 9 17 44} D.) {9 6 44 3 17}

D.) 2

Given a vector/array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list? A.) 3 B.) 4 C.) 5 D.) 2

D.) scorePerQuiz(6) = 8;

Given array scorePerQuiz has 10 elements. Which assigns element 7 with the value 8? A.) scorePerQuiz(0) = 8; B.) scorePerQuiz = 8; C.) scorePerQuiz(8) = 7; D.) scorePerQuiz(6) = 8;

C.) (10, j, k + 5)

Given integers i, j, k, which XXX correctly passes three integer arguments for the following function call? addInts XXX; A.) (j, 6 + 7) B.) (i + j + k) C.) (10, j, k + 5) D.) ( 10 15 20)

A.) Error: Invalid Access

Given that integer array x has elements 4, 7, 3, 0, 8, what are the elements after the loop? int i; for (i = 0; i < 4; ++i) { x[i] = x[i + 1]; } A.) Error: Invalid Access. B.) 7, 3, 0, 8, 4 C.) 4, 4, 7, 3, 0 D.) 7, 3, 0, 8, 8

B.) Error: Invalid access.

Given that integer array x has elements 5, 10, 15, 20, what is the output? int i; for (i = 0; i < 4; ++i) { cout << x[i] + x[i + 1]; } A.) 10, 15, 20, 5 B.) Error: Invalid access. C.) 15, 25, 35, 20 D.) 15, 25, 35, 25

A.) Test('x');

Given the following function definition, which function call prints x, y, 0? void Test(char a, char b = 'y', int num = 0) { cout << a << ", " << b << ", " << num << endl; } A.) Test('x'); B.) Test(x, y); C.) Test('x', 'y'); D.) Test('x','y','z');

1

Given the following function. To change the function to return the product instead of the sum, how many lines of code need to be changed? int Calculate(int a, int b) { return a + b; } int main() { cout << Calculate(3, 4); cout << Calculate(5, 2); cout << Calculate(6, 7); return 0; }

-6

Given the function definition, what is returned for the call: DoCalc(2, 3) int DoCalc(int x, int y, int z = -1) { return x * y * z; }

3

Given the list {12 30 40 0 47}, how many swaps will occur during the outer loop execution (j = 3)?

B.) studentScores[i] / studentNames[i]

Given two arrays, studentNames that maintains a list of students, and studentScores that has a list of the scores for each student. Which XXX and YYY print out only the student names who score is above 80? string studentNames[NUM_STUDENTS]; int studentScores[NUM_STUDENTS]; unsigned int i; for (i = 0; i NUM_STUDENTS; ++i) { if (XXX > 80) { cout << YYY << " "; } } A.) studentScores[i] / studentScores[i] B.) studentScores[i] / studentNames[i] C.) studentNames[i] / studentScores[i] D.) studentName[i] / studentNames[i]

B.) for (i = 0; i < SIZE_LIST; ++i) { cout << keyList[i] << ", " << itemsList[i] << endl; }

Given two arrays, which code will output all the array's elements, in the order key, item followed by a newline? int keyList[SIZE_LIST]; int itemsList[SIZE_LIST]; A.) for (i = 0; i < SIZE_LIST - 1; ++i) { cout << keyList[i] << ", " << itemsList[i] << B.) for (i = 0; i < SIZE_LIST; ++i) { cout << keyList[i] << ", " << itemsList[i] << endl; } C.) cout << keyList[SIZE_LIST] << ", " << itemsList[SIZE_LIST] << endl; D.) cout << keyList[SIZE_LIST - 1] << ", " << itemsList[SIZE_LIST - 1] << endl;

D.) cout << origList[1] + offsetList[1] << endl;

Given two integer arrays origList = {2, 3, 4, 5} and offsetList = {6, 7, 8, 9}, which statement prints out the sum of the second element in the origList with the corresponding element in the offsetList? A.) cout << origList[2] + offsetList[2] << endl; B.) cout << origList + offsetList << endl; C.) cout << origList[3] + offsetList[7] << endl; D.) cout << origList[1] + offsetList[1] << endl;

x = 8, y = 8

Given x = 4 and y = 8, what are the ending values of x and y? x = y; y = x; x = y;

8

How many elements are in the array? int userVals[2][4];

10

How many elements does the array declaration create? int scores[10]; // Array declaration scores[0] = 25; scores[1] = 22; scores[2] = 18; scores[3] = 28;

3

How many function arguments exist in the code? double FahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32.0) * 5.0 / 9.0; } int main() { double fahrenheit; cin >> fahrenheit; int c1; int c2; int c3; c1 = FahrenheitToCelsius(fahrenheit); c2 = FahrenheitToCelsius(32); c3 = FahrenheitToCelsius(fahrenheit + 5.0); }

1

How many function parameters exist in the code? double FahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32.0) * 5.0 / 9.0; } int main() { double fahrenheit; cin >> fahrenheit; int c1; int c2; int c3; int c4; c1 = FahrenheitToCelsius(fahrenheit); c2 = FahrenheitToCelsius(32); c3 = FahrenheitToCelsius(78); c4 = FahrenheitToCelsius(100); }

4

How many recursive calls are made while computing the sum of 3, 6, 9, 12, and 15? int ArithSum(int num) { if (num == 1) { cout << num*3 << " "; return num*3; } else { cout << num * 3 << ", "; return (num*3 + ArithSum(num - 1)); } } int main() { cout << "Sum of "; cout << "is " << ArithSum(5); return 0; }

A.) Error: Incorrect base case condition. The recursive calls do not reach the base case leading to infinite recursion.

Identify the error in the recursive function that finds the sum of the digits. int digitSum(int number) { static int sum = 0; if (number >= 0) { sum += (number % 10); digitSum(number / 10); } else { return sum; } } A.) Error: Incorrect base case condition. The recursive calls do not reach the base case leading to infinite recursion. B.) There is no base case, and hence the recursive calls do not come out of the function. C.) Error: Incorrect base case condition. The recursive calls straight away go to the base case leading to no recursion. D.) There is no recursive case, and hence the function is not recursive.

D.) {ABBEY ABBIE ADRIA ADRIAN}

Identify the sorted list for A = {ADRIAN ABBEY ADRIA ABBIE}. A.) {ABBEY ABBIE ADRIAN ADRIA} B.) {ABBIE ABBEY ADRIAN ADRIA} C.) {ABBIE ABBEY ADRIA ADRIAN} D.) {ABBEY ABBIE ADRIA ADRIAN}

that parameter must be the last parameter

If a function definition has three parameters, one of which the programmer gives a default value, then ____.

2048 us

If a list has 1024 elements and each comparison takes 2 us, then what is the longest possible runtime for linear search on this list?

NOT 1024 us

If a list has 1024 elements and if each comparison takes 2 us, then what is the longest possible runtime for binary search on this list?

7

If the list {29 18 45 7 16} is sorted in ascending order using selection sort, what will be the value of the 0th element after the first pass over the outer loop (i = 0)?

{1 3 7 18 9}

If the list {3 9 7 18 1} is being sorted in ascending order using selection sort, what will be the list after completing the second outer loop iteration?

30 us

In the worst case, assuming each odd comparison takes 2 us and each even comparison takes 1 us, how long will an insertion sort algorithm take to sort a list of 6 elements?

D.) O(N^3)

What is the correct representation of O(5 * N^3 + 3 * N + 50)? A.) O(N) B.) O(5 * N^3) C.) O(50) D.) O(N^3)

64

Using selection sort, how many times longer will sorting a list of 40 elements take compared to a list of 5 elements?

9

Using the Big-O runtime complexity, how many times longer will sorting a list of 15 elements take compared to sorting a list of 5 elements?

numSize - 1

What XXX will generate 1 3 5 7 8 9 as the output? void selectSort(int numbers[], int numSize) { int i; int j; int indx; int temp; for (i = 0; i < XXX; ++i) { indx = i; for (j = i + 1; j < numSize; ++j) { if (numbers[j] < numbers[indx]) { indx = j; } } temp = numbers[i]; numbers[i] = numbers[indx]; numbers[indx] = temp; } } int main() { int numbers[] = {9, 5, 7, 3, 1, 8}; const int N_SIZE = 6; int i; selectSort(numbers, N_SIZE); for(i = 0; i < N_SIZE; ++i) { cout << numbers[i] << ' '; cout << endl; return 0; A.) N_SIZE - 1 B.) numSize - 1 C.) N_SIZE D.) numSize

10, 5, 5

What are the ending contents of the array? Choices show elements in index order 0, 1, 2. int yearsList[3]; yearsList[0] = 5; yearsList[1] = yearsList[0]; yearsList[0] = 10; yearsList[2] = yearsList[1];

C.) tmp = a / (nothing)

What code for XXX / YYY correctly swaps a and b? XXX; a = b; YYY; b = tmp; A.) (nothing) / tmp = a B.) tmp = b / (nothing) C.) tmp = a / (nothing) D.) tmp = a / tmp = b

A.) the program will lead to infinite recursion.

What happens if a recursive function in a program never reaches the base case? A.) the program will lead to infinite recursion. B.) the recursive function will not run. C.) the program will run until the recursive case ends. D.) the base case will be forced to run by the program.

6.0

What is output? double myFct(double a, double b) { return (a + b) / 2.0; } int main() [ double x = 3.0; double y = 5.0; double z = 8.0; double t; t = MyFct(x, y); t = MyFct(t, z); cout << t << endl; return 0; }

1 4

What is output? #include <iostream> using namespace std; void NumSeries(int num) { if (num == 0) { cout << 1 << " "; else { NumSeries(num - 1); num = num + 1; cout << num * 2 << " "; } } int main() { NumSeries(2); return 0; }

35

What is the ending value of the element at index 0? int numbers[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4;

39

What is the ending value of the element at index 1? int numbers[10]; numbers[0] = 35; numbers[1] = 37; numbers[1] = numbers[0] + 4;

MyFct() cannot be called with a literal

What is the error in this code? void MyFct(int& x) { x = (x * x) + 1; } int main() { cout << MyFct(9); }

49

What is the index of the last element? int numList[50];

Invalid seconds 2:24:1

What is the output for the call DisplayTime(2, 24, 65)? void DisplayTime(int hours, int minutes, int seconds) { // Parameter error checking if ((hours < 1) || (hours > 12) { cout << "Invalid hours" << endl; hours = 1; } if ((minutes < 1) || (minutes > 60)) { cout << "Invalid minutes" << endl; minutes = 1; } if ((seconds < 1) || (seconds > 60)) { cout << "Invalid seconds" << endl; seconds = 1; } cout << hours << ":" << minutes << ":" << seconds; }

10

What is the output? int FindSqr(int a) { int t; t = a * a; return a; } int main() { int square; square = FindSqr(10); cout << square; return 0; }

5 11

What is the output? int MyFct(int x) { int y; x = x * 2; y = x + 1; return y; } int main() { int a; a = 5; cout << a << " " << MyFct(a); }

Error: Complier cannot determine which function to call.

What is the output? void Area(double x, double y) { cout << x * y; } void Area(int base, int height) { cout << (base * height) / 2; } void Area(int length, int width) { cout << length * width; } int main() { int b; int h; b = 4; h = 5; Area(b, h); }

Function 2: 10

What is the output? void Display(int i) { cout << "Function 1: " << i << endl; } void Display(double f) { cout << "Function 2: " << f << endl; } int main() { double i; i = 10.0; Display(i); return 0; }

NOT No output: call to isEven() fails due to no return value

What is the output? void IsEven(int num) { int even; if (num % 2 == 0) { even = 1; } else { even = 0; } } int main() { IsEven(7); cout << even; return 0; }

p = 3, q = 3

What is the output? void Swap(int& x, int y) { int tmp; tmp = x; x = y; y = tmp; } int main() [ int p = 4, q = 3; Swap (p, q); cout << "p = " << p << ", q = " << q << endl; }

Perfect temperature.Too cold.

What is the output? void WaterTemperatureForCoffee(int temp) { if (temp < 195) { cout << "Too cold."; } else if ((temp >= 195) && (temp <= 205)) { cout << "Perfect temperature."; } else if (temp > 205) { cout << "Too hot."; } } int main() { WaterTemperatureForCoffee(205); WaterTemperatureForCoffee(190); return 0; }

22

What is the output? const double LB_PER_KG = 2.2; double KgsToLbs(double kilograms) { double pounds; pounds = kilograms * LB_PER_KG; return pounds; } int main() { double pounds; pounds = KgToLbs(10); cout << pounds; return 0; }

16

What is the output? int Calc(int num1, int num2) { return 1 + num1 + num2; } int main() { int x; x = Calc(4, 5); cout << Calc(x, 5); return 0; }

logarithmic

What is the runtime complexity of the following code? NumberSearch(numbers, N, key) { mid = 0; low = 0; high = N - 1; while (high >= low) { mid = (high + low) / 2; if (numbers[mid] < key) { low = mid + 1; } else if (numbers[mid] > key) { high = mid - 1; else { return mid; } } return - 1 }

logarithmic

What is the runtime complexity of the following code? SelectionSort(numbers, N) { for(i = 0; i < N; ++i) { indexSmallest = i; for(j = i + 1; j < N; ++j) { if(numbers[j] < numbers[indexSmallest]) { indexSmallest = j; } } temp = numbers[i]; numbers[i] = numbers[indexSmallest]; numbers[indexSmallest] = temp; }

quadratic

What is the runtime complexity of the following code? SelectionSort(numbers, N) { for(i = 0; i < N; ++i) { indexSmallest = i; for(j = i + 1; j < N; ++j) { if(numbers[j] < numbers[indexSmallest]) { indexSmallest = j; } } temp = numbers[i]; numbers[i] = numbers[indexSmallest]; numbers[indexSmallest] = temp; }

A.) O(N^3)

What is the simplified Big-O notation for 3 * N^3 + O(2N^2)? A.) O(N^3) B.) O(N^2) C.) O(N^3 + N^2) D.) O(3 * N^3)

A.) O(N^2)

What is the typical runtime of insertion sort? A.) O(N^2) B.) O(logN) C.) O(N) D.) O(NlogN)

individually testing a small part (or unit) of a program, typically a function, using a separate program called a test harness.

What is unit testing?

D.) MAX_SIZE / MAX_SIZE

Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1? A.) MAX_SIZE - 1 / MAX_SIZE - 1 B.) MAX_SIZE - 1 / MAX_SIZE C.) MAX_SIZE / MAX_SIZE - 1 D.) MAX_SIZE / MAX_SIZE

NOT (nothing) / i <= arrayVals.arraySize

Which XXX and YYY best complete the code to return the minimum value in the array? int GetMinValue(int arrayVals[] XXX) { int i; int minVal; minVal = arrayVals[0]; for(i = 0; YYY; ++i) { if(arrayVals[i] < minVal) { minVal = arrayVals[i]; } } return minVal; }

-1

Which XXX will generate the following output? Enter a value: 0 was not found. int BinarySearch(int list[], int listSize, int key) { int mid; int low; int high; low = 0; high = listSize - 1; while (high >= low) { mid = (high + low) / 2; if (list[mid] < key) { low = mid + 1; } else if (list[mid] > key) { high = mid - 1; } else { return XXX; } } return -1; } int main() { int list[] = { 2, 4, 7, 10, 11, 32, 45, 87}; const int LIST_SIZE = 8; int i; int key; int keyIndex; cout << "Enter a value: "; cin >> key; keyIndex = BinarySearch(list, LIST_SIZE, key); if (keyIndex == -1) { cout << key << " was not found." << endl; } else { cout << "Found " << key << " at index " << keyIndex << "." << endl; } return 0; }

D.) minVal = userVals[0]; / userVals[i] < minVal;

Which XXX and YYY correctly output the smallest values? Array userVals contains 100 elements that are integers (which may be positive or negative). // Determine smallest (min) value int minVal; XXX for (i = 0; i < 100; ++i) { if (YYY) { minVal = userVals[i]; } } cout << "Min: " << minVal << endl; A.) minVal = 0; / userVal < minVal; B.) minVal = 0; / userVal > minVal; C.) minVal = userVals[0]; / userVals[i] > minVal D.) minVal = userVals[0]; / userVals[i] < minVal;

C.) userVals[i][j] < minVal / minVal = userVals[i][j]

Which XXX and YYY will find the minimum value of all the elements in the array? Choices are in the form XXX / YYY. int userVals[NUM_RowS][NUM_COLS]; int minVal = userVals[0][0]; for (i - 0; i < NUM_ROWS; ++i) { for (j = 0; j < NUM_COLS; ++j) { if (XXX) { YYY; } } } A.) userVals[i][j] > minVal / minVal = userVals[i][j] B.) userVals[i] < minVal / minVal = userVals[i] C.) userVals[i][j] < minVal / minVal = userVals[i][j] D.) userVals[j] < minVal / minVal = userVals[j]

return PI_VAL * CalcSquare(r);

Which XXX calculates the area using the CalcSquare() function? The formula to calculate the area of a circle is pi * r^2. double CalcSquare(double x) { return x * x; } double CalcArea(double r) { const double PI_VAL = 3.14159265; XXX; } int main() { cout << CalcArea(5.0); return 0; }

PrintMessage()

Which XXX causes the program to output the message "Hello!"? void PrintMessage() { cout << "Hello!"; } int main() { XXX; return0; }

B.) (n > 0)

Which XXX is the base case in the recursive function to find the factorial of a number, where n is the input of the function? int factorial (int n) { if XXX { return n * factorial(n - 1); } else { return 1; } } A.) (n <= 0) B.) (n > 0) C.) (n > 1) D.) (n < 1)

C.) n <= 0

Which XXX is the recursive base case used to find the factorial of a number? int factorial(int n) { if (XXX) { return 1; } return n * factorial(n - 1); } A.) n < 0 B.) n <= 1 C.) n <= 0 D.) n > 1

y = CalcSum(4, 5);

Which XXX is valid for the following code? int CalcSum(int a, int b) { return a + b; } int main () { int y; XXX return 0; }

strcmp(str1, str2) == 0

Which XXX prints the message only if str1 and str2 are equal? if (XXX) { // Print "strings are equal" }

cout << "4, expecting 16, got: " << SquareNum(4) << endl;

Which XXX test the input value 4 using a print statement (without using assert)? int SquareNum(int origNum) { return origNum * origNum; } int main() { cout << "Testing started" << endl; XXX; cout << "Testing completed" << endl; return 0; }

NOT -1 (1, 0 or 2)

Which XXX will generate the following output? LIST 3 5 8 11 12 33 46 88 Enter a value: 0 was not found. int LinearSearch(int list[], int listSize, int key) { int i; for (i = 0; i < listSize; ++i) { if (list[i] == key) { return i; } } return XXX; } int main() { int list[] = { 3, 5, 8, 11, 12, 33, 46, 88}; const int LIST_SIZE = 8; int i; int key; int keyIndex; cout << "LIST: "; for (i = 0; i < LIST_SIZE; ++I) { cout << list[i] << ' '; } cout << endl; cout << "Enter a value: "; cin >> key; keyIndex = LinearSearch(list, LIST_SIZE, key); if (keyIndex == -1) { cout << key << " was not found." << endl; } else { cout << "Found " << key << " at index " << keyIndex << "." << endl; } return 0; }

A.) myVector[0] = 99;

Which assigns the array's first element with 99? int myVector[4]; A.) myVector[0] = 99; B.) myVector[] = 99; C.) myVector[1] = 99; D.) myVector[-1] = 99;

A.) userNum[N_SIZE - 1] = 20;

Which assigns the last array element with 20. int userNum[N_SIZE]; A.) userNum[N_SIZE - 1] = 20; B.) userNum[] = 20; C.) userNum[20]; D.) userNum[N_SIZE] = 20;

last value is negative

Which best describes the meaning of a 1 (true) being output? Assume v is a large array of ints. int i; bool s; for (i = 0; i < N_SIZE; ++i) { if (v[i] < 0) { s = true; } else { s = false; } } cout >> s;

min value in v

Which best describes what is output? Assume v is a large array of ints. int i; int s; s = v[0]; for (i = 0; i < N_SIZE; ++i) { if (s > v[i]) { s = v[i]; } } cout << s;

D.) char name[5] = "Alex";

Which character array declaration is appropriate? A.) char name[5] = "Alexandria"; B.) char name[5] = "Alexa"; C.) char name[5] = "Alexander"; D.) char name[5] = "Alex";

D.) int personName[50]; int personAge[50];

Which declares two related integer arrays named personName and personAge each with 50 elements? A.) int personName, personAge; B.) int personName = 50; int personAge = 50; C.) int personName, personAge = 50; D.) int personName[50]; int personAge[50];

double ComputeEnergyConsumed(double power, double hours) { return (power * hours) / 1000; }

Which function is most appropriate to improve the given code? int main() { double powerConsumptionApp1; double powerConsumptionApp2; double powerConsumptionApp3; double hoursOfUse1; double hoursOfUse2; double hoursOfUse3; double energyPerDay1; double energyPerDay2; double energyPerDay3; double totalEnergyConsumed; powerConsumptionApp1 = 600.85; hoursOfUse1 = 12.8; energyPerDay1 = (powerConsumptionApp1 * hoursOfUse1) / 1000; powerConsumptionApp2 = 1800.45; hoursOfUse2 = 0.45; energyPerDay2 = (powerConsumptionApp2 * hoursOfUse2) / 1000; powerConsumptionApp3 = 70; hoursOfUse3 = 1.5; energyPerDay3 = (powerConsumptionApp3 * hoursOfUse3) / 1000; totalEnergyConsumed = energyPerDay1 + energyPerDay2 + energyPerDay3; cout << "The total energy consumed per day is " << totalEnergyConsumed << "." << endl; return 0; }

C.) the algorithm starts at the end of the list.

Which is not true for a linear search algorithm? A.) the algorithm checks each element until the search key is found. B.) the algorithm will compare all elements if the search key is not found. C.) the algorithm starts at the end of the list. D.) the algorithm starts at the beginning of the list.

D.) {A, C, Z, a, b, x}

Which is the sorted list for the array char check[] = { 'A', 'b', 'C', 'a', 'Z', 'x'}? A.) {A, a, b, C, x Z} B.) {a, A, b, C, x, Z} C.) {a, b, x, A, C, Z} D.) {A, C, Z, a, b, x}

B.) an array is automatically passed to a function as a pointer.

Which is true about arrays and functions? A.) passing an array to a function creates a copy of that array within the function. B.) an array is automatically passed to a function as a pointer. C.) Arrays cannot be passed to functions. D.) A programmer must first make a copy of an array to pass the array as a function.

B.) using a variety of calls involving assert() is a good way to test the method.

Which is true about testing a function with three integer parameters and one integer return value? A.) a good programmer would test all possible input values. B.) using a variety of calls involving assert() is a good way to test the method. C.) three test vectors are likely sufficient. D.) each test case should include a negative value as a parameter.

D.) printing the array userText will work fine because the new string is 4 characters and the array size is 10.

Which is true for the following code? char userText[10]; userText[0] = 'B'; userText[1] = 'o'; userText[2] = 'o'; userText[3] = 'k'; userText[4] = '\0'; ... userText[3] = 't'; A.) the compiler generates an error because element 3 can't be overwritten. B.) the first five characters of the array userText are Bookt. C.) the first four characters of the array userText are Book. D.) printing the array userText will work fine because the new string is 4 characters and the array size is 10.

D.) a function's local variables are discarded upon a function's return, each new call creates new local variables in memory.

Which is true regarding how functions work? A.) a return address indicates the value returned by the function. B.) if a function returns a variable, the function stores the variable's value until the function is called again. C.) after a function returns, its local variables keep their values which serve as their initial values the next time the function is called. D.) a function's local variables are discarded upon a function's return, each new call creates new local variables in memory.

Line 3

Which line of the function has an error? int ComputeSumOfSquares(int num1, int num2) { int sum; // Line 1 sum = (num1 * num1) + (num2 * num2); // Line 2 return; // Line 3 }

Trimming the hedges: Turn on the hedge trimmer. Run the hedge trimmer along the top and again on the sides for a section. Trim along each section of the hedge.

Which of the following is an example of a recursive function?

D.) strcat(myStr, "!");

Which statement appends an exclamation point at the end of myStr? char myStr[10] = "Yay"; A.) strcpy(myStr, "!"); B.) strcat(myStr, '!'); C.) strncpy(myStr, "!", 2); D.) strcat(myStr, "!");

B.) int myArray[3][4];

Which statement declares a two-dimensional integer array called myArray with 3 rows and 4 columns? A.) int myArray[7]; B.) int myArray[3][4]; C.) int myArray[4][3]; D.) int myArray[3, 4];

selection

____ sort is a sorting algorithm that treats the input as two parts, a sorted part and an unsorted part, and repeatedly picks the proper next value to move from the unsorted part to the end of the sorted part.


Kaugnay na mga set ng pag-aaral

Algebra II Mid Term Exam Study Guide

View Set

Unit 4: Close Analysis of Poetic Form and Content

View Set

Introduction to Instruction and Presentation

View Set

Cognitive Final Chapters: 11 and onward

View Set

PART 1 - STRATEGIC ANALYSIS. Chapter 1 - Strategic Management: Creating Competitive Advantages

View Set

BIOL 1262 - (BOTANY) Adaptations Of Plants To Extreme Habitats

View Set