Comp 1220 Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? string s = "Go"; while ((s != "q") && (s != "Q")){printf("In loop\n");scanf("%s", s);}

"Quit"

What is the output if count is 4? for (i = count; i > 0; --i) {// Output i}

4321

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

49

What is the output? int MyFct(int x) {int y;x = x * 2;y = x + 1;return y;}int main(void) {int a;a = 5;printf("%d %d", a, MyFct(a));return 0;}

5 11

How many x's will be output? i = 1;while (i <= 3) {j = 1;while (j <= i) {printf("x");++j;}printf("\n");++i;}

6

What is output, if the input is 3 2 1 0? scanf("%d", &x);while (x > 0) {printf("%d ", 2 * x);}

6 6 6 6 6 ... (infinite loop)

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

7, 3, 0, 8, 8

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

8

What is output? #include <stdio.h>void CheckValue(int* pointVar1, int* pointVar2) { if (pointVar1 == NULL && pointVar2 == NULL) {printf("Pointer is null\n");} else if (*pointVar2 > *pointVar1) {printf("%p\n", *pointVar2);} else if (*pointVar1 > *pointVar2) {printf("%p\n", *pointVar1);}}int main() {int num1 = 5;int num2 = 9; CheckValue(&num1, &num2); return 0;}

9

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;scanf("%s",userState);foundState = false;for (i = 0; XXX; ++i) {if (stateNames[i] == userState) {foundState = true;printf("%s, %d \n", userState, statePop[i]);}}

<code>(i < NUM_STATES) && (!foundState)</code>

Which XXX will result in output "4.00 feet is equal to 1.22 meters"? #include <stdio.h>void ConvertFeetToMeters(float numFeet, float* numMeters) {*numMeters = numFeet / 3.28084;}int main(void) {float numFeet = 4.0;float numMeters = 0.0;XXXprintf("%.2f feet is equal to %.2f meters", numFeet, numMeters);return 0;}

<code>ConvertFeetToMeters(numFeet, &numMeters);</code>

Given integer variables totalTickets, vipTickets, and sessionNum, which line of code correctly calls the following function? void FinalTally(int* totalAttendees, int* vipAttendees, int numSessions);

<code>FinalTally(&totalTickets, &vipTickets, sessionNum);</code>

Given an integer array of size NUM_ELEMENTS, which XXX, YYY, and ZZZ will count the number of times the value 4 is in the array? Choices are in the form XXX / YYY / ZZZ. int myArr[NUM_ELEMENTS];int cntFours;XXXfor (i = 0; YYY; ++i) {if (myVals[i] == 4) {ZZZ;}}

<code>cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;</code>

A loop should output 1 to n. If n is 5, the output is 12345. What should XXX and YYY be? Choices are in the form XXX / YYY. scanf("%d", &n);for (XXX; i++) {printf("%d", YYY);}

<code>i = 0; i < n / i + 1;</code>

Which XXX causes every character in string inputWord to be output? for (XXX) {printf("%c\n" &inputWord[i]); }

<code>i = 0; i < strlen(inputWord); ++i</code>

Which XXX and YYY correctly output the smallest values? Array userVals contains 100 elements that are integers (which may be positive or negative). Choices are in the form XXX / YYY. // Determine smallest (min) valueint minVal;XXX for (i = 0; i < 100; ++i) {if (YYY) {minVal = userVals[i]; }}printf("Min: %d" ,minVal);

<code>minVal = userVals[0]; / userVals[i] < minVal</code>

What line of code makes the character pointer studentPointer point to the character variable userStudent? char userStudent = 'S';char* studentPointer;

<code>studentPointer = &userStudent;</code>

Which of the following statements is true about a memory leak?

A memory leak occurs when a program allocates memory but loses the ability to access the allocated memory.

Which is true about incremental program development?

All programmers, regardless of experience, benefit from incremental development

What is the output? void PrintFeverCheck (double temperature) {const double NORMAL_TEMP = 98.6;const double CUTOFF_TEMP = 95;double degreesOfFever;if (temperature > NORMAL_TEMP) {degreesOfFever = temperature - NORMAL_TEMP;printf("You have %lf degrees of fever.", degreesOfFever);} else if (temperature < CUTOFF_TEMP) {degreesOfFever = CUTOFF_TEMP - temperature;printf("Your temperature is %lf below 95.", degreesOfFever);}}int main(void) { double bodyTemperature;bodyTemperature = 96.0;printf("Checking for fever...");PrintFeverCheck(bodyTemperature);return 0;}

Checking for fever...

What is output? #include <string.h>typedef struct Data_struct { int ID; char type[8];} Data;int main(void) { const int NUM_DATA = 2;Data idData[NUM_DATA];int i;idData[0].ID = 0; strcpy(idData[0].type, "Public"); idData[1].ID = 1; strcpy(idData[1].type, "Private"); printf("Total data: %d", NUM_DATA); for (i = 0; i < NUM_DATA; ++i) {printf(" ID: %d", idData[i].ID); printf(" type %s", idData[i].type);} return 0;}

Total data: 2 ID: 0 type: Public ID: 1 type: Private

A programmer must write a 500 line program. Which is most likely the best approach?

Write 10-20 lines, run and debug, write 10-20 more lines, run and debug, repeat

If realloc() cannot locate enough memory at the end of a memory block, what does realloc() return?

a pointer to a new memory block, where existing data has been copied

What does malloc() return when dynamically allocating an array?

a pointer to the memory location of the array's first element

Assuming bodyWeights = (double*)malloc(3 * sizeof(double)); , which declaration will reallocate an array of 10 doubles?

bodyWeights = (double*)realloc(bodyWeights, 10 * sizeof(double));

A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative integers are replaced by zeros. The function _____.

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

Which statement correctly declares a char array named myText initialized to Hello?

char myText[10] = "Hello";

Which character array declaration is appropriate?

char name[5] = "Alex";

For the given pseudocode, which XXX and YYY will output the number of times that 10 is input (stopping when 0 is input)? Choices are in the form XXX / YYY. count = 0val = Get next inputWhile val is not 0If val == 10XXXElseYYYval = Get next inputPut count to output

count = count + 1 / (nothing)

Given two integer arrays prevValues and newValues. Which code copies the array prevValues into newValues? The size of both arrays is NUM_ELEMENTS.

for (i = 0; i < NUM_ELEMENTS; ++i) { newVals[i] = prevVals[i];}

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?

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

Which function can be used to avoid a memory leak?

free

What function will deallocate memory after realloc() has been used?

free()

Which function deallocates memory allocated by the malloc() function?

free()

Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i;string str;scanf("%s", str);for (YYY) {printf("%c", str[i]);}

i = str.length() - 1; i >= 0; --i

The program should determine the largest integer seen. What should XXX be, if the input values are any integers (negative and non-negative)? All variables are integers and have unknown initial values. for (i = 0; i < 10; ++i) {scanf("%d", &currValue);if (i == 0) {// First iterationXXX;}else if (currValue > maxSoFar) {maxSoFar = currValue;}}

maxSoFar = currValue

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]; }}printf("%d \n", s);

min value in v

Given a vector/array with values 5, 10, 15, 20, 25, what are the fewest number of swaps needed to reverse the list?

2

How many times will the loop iterate, if the input is 105 107 99 103? scanf("%d", &x);while (x > 100) {// Do somethingscanf("%d", &x);}

2

How many x's will be output? Assume row and col are integers. for (row = 0; row < 2; ++row) {printf("x");for (col = 0; col < 3; ++col) {// Do something}}

2

What is output, if the input is 3 2 4 5? All variables are integers. scanf("%d", &num);for (i = 0; i < num; ++i) {scanf("%d", &curr);printf("%d", curr);}

245

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

, int numVals / i < numVals

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

0

What value is automatically assigned to the first item in the Capitals enumerator? enum Capitals {ATLANTA, BIRMINGHAM, HONOLULU, DENVER};

0

What is the output? for (i = 0; i <= 10; ++i) {if (i == 6)continue;elseprintf("%d ", i); }

0 1 2 3 4 5 7 8 9 10

What is the output? int n;for (n = 0; n < 10; n = n + 3) {printf("%d ", n);}

0 3 6 9

What is the output? j and k are ints. for (j = 0; j < 2; ++j) {for (k = 0; k < 4; ++k) {if (k == 2) {break;}printf("%d%d ", j, k);}}

00 01 10 11

How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3;while (userNum > 0) {// Do something// Get userNum from input}

1

What is the output, if n is 3? for (int i = 1; i <= n; ++i) {int factorial = 1;factorial = factorial * i;printf("%d ", factorial);}

1 2 2003

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;

35

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

10

What is the output? int num = 10;while (num <= 15) {printf("%d ", num);if (num == 12) {break;}++num; } printf("Done");

10 11 12 Done

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

10, 5, 5

What is the ending value of sum, if the input is 2 5 7 3? All variables are integers. scanf("%d", &x);sum = 0;for (i = 0; i < x; ++i) {scanf("%d", &currValue);sum += currValue;}

12

What is output? #include <string.h>#include <stdio.h>typedef struct Sample_struct {int value;} Sample;int main(void) {int x = 5;Sample myVect[x];for (int a = 0; a < x; a++) {myVect[a].value = myVect[a].value + 12;printf("%d ",myVect[a].value);}return 0;}

12 12 12 12 12

What is the output? int x = 18;while (x > 0) {// Output x and a spacex = x / 3; }

18 6 2

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

2

Which is true about arrays and functions?

An array is automatically passed to a function as a pointer

What is not a key advantage of using an enum as below have versus using a string for myCar and string literals like "red", "green", and "black"? enum CarColor {RED, GREEN, BLACK};CarColor myCar;myCar = GREEN;if (myCar == GREEN) {cout << "Green is our most popular color";}

Capital letters can be used to make the colors clearer

For an enumerated type like below, the compiler assigns what kind of value to RED, GREEN, and BLACK. enum CarColor {RED, GREEN, BLACK};

Integer

Given that integer array x has elements 5, 10, 15, 20, what is the output? int i;for (i = 0; i < 4; ++i) {printf("%d ", x[i] + x[i + 1]); }

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) {printf("%d ", x[i] + x[i + 1]); }

Error: Invalid access

What is the output? int main() {for (int i = 0; i < 3; ++i) {printf("%d", i);}printf("%d", i);return 0;}

Error: The variable i is not declared in the right scope

What is the output? int i;for (i = 0; i < 4; ++i) {int factorial = i + 1;for (int j = 0; j < 2; ++j) {printf("%d", factorial * j);}}printf("%d\n", factorial);

Error: factorial is out of scope

What is the ending value of myStr? char myStr[5] = "Hi"; strcpy(myStr, "Hey");

Hey

Which XXX / YYY declare an array having MAX_SIZE elements and initializes all elements with -1? const int MAX_SIZE = 4;int myNumbers[XXX];int i;for (i = 0; i < YYY; ++i) {myNumbers[i] = -1;}

MAX_SIZE / MAX_SIZE

Which input for char c causes "Done" to be output next? c = 'y';while (c = 'y') {// Do somethingprintf("Enter y to continue, n to quit: \n");scanf("%c", &c);}printf("Done\n");

No such value (infinite loop)

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

Perfect temperature.Too cold.

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

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

What is the purpose of the & symbol in the following code? int* myVar;int distance = 132;myVar = &distance;

Refers to the memory address of distance

What is output? #include <stdio.h>int main() { int myInt; int* myRestaurant; int myVar; int* myBill = NULL; myInt = 10; myRestaurant = &myInt; myVar = *myBill; myVar = *myRestaurant + 10.5; printf("%f\n", myVar); return 0;}

Runtime errors

What type of value will myFunc() return for the given program? typedef struct Sample_struct { int a; int b;} Sample;Sample myFunc() { Sample s;printf("%d", s.a);return s;} int main(void) { Sample sam; sam = myFunc();}

Sample

Which is an essential feature of a while loop having the following form? while (LoopExpression) {LoopBody}

The LoopExpression should be affected by the LoopBody

Why is it best to develop a large program incrementally?

The program is easier to debug

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;}}printf("%d", s);

last value is negative

Which assigns the array's first element with 99? int myVector[4];

myVector[0] = 99;

Assuming int* newDimensions; is already declared, which declaration will dynamically allocate an array of 5 integers?

newDimensions = (int*)malloc(5 * sizeof(int));

A common error when allocating a string is forgetting to account for the _____.

null character

Which statement correctly assigns 12 into the fifth row and third column of array itemArray?

numVals[4][2] = 12;

Which is an invalid access for the array? int numsList[5];int x = 3;

numsList[x+2]

What line of code assigns a char variable outputGames with the value the gamesPointer points to?

outputGames = *gamesPointer;

What line of code assigns a char variable outputGames with the value the gamesPointer points to? char userGames = 'B';char* gamesPointer;

outputGames = *gamesPointer;

What is the output? char letter1;char letter2;letter1 = 'p';while (letter1 <= 'q') {letter2 = 'x';while (letter2 <= 'y') {printf("%c%c ", letter1, letter2);++letter2;}++letter1;}

px py qx qy

Given array scorePerQuiz has 10 elements. Which assigns element 7 with the value 8?

scorePerQuiz[7] = 8;

Which statement appends an exclamation point at the end of myStr? char myStr[10] = "Yay";

strcat(myStr, "!");

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 prints out only the student names whose score is above 80? Choices are in the form XXX / YYY. char studentNames[NUM_STUDENTS]; int studentScores[NUM_STUDENTS]; int i; for (i = 0; i < NUM_STUDENTS; ++i) {if (XXX > 80) {printf("%s \n", YYY ); }}

studentScores[i] / studentNames[i]

A loop should sum all inputs, stopping when the input is 0. If the input is 2 4 6 0, sum should end with 12. What should XXX, YYY, and ZZZ be? Choices are in the form XXX / YYY / ZZZ. int sum;int currVal;XXX;scanf("%d", &currVal);while (YYY) {ZZZ;scanf("%d", &currVal);}

sum = 0 / currVal != 0 / sum = sum + currVal

For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY. val = Get next inputXXXWhile val is not -1YYYval = Get next inputPut sum to output

sum = 0 / sum = sum + val

Select the statement XXX that will correctly swap the value in smallArray[0] with smallArray[2]. int smallArray[] = {5, 12, 16};XXX

temp = smallArray[0];smallArray[0] = smallArray[2];smallArray[2] = temp;

What code for XXX, YYY correctly swaps a and b? Choices are written as XXX / YYY. XXX;a = b;YYY;b = tmp;

tmp = a / (nothing)

Which assigns the last array element with 20. int userNum[N_SIZE];

userNum[N_SIZE - 1] = 20;

Which XXX is most appropriate for printing out the char array userText? userText[10] = "zyBook";for (i = 0; XXX; ++i) {// Print userText[i]}

userText[i] != '\0'

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

userVals[i][j] < minVal / minVal = userVals[i][j]

For the given pseudocode, which XXX and YYY will output the smallest non-negative input (stopping when a negative value is input)? Choices are in the form XXX / YYY. min = 0val = Get next inputmin = valWhile val is not negativeIf XXXYYYval = Get next inputPut min to output

val < min / min = val

A pointer is a(n) _____ that contains a _____.

variable, memory address

Based on the function names, which function most likely should allow the inputVals array parameter to be modified? (Not all parameters are shown.)

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

Which function has two int parameters, startVal and endVal, that are passed by pointer?

void function SwapVals(int* startVal, int* endVal)

Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY. scanf("%d", &w);while (XXX) {// Do somethingYYY;}

w < 100 / scanf("%d", &w)

What is the output? int columns;int rows;for (rows = 0; rows < 2; ++rows) {for (columns = 0; columns < 3; ++columns) {printf("x");}printf(" ");}

xxx xxx


Set pelajaran terkait

DIAMANTE - poema de Rubén Darío - Diamond - a poem by Ruben Dario

View Set

Exam #1 Topic: Analyzing Business Processes

View Set

Health and Life Insurance Exam, Questions

View Set

Microeconomic Chapters 5,7, and 11

View Set

Unit 4: Financial Markets (Kahoot and Notes)

View Set

ASVAB - Arithmetic Reasoning / Mathematics Knowledge 2

View Set

Writing Equations in Point-Slope and Slope-Intercept Form

View Set