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!

 

Which of the following is NOT a good way to think of the C statement

x = y;

A.

"x and y have the same value after the statement has executed, at least they will if they are of the same type."

B.

"the value of y is placed in the location of the variable x."

C.

"x receives the value which is stored at the location of y."

D.

"x gets whatever y is."

E.

"x equals y"

Answer: E

 

 

int main (void)

{

int a, b, c, d=10; /*line 1*/

b=5; /*line 2*/

d=6; /*line 3*/

scanf("%d %d", &b, &c); /*line 4*/

}

I. a is initialized in line 1

II. b is initialized in line 2

III. d is initialized in line 3

Which of the above statements are correct?

A.

I. only

B.

II. only

C.

I. and II. only

D.

II. and III. only

E.

I., II., and III. are all correct

Answer: B

 

 

Given the function:

void foo(int x, int y)

{

int temp;

temp = x;

x = y;

y = temp;

}

What will be displayed after executing the following code?

int x = 3;

int y = 4;

foo(x, y);

printf("x is %d, y is %d", x, y);

A.

x is 4, y is 4

B.

x is 4, y is 3

C.

x is 3, y is 3

D.

x is 3, y is 4

E.

x is 3, y is 3

Answer: D



 

Given the following function prototype:

int test(int number);

and the following declaration

int x;

What is true about the following statement?

printf("%d", test(test(x)) );

A.

you can't call a function inside a printf

B.

test is the name of a #define constant

C.

the type of the expression test(test(x)) is int

D.

it should be %f, instead of %d

E.

you can't pass test(x) as an argument in a function call because test is a function itself

Answer: C



 

What gets printed after the following code is executed?

a = 5;

b = 7;

if ( a + b > 12 || (b - a > 1))

printf("a");

else

printf("b");

if ( !(b * a != 30) && a * b + b == 41)

printf("c");

printf("d");

A.

acd

B.

bd

C.

bc

D.

ad

E.

bcd

Answer: D



 

What does the following program output?

#include <stdio.h>

int main (void)

{

int i, j;

for (i = 0; i <= 4; i=i+1)

{

   for (j = 0; j <= 4; j=j+1)

   {

     if (j == (4-i))

     printf("*");

     else

     printf(" ");

   }

   printf("\n");

}

return 0;

}

A.

*

**

***

****

*****

B.

         *

       *

     *

   *

*

C.

*      *

  *  *

    *

  *  *

*      *

D.

*****

*      *

*      *

*      *

*****

E.

*****

****

***

**

*

Answer: B

 

 

What will the following code fragment print?

int x = 15;

int y = 4;

for (x = 0; x < 9; x = x + 2)

{

y = y + 1;

}

printf ("x is: %d and y is: %d", x, y);

A.

x is: 10 and y is: 9

B.

x is: 9 and y is: 10

C.

x is: 9 and y is: 8

D.

x is: 8 and y is: 10

E.

x is: 10 and y is: 10

Answer: A

 

 

What does the following code fragment output?

double a = 3.9;

double b = 4.4;

int c, d;

c = (int) a + (int) b;

d = (int) (a + b);

printf("c is : %d and d is: %d", c, d);

A.

c is: 8 and d is: 7

B.

c is: 7 and d is: 8

C.

c is: 8 and d is: 8

D.

c is: 7 and d is: 7

E.

c is: 8.0 and d is: 7.0

Answer: B



 

#include <stdio.h>


int bar (int foo)

{

int var = 3;

var = var * foo;

printf("%d ", var);

return (var * var);

}


void foo (int var)

{

var = bar ( var );

printf("%d ", var);

}


int main (void)

{

int var = 2;

printf("%d ", var);

foo(var);

bar(var);

printf("%d", var);

return 0;

}

What will the program above print out?

A.

2 6 36 6 2

B.

2 6 2 48 48

C.

2 36 6 6 2

D.

2 36 6 18 18

E.

2 6 9 6 2

Answer: A



 1   foo (  2   x, int  3 )

{

int result;

scanf("%lf", &x);

result = ( 4 ) x * bar;

return result;

}

Given the function above and knowing that it was written correctly with good style, what below best describes what should go in the blanks?

      ___1        _2        3      4 _

A.

double, double, int, int

B.

int, double, bar, int

C.

void, double, result, double

D.

int, int, bar, char

E.

double, int, result, bar

Answer: B



 

What will be printed out after the following code is executed?

int j = 1;

int k = 1;

while (j <= 2)

{

while (k <= 4)

{

   if (j % 2 == 0)

     if (k % 2 == 1) printf("a");

     else printf("b");

   else

     if (k % 2 == 0) printf("a");

     else printf("b");

   k = k + 1;

}

printf("\n");

k = 1;

j = j + 1;

}

A.

baba

abab

B.

abab

baba

C.

aaaa

bbbb

D.

bbaa

aabb

E.

bbbb

aaaa

Answer: A



 

What wil be printed after the following code is executed?

int foo = 10, bar = 6;

if (foo > 15 || bar == 4)

{

printf("stapler ");

bar = 3;

}

else if (bar <= 6)

{

printf("post-its ");

foo = 12;

}

if (foo > 10 && bar < 10)

printf("pencils ");

printf("chair ");

A.

stapler chair

B.

post-its pencils chair

C.

post-its

D.

stapler post-its chair

E.

stapler post-its pencils chair

Answer: B



 

Which of the following is not a legal variable name in C?

A.

_temp

B.

12th_hour

C.

Char

D.

terminator2000

E.

a_200_and_7931

Answer: B



 

Which of the following is the correct syntax for a for loop that will repeat 5 times?

A.

for ( j = 0; j < 4; j++)

B.

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

C.

for ( j = 0, j < 5, j = j + 1)

D.

for (j = 10; j > 0; j = j - 2)

E.

for ( j = 9; j > 5; j--)

Answer: D



 

int rose, carnation;

...

if ( !( !(rose) && carnation) ) ...

Which condition is equivalent to the one in the if statement above? (Hint: apply DeMorgan's law.)

A.

( !(rose || carnation) )

B.

( !(rose) && carnation)

C.

( !(rose) || carnation )

D.

(rose || !(carnation))

E.

( !(rose && carnation) )

Answer: D

 

 

 

 

Part II: Programming Questions (20 points)

In the Olympics, athletes compete for medals. The athlete in first place wins a gold medal, the athlete in second place wins a silver medal, and the athlete in third place wins a bronze medal.

Complete the following C program so that it asks the user for a character (standing for an Olympic medal) and displays the number of the athlete who won that medal. In this application, the characters and numbers are as follows:

'G' stands for Gold medal (number 1)

'S' stands for Silver (number 2)

'B' stands for Bronze (number 3)

A sample run of the program would look like the following, if the user types S:

Which medal did you win? S

You were number 2

Complete the program in the space provided below. There are comments to guide where your code should go.

#include <stdio.h>

int main (void)

{

/* place all variable declarations here */

int number;

char medal;

/* Prompt the user for a character (which medal they won) */

printf("Which medal did you win? ");

/* Read in a character*/

scanf("%c", &medal);

/* Write statements which figure out which number the athlete is */

if (medal == 'G') number = 1;

if (medal == 'S') number = 2;

if (medal == 'B') number = 3;

/* Print the message stating which number the athlete is */

printf("You were number %d", number);

return 0;

}



Write a function which takes two integer parameters and returns the sum of all odd integers between the two input parameters. For instance, if you called the function passing in the values 18 and 23, the function should return the value 63 since 19 + 21 + 23 = 63. If the function was called passing in the values 5 and 11 then the function should return the value 32. There should not be any print or scanf statements in your function! you may assume that the first value passed in to the function is less than or equal to the second value.

int sum_odds(int lower_bound, int upper_bound)

{

int count, total = 0;

for (count = lower_bound; count <= upper_bound; count++)

{

   if (count % 2 == 1) total += count;

}

return (total);

}

or alternatively...

int sum_odds(int lower_bound, int upper_bound)

{

int count, total = 0;

if (lower_bound % 2 == 0) lower_bound++;

for (count = lower_bound; count <= upper_bound; count+=2)

{

   total += count;

}

return (total);

}