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

Arrays Questions and Answers

101.

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

Answer: D

Correct answer: Theoretically no limit. The only practical limits are memory size and compilers

The standard fixes no maximum number of dimensions; it only requires an implementation to support a certain depth of declarator nesting. So the limit is practical - how much memory the array would need, and how deeply the compiler will nest. Note that memory grows as the product of the bounds, so even four modest dimensions can exhaust it.

Please log in or register to report this question.

Answer: B

Correct answer: the first (row) subscript may be left empty

For a two-dimensional parameter the compiler needs the row length - the number of columns - to compute a[i][j] as *(base + i*cols + j). The number of rows is never used in that arithmetic, so void f(int a[][10]) is legal while void f(int a[10][]) is not. Leaving the first subscript out is optional, not required.

Please log in or register to report this question.

103.

Predict the output of below code:

#include "stdio.h"
#include "stdlib.h"

int main(int argc, char *argv[]) { 
    char temp[20];
    gcvt(23.45,2, temp);
    printf("%s", temp);
    return 0;
}

 

Answer: D

Correct answer: Output : 23

gcvt(value, ndigit, buf) converts a double to a string with ndigit significant digits - not decimal places. With ndigit = 2, 23.45 rounds to two significant figures, giving 23, which is what is printed.

Please log in or register to report this question.

104.

While sorting a set of names, representing the names as an array of pointers is preferable to representing the names as a two dimensional array of characters because

Answer: D

Correct answer: All of the above

All three reasons hold. Storage is proportional to the actual data rather than to the longest name times the count, since each string occupies only what it needs. Swapping exchanges two pointers instead of copying whole rows of characters. And because the swaps are so much cheaper, the sort runs faster.

Please log in or register to report this question.

106.

Is there any difference in the following declarations?

int fun(int arr[]);
int fun(int arr[2]);

Answer: B

Correct answer: No

There is no difference. In a parameter list an array declarator is adjusted to a pointer, so both int arr[] and int arr[2] become int *arr. The compiler ignores the bound entirely - which is why a function cannot tell how long the array it was passed really is, and why the length is usually passed as a second parameter.

Please log in or register to report this question.

107.

The maximum number of dimension an array can have in C is

Answer: D

Correct answer: compiler dependent

The C standard sets no fixed maximum. It specifies only a minimum that a conforming implementation must support - historically 12 levels of array, pointer and function declarators - so the practical ceiling is whatever the compiler allows. In practice arrays of more than three or four dimensions are rare.

Please log in or register to report this question.

108.

Assuming int is of 4bytes, what is the size of int arr[15];?

Answer: D

Correct answer: 60

The size of an array is the number of elements times the size of one element:

\[ 15 \times 4 = 60 \text{ bytes} \]

So sizeof(arr) gives 60. Note that this holds only where the array itself is in scope - passed to a function it decays to a pointer, and sizeof then yields the pointer’s size.

Please log in or register to report this question.

109.

A program P reads in 500 integers in the range [0..100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies? 

Answer: A

Correct answer: An array of 50 numbers

Only scores above 50 are to be reported, and the scores run from 0 to 100. That leaves the 50 values 51, 52, …, 100, so an array of 50 counters is enough - score \(s\) increments the cell at index \(s - 51\). Arrays of 100, 500 or 550 would work but waste space; the question asks for the best choice.

Please log in or register to report this question.

Answer: C

Correct answer: Array of Integer Pointers of size 20

Subscript binds more tightly than the dereference, so int *ptr[20] groups as int *(ptr[20]): an array of 20 pointers to int. A pointer to an array of 20 ints would need parentheses in the other place - int (*ptr)[20].

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