Midterm 2
Midterm 2
2.
1234
-1 -1 -1 -1
1 -1 -1 4
-1 -1 -1 4
1 -1 -1 -1
What does the following code print? (Assume the program compiles without error). Trick
question! Note the semicolon at the end of the second line.
int i;
for(i = 1; i <= 10; i = i + 1);
printf("%d", i);
A.
B.
C.
D.
E.
3.
C.
D.
E.
Page 2
4.
5.
Which of these three program fragments have the same output (apart from spacing)? All
variables are ints.
i. printf("1\n2\n3\n4\n5\n6\n7\n8\n9\n");
ii. for (i = 9; i > 0; i--)
{
j = i - 10;
printf("%d\n", -j);
}
iii. i = 1;
while (i < 10)
{
printf("%d\n", i);
i++;
}
A.
B.
C.
D.
E.
i. and ii.
ii. and iii.
i., ii., and iii.
None have the same output
i. and iii.
Page 3
6.
/* line A */
7.
The following program fragment has its lines numbered for reference along the left-hand side.
The code prints a figure that looks like
this:
*
**
***
****
1)
...
2)
3)
4)
5)
6)
7)
8)
#define SIZE 4
int col;
int row = 1;
while (row <= SIZE) {
col = 1;
while (col <= row){
printf("*");
col = col + 1;
}
10) printf("\n");
11) row = row + 1;
}
...
Suppose we wanted the output look like this instead:
****
***
**
*
What changes to the code would accomplish this?
A.
B.
C.
D.
E.
3) int row = 4;
4) while (row >= 1){
11) row = row - 1;
1) #define SIZE (-4)
5) col = 4;
6) while (col >= row){
3) int row = 4;
11) row = row - 1;
5) col = 4;
Page 4
8.
What values of a, b, and c (respectively) make the following condition evaluate to "true" (or 1) ?
( !(b && a) && (c && b) )
I) 0,0,0
II) 1,1,1
III) 1,1,0
IV) 1,0,0
A.
B.
C.
D.
E.
9.
A only
B only
C only
None of A, B, or C
All of A, B, and C
10. If x (an integer variable) has the value 1 at the start of the following code fragment, what is its
value at the end?
if (x < 0 || x == 3) {
x = x - 1;
}
if ( x < 2*x && x - 2 < 0) {
x = x + 1;
}
else {
x = x + 3;
}
A.
B.
C.
D.
E.
0
1
2
3
4
11. Which of the following does not itself cause a change in the sequential control flow of a program
in execution?
A.
B.
C.
D.
E.
assignment statement
function call
return statement
switch statement
if statement
Page 5
13. Among the choices offered, which condition is equivalent to the following (assume the variables
are chars):
(answer != y) && (option == q)
A.
B.
C.
D.
E.
...
Page 6
15. The function AddOne can be used to increment a variable. The code for AddOne is:
void AddOne(int *x){
*x = *x + 1;
}
Which of the following is a correct implementation of the AddTwo function, which adds two to a
variable:
A.
B.
C.
D.
E.
16. Here is an incomplete truth table for !(P&&Q). If the last column were filled in, how many Ts
would that column contain?
P Q
!(P&&Q)
--------------------------------------T T
T F
F T
F F
A.
B.
C.
D.
E.
0
1
2
3
4
Page 7
Page 8
Action
Program ends
Gets an integer in the range [2-6] from the user
Displays the eyes using the users input as the size
n
Gets an integer in the range [0-8] from the user
Displays the nose using the users input as the size
m
Gets an integer in the range [10-16] from the user
Displays the mouth using the users input as the size
all others
Displays an error message Invalid input
You can use the following functions to complete the program (i.e., these are written for you; do
NOT write these functions, just call them. You should ONLY write main), in addition to the
getIntInRange function you have already written:
void drawEyes(int size);
void drawNose(int size);
void drawMouth(int size);
#include <stdio.h>
int main(void) {
/*your answer starts here */
return 0;
}