Machine-translated by Google. Questions are authored in English.
Top

Arrays Questions and Answers

1.

What is printed by the following ANSI C program?

#include\(\lt\)stdio.h\(\gt\)
int main(int argc, char *argv[])
{
    int a[3][3][3] =
    {{1, 2, 3, 4, 5, 6, 7, 8, 9},
     {10, 11, 12, 13, 14, 15, 16, 17, 18},
     {19, 20, 21, 22, 23, 24, 25, 26, 27}};
  int i = 0, j = 0, k = 0;
  for( i = 0; i \(\lt\) 3; i++ ){
    for(k = 0; k \(\lt\) 3; k++ )
      printf("%d ", a[i][j][k]);
    printf("\n");
  }
  return 0;
}

Exam Year(s)

GATE CS - 2022,

Answer: A

The program prints 1 2 3 / 10 11 12 / 19 20 21, one group per line.

Each inner brace list initialises one 3 by 3 block a[i] with nine values, filled row by row, so a[0] holds 1 to 9, a[1] holds 10 to 18 and a[2] holds 19 to 27. In particular a[i][0] is the first row of each block: 1 2 3, then 10 11 12, then 19 20 21.

The loops vary i and k but never change j, which stays 0. So for each i the program prints a[i][0][0], a[i][0][1], a[i][0][2] and then a newline, which is the first row of each block.

'1 4 7 ...' would come from varying j instead of k. '1 2 3 / 4 5 6 / 7 8 9' prints the three rows of a[0] only, as if i stayed 0 while j changed. '1 2 3 / 13 14 15 / 25 26 27' moves along the diagonal blocks, as if j rose with i.

Please log in or register to report this question.

2.

The first index number in an array starts with................and the index number of an array of size n will be :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: A

The first index is 0 and the last is n - 1, so the answer is 0, n - 1.

In C and C++, an array index is an offset from the start of the array. The first element is at offset 0, so it is a[0]. An array of size n therefore has elements a[0] to a[n-1], which is n elements in all.

Starting at 1 is the convention of languages such as Fortran, not C. Using n as the last index reads one element past the end of the array, a classic off-by-one error that causes undefined behaviour.

Please log in or register to report this question.

Answer: D

The correct choice is Both (a) and (b) above.

(a) is correct: strictly speaking, C has only one-dimensional arrays. What looks like a two-dimensional array, int m[3][4], is an array of 3 elements, each of which is an array of 4 integers. (b) is correct for the same reason: an element of an array can itself be an array.

(c) is wrong: the elements of a C array always occupy contiguous memory locations, which is what makes pointer arithmetic on arrays work.

Please log in or register to report this question.

4.

An array elements are always stored in ______ memory locations.

Exam Year(s)

Tripura Public Service Commission - 2019,

Answer: A

Array elements are always stored in sequential memory locations.

An array occupies one contiguous block of memory, with each element placed immediately after the previous one. That is why the address of element i can be computed directly as the base address plus i times the element size, giving constant-time access.

Random placement describes structures such as linked lists, whose nodes can be anywhere in memory, so 'random' and 'sequential and random' are wrong.

Please log in or register to report this question.

5.

What will be the output of the below code ?

#include <iostream>
using namespace std;
int main()
{
int arr[2] = { 1, 2 };
cout << 0 [arr] << ", " << 1 [arr] << endl;
return 0;
}

Exam Year(s)

Tripura Public Service Commission - 2025,

Answer: A

The output is 1, 2.

In C and C++ the subscript expression a[i] is defined as *(a + i). Addition is commutative, so 0[arr] is *(0 + arr), the same as arr[0], and 1[arr] is arr[1].

The statement therefore prints arr[0], a comma and space, then arr[1]: 1, 2. It looks odd but is legal, so there is no syntax error, and it reads only inside the array, so there is no run-time error.

Please log in or register to report this question.

Answer: B

The word NEHA has four characters, and a C string needs one more cell for the terminating null character, so the array must hold five elements: four letters and the null. The declaration in the second option has size 5 and ends the initialiser with the null character, so it is the proper declaration. The first option, with size 4 and four character constants, makes an array that is not a string because it has no null terminator. The option with size 4 initialised by the string NEHA is wrong because the string needs five bytes, and the last option uses single quotes around four characters, which is a multi-character constant and not a string. The answer is Char name [5] = {'N', 'E', 'H', 'A', '\0'};

Please log in or register to report this question.

7.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}

 

Answer: B

Correct answer: 10

Subscripting is defined as a[i] being exactly *(a + i). Addition commutes, so *(a + i) is also *(i + a), which is i[a]. Therefore 0[arr] means arr[0], printing 10. It is legal but nobody writes it outside puzzles.

Please log in or register to report this question.

9.

What is the value of a[4]?

int a[5]={1,2,4,1,0}

Answer: C

Correct answer: 0

The initialiser list fills the array in order, so a[0] = 1, a[1] = 2, a[2] = 4, a[3] = 1 and a[4] = 0 - the last value in the list, since indexing starts at 0.

Please log in or register to report this question.

Answer: B

Correct answer: Both A and B

A is true: every element of an array has the same type and the same storage class, which is what makes uniform address arithmetic possible. B is true: the number of subscripts is the dimension - a[3][4] is two-dimensional. The remaining option is false, since a subscript may perfectly well be zero; indeed index 0 is the first element.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Arrays Questions and Answers || Arrays MCQ Questions and Answers || Arrays GK Questions and Answers || Arrays GK MCQ Questions || Arrays Multiple Choice Questions and Answers || Arrays GK || GK on Arrays || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming