Systems Programming Midterm Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which expression can be used to decide if x is not between 10 and 20?

!(10<x<20)

Identify the symbol used to terminate a structure.

;

Which is a valid compound operator?

%=

Which of the following code snippets result in a memory leak? void StudentClass() { int * ptr = (int*)malloc(sizeof(int)); }

(NOT- int main() { StudentClass(); free(StudentClass()); return 0; } )

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

18 6 2

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

6

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

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

9

Assuming char* firstSymbol; is already declared, return a pointer to the first instance of an '!' in userInput.

firstSymbol = strchr(userInput, '!');

Which XXX will result in the output "25"? #include <stdio.h> #include <stdlib.h>> void main() { typedef struct userData_struct { int myInt; char initial; } userData; userData* newData = NULL; newData = (userData*)malloc(sizeof(userData)); XXX printf("%d", newData->myInt); free(newData); }

(*newData).myInt = 25;

What is the output? #include <stdio.h> const double LB_PER_KG = 2.2; double KgsToLbs(double kilograms) { double pounds; pounds = kilograms * LB_PER_KG; return pounds; } int main(void) { double pounds; pounds = KgsToLbs(10); printf("%lf", pounds); return 0; }

22.0

Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. <code>(x == 5) || (y == 2) && (z == 5)</code>

False OR (true AND false) --> false OR false --> false

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

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

char myText[10] = "Hello";

Which XXX will delete a node from a list? DeleteNode(IntNode* priorNode) { IntNode* delNode = NULL; XXX }

delNode = priorNode->nextNodePtr; priorNode->nextNodePtr = delNode->nextNodePtr;

Which expression best determines if double variable x is 74.7?

fabs(x-74.7) < 0.0001

The automatic process of finding and freeing unreachable allocated memory locations is called a _____.

garbage collection

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

i = 0; i < strlen(inputWord); ++i;

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

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

What is output? #include <stdo.h> int main() { float newSize = 1.0; int i; for (i=100; i<120; ++i) newSize *= i; } printf("%f", newSize); return 0; }

infinity

A linked list supports faster ___ than a vector

inserts or deletes

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

min value in v

What is the output? int main(void) { double dividend = 0.0; double divisor = 0.0; printf("%lf\n", dividend / divisor); dividend = 3.0; printf("%lf\n", dividend / divisor); return 0; }

nan inf

What is the output? const int MAX_LEN = 10; char userStr[MAX_LEN] = "PASS123"; int i; for (i = 0; userStr[i] != '\0'; ++i) { userStr[i] = tolower(userStr[i]); }

pass123

Given str = "Cat", what is the result of this statement? str[1] = "ab";

(NOT- str is "Cab" )

Which expression for XXX outputs "Modern era" for any year 1980 and later? <code>if (year < 2000) { // Output "Past" } else if (XXX) { // Output "Modern era" }</code>

(No such expression exists)

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

(i < NUM_STATES) && (!foundState)

What values for x cause Branch 1 to execute? If x > 100 : Branch 1 Else If x > 200: Branch 2

101 or larger

What is output? double Myfct(double a, double b){ return (a+b) / 2.0; } int main(void) { double x = 3.0; double y = 5.0; double z = 8.0; double t; t = MyFct(x,y); t = MyFct(t,z); printf("%lf\n", t); return 0; }

6.0

Which value of y results in short circuit evaluation, causing z == 99 to not be evaluated? (y > 50) || (z == 99)

60

What is the ending value of y? sqrt(u) returns the square root of u. pow(u, v) returns u raised to the power of v. x = 9.0; y = 4.0; y = pow(sqrt(x), sqrt(y));

9.0

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 = 0 val = Get next input While val is not 0 If val == 10 XXX Else YYY val = Get next input Put count to output

count = count + 1 / (nothing)

A linked list supports faster _____ than a vector.

inserts or deletes

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 Min. of 13 miles left to go"

Which is a valid preprocessor directive?

(?) #include filename.h (prob) #include "filename.h"

What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1;

(?) 8 (probably) 7

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

(NOT- 01020304)

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

(NOT- 15) probably 12

What is the output if myContact.txt file does not exist? int main(void) { FILE* inputFile = NULL; printf("Opening the file."); inputfile = fopen("myContact.txt", "r"); if (inputFile = NULL_ { printf("Could not open the file."); return 1; } fclose(inFS_; return 0; }

(NOT- Could not open the file) Probably - Opening the file. Could not open the file.

What does the malloc() function return if the function fails to allocate memory?

(NOT- a null pointer )

Complete the correct code for inserting a node in a list. InsertAfter(IntNode* nodeLoc) { IntNode* tmpNext = NULL; XXX }

tmpNext = this->nextNodePtr; this->nextNodePtr = nodeLoc; nodeLoc->nextNodePtr = tmpNext;

Which expression for YYY correctly outputs that x is between 50-100? if (YYY) { // Output "50, 51, ..., 99, 100" }

(x >= 50) && (x <= 100) PreviousNext

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 function parameters exist in the code? double FahrenheitToCelsius(double fahrenheit) { return (fahrenheit - 32.0) * 5.0 / 9.0; } int main(void) { double fahrenheit; scanf("%lf", & fahrenheit); int c1; int c2; int c3; int c4; c1 = FahrenheitToCelsius(fahrenheit); c2 = FahrenheitToCelsius(32); c3 = FahrenheitToCelsius(78); c4 = FahrenheitToCelsius(100); return 0; }

1

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

If the heap memory location starts at 9403, what is the value of the integer variable myAge at position 1? #include <stdio.h> #include <stdlib.h> int a = 10; int main() { int b; int * myAge = NULL; b = 20; myAge = (int * ) malloc(sizeof(int)); // Position 1 * myAge = 30; free(myAge); return 0; }

9403?

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

? 20.5 probably one of the error messages)

What is output? printf("Calculation: %+09.4lf \n", -13.785742835);

? Calculation: +13.7857 possibly Calculation: -013.7857

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

? The first four characters of the array userText are Book. maybe Printing the array userText will work fine because the new string is 4 characters and the array size is 10.

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

? double CalculateAverage (int inputVals[], ...)

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.

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

Which is true regarding header files?

Header files should contain function definitions for functions declared in another file

What is the error in this code? void MyFct(int & x) { x = (x * x) + 1; } int main(void) { printf("%d", MyFct(9)); return 0; }

MyFct() cannot be called with a literal

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 true about unit testing?

Using assert() makes testing less complicated and less error prone

Which is true about testing a function with three integer parameters and one integer return value?

Using various calls involving assert() is a good way to test the function

Which of the following statements allocates memory in the heap?

a = (int*)malloc(sizeof(int));

A linked list's head node stores _____ in the list.

a pointer to the first item node

Which expressions for XXX and YYY correctly output the text for pre-teen and teenager? Choices are in the form XXX / YYY. if (XXX) { // Output "Pre-teen" } else if (YYY) { // Output "Teenager" }

age < 13 / age <= 19

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 statement XXX outputs each element of array customerName to a file called CustomerList.txt using a file pointer? char* customerName[5] = {"Bob", "Sally", "Jim", "Joe", "Denise"}; FILE *inputFile = NULL int numberPlayers; inputFile = fopen ("CustomerList.txt", "w"); for (int I = 0; I < (5); ++i) { XXX }

fprintf (inputFile, "%s\n", customerName[i]);

Which statement outputs "Success!" to the system's standard output file using a file pointer?

fprintf(stdout, "Success!");

Which function can be used to avoid a memory leak?

free

Which is correct?

int main(void) { int pkgHeight; int pkgLength; int pkgWidth; int pkgVol; pkgVol = pkgHeight * pkgLength * pkgWidth; }

Which changes the value of m, where m = x * y + z / -8 % 9 + w?

m = (x * y) + z / (-8 % 9 + w) PreviousNext

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

null character

In the following code, where are variables a, b, c, and d stored? #include <stdio.h> int a = 10; int Add(int x, int y) { int b; b = x + y; return b; } int main() { int c; c = 20; int d; d = Add(a,c); printf("%d\n", d); return 0; }

stack

An identifier can _____ .

start with an underscore

Which header file is required to access the file pointer (FILE*)?

stdio.h

Given string str1 and string str2, which expression is true? str1 = "Flat"; str2 = "Last";

strcmp(str1, str2) < 0

If a program compiles without errors, the program is free from _____.

syntax errors

Heap is a region in program memory where

the "malloc" function allocates memory

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

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

Which type of pointer is returned by the following function call?malloc(sizeof(int));

void pointer

Which expression doubles x?

x *= 2;

Which assignments would yield a non-floating-point number for y? y = 32.0 + (x / (z + 1.0));

x = 1.0; z = -1.0;

Which statement causes overflow? int x = 3000; int y = 1000; int z;

z = x * y * y;

When passing an array as a parameter, which array element type does not require a second parameter defining the number of elements in the array?

(NOT - pointers) (Probs - Strings)

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

(NOT- maxValue = FindMax(15 25);

Which is not a benefit of using enumerators over strings and constants?

(not- The compiler will catch an enumerator's spelling error)

Dice have 6 sides, with values 1, 2, 3, 4, 5, and 6. Which expression randomly rolls one dice, directly yielding one of those values?

(rand() % 6) + 1

A restaurant serves breakfast before 11 am, after which they serve lunch. Which expression for XXX outputs the correct string for any time? Variable time ranges from 0 to 23 (e.g., 13 means 1 pm). mealString = XXX; // Output mealString

(time < 11) ? "Breakfast" : "Lunch"

Which expression follows the practice of using parentheses to make the intended order of evaluation explicit? <code>x > y && !a == b</code>

(x > y) && (!a == b)

When commenting out multiple lines with /* */, what common error might occur?

? (NOT THE PROGRAMMER MIGHT FORGET TO USE // TO COMMENT OUT FUNCTIONC ALLS NOT The programmer might include too many fixme comments

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 ending value of y? pow(u,v) returns u raised to the power of v. (x = 2.0; y =2.0; y = pow(pow(x,y)) + 1.0;)

Error: The compiler complains about calling a function within a function

Given two integer arrays, largerArray with 4 elements, and smallerArray with 3 elements. What is the result after this code executes? for (i = 0; i < 4; ++i) { largerArray[i] = smallerArray[i]; }

Error: The two arrays are not the same size

Which is true?

Format specifiers for scanf() and printf() are identical

What will a compiler do for the following code? /* numItems = 2; /* total items to buy */ rate = 0.5; */

Generate an error

A double variable stores a value in 64 bits, using some bits for mantissa, some for exponent, and one for sign. Which is true about a double variable>

Limited bits may cause some rounding of the mantissa

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

Modifies userVals' elements

The fopen(str) function has a string parameter str that specifies the ___ of the file to open.

Name

Which is likely not covered in style guidelines?

Number of files created

What is a benefit of using pointers over an array to store a list?

Pointers require fewer processor instructions to insert, delete, and shift data.

The world population is over 7 billion. Which declaration uses the fewest bits while gauranteeing that worldPopulation can be assigned the value 7 billion without error?

long long worldPopulation

Which XXX will result in the output "Area: 42"? #include <stdio.h> #include <stdlib.h> void main() { int area; typedef struct polygon_struct { int height; int width; } polygon; polygon* newRect = NULL; XXX newRect->height = 21; newRect->width = 2; area = newRect->height * newRect->width; printf("Area: %d", area); free(newRect); }

newRect = (polygon*)malloc(sizeof(polygon));

Which XXX outputs the following? You have 3 pencils. Total Price: 60

return myData.itemCount * myData.price;

Which statement, executed once at the start of main(), enables a program to generate a different sequence of pseudo-random numbers from rand() each time the program is run?

srand((int)time(0));

Which statement XXX will correctly parse input string userInput[] into 3 values?

sscanf(userInput, "%19s %19s %f", storeDepartment, itemType, &price)

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 = o; i < NUM_ROWS; ++i) for ( j = 0; j < NUM_COLS; ++j) { if (xxx) { YYY; } } }

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

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

variable, memory address

What is myChar assigned with? char myChar; myChar = '\'';

A single-quote: '

What is output? printf("Calculation: %+09.4lf \n", -13.785742835);

Calculation: -013.7857

Which is best declared as a constant

The number of feet in a yard

Given input 420, what is output?

You have 420 pennies or 4 dollars

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

Which XXX converts the string userStr to uppercase? for (i = 0; userStr[i] != '\0'; ++i) { XXX; }

userStr[i] = toupper(userStr[i]);

Which is an example of a process?

x+y

A program should compute two times x. Which statement has a logic error?

y = x * x;

What are the possible values for ((rand() % 9) + -4)?

-4...4

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

0 1 2 3 4 5 7 8 9 10

What are the possible values for rand()%6

0...5

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 regarding passing C strings to functions?

A C string is automatically passed by pointer

Which text most closely resembles the form in which one instruction is stored in a single location of a typical computer's memory?

Add 34, #9, 25

What is y's ending value? int x; int y =0; scanf("%d", &x); if (x = 20;) { y = 3; }

Always 3 no matter what the input

Given struct ListEntry, which is defined in ListEntry.h and used in ListEntry.c, why does the following compilation not include ListEntry.h? gcc -Wall -Wextra -std=c99 -pedantic ListEntry.c main.c

Because ListEntry.h is included in ListEntry.c

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

Which item converts a high-level language program to a low-level machine instructions?

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

What is an advantage of using separate files for structs?

Reduced compile time

Which statement defines the character search function strrchr()?

Returns a pointer to the last occurrence of the character

Which statement is true?

Strings passed as const char* can be modified by the function.

Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s), before recompiling?

The first error only

The following program generates an error. Why? const int NUM_ELEMENTS = 5; int userVals[NUM_ELEMENTS]; unsigned int i; userVals[0] = 1; userVals[1] = 7; userVals[2] = 4; for (i = 0; i <= NUM_ELEMENTS; ++i) { cout << userVals[i] << endl; }

The for loop tries to access an index that is out of the array's valid range

Which corrects the logic error in the following program? void FindPrevNext (int x, int prev, int next) { prev = x - 1; next = x + 1; } int main(void) { int x = 10; int y; int z; FindPrevNext (x, y, z); printf("Previous = %d, Next = %d", y, z); return 0; }

The variables prev and next should be passed by reference

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

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 " to string myString

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

Given only int variables a and b, which is a valid expression?

a+-2

A linked list is slower than a vector when _____.

accessing the i'th element

A character variable's value is stored in memory as _____ .

an integer value

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

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

Identify the correct declaration of an array of strings

char stringArry[10][50]

In a linked list, a node is comprised of a(n) _____.

data element and a pointer to the next node

Which is the best function stub for a function that calculates an item's tax?

double ComputeTax(double itemPrice) { printf("FIXME: Calculate tax\n"); return 0; }

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


संबंधित स्टडी सेट्स

Computer Hardware and Peripherals 2

View Set

keratinization and the epithelial system

View Set

8/11 Algebra - Polynomial Unit Review

View Set