Sample Midterm:



CSE/ENGR 142 – 1998, Autumn

Final, Version C

Part I: Multiple Choice (28 × 2½ = 70 points)

Answer all of the following questions. READ EACH QUESTION CAREFULLY. Fill the correct bubble on your mark-sense sheet. Each correct question is worth 2½ points. Choose the one BEST answer for each question. Assume that all given C code is syntactically correct unless a possibility to the contrary is suggested in the question.

Remember not to devote too much time to any single question, and good luck!

| |a and d are doubles, b is a character, and c is an integer. |

| |If the following line of code is run: |

| | |

| |scanf("%lf%c%d%lf", &a, &b, &c, &d); |

| | |

| |and the user inputs the following (followed by ENTER): |

| | |

| |272.373.374.219 |

| | |

| |What will be the value of d? |

| |A. |373.374 |

| |B. |219.0 |

| |C. |0.374 |

| |D. |0.219 |

| |E. |4.219 |

| |Answer: D |

| |Study the following declarations carefully. Then choose the best description of function Mystery. |

| | |

| |double min3(double n1, double n2, double n3); |

| |/* Returns the smallest of the values given as parameters */ |

| | |

| |double max3(double n1, double n2, double n3); |

| |/* Returns the largest of the values given as parameters */ |

| | |

| |double Mystery(double v1, double v2, double v3, double v4) { |

| |double temp1, temp2; |

| |temp1 = min3(v1, v2, v3); |

| |temp2 = min3(v2, v3, v4); |

| |if (temp1 == max3 (temp1, temp1, temp2)) |

| |return temp2; |

| |else |

| |return temp1; |

| |} |

| |A. |Returns the smallest of the values given as parameters. |

| |B. |Returns the next-to-smallest of the values given as parameters. |

| |C. |Returns the next-to-largest of the values given as parameters. |

| |D. |Returns the largest of the values given as parameters. |

| |E. |Returns the larger of the two values v2 and v3. |

| |Answer: A |

| |Study the following code fragment. All variables are integers. |

| | |

| |if (happy == sad) |

| |happy = happy || !hopeful; |

| |printf("happy=%d, sad=%d, hopeful=%d", |

| |happy, sad, hopeful); |

| | |

| |Suppose when this fragment of code runs, it displays |

| |happy = 0, sad = 0, hopeful = 1. |

| | |

| |What initial combination of the values of the variables (in the order happy, sad, hopeful) would lead to this outcome? |

| |A. |0, 0, 0 |

| |B. |0, 0, 1 |

| |C. |1, 0, 1 |

| |D. |1, 1, 0 |

| |E. |1, 1, 1 |

| |Answer: B |

| |Which of the following does NOT have to conform to the C rules for identifiers? |

| |A. |a variable |

| |B. |a function name |

| |C. |an array name |

| |D. |a file name |

| |E. |a (formal) parameter of a function |

| |Answer: D |

| |What is the output of : |

| | |

| |double values[3]; |

| |int i; |

| | |

| |for (i = 0; i < 3; i++) { |

| |values[i] = i * 3.0; |

| |printf("value %d is %.1f\n", i, values[i]); |

| |} |

| |A. |value 0 is 0.0 |

| | |value 1 is 1.0 |

| | |value 2 is 2.0 |

| |B. |value 1 is 3.0 |

| | |value 2 is 6.0 |

| | |value 3 is 9.0 |

| |C. |value 0 is 0.0 |

| | |value 1 is 3.0 |

| | |value 2 is 6.0 |

| |D. |value 0.0 is 0 |

| | |value 1.0 is 3 |

| | |value 2.0 is 6 |

| |E. |value 1 is 1.0 |

| | |value 2 is 2.0 |

| | |value 3 is 3.0 |

| |Answer: C |

| |What is the result printed by /* line A */? |

| |... |

| |int F22(int *p1, int *p2) { |

| |*p1 = 0; |

| |*p2 = *p1 + *p2; |

| |*p2 = *p2 + 1; |

| |return *p2 + 1; |

| |} |

| |… |

| |int val1 = 4, val2 = 5; |

| |printf("Result: %d", F22(&val1, &val2)); /* line A */ |

| |A. |Result: 4 |

| |B. |Result: 5 |

| |C. |Result: 6 |

| |D. |Result: 7 |

| |E. |Result: 9 |

| |Answer: D |

| |The array "GPA" contains the GPAs for a class of 400 students. Study the code given below. Then decide which of the |

