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

Arrays Questions and Answers

61.

A string that is a formal parameter can be declared

Answer: C

Correct answer: Both A and B

A string parameter may be written either as char s[] or as char *s, and the two are exactly equivalent in a parameter list: an array argument decays to a pointer to its first element, so the function receives an address either way. Outside a parameter list the two forms are quite different.

Please log in or register to report this question.

62.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}

 

Answer: C

Correct answer: 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4

p holds the five addresses arr … arr+4, and ptr walks along p. Take the four statements in turn.

  • ptr++ - ptr = p+1. Then ptr-p is 1, *ptr-arr is 1, **ptr is arr[1] = 1 → 1, 1, 1
  • *ptr++ - the ++ binds to ptr, and the dereference is thrown away, so ptr = p+2 → 2, 2, 2
  • *++ptr - again only ptr moves, to p+3 → 3, 3, 3
  • ++*ptr - here the increment applies to *ptr, that is to p[3], which becomes arr+4. ptr itself has not moved → ptr-p = 3, *ptr-arr = 4, **ptr = arr[4] = 4 → 3, 4, 4

The point of the question is that the last line increments what the pointer points to, while the three before it increment the pointer.

Please log in or register to report this question.

63.

Below is an example of -

int RollNum[30][4];

 

Answer: B

Correct answer: 2-D Array

int RollNum[30][4]; has two subscripts, so it is a two-dimensional array - 30 rows of 4 columns, 120 integers in all. The number of dimensions is simply the number of bracket pairs, not the values inside them.

Please log in or register to report this question.

Answer: B

Correct answer: var a:array[REAL]of REAL;

An array index type must be an ordinal type - one whose values can be counted off, so that each has a definite position. Enumerations, characters and booleans all qualify, which is why the other three definitions are legal. REAL is not ordinal: there is no “next” real number, so it cannot index an array.

Please log in or register to report this question.

66.

Predict the output of below code:

#include <stdio.h> 
int main() 
{ 
    struct site 
    { 
        char name[] = "GeeksforGeeks"; 
        int no_of_pages = 200; 
    }; 
    struct site *ptr; 
    printf("%d",ptr->no_of_pages); 
    printf("%s",ptr->name);  
    getchar(); 
    return 0; 
} 

 

Answer: B

Correct answer: Compilation Error

A structure declaration describes a type, not an object, so its members cannot be given initialisers - char name[] = "GeeksforGeeks"; and int no_of_pages = 200; are both rejected. A member array also cannot have an unspecified size. The code therefore fails to compile, and the uninitialised ptr never becomes an issue.

Please log in or register to report this question.

67.

If arr is a two dimensional array of 10 rows and 12 columns, then arr[5] logically points to the

Answer: A

Correct answer: sixth row

arr is an array of rows, so arr[5] is the row at index 5 - the sixth row, since indexing starts at 0. It has type int[12] and decays to a pointer to that row’s first element. The column count never enters into which row is selected.

Please log in or register to report this question.

68.

Minimum number of comparisons required to compute the largest and second largest element in array is

Answer: B

Correct answer: n + ⌈log2n⌉ − 2

Run a knockout tournament. Finding the largest takes \(n - 1\) comparisons, and the second largest must be one of the elements that lost directly to the winner - there are \(\lceil \log_2 n \rceil\) of them, since that is the depth of the tournament. Finding the best of those costs \(\lceil \log_2 n \rceil - 1\) more:

\[ (n - 1) + \lceil \log_2 n \rceil - 1 = n + \lceil \log_2 n \rceil - 2 \]

The naive approach - two separate passes - would cost about \(2n\), so the tournament saves nearly half.

Please log in or register to report this question.

69.

Array is an example of _______ type memory allocation

Answer: A

Correct answer: Compile time

An ordinary array such as int a[20]; has its size fixed in the source, so the space is reserved when the program is compiled and laid out - static, compile-time allocation. Run-time allocation is what malloc and new do, where the size can depend on a value computed while the program runs.

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