3 blocks × 15 questions. Grade each block independently as you work through them.
These are short-answer blanks (keywords, format specifiers, and so on). To fill in the blanks of a whole program and have it graded by running it, see Review & Final Exam Prep (problems 22–30).
Overall: 0 / 45
A. Basics
types, operators, if/for/while (Q1-15)
Score: 0/15
Variables, types, operators
Q1Type
Fill in the type so x holds 3.14.
[1] x =3.14;
[1]:
Answer: double
Q2printf format
Print int n in decimal and double f to 2 decimal places.
printf("n=%[1], f=%[2]\n", n, f);
[1]: [2]:
Answer: d, .2f
Q3compound ops
What is the final value of a?
int a =7;
a +=3;
a *=2;
a %=7;
a =
Answer: 6
Q4cast
Force floating-point division.
int a =7, b =2;
double r =[1]a / b;
[1]:
Answer: (double)
Q5increment
What's the value of b?
int a =5;
int b = a+++++a;
b =
Answer: 12
Q6sizeof
Typical value of sizeof(int) in bytes.
Answer: 4
Q7int division
Output of printf("%d\n", 7 / 2);?
Answer: 3 Integer division truncates toward zero.
Control flow (if / for / while)
Q8if
80 or higher: A. 60–79: B. Otherwise: C.
if (score >=80) printf("A");
[1] (score >=60) printf("B");
[2]printf("C");
[1]: [2]:
Answer: else if, else
Q9for
Sum the integers from 1 to 10.
int sum =0;
for (int i =[1]; i [2]10; i++) sum += i;
[1]: [2]:
Answer: 1, <=
Q10while output
Predict the output.
int n =10;
while (n >0) { printf("%d ", n); n /=2; }
Output:
Answer: 10 5 2 1
Q11switch
Which keyword prevents fall-through?
case'+': r = a + b; [1];
[1]:
Answer: break
Q12do-while
How many times does printf run?
int i =0;
do { printf("*"); i++; } while (i <0);
Count:
Answer: 1
Q13break/continue
What does it print?
for (int i =1; i <=5; i++) {
if (i ==3) continue;
if (i ==5) break;
printf("%d ", i);
}
Output:
Answer: 1 2 4
Q14nested loops
How many times does printf run?
for (int i =0; i <3; i++)
for (int j =0; j <4; j++)
printf("*");
Count:
Answer: 12
Q15ternary
Use the ternary operator to pick the larger of a and b.
Declare a 5-element int array a initialized to {10, 20, 30, 40, 50}.
int a[[1]] = {[2]};
[1]: [2]:
Answer: 5, 10, 20, 30, 40, 50
Q17array loop
What does it print?
int a[5] = {1, 2, 3, 4, 5};
int sum =0;
for (int i =0; i <5; i++)
if (a[i] %2==0) sum += a[i];
printf("%d\n", sum);
Output:
Answer: 6
Q18string NUL
Write the C string terminator (as it appears in source code).
Answer: \\0
Q19string funcs
Fill in the standard-library function names.
// concatenate[1](dst, src);
// lengthsize_t len =[2](s);
// compare (0 if equal)if ([3](a, b) ==0) { }
[1]: [2]: [3]:
Answer: strcat, strlen, strcmp
Q202D sizeof
For int m[3][4];, give the element count and sizeof(m) (assume int = 4).
elements: sizeof(m):
Answer: 12, 48
Q21array to function
Write the prototype of a function that takes an int array and its length and returns the sum.
Answer: int sum(int a[], int n);
Q22char array size
For char s[] = "Hello";, give sizeof(s) and strlen(s).
sizeof: strlen:
Answer: 6, 5
Functions, recursion, scope
Q23prototype
Write the prototype for an add(a, b) function that takes two ints and returns an int.
Answer: int add(int a, int b);
Q24return
Which keyword returns a value from a function?
if (a > b) [1] a;
else[1] b;
[1]:
Answer: return
Q25recursion
Complete the factorial function.
intfact(int n) {
if (n <=1) return[1];
return n *fact([2]);
}
[1]: [2]:
Answer: 1, n - 1
Q26Fibonacci
Given fib(0)=0 and fib(1)=1, what is fib(6)?
fib(6) =
Answer: 8
Q27pass-by-value
What is printed for a and b after swap()?
voidswap(int x, int y) { int t = x; x = y; y = t; }
int a =3, b =5;
swap(a, b);
printf("%d %d", a, b);
Output:
Answer: 3 5
Q28scope
After the inner block, what is the value of x?
int x =10;
{ int x =20; }
printf("%d", x);
Output:
Answer: 10
Q29global
What does it print?
int g =0;
voidadd(int x) { g += x; }
intmain(void) {
add(3); add(4);
printf("%d", g);
}
Output:
Answer: 7
Q30static
What is printed after 3 calls?
voidcount(void) {
staticint n =0;
n++;
printf("%d ", n);
}
Output:
Answer: 1 2 3
Block B result: not graded
C. Pointers & Advanced
pointer, struct, malloc, bitwise, mixed (Q31-45)
Score: 0/15
Pointers, struct, dynamic memory
Q31pointer basics
Make a pointer to n, then add 10 through the pointer.
int n =100;
int[1]p =[2]n;
[3]p +=10;
[1]: [2]: [3]:
Answer: *, &, *
Q32array vs pointer
Pick every correct statement.
A: an array name is the address of its first element.
B: a[i] is equivalent to *(a+i).
C: sizeof(a) always equals sizeof(&a[0]).
D: inside a function, sizeof on an array parameter yields the size of a pointer.
Answer:
Answer: A, B, D
Q33struct access
Fill in the correct member-access operators.
struct Point { int x, y; };
struct Point p = {3, 4};
printf("%d", p[1]x);
struct Point *pp =&p;
printf("%d", pp[2]y);
[1]: [2]:
Answer: ., ->
Q34malloc/free
Allocate n ints, then release them.
int*a = (int*)[1](n *sizeof(int));
[2](a);
[1]: [2]:
Answer: malloc, free
Q35swap via pointers
Complete the swap function.
voidswap(int[1]x, int[1]y) {
int t =[2]x;
[2]x =[2]y;
[2]y = t;
}
swap([3]a, [3]b);