| |choices is true, regardless of what values were actually in GPA. |

| | |

| |... |

| |double GPA[400]; |

| |int count, snum; |

| |int r0, r1, r2, r3, r4; |

| |... /* init GPA */ |

| |r0 = 0; r1 = 0; r2 = 0; r3 = 0; r4 = 0; |

| |for (snum = 0; snum < 400; snum++) { |

| |if (GPA[snum] < 1.0) r0++; |

| |if (GPA[snum] < 2.0) r1++; |

| |if (GPA[snum] < 3.0) r2++; |

| |if (GPA[snum] < 4.0) r3++; |

| |if (GPA[snum] >= 4.0) r4++; |

| |} |

| |A. |r0 400 |

| |E. |r0 + r1 + r2 + r3 > r4 |

| |Answer: A |

| |In the following expression, which operation is performed first? |

| | |

| |!(a == b + c) || d * e |

| |A. |! |

| |B. |== |

| |C. |+ |

| |D. ||| |

| |E. |* |

| |Answer: C |

| |i = 0; |

| |while (i < 10) { |

| |printf("%d", i); |

| |i = i + 1; |

| |} |

| | |

| |Which of the following would produce the same output as the above? (Hint: you shouldn't have to fully trace the |

| |execution of each possible answer to decide which one is correct.) |

| |A. |for (i = 0; i < 10; i = i + 1) { |

| | |printf("%d", i); |

| | |i = i+1; |

| | |} |

| |B. |for (i = 0; i < =10; i = i + 1) |

| | |printf("%d", i); |

| |C. |for (i = 10; i 0; i = i - 1) |

| | |printf("%d", 10 - i); |

| |E. |for (i = 0; i < 10; i) |

| | |printf("%d", i); |

| |Answer: D |

| |Which of the following correctly defines a function called "total" which takes an array of integers as input, as well as|

| |the number of elements in the array, and returns the sum of all elements in the array? |

| |A. |int total(int numbers[], int elements) { |

| | |int sum = 0, i; |

| | |for (i = 0; i < elements; i = i + 1) |

| | |sum = numbers[i]; |

| | |return sum; |

| | |} |

| |B. |int total(int numbers[], int elements) { |

| | |int sum = 0, i; |

| | |for (i = 0; i < = elements; i = i + 1) |

| | |sum = sum + numbers[i]; |

| | |return sum; |

| | |} |

| |C. |int total (int numbers[], int elements) { |

| | |int sum, i; |

| | |for (i = 0; i > elements; i = i + 1) |

| | |sum = sum + numbers[i]; |

| | |return sum; |

| | |} |

| |D. |int total (int numbers[], int elements) { |

| | |int sum = 0, i; |

| | |for (i = 0; i < elements; i = i + 1) |

| | |sum = sum + numbers[i]; |

| | |return sum; |

| | |} |

| |E. |int total (int numbers, int elements) { |

| | |int sum, i; |

| | |for (i = 0; i < elements; i = i + 1) |

| | |sum = sum + numbers[i]; |

| | |return sum; |

| | |} |

| |Answer: D |

| |A quadratic equation is of the form a*x*x + b*x + c, where a, b, and c are constant numbers and x is an unknown |

| |variable. You've been hired to write a function which solves such equations. Unfortunately, there is a complication: |

| |some quadratic equations have one solution (one possible value for x), some have two solutions, and some have none at |

| |all. Thus a user who calls your function needs to know the value(s) of the solution(s) as well as how many solutions |

| |there are. With this in mind, which of the following would be the BEST prototype for your function? |

| |A. |int Quadratic(double a, double b, double c, double x1, double x2); |

| |B. |void Quadratic(double a, double b, double c, double *x1p, double *x2p); |

| |C. |void Quadratic(double a, double b, double c, double *x1p, double *x2p, |

| | |int *number_of_solutions); |

| |D. |(double x1, double x2) Quadratic(double a, double b, double c); |

| |E. |int Quadratic(double a1, double b1, double c1, double a2, double b2, |

| | |double c2, double x1, double x2); |

| |Answer: C |

| |If you see the following in a legal C program: |

| | |

| |a = b(c); |

| | |

| |one thing you can say for sure is: |

| |A. |b is an array. |

| |B. |c is an integer, double, or char. |

| |C. |a and c have the same type. |

| |D. |c is a function with a void return. |

| |E. |b is a function which returns a value. |

| |Answer: E |

