Each program contains an array bug – some stop the compiler, some crash, and some quietly print the wrong answer. Choose the option that names the real problem.
Question 1Easy
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int main(void) {
int a[3] = {1, 2, 3}, b[3];
b = a;
printf("%d\n", b[0]);
return 0;
}
Ab = a; – an array cannot be assigned; copy with a loop or memcpy()
Bb must be initialised before use
CThere is no error; it prints 1
Db[0] should be written as *b
Correct Answer: Option A
An array name is not a modifiable lvalue, so it cannot be on the left of =. Use memcpy(b, a, sizeof a); (from <string.h>) or a loop.
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int main(void) {
int a[5];
for (int i = 0; i <= 5; i++)
a[i] = i * i;
printf("%d\n", a[4]);
return 0;
}
Aa[5] is written, one element past the end of the array
Bi * i overflows
Ca must be initialised with {0} first
DThere is no error; it prints 16
Correct Answer: Option A
Valid indexes are 0 to 4. With i <= 5 the last pass writes a[5], outside the array – undefined behaviour that may overwrite another variable or crash. Use i < 5.
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int *make(void) {
int a[3] = {1, 2, 3};
return a;
}
int main(void) {
int *p = make();
printf("%d\n", p[2]);
return 0;
}
Amake() cannot return int *
Ba is a local array; its memory is gone when make() returns, so p dangles
CThere is no error; it prints 3
Dp[2] should be *(p + 3)
Correct Answer: Option B
a lives in make()'s stack frame. Returning it returns the address of memory that no longer belongs to the program. Fill a caller's array instead, return a struct containing the array, or allocate with malloc().
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int n = 5;
int table[n];
int main(void) {
table[0] = 1;
printf("%d\n", table[0]);
return 0;
}
Atable must be declared inside main()
Bint table[n]; – a global array needs a constant size; n is a variable
CThere is no error; it prints 1
Dn must be unsigned
Correct Answer: Option B
Variable length arrays are only allowed inside a block. A global (or static) array's size must be a constant expression: use #define N 5, an enum constant, or C23 constexpr int n = 5;.
This program should print 15, the sum of the array. What does it print, and why?
main.c
#include <stdio.h>
int sum(int *a) {
int s = 0;
for (size_t i = 0; i < sizeof a / sizeof a[0]; i++)
s += a[i];
return s;
}
int main(void) {
int a[] = {1, 2, 3, 4, 5};
printf("%d\n", sum(a));
return 0;
}
AIt prints 15; the code is correct
BIt prints 3: inside sum(), sizeof a is the pointer size (8), so the loop runs only 2 times
CIt prints 1: the loop runs once
DIt does not compile
Correct Answer: Option B
Inside sum(), a is a pointer: 8 / 4 = 2, so only a[0] + a[1] = 3 is added. Compilers warn about this pattern. Pass the length: int sum(const int *a, size_t n).
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int corner(int m[][], int rows) {
return m[rows - 1][0];
}
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", corner(m, 2));
return 0;
}
Arows must be the first parameter
Bint m[][] – the number of columns must be given, e.g. int m[][3]
CThere is no error; it prints 4
Dm must be passed as &m
Correct Answer: Option B
To find m[i][j] the compiler needs the row length, so only the first dimension can be left empty: int corner(int m[][3], int rows), or as a VLA parameter int corner(int rows, int cols, int m[rows][cols]).
The program below has a problem. Which statement correctly describes it?
main.c
#include <stdio.h>
int main(void) {
int count[5];
int total = 0;
count[2] = 7;
for (int i = 0; i < 5; i++)
total += count[i];
printf("%d\n", total);
return 0;
}
Acount[2] = 7; is out of bounds
Bcount is never initialised, so 4 of the 5 elements hold garbage and total is unpredictable
Ctotal must be declared before count
DThere is no error; it prints 7
Correct Answer: Option B
A local array without an initializer is not zeroed. Reading the other four elements gives indeterminate values. Write int count[5] = {0}; (or = {} in C23). Global and static arrays, by contrast, start at zero.
The programmer wanted every element to be 1. What does this program actually print, and why?
main.c
#include <stdio.h>
#include <string.h>
int main(void) {
int a[4];
memset(a, 1, sizeof a);
printf("%d\n", a[0]);
return 0;
}
A1 – memset sets each element to 1
B16843009 – memset sets every byte to 1, so each int is 0x01010101
C4 – sizeof a is passed as the value
DIt does not compile
Correct Answer: Option B
memset() fills bytes. Each 4-byte int becomes 0x01010101 = 16843009. It only works for ints with 0 (and -1, all bytes 0xFF). To set ints to 1, use a loop.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·