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

Arrays Questions and Answers

11.

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.

12.

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

Answer: C

Correct answer: Base address of the array

An array argument decays to a pointer to its first element, so what the function receives is the base address, not a copy of the elements. This is why passing a large array costs nothing extra, why the function can modify the caller’s data, and why sizeof inside the function returns the size of a pointer rather than of the array.

Please log in or register to report this question.

13.

Under which of the following conditions, the size of an one-dimensional array need not be specified?

Answer: D

Correct answer: All of the above

All three permit the size to be left out. With an initialiser the compiler counts the values. In a declaration such as extern int a[]; the size comes from the definition elsewhere. And as a formal parameter the array is adjusted to a pointer, so no bound is needed - nor at the call site, where the name is passed alone.

Please log in or register to report this question.

14.

Predict the output of the code below, if the array begins at address 65472 and each integer occupies 2 bytes.

#include <stdio.h>

int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}

 

Answer: C

Correct answer: 65480, 65496

a is a 3×4 array of 2-byte ints. a decays to a pointer to its first row, so a+1 steps over 4 ints:

\[ 65472 + 4 \times 2 = 65480 \]

&a is a pointer to the whole array, so &a+1 steps over all 12:

\[ 65472 + 12 \times 2 = 65496 \]

Please log in or register to report this question.

Answer: D

Correct answer: Both (a) and (b)

(a) is true: C really has only one-dimensional arrays - int a[3][4] is an array of 3 objects, each of type int[4]. (b) says the same thing from the other side: an element may itself be an array. The third statement is false; elements are always contiguous, which is exactly what makes subscripting work.

Please log in or register to report this question.

16.

If S is an array of 80 characters, then the value assigned to S through the statement scanf("%s",S) with input 12345 would be

Answer: A

Correct answer: "12345"

%s reads a whitespace-delimited sequence of characters and stores them followed by a null, without interpreting them. The digits 12345 are just five characters here, so S holds the string "12345". Note that S is passed without &, because an array name already yields its base address.

Please log in or register to report this question.

17.

Consider a two dimensional array A[20][10]. Assume 4 words per memory cell, the base address of array A is 100, elements are stored in row-major order and first element is A[0][0]. What is the address of A[11][5] ?

Answer: A

Correct answer: 560

Row-major, so \(A[i][j]\) sits at \(\text{base} + w\,(i \times \text{columns} + j)\), with 10 columns and \(w = 4\):

\[ 100 + 4\,(11 \times 10 + 5) = 100 + 4 \times 115 = 560 \]

Please log in or register to report this question.

18.

C does no automatic array bound checking. This is best described as

Answer: D

Correct answer: C's shortcoming

C performs no bounds check, so an out-of-range subscript quietly reads or writes memory belonging to something else. It is generally counted a shortcoming - the source of buffer overflows and of many hard-to-find bugs. The omission was deliberate, since it keeps array access as cheap as raw address arithmetic, but later languages reinstated the check for good reason.

Please log in or register to report this question.

Answer: C

Correct answer: It is a collection of items of the same type and storage class that share a common name and occupy consecutive memory locations

The fullest definition is the right one. All three properties matter: one type and storage class, so every element is the same size; one name with an index; and consecutive memory, which is what lets a[i] be computed as an offset from the base. Drop any of the three and the index arithmetic no longer works.

Please log in or register to report this question.

Answer: A

Correct answer: All of these

All three statements are true. An array’s size is fixed when it is created and cannot be changed. The values of its elements may be reassigned as often as you like. And because indexing starts at 0, the nth element is reached as students[n-1].

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