| |Recall that string.h contains a "concat" function. Using this function, the result of concatenating the string "King" |

| |with the string "Kong" is the string which would be represented in memory as follows (one space is shown between each |

| |character for clarity): |

| |A. |K i n g K o n g |

| |B. |K i n g \0 K o n g \0 |

| |C. |K i n g K o n g \n |

| |D. |K i n g K o n g \0 |

| |E. |K i n g K o n g \0 |

| |Answer: D |

| |Bonus question (free points) |

| |True or False: |

| | |

| |Programming is fun. |

| |A. |True |

| |B. |False |

| |C. |Are you kidding? |

| |D. |I don't remember this being covered in class. |

| |E. |First tell me what my grade is… |

| |Answer: A |

| |Which of the following is NOT an important feature or function of main memory in a computer? |

| |A. |holds data temporarily for the CPU. |

| |B. |holds programs for the CPU to execute. |

| |C. |performs instructions such as adding two numbers together. |

| |D. |contains variables of a program |

| |E. |holds data received from an input device. |

| |Answer: C |

| |Suppose the line |

| | |

| |neutron = neutron + 0.01; |

| | |

| |appears in a legal and correctly operating C program. Which of the following remarks must be FALSE? |

| |A. |The statement does not serve as the initialization of neutron. |

| |B. |neutron might be the name of a variable. |

| |C. |neutron might be the name of a function. |

| |D. |neutron might be a parameter. |

| |E. |neutron is an identifier. |

| |Answer: C |

| |What is displayed by the printf below? |

| | |

| |char change(char ch, char carr[]) { |

| |ch = carr[0]; |

| |carr[0] = carr[1]; |

| |return 'z'; |

| |} |

| | |

| |char c1 = 'a' ; |

| |char frics[] = {'f', 'v', 's', 'z'}; |

| |c1 = change (frics[1], frics); |

| | |

| |printf ("%c %c %c", c1, frics[0], frics[1]); |

| |A. |z f v |

| |B. |z v v |

| |C. |z f f |

| |D. |a a f |

| |E. |v v v |

| |Answer: B |

| |Given this function prototype: |

| | |

| |int testf(double x[]); |

| | |

| |and these declarations and statements: |

| | |

| |double ar1[20]; |

| |int ai; |

| |... |

| |printf("%i", testf(ar1[ai])); /* line A */ |

| | |

| |Apart from any possible logic errors, this is not a valid C program because of something on line A. What is wrong? |

| |A. |You cannot call a function from inside printf. |

| |B. |Instead of testf (ar1[ai]) you should have testf (ar1[ ]). |

| |C. |The type of ar1[ai] doesn't match the type expected by the function testf. |

| |D. |%i is wrong for the type of value being printed -- it should be %f |

| |E. |ai should be declared as double, since that is the type of the elements in ar1. |

| |Answer: C |

| |Fill in the values for the !A && B column of this truth table. Then read down that column, and pick the answer which |

| |matches. |

| | |

| |A B !A && B |

| |T T |

| |T F |

| |F T |

| |F F |

| |A. |T |

| | |T |

| | |T |

| | |T |

| |B. |F |

| | |F |

| | |F |

| | |F |

| |C. |F |

| | |F |

| | |T |

| | |F |

| |D. |T |

| | |F |

| | |T |

| | |F |

| |E. |T |

| | |T |

| | |T |

| | |F |

| |Answer: C |

| |Consider this fragment of a function: |

| | |

| |void scan_number(double *value) { |

| |.... |

| |scanf("%lf", value); /* line A */ |

| |... |

| |} |

| | |

| |Consider whether "value" in line A is missing a '&' (ampersand) in front of it. Pick the best statement discussing |

| |this situation. |

| |A. |Yes, "value" should be "&value", because it is always an error not to use & before a variable used by scanf. |

| |B. |"value" should not have an ampersand, because it is already a pointer. |

| |C. |The ampersand is not needed, because value is a parameter; only local variables need ampersand, but parameters do|

| | |not. |

| |D. |Scanf needs an ampersand only when it is called from main; when called from other functions, the ampersand is not|

| | |used. |

| |E. |This scanf will generate a warning, but the programmer can ignore the warning if he or she is positive that the |

| | |logic is OK. |

| |Answer: B |

| |Suppose you see this in a legal C program: |

| | |

| |scanf("%lf", &a[b]); |

| | |

| |a and b are local variables. Which of the following are the most plausible declarations for a and b? |

