Array Questions with Answers

13 practice questions on Array (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
  1. 1, 1, 1, 1 2, 3, 2, 3 3, 2, 3, 2 4, 4, 4, 4
  2. 1, 2, 1, 2 2, 3, 2, 3 3, 4, 3, 4 4, 2, 4, 2
  3. 1, 1, 1, 1 2, 2, 2, 2 2, 2, 2, 2 3, 3, 3, 3
  4. 1, 2, 3, 4 2, 3, 4, 1 3, 4, 1, 2 4, 1, 2,

Answer: C. 1, 1, 1, 1 2, 2, 2, 2 2, 2, 2, 2 3, 3, 3, 3

Explanation

When the value i is 0 of j is 0 then loop moves 4 time and print 1 1 1 1.
Then i is 0 and j is 1 is then loop moves 4 time and print 2 2 2 2.
When i is 1 and j is 0 is then loop moves 4 time and print 2 2 2 2.
When i is 1 and j is 1 is then loop moves 4 time and print 3 3 3 3.

Step-by-step solution

  1. ```c Static int a[2][2] = {1, 2, 3, 4} ``` gives a[0] = {1, 2} and a[1] = {3, 4}, which lie in memory as 1, 2, 3, 4.
  2. p[0] points to element 0, p[1] points to element 1, and p[2] points to element 2 of the same memory.
  3. Each expression such as p[i][j] or p[j][i] picks the element at position i + j, so all four forms give the same value.
  4. For i = 0, j = 0 the value is 1; for i = 0, j = 1 and i = 1, j = 0 it is 2; for i = 1, j = 1 it is 3.
  5. So the output is 1, 1, 1, 1 2, 2, 2, 2 2, 2, 2, 2 3, 3, 3, 3, which is option C.

Why the other options are wrong

  • A. The second line shows 2, 3, 2, 3, but all four values on a line are equal.
  • B. Values differ within a line, but all four forms give the same element.
  • D. The values are printed in array order, but each line repeats one element.

Tip: p[i][j] with a pointer array reduces to base + i + j.

Question 2Medium

What will be the output of the program?

#include <stdio.h>
void fun(int **p);
int main()
{
  int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
  int *ptr;
  ptr = &a[0][0];
  fun(&ptr);
  return 0;
}
void fun(int **p)
{
  printf("%d\n", **p);
}
  1. 1
  2. 2
  3. 3
  4. 4

Answer: A. 1

Explanation

Step 1:

int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};

The variable a is declared as an multidimensional. integer array with size of 3 rows 4 columns.
Step 2:

int *ptr;

The *ptr is a integer pointer variable.
Step 3:

ptr = &a[0][0];

Here we are assigning the base address of the array ato the pointer variable *ptr.
Step 4:

fun(&ptr);

Now, the &ptr contains the base address of array a.
Step 5: Inside the function

fun(&ptr); The printf("%d\n", **p);

prints the value '1'.because the *p contains the base address or the first element memory address of the array a (ie. a[0])
**p contains the value of *p memory location (ie. a[0]=1). Hence the output of the program is '1'

Step-by-step solution

  1. The variable a is declared as a multidimensional integer array with size 3 rows and 4 columns.
  2. The pointer ptr is declared to hold an integer address.
  3. The base address of the first element of array a is assigned to ptr.
  4. The function fun is called with the address of ptr.
  5. Inside fun, p dereferences twice, accessing the value at the base address of a, which is 1.

Why the other options are wrong

  • B. The pointer ptr already holds the base address of a[0][0]. Dereferencing it once more with p gives the value at that address, not the next element.
  • C. Similar to option B, dereferencing ptr twice with p accesses the first element of a, which is 1, not the second element.
  • D. Dereferencing ptr twice with p accesses the first element of a, which is 1, not the third element.

Tip: Remember: Dereferencing a pointer twice with p accesses the base address of the array.

Question 3Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}
  1. 0, 0, 0 1, 1, 1 2, 2, 2 3, 3, 3
  2. 1, 1, 2 2, 2, 3 3, 3, 4 4, 4, 1
  3. 1, 1, 1 2, 2, 2 3, 3, 3 3, 4, 4
  4. 0, 1, 2 1, 2, 3 2, 3, 4 3, 4, 5

Answer: C. 1, 1, 1 2, 2, 2 3, 3, 3 3, 4, 4

Explanation

A pointer holds an address, and a pointer to pointer holds the address of another pointer. Here p is an array of pointers to the elements of arr, and **ptr is the value reached through two pointers.

