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

Arrays Questions and Answers

31.

The following program

main()
{
  static char a[3][4] = {"abcd", "mnop", "fghi"};
  putchar(**a);
}

 

Answer: D

Correct answer: none of the above

The program is valid and prints a. Each row is declared char[4] and each initialiser has exactly four characters, so C drops the terminating null - a special allowance for character arrays initialised from a string. **a is a[0][0], the character 'a', which putchar writes. Since it neither fails to compile, nor crashes, nor prints garbage, the answer is none of the above.

Please log in or register to report this question.

32.

Pick out the output

#include <stdio.h>
void foo(void);

int main()
{
    foo();
    return 0;
}

void foo(void)
{
    int i = 10, k = 10, j = 2;
    int *ary[2];
    ary[0] = &i;
    ary[1] = &j;
    printf("%d\n", ary[0][1]);
}

Answer: D

Correct answer: Undefined Behaviour

The array ary holds two pointers: ary[0] points at i and ary[1] points at j. Now read the expression ary[0][1]. Subscripting is defined as x[n] meaning *(x + n), so this is *(ary[0] + 1) - take the pointer to i, step one int past it, and read what is there. But i is a single int, not the first element of an array, so there is nothing legitimately one step past it. The standard permits forming a pointer one past a single object but not dereferencing it, and that makes this undefined behaviour. In practice the program will usually print whatever happens to sit next on the stack - often 10, the value of k, since many compilers lay these locals out adjacently - but that is an accident of the compiler and the optimisation level, not a result the language guarantees. It will not print 20, and it is neither a compile error nor a diagnosed runtime error, because nothing in the code is ill-formed and nothing traps. The listing in this record had lost the declaration the call in main depended on, and gave foo a parameter with the same name as one of its own locals; it is repaired above so that the program compiles and exhibits exactly the behaviour the question is about.

Please log in or register to report this question.

33.

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;
}

 

Answer: C

Correct answer: The code is erroneous since the statement declaring array is invalid

int arr[size]; is placed after an executable statement and sizes the array from a variable read at run time. Under the C89 rules this question is set against, both are illegal: declarations must precede statements in a block, and an array bound must be a compile-time constant. (C99 added variable-length arrays, which is why some modern compilers accept it.) There is a second bug worth noticing - scanf("%d", arr[i]) passes a value where an address is required - but the declaration is what the question is about.

Please log in or register to report this question.

Answer: D

Correct answer: Both (a) & (b)

(a) An entire array is passed simply by naming it, which hands over its base address. (b) A part can be passed just as easily, by passing the address of an interior element - f(&a[3], n) gives the function the tail of the array. (c) is false: since the function holds the caller’s address, changes it makes are not local - they are visible to the caller.

Please log in or register to report this question.

35.

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);
}

 

Answer: A

Correct answer: 1

ptr is set to &a[0][0], the address of the first element. fun receives &ptr, so inside it p points at ptr: one dereference gives ptr itself, and the second gives what ptr points to. So **p is a[0][0], which is 1.

Please log in or register to report this question.

Answer: B

Correct answer: Container of objects of similar types

An array is a container holding a fixed number of values of a single type, laid out in consecutive memory. A hierarchy describes a tree, not an array; an array’s elements can be reassigned after initialisation, so it is not immutable; and it is very much a data structure - the most basic one there is.

Please log in or register to report this question.

Answer: B

Correct answer: what is assigned is compiler-dependent

The statement a[++n] = n++; both reads and modifies n twice between sequence points, and it uses the value of n on the right while also changing it on the left. The standard leaves the order unspecified, so the compiler may evaluate the subscript before or after the right-hand side. The result is undefined behaviour - what is assigned, and where, depends on the compiler.

Please log in or register to report this question.

38.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

 

Answer: B

Correct answer: 4

This is the standard idiom for the length of an array:

\[ \frac{\text{sizeof(arr)}}{\text{sizeof(arr[0])}} = \frac{4 \times 4}{4} = 4 \]

It works whatever the element type, because the element size divides out. It works only where the array itself is visible - inside a function that received it as a parameter, sizeof would give the size of a pointer.

Please log in or register to report this question.

39.

Array can be considered as set of elements stored in consecutive memory locations but having __________.

Answer: A

Correct answer: Same data type

The two defining properties of an array are that its elements lie in consecutive memory locations and that they are all of the same data type. The second follows from the first: address arithmetic can only step through the array if every element occupies the same number of bytes.

Please log in or register to report this question.

40.

If storage class is missing in the array definition, by default it will be taken to be 

Answer: A

Correct answer: either automatic or external depending on the place of occurrence

With no storage class written, the default depends on where the definition appears. Inside a function it is auto - allocated on the stack, uninitialised, and gone when the function returns. At file scope it is external - allocated for the life of the program and zero-initialised.

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