| |A. |int *a; |

| | |int *b; |

| |B. |double a[10]; |

| | |int b; |

| |C. |double a[]; |

| | |int b; |

| |D. |double &a[10]; |

| | |index b; |

| |E. |int a; |

| | |double &b; |

| |Answer: B |

| |typedef struct { |

| |char beneficiary[80]; |

| |char owner[80]; |

| |double interest; |

| |} bond; |

| | |

| |Given the declarations |

| | |

| |bond bond1, bond2; |

| | |

| |Suppose that bond1 and bond2 have been initialized properly, and that it is desired to change bond1 so that its owner is|

| |the same as the owner of bond2, but not changing any other fields of bond1. Which statement does this? |

| |A. |bond1 = bond2; |

| |B. |bond1.owner = bond2.owner; |

| |C. |strcpy(bond1.owner, bond2.owner); |

| |D. |bond.owner[1] = strcpy(bond.owner[2]); |

| |E. |None of the above. |

| |Answer: C |

| |int rf1(int value) { |

| |if (value >= 0) |

| |return value; |

| |else return rf1(value + 2); |

| |} |

| | |

| |What is the value of the expression |

| |rf1(-1) |

| |A. |-3 |

| |B. |-1 |

| |C. |0 |

| |D. |1 |

| |E. |Can't tell, because there will be infinite recursion. |

| |Answer: D |

| |Given the following: |

| | |

| |char critter1[8] = "snake"; |

| |char critter2[9] = "turkey"; |

| |int i; |

| |for (i = 0; i < 8; i++) |

| |critter1[i] = critter2[i]; |

| |/* point A */ |

| | |

| |At point A in the program, the value of strlen(critter1) is: |

| |A. |5 |

| |B. |6 |

| |C. |7 |

| |D. |8 |

| |E. |9 |

| |Answer: B |

| |You can use = to assign them, but you can't use them directly in printf or scanf to input or output their values. What |

| |are they? |

| |A. |ints |

| |B. |chars |

| |C. |arrays |

| |D. |structs |

| |E. |strings |

| |Answer: D |

| |The following program fragment repeatedly reads two integer values from the keyboard and stores the values in variables |

| |x and y. We would like the loop to stop executing when the integer values read into x and y are both 42. |

| | |

| |int x, y; |

| |… |

| |scanf("%d%d", &x, &y); |

| |while ( _______________ ) { |

| |scanf("%d%d", &x, &y); |

| |} |

| |/* x and y both contain 42 here */ |

| | |

| |What is the correct condition that should appear in the while statement so that execution of the loop will stop when |

| |both x and y have the value 42? |

| |A. |x == 42 && y == 42 |

| |B. |x != 42 && y != 42 |

| |C. |x == 42 || y == 42 |

| |D. |x != 42 || y != 42 |

| |E. |x != 42 || y == 42 |

| |Answer: D |

| |The following program fragment copies integer values from the file infilep to the file outfilep. You should assume that|

| |file variables infilep and outfilep have been initialized appropriately. The file infilep contains only correctly |

| |formatted integer values, so fscanf is guaranteed not to fail because of bad input data. |

| | |

| |#include |

| |… |

| |int x, y; |

| |… |

| |y = fscanf(infilep, "%d", &x); |

| |while ( ________________ ) { |

| |fprintf(outfilep, "%d", x); |

| |y = fscanf(infilep, "%d", &x); |

| |} |

| | |

| |What is the correct condition that should appear in the while statement so that execution will terminate after all of |

| |the data has been copied from infilep to outfilep? |

| |A. |x != EOF && y != EOF |

| |B. |x != EOF || y != EOF |

| |C. |x != EOF |

| |D. |y != EOF |

| |E. |x > EOF && y > EOF |

| |Answer: D |

| |Suppose an array contains 1200 integer values, and suppose the array is sorted. Soppose we want to know whether the |

| |value 42 is contained in the array. Using the Binary Search algorithm, approximately how many of these array values (at|

| |most) will need to be examined to see if the array contains the value 42? |

| |A. |6 |

| |B. |11 |

| |C. |21 |

| |D. |42 |

| |E. |120 |

| |Answer: B |

Part II: Written (30 28 points)

29. (4 points) Following are the insert and sort functions for ints as discussed in class:

void insert(club A[], int j)

{

int i;

club temp;

temp = A[j];

for (i = j; i > 0 && A[i-1].funding > temp.funding; i = i - 1)

A[i] = A[i-1];

A[i] = temp;

}

