CSE/ENGR 142 – 1998, Autumn

Midterm 1, Version A

Part I: Multiple Choice (72 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 4 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!

 

Suppose x is a double. After the statements

x = 5.9;

a = (int) x;

have executed, what is the value of x?

A.

5.9

B.

5

C.

5.0

D.

6

E.

Depends on how a was declared

Answer: A

 

Choose the best conclusion to the statement.

In C, the word double…

A.

is a special, floating point value.

B.

is a reserved word.

C.

must be part of the declaration of every variable which will hold numeric values.

D.

indicates the operation "multiply by 2.0"

E.

is available for use as a variable name.

Answer: B

 

Suppose # is a left-associative binary operator which has lower precedence than +. Given the following expression, which expression is equivalent to it?

3 + 5 # 6 # 7

A.

3 + (5 # (6 # 7))

B.

(3 + 5) # (6 # 7)

C.

3 + ((5 # 6) # 7)

D.

((3 + 5) # 6) # 7

E.

(3 +( 5 # 6)) # 7

Answer: D

 

For the operators *, +, /, %, -, which of the following statements about precedence in C is true:

A.

Highest *, /, %

Lowest +, -

B.

Highest +, /, *

Lowest %, -

C.

Highest *, %, -

Lowest /, +

D.

Highest *, +, %

Lowest -, /

E.

Higest *, /

Lowest +, -, %

Answer: A

 

If you see the following line in an error-free C program:

y = combine (a / b + c);

which statement could not be true:

A.

b is used in this line as an argument (actual parameter) of combine.

B.

combine does not have a "void" return value.

C.

The definition of combine contains one and only one return statement.

D.

The definition of combine does not have a "void" parameter list.

E.

The function combine has three formal parameters.

Answer: E

 

Suppose a valid C program contains the statement

TREE = TRUNK;

Which of the following is true? (Don't assume anything about the style used in naming identifiers.)

A.

TREE and TRUNK must both be #defines.

B.

TREE must be a #define but TRUNK doesn't have to be.

C.

TRUNK must be a #define but TREE doesn't have to be.

D.

TREE cannot be a #define but TRUNK might be.

E.

Neither TREE nor TRUNK could be a #define.

Answer: D

 

In C, the value of a condition can be thought of informally as "yes" or "no" but is actually represented by the values

A.

true or false

B.

TRUE or FALSE

C.

-1 or 1

D.

ON or OFF

E.

non-zero (usually 1) or 0

Answer: E

 

#include <stdio.h>

int F1 (int a) {

a = 2;

return 3;

}

int main (void) {

int a;

a = 0;

a = 4;

a = F1 (a); /*line 1*/

printf ("a = %d", a);

return 0;

}

When this program runs, what will it display (assume no syntax errors, except possibly as indicated in the answers)

A.

a = 0

B.

a = 2

C.

a = 3

D.

a = 4

E.

Won't run: line 1 has a syntax error

Answer: C

 

The following function is supposed to find the smallest among four characters. Choose the best answer.

char smallest_of_4 (char c1, char c2, char c3, char c4) {

char smallest_of_1and2, smallest_of_3and4, smallest_of_all;

if (c1 <= c2)

smallest_of_1and2 = c1; else smallest_of_1and2 = c2;

if (c3 <= c4)

smallest_of_3and4 = c3; else smallest_of_3and4 = c4;

if (smallest_of_1and2 <= smallest_of_3and4)

smallest_of_all = smallest_of_1and2;

else

smallest_of_all = smallest_of_3and4;

return smallest_of_all;

}

A.

This function never gives the right answer.

B.

If c1 happens to be the smallest character, this function gives the right answer (regardless of the values of c2, c3, c4). If c1 is not the smallest character, some values of c2, c3, or c4 will cause it to give a wrong answer.

C.

If c2 happens to be the smallest character, this function gives the right answer (regardless of the values of c1, c3, c4). If c2 is not the smallest character, some values of c2, c3, or c4 will cause it to give a wrong answer.

D.

If c3 is less than either c1 or c2, this function always gives the right answers. If c3 is not less than c1 or c2, it gives some incorrect answers.

E.

This function always gives the right answer.

Answer: E

 

What is printed by main when this program fragment runs?

int F1 (int veggie) {

return veggie + 1;

}

int main (void) {

int veggie= 2, onion = 3;

onion = F1 (4);

printf ("%d %d", veggie, onion);

}

A.

1 2

B.

2 3

C.

3 3

D.

2 4

E.

2 5

Answer: E

 

If you see this statement in a legal C program:

Jason = Medusa;

Which one of the following could be true?

A.

Jason is a function.

B.

Medusa is a function.

C.

Jason is a #define symbol standing for an integer.

D.

Medusa is a parameter.

E.

Jason is the name of the function that this statement appears in.

Answer: D

 

"C is strongly typed" means...

A.

you need strong typing and keyboarding skills to be a C programmer.

B.

there is a strongly defined type or category to which the C programming language belongs.

C.

every value has a type, and C cares about what those types are.

D.

you can never assign a variable of type int to a variable of type double.

E.

C checks type matching in all places where it matters, such as checking that a placeholder type letter (%d, %f, etc.) matches the type of the corresponding variable).

Answer: C

 

Which of the following could NOT be a variable name in C?

A.

1st_value

B.

v300000

C.

integer

D.

Double

E.

_rock_it__duuude

Answer: A

 

What is the value of the following expression (given the following declarations)?

int a = 8;

int b = 2;

int c = 4;

a + b / c * a - c / b

A.

10.0

B.

4

C.

6

D.

10

E.

8

Answer: C

 

Which of the following was NOT given as a motivation for using functions in programming?

A.

Functions raise the level of discourse.

B.

Functions are more efficient (run faster) than the same solution without functions.

C.

That's how modern programming is done.

D.

Functions permit code to be shared between programs.

E.

Functions make it easier to perform the same computation over and over again.

Answer: B

 

For which values of x is the following condition true? You may assume that x is a positive integer.

(2 % (x + 1)) > 0

A.

true for all even values of x

B.

true for all odd values of x

C.

true for all positive values of x

D.

true when x is greater than 1

E.

never true

Answer: D

 

Saying "A invokes B" is the same as saying

A.

A is called by B

B.

B calls A

C.

B is the callee, and A is the caller

D.

A returns to B

E.

A and B are both called from the Operating System

Answer: C

 

Part II: Written (28 points)

 

Complete the following C program so it reads in a length in centimeters and prints the corresponding length in feet and inches.

A sample run of the program would look like the following:

 

Enter a length in cm: 42

42 cm is 8 feet 2 inches

Useful fact: 1 inch = 2.54 cm, 1 foot = 12 inches.

You should use floating-point (double) arithmetic to calculate the total number of inches corresponding to the input, but use integer (int) arithmetic to calculate the final number of feet and leftover inches. The final value in inches should be an integer between 0 and 11. Fractions of an inch should be discarded (truncated) from the final value.

 

#include <stdio.h>

void main(void) {

int cm; /* length in centimeters (input) */

int feet, inches; /* number of feet and leftover inches */

/* corresponding to cm. */

/* declare additional variables here if you need them */

double total_inches;

/* read desired length and store in cm */

printf( "Enter a length in cm: " );

scanf ("%d", &cm);

/* calculate number of feet and inches */

/* corresponding to cm centimeters */

total_inches = (double)cm / 2.54;

feet = (int)(total_inches / 12.0);

inches = (int)total_inches % 12;

 

/* print feet and inches */

printf("%d cm is %d feet %d inches\n", cm, feet, inches);

}