Step-by-step solution

  1. arr holds 0 to 4, and p[k] holds the address arr + k.
  2. ptr++ moves ptr to p[1]; ptr - p = 1, *ptr - arr = 1 and **ptr = 1, so the line is 1, 1, 1.
  3. *ptr++ moves ptr to p[2] (the value read is discarded); the line is 2, 2, 2.
  4. *++ptr moves ptr to p[3]; the line is 3, 3, 3.
  5. ++*ptr changes p[3] from arr + 3 to arr + 4, so the last line is 3, 4, 4.
  6. The output is 1, 1, 1 then 2, 2, 2 then 3, 3, 3 then 3, 4, 4, one line each.

Why the other options are wrong

  • A. ptr++ moves ptr, so the first line is not 0, 0, 0.
  • B. The later lines are wrong; the second line should be 2, 2, 2.
  • D. The first line is 1, 1, 1, not 0, 1, 2.

Tip: Track ptr and *ptr separately; ++*ptr changes the pointed value, not ptr.

Question 4Medium

What will be the output of the program?

#include <stdio.h>
int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(++i < n)
        p = &arr[i];
    *p=0;
}
  1. 2, 3, 4, 5
  2. 1, 2, 3, 4
  3. 0, 1, 2, 3
  4. 1,2,3,0,

Answer: D. 1,2,3,0,

Explanation

fun receives the address of arr. The loop runs for i = 1, 2, 3, so p ends as &arr[3], and *p = 0 sets arr[3] to 0. main then prints 1,2,3,0,.

Step-by-step solution

  1. Arrays are passed as their address, so fun changes arr in main.
  2. while(++i < n) runs for i = 1, 2 and 3, so p is last set to &arr[3].
  3. *p = 0 sets arr[3] = 0.
  4. main prints 1,2,3,0, which is option D.

Why the other options are wrong

  • A. fun does not add to the values.
  • B. arr[3] is changed to 0, so the original values are not all printed.
  • C. arr[0] stays 1; only arr[3] becomes 0.

Tip: Check the loop bounds; writing past the end of an array is a common trap.

Question 5Medium

What does the following declaration mean?

int (*ptr)[10];
  1. ptr is array of pointers to 10 integers
  2. ptr is a pointer to an array of 10 integers
  3. ptr is an array of 10 integers
  4. ptr is an pointer to array

Answer: B. ptr is a pointer to an array of 10 integers

Explanation

int (*ptr)[10];

This syntactical Expression show that *ptr is a pointer variable and [10] is the size of the array.So we will say that pointer to an Array of 10 integer.

Step-by-step solution

  1. In a declaration, read outward from the name, starting with the brackets and stars.
  2. (*ptr) says ptr is a pointer.
  3. [10] after it says the pointer points to an array of 10 items.
  4. The items are int, so ptr points to an array of 10 integers.
  5. The answer is ptr is a pointer to an array of 10 integers.

Why the other options are wrong

  • A. That would be int *ptr[10], without the brackets around *ptr.
  • C. Ptr is a pointer, because of the brackets, not an array.
  • D. It is a pointer to an array, but this option leaves out the array size and element type.

Tip: Brackets around *name make it a pointer to an array; without them it is an array of pointers.

Question 6Medium

Which of the following statements are correct about 6 used in the program?

int num[6];
num[6]=21;
  1. In the first statement 6 specifies a particular element, whereas in the second statement it specifies a type.
  2. In the first statement 6 specifies a array size, whereas in the second statement it specifies a particular element of array.
  3. In the first statement 6 specifies a particular element, whereas in the second statement it specifies a array size.
  4. In both the statement 6 specifies array size.

Answer: B. In the first statement 6 specifies a array size, whereas in the second statement it specifies a particular element of array.

Explanation

The statement 'B' is correct, because int num[6]; specifies the size of array and num[6]=21; designates the particular element(7th element) of the array.

Step-by-step solution

  1. Int num[6] declares an array with 6 elements, so 6 is the array size here.
  2. num[6] = 21 uses 6 as an index, which picks one particular element.
  3. Valid indexes are 0 to 5, so index 6 is out of bounds, but 6 is still an index.
  4. So in the first statement 6 is an array size, and in the second it is a particular element.

Why the other options are wrong

  • A. In the first statement 6 is the array size, not a particular element.
  • C. The roles are reversed: 6 is the size in the first statement and an index in the second.
  • D. In the second statement 6 is an index, not a size.

Tip: In a declaration the number is a size; in an assignment inside brackets it is an index.

Question 7Medium

