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) { } 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) { } 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) { } |
||
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) { } 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) { } void foo (int var) { } int main (void) { } 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 ) { } Given the function above and knowing that it was written correctly with good style, what below best describes what should go in the blanks? |
||
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) { } |
||
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) { } else if (bar <= 6) { } if (foo > 10 && bar < 10) 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:
Complete the program in the space provided below. There are comments to guide where your code should go.
#include <stdio.h>
int main (void)
{
}
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)
{
}
or alternatively...
int sum_odds(int lower_bound, int upper_bound)
{
}