LearnFrenzy
Sign in Take a free test

Arrays · Exercise 3 of 4

Arrays Find the Error

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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 1 Easy

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;
}
A b = a; – an array cannot be assigned; copy with a loop or memcpy()
B b must be initialised before use
C There is no error; it prints 1
D b[0] should be written as *b
Question page & discussion
Question 2 Easy

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;
}
A a[5] is written, one element past the end of the array
B i * i overflows
C a must be initialised with {0} first
D There is no error; it prints 16
Question page & discussion
Question 3 Medium

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;
}
A make() cannot return int *
B a is a local array; its memory is gone when make() returns, so p dangles
C There is no error; it prints 3
D p[2] should be *(p + 3)
Question page & discussion
Question 4 Hard

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;
}
A table must be declared inside main()
B int table[n]; – a global array needs a constant size; n is a variable
C There is no error; it prints 1
D n must be unsigned
Question page & discussion
Question 5 Medium

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;
}
A It prints 15; the code is correct
B It prints 3: inside sum(), sizeof a is the pointer size (8), so the loop runs only 2 times
C It prints 1: the loop runs once
D It does not compile
Question page & discussion
Question 6 Medium

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;
}
A rows must be the first parameter
B int m[][] – the number of columns must be given, e.g. int m[][3]
C There is no error; it prints 4
D m must be passed as &m
Question page & discussion
Question 7 Easy

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;
}
A count[2] = 7; is out of bounds
B count is never initialised, so 4 of the 5 elements hold garbage and total is unpredictable
C total must be declared before count
D There is no error; it prints 7
Question page & discussion
Question 8 Hard

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;
}
A 1 – memset sets each element to 1
B 16843009 – memset sets every byte to 1, so each int is 0x01010101
C 4 – sizeof a is passed as the value
D It does not compile
Question page & discussion
Up next · Exercise 4Array Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Popular