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

Arrays Questions and Answers

Answer: B

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

In a declaration, int num[6];, the 6 is the size - how many elements to reserve. In an expression, num[6] = 21;, the 6 is a subscript naming one element. The same notation means different things in the two positions, and here the second use is in fact out of bounds: valid subscripts run 0 to 5.

Please log in or register to report this question.

32.

The parameter passing mechanism for an array is

Answer: C

Correct answer: call by reference

C passes everything by value, but what is passed for an array is its base address, so the function works on the caller’s own storage and any change it makes is visible afterwards. That effect is what “call by reference” describes, and it is why a function can fill an array supplied by its caller.

Please log in or register to report this question.

33.

What is the maximun number of dimensions an array in C may have?

Answer: D

The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details.

Please log in or register to report this question.

34.

Predict the output of below code: 

#include <stdio.h>

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

 

Answer: B

Correct answer: Output : 10

a[i] is defined as *(a + i), and addition commutes, so i[a] means the same thing. 0[arr] is therefore arr[0], and the program prints 10.

Please log in or register to report this question.

35.

How many elements does the array below have?

int a[20]

 

Answer: C

Correct answer: 20

The number in the declarator is the count of elements, so int a[20] has 20 of them, with subscripts 0 to 19. Do not confuse this with the number of bytes, which would be \(20 \times \text{sizeof(int)}\).

Please log in or register to report this question.

36.

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.

37.

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.

38.

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.

40.

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.

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