Practice Midterm

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); } "q" only "Q" only Either "q" or "Q" "Quit"

"Quit"

What is output? char level[] = "Minimum"; int amount = 13; printf("You have a %-.3s. of %d miles left to go \n", level, amount); return 0; "You have a Minimum. of 13 miles left to go" "You have a Min of 13 miles left to go" "You have a Min. of 13 miles left to go" Error: The string sub-specifier is not correct

"You have a Min. of 13 miles left to go"

What is the ending value of len? str = "Mid-wife "; len = strlen(str); 7 9 8 11

9

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

(10, j, k +5)

What is the ending value of z? x = 0; y = 3; z = pow(x + 2, y); 0 4 8 Error: Cannot have an expression within a function call

8

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

4321

What is output, if userVal is 5? int x; x = 100; if (userVal != 0) { int tmpVal; tmpVal = x / userVal; printf("%d", tmpVal); } 0 5 20 Error: The compiler generates an error

20

When commenting out multiple lines with /* */, what common error might occur? The programmer might include too many FIXME comments The programmer might forget to use // to comment out function calls Too many multi-line comments might cause the compile to fail Another set of /* */ symbols might be embedded inside

Another set of /* */ symbols might be embedded inside

For what values of x will "Medium" be output? If x > 40: Output "Large" Else If x > 20: Output "Medium" Else If x > 10: Output "Small" Any x smaller than 20 Any x larger than 40 Any x from 21 to 40 Any x from 10 to 40

Any x from 21 to 40

Why should programmers avoid making the following comparison with double x? x == 0.3 Because 0.3 may not be exactly represented as 0.3 Because doubles should not be compared with integers Because variables should not be compared with literals Because the 0.3 should come first: 0.3 == x

Because 0.3 may not be exactly represented as 0.3

Which item converts a high-level language program to low-level machine instructions? Compiler Machine instruction Assembly language Memory

Compiler

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; XXX printf("%.2f feet is equal to %.2f meters", numFeet, numMeters); return 0; } ConvertFeetToMeters(*numFeet, &numMeters); ConvertFeetToMeters(numFeet, *numMeters); ConvertFeetToMeters(numFeet, &numMeters); ConvertFeetToMeters(&numFeet, *numMeters);

ConvertFeetToMeters(numFeet, &numMeters);

What is the output? int main() { for (int i = 0; i < 3; ++i) { printf("%d", i); } printf("%d", i); return 0; } 12 122 123 Error: The variable i is not declared in the right scope

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

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

FinalTally(&totalTickets, &vipTickets, sessionNum);

Information hiding allows the ADT user to _____. improve implementation of data types like arrays develop without the need for error handling (wrong) override the implementation of an ADT focus on higher-level concepts

Focus on Higher Level Concepts

Which is true about unit testing? The best test harness prints information for every test vector Using assert() makes testing less complicated and less error prone Good test vectors use only expected user input Good practice calls for testing the entire program first

Good practice calls for testing the entire program first(Possibly)

What does the malloc() function return if the function fails to allocate memory? 0 void pointer a null pointer NULL

NULL

For what values of x does the default case execute in the code below? x is declared as an integer. switch (x) { case 2: ... break; case 3: ... break; case 4: ... break; default: ... // When does this execute? } Only for value 5 Only for all values greater than 4 Only for values that are not 2, 3, or 4 For any value PreviousNext

Only for values that are not 2, 3, or 4

Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s), before recompiling? All the errors The first error only The middle error only The last error only

The first error only

What is output? typedef struct Data_struct { int x; int y; int result; } Data; Data Greater(int val1, int val2) { Data inData; inData.x = val1; inData.y = val2; if(val1 > val2) { inData.result = val1; } else { inData.result = val2; } return inData; } int main(void) { int a = 5; int b = 9; int result; Data myInput; myInput = Greater(a, b); printf("Result: %d", myInput.result); return 0; } Result: 9 Result: 5 Result: 1 Result: True

Result: 9

Which is the first thing the compiler does when a function is called? Converts the function's code to assembly Creates instructions to jump to the function Stores the address of the function call Creates a jump instruction to the function

Stores the address of the function call

If a program compiles without errors, the program is free from _____. syntax errors logic errors runtime errors semantic errors

Syntax Errors

What is output? int pop = 1340; printf("There are %06d residents of the city. \n", pop); There are 001340 residents of the city. There are 1340 residents of the city. There are 0001340 residents of the city. There are +0001340 residents of the city.

There are 001340 residents of the city.

Given input "Samantha Jones", what does the following code do? char myString[10]; fgets(myString, 10, stdin); Writes "Samantha Jones" to string myString Writes "Samantha " to string myString Writes "Samantha J" to string myString Error: input is larger than the array size

Writes "Samantha " to string myString

What is the output if the input is "Yes", "No", "Yes", "Maybe"? char response[10] = ""; scanf("%s", response); while (strcmp(response, "Maybe") != 0) { if (strcmp(response, "Yes") == 0) { printf("Wrong "); } else { printf("%s ", response); } scanf("%s", response); } Yes No Yes Wrong No Wrong Maybe Wrong No Wrong Yes No Yes Maybe

Wrong No Wrong

Given input 420, what is output? typedef struct PenniesToDollars_struct { int dollars; } PenniesToDollars; PenniesToDollars ComputeDollars(int pennies) { PenniesToDollars cal; cal.dollars = pennies / 100; return cal; } int main(void) { int somePennies; PenniesToDollars dollarValue; printf("Enter penny value:"); scanf("%d", &somePennies); dollarValue = ComputeDollars(somePennies); printf("You have %d pennies or %d dollars", somePennies, dollarValue.dollars); return 0; } You have 420 pennies or 4 dollars You have 420 pennies or 4.2 dollars You have 420 pennies or 5 dollars Error: 'dollarvalue' declaration is incorrect.

You have 420 pennies or 4 dollars

What is output? typedef struct Sum_struct { int a; int b; } Sum; void Display(Sum obj) { obj.a = obj.a + 5; obj.b = obj.b + 10; printf("a = %d b = %d\n " , obj.a , obj.b); printf("Sum = %d\n" , obj.a + obj.b); } int main(void) { Sum point; point.a = 10; point.b = 20; Display(point); return 0; } a = 10 b = 20 Sum = 30 a = 5 b = 10 Sum = 15 a = 15 b = 30 Sum = 45 Error: Struct declared incorrectly

a = 15 b = 30 Sum = 45

What does malloc() return when dynamically allocating an array? a pointer to the array elements' data type the memory address location of the array's first element a pointer to the memory location of the array's first element a null pointer

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

A sequence of instructions that solves a problem is called a(n) _____ . instruction process algorithm variable

algorithm

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; XXX for (i = 0; YYY; ++i) { if (myVals[i] == 4) { ZZZ; } } cntFours = myArr[0]; / i > NUM_ELEMENTS; / cntFours = myArr[i]; cntFours = myArr[1]; / i < NUM_ELEMENTS; / cntFours = myArr[i] + 1; cntFours = 1; / i > NUM_ELEMENTS; / cntFours = cntFours + 1; cntFours = 0; / i < NUM_ELEMENTS; / ++cntFours;

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

Given two integer arrays prevValues and newValues. Which code copies the array prevValues into newValues? The size of both arrays is NUM_ELEMENTS. newValues = prevValues; newValues[] = prevValues[]; newValues[NUM_ELEMENTS] = prevValues[NUM_ELEMENTS]; for (i = 0; i < NUM_ELEMENTS; ++i) { newVals[i] = prevVals[i]; }

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 < valsArray.size(); ++i) where size is a built-in function for arrays for (i = 0; i < valsArray; ++i) due to the compiler knowing the size of arrays for (i = 0; i < arraySize; ++i) where arraySize is a second function parameter for (i = 0; i != lastItem; ++i) where lastItem is the value of the array's last element

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

Which XXX is used to read and store the data into the variable numberPlayers from the file that is opened by the stream inputFile? int main(void) { FILE* inputFile = NULL; int numberPlayers; inputFile = fopen("playerNumber.txt", "r"); if (inputFile==NULL) { printf("Could not open file numFile.txt."); return 1; } else { XXX; printf("Players Number: " , numberPlayers); } return 0; } fscanf(inputFile, "%d", &numberPlayers); scanf(inputFile, "%d", &numberPlayers); sscanf(inputFile, "%d", &numberPlayers); scanf("%d", &numberPlayers);

fscanf(inputFile, "%d", &numberPlayers);

Which variable naming convention is used for distanceFromSun? snake case hill valley case lower camel case varied capitalization case

lower camel case

Which statement correctly assigns 12 into the fifth row and third column of array itemArray? numVals[5][3] = 12; numVals[3][5] = 12; numVals[4][2] = 12; numVals[2][4] = 12;

numVals[4][2] = 12;

Which XXX outputs the following? You have 3 Pencils. Total Price: 60 #include <stdio.h> #include <string.h> typedef struct Data_struct { int itemCount; double price; char name[20]; } Data; Data myData; void updateItemCount(int x) { myData.itemCount = x; } void updateItemPrice(double p) { myData.price = p; } void updateItemName(char n[20]) { strcpy(myData.name,n); } double totalPrice() { XXX } int main(void) { int number = 3; double price = 20.0; char name[] = "Pencil"; double total; updateItemCount(number); updateItemPrice(price); updateItemName(name); total = totalPrice(); printf("You have %d %ss. ", number, name); printf("Total Price: %.0f ", total); return 0; } return itemCount * price; return myData.itemCount * myData.price; return price * number; total = myData.itemCount * myData.price;

return myData.itemCount * myData.price;

Given char x, which reads an input character into x? scanf("%c", &x); scanf("%s", x); scanf("%s", &x); scanf("%c", x);

scanf("%c", &x);

Which header file is required to access the file pointer (FILE*)? stdlib.h stddef.h stdio.h stdint.h

stdio.h

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

strcat(myStr, "!");

Identify the statement that checks whether myChar exists within myString, returning NULL if myChar does not exist, or returning the address of the first occurrence if myChar does exist? strchr(myString, myChar) strcat(myString, myChar) if(myChar != NULL) strcmp(myString, myChar)

strchr(myString, myChar)

Given string str1 and string str2, which expression is true? str1 = "Flat"; str2 = "Last"; strcmp(str1, str2) == 0 strcmp(str1, str2) < 0 strcmp(str1, str2) > 0 (Comparison not possible)

strcmp(str1, str2) < 0

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

studentPointer = &userStudent;

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 sum = currVal / currVal == 0 / sum = currVal sum = 1 / currVal != 0 / sum = sum + 1 scanf("%d", &sum) / currVal == 0 / scanf("%d", &sum)

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

For the string "on time", what character is at index 3? (Space) t i m

t

Which assigns the last array element with 20 int userNum[N_SIZE]; userNum[20]; userNum[] = 20; userNum[N_SIZE] = 20; userNum[N_SIZE - 1] = 20;

userNum[N_SIZE - 1] = 20;


Set pelajaran terkait

Chapter 4: Describing Data- Displaying and exploring data

View Set

Chapter 33 Accounts Payable and Accounting Procedures

View Set

HESI Case Studies Spinal Cord Injury (RYAN)

View Set

PN Fundamentals Online Practice 2020 A

View Set

Chapter 6.4.8 Practice Questions

View Set

RN pharmacology online practice 2019 B

View Set