void sort(club A[], int size)

{

int j;

for (j = 1; j < size; j = j + 1)

insert(A, j);

}

The following struct represents student clubs:

typedef struct {

char cname[30], president[20];

double funding;

int members;

} club;

It is desired to sort an array of these structs so that the clubs can be listed in order, from those with smallest to those with largest funding. (If two clubs have the same funding, the order between those two does not matter).

Modify the sort and insert functions so that they will properly sort an array of club. Mark your changes directly in the code printed above by crossing out and rewriting ONLY parts that must be changed. Do not make unecessary changes. Do not rewrite the functions.

30. (8 points) In the year 2001, Congress passes the Uniform Spelling Reform Act. Among other things, the hyphen character '-' is abolished, and its use becomes illegal.

Write a function which takes a string, holding some English word or sentence, and removes all hyphen characters from it. No other changes are made. No character is substituted for the removed hyphens, so the new string is shorter if the old one had hyphens. For example, "Apple turn-overs are taste-tempting." becomes "Apple turnovers are tastetempting.". If the string contained no hyphens to begin with, it is unchanged.

(Hint: start by deciding what an appropriate prototype for the function is.)

void respell(char sentence[]) {

int oldc = 0, newc = 0;

while (oldc != '\0') {

if (sentence[oldc] != '-') {

sentence[newc] = sentence[oldc];

newc++;

}

oldc++;

}

sentence[newc] = '\0';

}

31. (16 points) This question uses the data structures from Program 5, but uses them in a different way. Please be sure to read the question carefully, and do not assume anything based on program 5 that is not specified here.

We want to add a new rule to the game. The new rule is that RED cells are hot. For each cell on the game board, if there are too many RED cells nearby, the cell catches fire and becomes BLACK. The exact rule is as follows: For each cell, examine that cell and its 8 immediate neighbors (i.e., the cells immediately above, below, to the left, and to the right). If 5 or more of these cells are RED, then the cell should be marked for burning. After all cells have been checked, the marked cells should be set to BLACK.

Example 1: Look at the following 3x3 set of cells:

|GREEN |RED |BLUE |

|YELLOW |RED |BLACK |

|GREEN |BLUE |ORANGE |

The cell in the middle stays RED, because there are only 2 RED cells.

Example 2:

|RED |ORANGE |RED |

|RED |BLUE |RED |

|GREEN |RED |YELLOW |

In this case, there are 5 RED cells surrounding the BLUE cell. The BLUE cell will become BLACK after the update.

Example 3:

|RED |ORANGE |BLUE |

|RED |RED |RED |

|GREEN |RED |YELLOW |

In this case, there are 5 RED cells including the one in the middle. The middle cell will become BLACK after the update.

(continued on next page...)

The data structures for this problem are the same as in Homework 5, with the addition of field burn in each CELL. Burn is TRUE if a cell should be turned to BLACK and is FALSE otherwise. Here are the declarations, copied from the original Homework 5 code with the addition of field burn.

#include "gp142.h"

#define TRUE 1

#define FALSE 0

#define BOARD_WIDTH 10 /* number of columns in game area */

#define BOARD_HEIGHT 20 /* number of rows in game area */

typedef struct {

int color; /* current color of this cell */

int deleted; /* True if square marked for deletion */

int burn; /* True if cell burns and becomes black */

} cell;

cell board[BOARD_HEIGHT][BOARD_WIDTH]; /* the game area board */

Answer this question in two steps. First, create a function that checks a cell to see if it should burn (turn to BLACK). Second, write a sequence of statements to check and update the entire board. You may define additional functions if you find that they simplify your solution.

(a) (6 points) Implement the following function exactly as specified.

/* Return TRUE if 5 or more of the 9 cells centered at

* row r and column c of board are RED */

int will_burn(int r, int c, cell board[BOARD_HEIGHT][BOARD_WIDTH])

{

int rmin, rmax, cmin, cmax, nred, i, j;

rmin = r - 1; rmax = r + 1;

cmin = c - 1; cmax = c + 1;

if (rmin < 0) rmin = 0;

if (cmin < 0) cmin = 0;

if (rmax >= BOARD_HEIGHT) rmax = BOARD_HEIGHT - 1;

if (cmax >= BOARD_WIDTH) cmax = BOARD_WIDTH - 1;

nred = 0;

for (i = rmin; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download