C Programming Quiz
Questions: 16 · 10 minutes
1. What is the value of sizeof(s) in this declaration?
char s[] = ""cat"";
3, because only the visible letters count
4, because the array also contains the terminating null character
5, because the declaration adds a length byte
The size of a char pointer
2. What happens when this condition is evaluated with x equal to 0?
if (x != 0 && 10 / x > 2)
Both operands are evaluated, causing division by zero
The condition is true because the second operand is ignored
The condition is false, and 10 / x is not evaluated
The code fails to compile because && cannot contain arithmetic
3. What does a directive such as #define MAX 100 primarily do in C?
It creates a variable named MAX at runtime
It instructs the linker to find a symbol named MAX
It defines a macro handled during preprocessing
It creates a type-checked constant object in memory
4. What is the usual purpose of a break statement inside a switch case?
To restart the switch using the next expression value
To skip directly to the default label
To exit the switch and prevent fall-through into later cases
To end the entire program immediately
5. What does malloc return when an allocation succeeds?
A fully initialized object of the requested type
A pointer to a dynamically allocated block of the requested size
A character array that is always filled with zeros
The number of bytes reserved on the stack
6. Which standard header declares the printf function?
<stdio.h>
<stdlib.h>
<string.h>
<math.h>
7. What does this code print?
int next(void) {
static int n = 0;
n++;
return n;
}
int first = next();
int second = next();
printf(""%d %d"", first, second);
1 2
1 1
0 1
2 2
8. Given int a[10]; in the same scope where the array is declared, which expression evaluates to the number of elements in a?
sizeof(a[0])
sizeof(a)
sizeof(a) / sizeof(a[0])
sizeof(&a) / sizeof(int)
9. If p is a pointer to a structure, which operator accesses one of that structure's members through p?
The dot operator: p.member
The scope operator: p::member
The address operator: p&member
The arrow operator: p->member
10. What value does this code print?
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) continue;
sum += i;
}
printf(""%d"", sum);
6
15
10
9
11. What does this code print?
int x = 5;
int *p = &x;
*p = 8;
printf(""%d"", x);
5
An indeterminate value
The address of x
8
12. What is printed by this code?
int a[3] = {10, 20, 30};
int *p = a;
printf(""%d"", p[2]);
10
30
20
The memory address of the third element
13. What character marks the end of a C string?
The null character '\0'
A newline character '\n'
A space character ' '
The end-of-file marker EOF
14. What does this program print?
void set_to_zero(int n) {
n = 0;
}
int main(void) {
int x = 7;
set_to_zero(x);
printf(""%d"", x);
}
7, because the function receives a copy of x
An indeterminate value, because the function changes n
0, because n refers directly to x
Nothing, because an int cannot be passed to a void function
15. Which mode opens a text file so new output is written at its end rather than replacing its current contents?
""w""
""a""
""rb""
""r+""
16. What value is printed by this loop?
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
printf(""%d"", sum);
6
9
15
10