Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Part I: Multiple Choice (30 Points) : Answer: B

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

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

1. Which of the following is NOT a valid variable name in C?

A. Char
B. 1st_answer
C. Y2K

D. CSE142_IS_FUN

E. integer
Answer: B

2. What is true about x <= 0 in the following line of code?

if (x <= 0) printf("Error!");

A. x <= 0 is an expression
B. x <= 0 is an argument to a function
C. x <= 0 is a compound statement

D. x <= 0 is an operator

E. x <= 0 is a variable declaration


Answer: A
Page 2

3. Consider the following line of code.:

int number = 17;

What is true about this line of code?

i. It is a declaration of number
ii. It is an initialization to number
iii. It is a call to the function number

A. i. only
B. ii. only
C. iii. only

D. i. and ii. only

E. i., ii., and iii.


Answer: D

4. Consider the following line of code:

printf("Twice x is %d", 2*x);

What is true about this line of code?

i. 2*x is an expression
ii. 2*x is a variable
iii. 2*x is an argument to a function

A. i. only
B. ii. only
C. iii. only

D. i. and ii. only

E. i. and iii. only


Answer: E
Page 3

5. Suppose grade is a double. After the statements


grade = 3.9;
score = (int) grade;
have executed, what is the value of grade?

A. 3
B. 3.0
C. 3.9

D. 4

E. 4.0
Answer: C

6. Consider the following expression:

a+5*a-8

Which of the following expressions with parathenses shows the order in


which C will evaluate the above expression?

A. (a+5)*(a-8)
B. ((a+5)*a)-8
C. (a)(+5)*(a)(-8)

D. (a+(5*a))-8

E. ((a)+(5))*((a)-(8))
Answer: D

7. Which of the following is the proper heading for a function that has an
integer parameter and returns a value of type double?

A. int foo(x)
B. foo(int x, double y)
C. void foo(int x)

D. double foo(int x)

E. int foo (double x)


Answer: D
Page 4

8. What is the output of the following program?

#include <stdio.h>

int add(int x, int y)


{
return(x+y);
}

int main (void)


{
int a,b,c;
a=5;
b=2;
c = add(a*2,b);
printf("%d", c);
return 0;
}

A. 7
B. 8
C. 9

D. 12

E. 15
Answer: D
Page 5

9. What will the following function return?

double foo(void)
{
int x, y;
double z;
x = 5;
y = 2;
z = (x/y);
z = z+0.2;
return z;
}

A. 2
B. 2.2
C. 2.5

D. 2.7

E. 3.2
Answer: B

10. After executing the following code, what will the value of x be?

int x, y;
double a;

y = 3;
x = y + 2;
a = 2.1;

y = x;

x = x * a;

A. 11
B. 10.5
C. 10

D. 6.3

E. 6
Answer: C
Page 6

11. What would this evaluate to?


17%9+2/3+5%2

A. 3
B. 4.00000
C. 9

D. 9.66666

E. 10
Answer: C

12. Adam Asinment wrote the following version of max which takes three
arguments and (he says) returns the largest. What is true about the value
returned by the function?

double my_max(double x, double y, double z) {


if (x<z) y=z;
else
{
if (z>x) x=z;
}
return x;
}

A. the function always returns the maximum of its arguments


B. the function never returns the maximum of its arguments
C. the function returns the highest of the first two arguments

D. the function returns the highest of the first and third arguments

E. the function always returns the value of its first argument


Answer: E

13. What is the value of the following expression?


(-100) - 15 - 4 - 9

A. 72
B. 80
C. 128

D. -128

E. -72
Answer: D
Page 7

14. Suppose x and y are declared as ints, and both are greater than 0.
Which of the following expressions rounds x/y to the nearest integer?
(Examples of rounding to the nearest integer are 6/5 = 1, and 38/10 = 4)

A. (x + 0.5) / y
B. (x + y - 1) / y
C. (x + y / 2) / y

D. (int) ((double) x / (double) y)

E. None of the above


Answer: C

15. #include <stdio.h>


int main (void)
{
int x=1;
double y=1.5, z;
z=x+y;
x=(int)(y+z);
y=z+x;
if(z<x)
{
if(x<z)
printf("%.2f %d %.2f",z,x,y);
else
printf("%d %.2f %.2f",x,y,z);
}
return 0;
}

What does the above program print?

A. 5.50 5
B. 2.50 4 6.50
C. 4 6.50 2.50

D. 2.50 3 5.50

E. The program does not compile


Answer: C
Part II: Programming Question (10 points)
Complete the following C program so it reads in a weight in kilograms and prints the
corresponding weight in pounds and ounces.

A sample run of the program is shown below:

Enter a weight in kg: 9


9.000000 kg is 19 pounds and 12 ounces

Another sample run of the program:

Enter a weight in kg: 2.5


2.500000 kg is 5 pounds and 8 ounces

Useful fact: 1 kg = 2.2 pounds, 1 pound = 16 ounces.

Be sure to use integer (int) and floating-point (double) variables and expressions
appropriately. The final output should have integer values for number of pounds and
ounces. The number of ounces should be between 0 and 15, and any fractions of an
ounce should be discarded (truncated) from the final value. However, be careful to
retain any fractional parts of intermediate results if they are needed to calculate
correct values for the final answers.

/* put any #define macros here (for conversion factors or anything else appropriate)*/
#define LBS_PER_KG 2.2
#define OZ_PER_LB 16

#include <stdio.h>

void main(void) {

/* declare variables here */


double kg; /* weight in kilograms (input) */
int pounds, ounces; /* weight in pounds and ounces */

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


double total_pounds;

/* read desired weight and store in kg */


printf( "Enter a weight in kg: " );
scanf ("%lf", &kg);

/* calculate number of pounds and ounces corresponding to kg kilograms */


total_pounds = kg * LBS_PER_KG;
pounds = (int)(total_pounds);
ounces = (int)((total_pounds-(double)pounds)*OZ_PER_LB);
/* print pounds and ounces */
printf("%f kg is %d pounds %d ounces\n", kg, pounds,
ounces);

}
Page 9

You might also like