Which of the following statements are correct about an array?
1: The array int num[26]; can store 26 elements.
2: The expression num[1] designates the very first element in the array.
3: It is necessary to initialize the array at the time of declaration.
4: The declaration num[SIZE] is allowed if SIZE is a macro.

  1. 1
  2. 1,4
  3. 2,3
  4. 2,4

Answer: B. 1,4

Explanation

1. The array int num[26]; can store 26 elements. This statement is true.
2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array.
3. It is necessary to initialize the array at the time of declaration. This statement is false.
4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value.
Hence the statements '1' and '4' are correct statements.

Step-by-step solution

  1. Statement 1 is true, since int num[26] holds 26 elements with indexes 0 to 25.
  2. Statement 2 is false, because num[1] is the second element, and num[0] is the first.
  3. Statement 3 is false, since C arrays do not have to be initialised at declaration.
  4. Statement 4 is true, because a macro SIZE is replaced by its value before compilation.
  5. So the correct statements are 1 and 4.

Why the other options are wrong

  • A. Statement 4 is also correct, so 1 alone is incomplete.
  • C. Statement 2 is false, so the pair 2,3 is wrong.
  • D. Statement 2 is false, so the pair 2,4 is wrong.

Tip: Array indexes start at 0, so num[1] is the second element.

Question 8Medium

In C, if you pass an array as an argument to a function,what actually gets passed?

  1. Value of elements in array
  2. First element of the array
  3. Base address of the array
  4. Address of the last element of array

Answer: C. Base address of the array

Explanation

When an array is passed to a function, C passes its base address, which is the address of the first element. The elements are not copied.

Step-by-step solution

  1. When an array is passed to a function in C, it is converted to a pointer.
  2. That pointer holds the base address, which is the address of the first element.
  3. So the function receives the base address of the array, which is option C.

Why the other options are wrong

  • A. The elements are not copied when an array is passed.
  • B. Only the address of the first element is passed, not its value.
  • D. The address of the last element is not passed; the base address is.

Tip: Arrays are passed as pointers, so changes inside the function affect the original array.

Question 9Medium

Which of the following is correct way to define the function fun() in the below program?

#include <stdio.h>
int main()
{
    int a[3][4];
    fun(a);
    return 0;
}
  1. void fun(int p[][4]){}
  2. void fun(int *p[4]){}
  3. void fun(int *p[][4]){}
  4. void fun(int *p[3][4]){}

Answer: A.

Explanation

void fun(int p[][4]){}

is the correct way to write the function fun(). while the others are considered only the function fun() iscalled by using call by reference

Step-by-step solution

  1. The array a in main has type int[3][4], which becomes a pointer to a row of 4 int values.
  2. The matching parameter is therefore a pointer to int[4], which is written as int p[][4].
  3. Option A declares p as int p[][4], which has this type.
  4. So the answer is void fun(int p[][4]){}.

Why the other options are wrong

  • B. Int *p[4] is an array of 4 pointers to int, not a pointer to rows of 4 ints.
  • C. Int *p[][4] is an array of pointers, so the type is wrong.
  • D. Int *p[3][4] is a 2D array of pointers, not a 2D array of ints.

Tip: A 2D array parameter needs the column size, and the first size can be left empty.

Question 10Medium

Which of the following statements are correct about the program below?

#include <stdio.h>
int main()
{
    int size, i;
    scanf("%d", &size);
    int arr[size];
    for(i=1; i<=size; i++)
    {
        scanf("%d", arr[i]);
        printf("%d", arr[i]);
    }
    return 0;
}
  1. The code is erroneous since the subscript for array used in for loop is in the range 1 to size.
  2. The code is erroneous since the values of array are getting scanned through the loop.
  3. The code is erroneous since the statement declaring array is invalid.
  4. The code is correct and runs successfully.

Answer: A. The code is erroneous since the subscript for array used in for loop is in the range 1 to size.

Explanation

Loop runs 1 to size, out of range for arr; VLAs are valid in C99.

Step-by-step solution

  1. The loop variable i runs from 1 to size, inclusive.
  2. The array arr has valid indexes 0 to size minus 1, so arr[size] is outside it.
  3. The loop reads and writes past the end of arr, which is an error.
  4. Option A is the correct answer, since the subscript range is the problem.

Why the other options are wrong

  • B. Scanning values in a loop is normal. The error is the index range, not the scanning.
  • C. A variable length array with a size read at run time is valid in C99.
  • D. The loop uses an index past the end of arr, so the code is not correct.

Tip: In C, arrays of length n use indexes 0 to n minus 1; a loop from 1 to n goes one too far.

Practise all 13 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics