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

C Fill-in Exam Prep β€” 45 Questions

3 blocks Γ— 15 questions. Grade each block separately as you progress.

Overall: 0 / 45

A. Basics

types, operators, if/for/while (Q1-15)
Score: 0/15

Variables, types, operators

Q1Type
Complete so x holds 3.14.
[1] x = 3.14;
[1]:
Answer: double
Q2printf format
Print int n in decimal and double f with 2 decimals.
printf("n=%[1], f=%[2]\n", n, f);
[1]: γ€€[2]:
Answer: d, .2f
Q3compound ops
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
Value of b?
int a = 5; int b = a++ + ++a;
b =
Answer: 12
Q6sizeof
Typical sizeof(int) in bytes.
Answer: 4
Q7int division
Output of printf("%d\n", 7 / 2);?
Answer: 3
Integer division truncates.

Control flow (if / for / while)

Q8if
80+: A, 60-79: B, else: C.
if (score >= 80) printf("A"); [1] (score >= 60) printf("B"); [2] printf("C");
[1]: γ€€[2]:
Answer: else if, else
Q9for
Sum 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
Keyword to prevent fall-through?
case '+': r = a + b; [1];
[1]:
Answer: break
Q12do-while
How many printfs?
int i = 0; do { printf("*"); i++; } while (i < 0);
Count:
Answer: 1
Q13break/continue
Output?
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 printfs?
for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) printf("*");
Count:
Answer: 12
Q15ternary
Pick the larger of a and b with the ternary operator.
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
5-element int array a with {10,20,30,40,50}.
int a[[1]] = {[2]};
[1]: γ€€[2]:
Answer: 5, 10, 20, 30, 40, 50
Q17array loop
Output?
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
C string terminator (source form)?
Answer: \\0
Q19string funcs
Standard library 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
int m[3][4]; elements and sizeof (int=4).
elements: γ€€sizeof(m):
Answer: 12, 48
Q21array to function
Prototype of a function that takes an int array and 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
Prototype for int add(a,b) returning int.
Answer: int add(int a, int b);
Q24return
Keyword for returning a value?
if (a > b) [1] a; else [1] b;
[1]:
Answer: return
Q25recursion
Complete factorial.
int fact(int n) { if (n <= 1) return [1]; return n * fact([2]); }
[1]: γ€€[2]:
Answer: 1, n - 1
Q26Fibonacci
fib(0)=0, fib(1)=1; fib(6)?
fib(6) =
Answer: 8
Q27pass-by-value
Output of a, 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's x?
int x = 10; { int x = 20; } printf("%d", x);
Output:
Answer: 10
Q29global
Output?
int g = 0; void add(int x) { g += x; } int main(void) { add(3); add(4); printf("%d", g); }
Output:
Answer: 7
Q30static
Output 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
Pointer to n, then add 10 through pointer.
int n = 100; int [1]p = [2]n; [3]p += 10;
[1]: γ€€[2]: γ€€[3]:
Answer: *, &, *
Q32array vs pointer
Pick all correct.
A: array name is address of first element
B: a[i] == *(a+i)
C: sizeof(a) equals sizeof(&a[0]) always
D: inside a function, sizeof on an array param gives pointer size
Answer:
Answer: A, B, D
Q33struct access
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.
int *a = (int*)[1](n * sizeof(int)); [2](a);
[1]: γ€€[2]:
Answer: malloc, free
Q35swap via pointers
Complete swap.
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 malloc failure.
int *a = malloc(100); if (a == [1]) { /* error */ }
[1]:
Answer: NULL
Q37memory leak
Name the bug in one word:
void leaky(int n) { int *a = malloc(n * sizeof(int)); return; }
Answer: memory leak
Q38pointer arith
What does *p yield?
int a[] = {10, 20, 30, 40, 50}; int *p = a; p += 3;
*p =
Answer: 40

Advanced / mixed

Q39bitwise
x = 0b1010 = 10.
x & 0b1100 = [1] x | 0b0101 = [2] x << 2 = [3]
[1]: γ€€[2]: γ€€[3]:
Answer: 8, 15, 40
Q40file IO
Open for read and check.
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
Expands to 3+1*3+1.
Q42bubble sort
{5, 2, 4, 1, 3} ascending, state after one outer pass.
Answer: 2, 4, 1, 3, 5
Q43memory
Name 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
Size of this union (int=4, double=8)?
union U { int i; char c; double d; };
sizeof(union U) =
Answer: 8
Union size = largest member.
Block C result: not graded