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.

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.

4.

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.

Answer: D

Correct answer: All of the above

All three reasons hold. A file persists after the program that wrote it ends, while an array vanishes with the process. A file need not be sized in advance, so no space is reserved and wasted. And the operating system provides tools - copy, search, sort, back up - that work on files and not on a program’s memory.

Please log in or register to report this question.

7.

The information about an array used in program will be stored in

Answer: C

Correct answer: Dope Vector

A dope vector is the descriptor the implementation keeps for an array: its base address, element size, number of dimensions and the bounds of each. It is what makes address computation and bounds checking possible. The symbol table holds compile-time names and types; an activation record holds a call’s local storage.

Please log in or register to report this question.

8.

What is the syntax for accessing the element at index 7 of an array a?

Answer: D

Correct answer: a[7]

C uses square brackets for subscripting, so the element at index 7 is a[7]. Braces are for initialiser lists, parentheses would be read as a function call, and quotes make a string literal. Note that a[7] is the eighth element, since indexing starts at 0.

Please log in or register to report this question.

9.

If x is an array of integers, then the value of &x[i] is same as

Answer: A

Correct answer: x+i

By definition x[i] is *(x + i), so taking the address of both sides gives &x[i] == x + i. The scaling is already built into pointer arithmetic: adding 1 to an int * advances by sizeof(int) bytes. That is why the two options that multiply or add sizeof(int) explicitly are wrong - they would scale twice.

Please log in or register to report this question.

10.

A one dimensional array A has indices 1....75. Each element is a string and takes up three memory words. The array is stored at location 1120 decimal. The starting address of A[49] is

Answer: A

Correct answer: 1264

Indices start at 1 here, not 0, so \(A[49]\) is the 49th element and 48 elements precede it. Each takes 3 words:

\[ 1120 + 3 \times (49 - 1) = 1120 + 144 = 1264 \]

Forgetting the 1-based start gives 1267, which is the distractor.

Please log in or register to report this question.

Loading…
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