COMP 1200 Final

Ace your homework & exams now with Quizwiz!

In the code below, if you wanted to open file1 so that you could write to it, and you also wanted to ensure that anything currently in the file was "erased", how would you finish the fopen command? FILE * file1;file1 = fopen("file1.txt", __________);

"w"

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));XXXprintf("%d", newData->myInt);free(newData);}

(*newData).myInt = 25;

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

Which operator is used when dereferencing a pointer to access a struct's member variable?

->

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

0

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

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

2

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

Given input "201, 5.74, 42", what is the value of metric2? scanf("%d %f %d", &metric1, &metric2, &metric3);

5.74

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)

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

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

<code>ConvertFeetToMeters(numFeet, &numMeters);</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) value int 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>

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 arrays and functions?

An array is automatically passed to a function as a pointer

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 XXX declares a variable called myDate of type Date? typedef struct Date_struct { int x; int y;} Date;int main(void) { XXX //do something}

Date myDate;

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

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 the following input, what is output? 4 1 1990 typedef struct Date_struct { int month; int day; int year;} Date;int main(void) { Date myDate; scanf("%d",&myDate.month); scanf("%d", &myDate.day); scanf("%d", &myDate.year); myDate.month = myDate.month+1; printf("Month %d Day %d Year %d\n", myDate.month, myDate.day , myDate.year); return 0;}

Month 5 Day 1 Year 1990

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

Opening the file. Could not open the file.

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

Why is it best to develop a large program incrementally?

The program is easier to debug

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

There are 001340 residents of the city.

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

Given the following input, what is output? English 50 50.0 Math 120 75.5 #include <string.h>typedef struct Books_struct {char name[12];int pages;float price;} Books;int main(void) {int x = 2;char book_name[x][12];int no_pages[x];float book_price[x];Books myBooks[x];for (int i = 0; i < x ; i++) {printf("Enter a books name: \n");scanf("%s", book_name[i]); printf("Enter book's no of pages: \n");scanf("%d", &no_pages[i]);printf("Enter book's price: \n");scanf("%f", &book_price[i]);strcpy(myBooks[i].name, book_name[i]);myBooks[i].pages = no_pages[i];myBooks[i].price = book_price[i];}for (int a = 0; a < x; a++) {printf("You have: %s book.\n" , myBooks[a].name);}return 0;}

You have: English book. You have: Math book.

Which XXX calculates and prints the total cost? #include <stdio.h>#include <string.h>typedef struct Item_struct { double price; char name; int quantity; double total;} Item;typedef struct Total_struct { double totalCost;} Total;int main(void) { Item book1; Item book2; Total cost; book1.total = book1.price * book1.quantity; book2.total = book2.price * book2.quantity;XXX }

cost.totalCost = book1.total + book2.total;printf("Total: %f dollars\n", cost.totalCost);

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)

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

How would you print "1 2 3" to a file, given the code below? FILE * file1;file1 = fopen("file1.txt", INTENTIONALLY LEFT BLANK );

fprintf(file1, "1 2 3");

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

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 following program generates an error. Why? typedef struct Date_struct {int date; int month; int year;} Date;typedef struct Time_struct { int hour; int minute; int second;} Time;int main(void) { Date myDate;Time myTime;myDate.date = 01; myDate.month = 01; myDate.year = 19; myTime.hour = 00; myTime.minute = 00; myTime.second = 00; myTime.second = myDate.date; myTime.minute = myDate.hour;printf("New year starts at %d / %d / %d\n ", myDate.date, myDate.month, myDate.year); printf("%d : %d : %d\n", myTime.hour, myTime.minute, myTime.second);}

myDate cannot access variable hour

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

name

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

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

outputGames = *gamesPointer;

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?

printf("%d \n", origList[1] + offsetList[1]);

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 myData.itemCount * myData.price;

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

stdio.h

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

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]


Related study sets

Political Geography - Colonialism and post-colonialism

View Set

CHAPTER 1: CALIFORNIA REAL ESTATE LICENSE VOCAB

View Set

Chapter 2.7 Visual Communication Design Art 1301

View Set

Academic Foundations Seminar Final Exam

View Set

IB EXAM II: Ch. 8 Foreign Direct Investment

View Set