How to use this page
Try to solve each task first. Then open the answer. Some questions are intentionally close to test format, while others connect directly to the laboratory workflow.
1 Reading integers with scanf
What values are read into n and p?
scanf("%d %d", &n, &p);
| Case | User input | n | p |
|---|---|---|---|
| A | 123⌂45⇒4500⌐ | ? | ? |
| B | 123⌐⌐⌂456⇒⇒⇒⇒⌂789⌐ | ? | ? |
| C | 123⌂456⌂789⌐ | ? | ? |
| D | ⌂000⌂⌂⌂123⇒⇒⇒456⌐ | ? | ? |
| E | ⇒⌐⇒000⇒123⇒456⌐ | ? | ? |
Answer
| Case | n | p |
|---|---|---|
| A | 123 | 45 |
| B | 123 | 456 |
| C | 123 | 456 |
| D | 0 | 123 |
| E | 0 | 123 |
%d skips leading whitespace. It stops reading when the number ends.
2 Field width in scanf
What values are read into n and p?
scanf("%4d %2d", &n, &p);
| Case | User input | n | p |
|---|---|---|---|
| A | ⇒123⇒45⇒00⌐ | ? | ? |
| B | ⇒12345⌂⌂⌂678⌐ | ? | ? |
| C | ⌂⌂⌂123⌂⌂⌂456⌂⌂⌂789⌐ | ? | ? |
| D | 1234567⌂89⌐ | ? | ? |
| E | ⌂⌂⌂0001234⌂⌂⌂456⌐ | ? | ? |
Answer
| Case | n | p |
|---|---|---|
| A | 123 | 45 |
| B | 1234 | 5 |
| C | 123 | 45 |
| D | 1234 | 56 |
| E | 1 | 23 |
%4d reads at most four characters for the first integer; %2d reads at most two characters for the second one.
3 Valid identifiers
Which identifiers are valid C identifiers?
Answer
Valid: A, B, E, F, H, J.
Invalid: C, D, G, I.
float is a keyword, so it cannot be used as an identifier. VoiD is valid because C is case-sensitive and it is not the same token as void.
4 Pointers to array elements
What is printed by this program?
#include <stdio.h>
int main(void) {
int t[4] = { 10, 20, 30, 40 };
int *p[4];
for (int i = 0; i < 4; i++) {
p[i] = t + i;
}
for (int i = 0; i < 4; i++) {
printf("%d ", *p[i]);
}
printf("%d %d\n", *(p[1] + 1), *p[1] + 1);
return 0;
}
Answer and explanation
Correct answer: B.
10 20 30 40 30 21
p[i] = t + i means that p[0] points to t[0], p[1] points to t[1], and so on. Therefore *p[i] prints 10 20 30 40.
p[1] points to t[1], whose value is 20. So *(p[1] + 1) means the next element, t[2], which is 30. But *p[1] + 1 means 20 + 1, which is 21.
5 Loop tracing with continue
What is the output?
int n = 0;
for (;;) {
if (n % 2 == 0) {
printf("%d (2)\n", n);
n += 3;
continue;
}
if (n % 7 == 0) {
printf("%d (7)\n", n);
n += 3;
continue;
}
n++;
if (n > 20) break;
}
Answer and explanation
Correct answer: A.
The key point is that continue skips the rest of the current loop iteration. When n is even, the program prints it, adds 3, and immediately starts the next iteration.
| n at start | condition used | printed | n after action |
|---|---|---|---|
| 0 | n % 2 == 0 | 0 (2) | 3 |
| 3 | none | — | 4 |
| 4 | n % 2 == 0 | 4 (2) | 7 |
| 7 | n % 7 == 0 | 7 (7) | 10 |
| 10 | n % 2 == 0 | 10 (2) | 13 |
| 13 | none | — | 14 |
| 14 | n % 2 == 0 | 14 (2) | 17 |
| 17 | none | — | 18 |
| 18 | n % 2 == 0 | 18 (2) | 21 |
| 21 | n % 7 == 0 | 21 (7) | 24 |
| 24 | n % 2 == 0 | 24 (2) | 27 |
6 Formatted output — choose the correct printf
The following code has one missing line. Which version prints the value of a right-aligned in a field of width 4?
int a = 123;
/* missing line */
Expected output:
123
Answer
Correct answer: B. %4d prints an integer in a field of width 4 and pads with spaces on the left. %-4d aligns left, and %04d pads with zeroes.
7 Calculator with switch — missing case
Choose the correct missing fragment for multiplication.
switch (op) {
case '+':
printf("%g", a + b);
break;
/* missing fragment */
default:
printf("Unsupported operator");
}
Answer
Correct answer: A. In a switch, a case label needs a colon. The operator read from input is usually '*', not 'x'. The break prevents falling through into the next case or default.
8 scanf and whitespace before %c
After reading a number, the program should ask for one character answer: Y or N. Which scanf is the best choice?
int value;
char answer;
scanf("%d", &value);
printf("Continue? (Y/N) ");
/* missing scanf */
Answer
Correct answer: B. The space before %c tells scanf to skip whitespace, including the ENTER left after the previous number. Without the space, %c may read the newline character instead of the student’s answer.
9 Code tracing — loop output
What is printed?
for (int i = 1; i <= 6; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
Answer
Correct answer: B. Only numbers divisible by 2 are printed.
10 Code tracing — nested loops
What is printed?
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
printf("%2d", row * col);
}
printf("\n");
}
Answer
Correct answer: A. Each cell contains row * col.
11 Code tracing — pattern
What is printed?
for (int row = 1; row <= 4; row++) {
for (int star = 1; star <= row; star++) {
printf("*");
}
printf("\n");
}
Answer
Correct answer: B. In row 1 the inner loop runs once, in row 2 twice, and so on.
13 Function that remembers calls — fill in static
Which declaration should be inserted so that counter remembers its value between calls?
int counter(void) {
/* missing declaration */
calls++;
return calls;
}
Answer
Correct answer: B. A local static variable is initialized only once and keeps its value between function calls. A normal local variable would be created again on every call.
14A POINT with typedef
Given this type definition, choose the correct declarations or initializations.
typedef struct {
float x;
float y;
} POINT;
Answer
Correct: A, B. Incorrect: C, D, E.
With typedef, POINT becomes a type alias, so variables can be declared as POINT p. struct POINT is not available here, because the structure has no tag name. POINT.x is wrong because POINT is a type, not a variable.
14B struct POINT without typedef
Given this structure definition, choose the correct declarations or initializations.
struct POINT {
float x;
float y;
};
Answer
Correct: A, B, E. Incorrect: C, D.
Without typedef, the type name is struct POINT, not POINT. Dot notation is used on variables such as p2.x, not on the type name itself.
15 Header file and source file
For each element, choose where it should usually be placed in a small C module.
| Element | A. Header file .h | B. Source file .c |
|---|---|---|
| Include guards | ? | ? |
Public typedef needed by other files | ? | ? |
| Function declaration / prototype | ? | ? |
| Function definition / body | ? | ? |
| Private helper function used only in this one file | ? | ? |
Answer
| Element | Place |
|---|---|
| Include guards | .h |
Public typedef needed by other files | .h |
| Function declaration / prototype | .h |
| Function definition / body | .c |
| Private helper function used only in this one file | .c, usually with static |
#ifndef POINT_H
#define POINT_H
/* public type definitions and function declarations */
#endif
16 Separate compilation
You have three files: main.c, point.c, point.h. Which command compiles and links the program?
Answer
A is correct. The header is included by source files; it is not compiled as a separate translation unit.
17 Math library
You use sqrt in a program. Which command is normally correct on the AGH UNIX server?
Answer
B. #include <math.h> gives declarations. -lm links the math library.
22 Types of expressions
Write the type of each expression.
| Declaration | Expression | Type |
|---|---|---|
int x; | x | ? |
int x; | &x | ? |
int x[2]; | x | ? |
int x[2]; | *x | ? |
int x[2]; | &x | ? |
int x[4][3]; | x | ? |
int x[4][3]; | *x | ? |
int x[4][3]; | **x | ? |
int x[4][3]; | &x | ? |
Answer
| Expression | Type |
|---|---|
x where int x; | int |
&x where int x; | int * |
x where int x[2]; | int * in most expressions |
*x where int x[2]; | int |
&x where int x[2]; | int (*)[2] |
x where int x[4][3]; | int (*)[3] in most expressions |
*x where int x[4][3]; | int * in most expressions |
**x where int x[4][3]; | int |
&x where int x[4][3]; | int (*)[4][3] |
Checklist before the test
- I know how
scanftreats whitespace and field width. - I can trace loops with
continueandbreak. - I understand array decay and pointer arithmetic in simple examples.
- I can split a program into
.hand.cfiles.