πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

C Fill-in Exam Prep — 45 Questions

3 blocks × 15 questions. Grade each block independently as you work through them.

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.
int max = (a > b) [1] a [2] b;
[1]: γ€€[2]:
Answer: ?, :
Block A result: not graded

B. Functions & Globals

arrays, strings, functions, recursion, scope (Q16-30)
Score: 0/15

Arrays & strings

Q16array init
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); // length size_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.
int fact(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()?
void swap(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; void add(int x) { g += x; } int main(void) { add(3); add(4); printf("%d", g); }
Output:
Answer: 7
Q30static
What is printed after 3 calls?
void count(void) { static int 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.
void swap(int [1]x, int [1]y) { int t = [2]x; [2]x = [2]y; [2]y = t; } swap([3]a, [3]b);
[1]: γ€€[2]: γ€€[3]:
Answer: *, *, &
Q36NULL check
Detect a failed malloc call.
int *a = malloc(100); if (a == [1]) { /* error */ }
[1]:
Answer: NULL
Q37memory leak
Name the bug in two words:
void leaky(int n) { int *a = malloc(n * sizeof(int)); return; }
Answer: memory leak
Q38pointer arith
What is the value of *p?
int a[] = {10, 20, 30, 40, 50}; int *p = a; p += 3;
*p =
Answer: 40

Advanced / mixed

Q39bitwise
Given x = 0b1010 (= 10), compute each expression.
x & 0b1100 = [1] x | 0b0101 = [2] x << 2 = [3]
[1]: γ€€[2]: γ€€[3]:
Answer: 8, 15, 40
Q40file IO
Open a file for reading and verify the result.
FILE *fp = fopen("data.txt", "[1]"); if (fp == [2]) { return 1; }
[1]: γ€€[2]:
Answer: r, NULL
Q41macro pitfall
#define SQUARE(x) x * x. Value of SQUARE(3+1)?
Answer: 7
The macro expands to 3+1*3+1.
Q42bubble sort
Bubble-sorting {5, 2, 4, 1, 3} into ascending order — what does the array look like after one outer pass?
Answer: 2, 4, 1, 3, 5
Q43memory
Name each of the four memory regions.
// locals, call frames [1] // malloc [2] // globals, static [3] // code [4]
[1]: γ€€[2]:
[3]: γ€€[4]:
Answer: stack, heap, data, text
Q44const
Which line fails to compile?
1: const int x = 10; 2: printf("%d", x); 3: x = 20; 4: printf("%d", x);
line:
Answer: 3
Q45union
What is the size of this union (assume int = 4, double = 8)?
union U { int i; char c; double d; };
sizeof(union U) =
Answer: 8
A union's size equals that of its largest member.
Block C result: not graded