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

Arrays Questions and Answers

111.

The parameter passing mechanism for an array is

Answer: B

The parameter passing mechanism for an array is call by reference.This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data.

Please log in or register to report this question.

112.

What will be the output of the program if the array begins at address 65486?

#include <stdio.h>

int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}

 

Answer: B

Correct answer: 65486, 65486

arr decays to a pointer to its first element and &arr is a pointer to the whole array. Both start at the same place, so both print 65486. They differ only in type, which shows up when you add to them: arr+1 moves by one int, &arr+1 by the whole array.

Please log in or register to report this question.

113.

Consider the array definition

int num [10] =  {3, 3, 3};

Pick the Correct answers

Answer: A

Correct answer: num [9] is the last element of the array num

The array has 10 cells, so the last is num[9]. The initialiser supplies only three values, and when an array is partly initialised the remaining elements are set to zero. So num[0], num[1] and num[2] are 3, while num[3] and num[8] are both 0 - which is why the other two statements are false.

Please log in or register to report this question.

114.

What will be the output of the program ?

#include <stdio.h>

int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}

 

Answer: C

Correct answer: 3, 2, 15

Take the three statements in order, starting from a = {5, 1, 15, 20, 25}.

  1. i = ++a[1]; - pre-increment: a[1] becomes 2 and that new value is assigned, so i = 2.
  2. j = a[1]++; - post-increment: j = 2 (the old value), and a[1] becomes 3.
  3. m = a[i++]; - the subscript is the old i, so m = a[2] = 15, and i then becomes 3.

The printed values are therefore 3, 2, 15.

Please log in or register to report this question.

Answer: D

Correct answer: All of the above

All three are true. An array holds one type. An array may be a member of a structure, as a fixed-length name field commonly is. And an array of structures is entirely ordinary - a table of records is exactly that.

Please log in or register to report this question.

116.

The minimum number of interchanges needed to convert the array 89,19,40,17,12,10,2,5,7,11,6,9,70 into a heap with maximum element at the root is

Answer: B

Correct answer: 2

Build the max-heap bottom-up. Numbering the array from 1:

1:89  2:19  3:40  4:17  5:12  6:10  7:2  8:5  9:7  10:11  11:6  12:9  13:70

  • Node 6 holds 10 with children 9 and 70 - swap with 70.
  • Nodes 5 and 4 are already correct (12 over 11 and 6; 17 over 5 and 7).
  • Node 3 holds 40 with children 70 and 2 - swap. The 40 that sinks to node 6 still beats its children, 9 and 10.
  • Node 2 (19 over 17 and 12) and the root (89 over 19 and 70) need nothing.

Two interchanges suffice, so the answer is 2.

Please log in or register to report this question.

117.

The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is

Answer: B

Correct answer: Θ(logn)

If a value occupies more than \(n/2\) positions of a sorted array, those positions are contiguous and must cover the middle, so the candidate can only be \(A[n/2]\). Read that one element, then binary search for its first and last occurrence and check whether the run exceeds \(n/2\). Two binary searches cost \(\Theta(\log n)\), and no scan of the whole array is needed.

Please log in or register to report this question.

118.

What is the output of the following piece of code?

public class array
{
 public static void main(String args[])
 {
  int []arr = {1,2,3,4,5};
  System.out.println(arr[5]);
 }
}

 

Answer: C

Correct answer: ArrayIndexOutOfBoundsException

The array has five elements, so its valid indices are 0 to 4. arr[5] is one past the end. Java checks every index at run time and throws an ArrayIndexOutOfBoundsException - unlike C, which would read whatever happens to lie at that address.

Please log in or register to report this question.

Answer: A

Correct answer: char name[6] = {'I','N','D','I','A','\0'}

A C string is a character array ending in a terminating null, so “INDIA” needs six cells: five letters and '\0'. Single quotes make characters; double quotes would make five separate strings. And an array cannot be assigned to after declaration, so name = "INDIA"; is not valid either. (char name[6] = "INDIA"; would also work - the compiler supplies the null.)

Please log in or register to report this question.

120.

Which of the following statements mentioning the name of the array does NOT yield the base address?

1. When array name is used with the sizeof operator.

2. When array name is operand of the & operator.

3. When array name is passed to scanf() function.

4. When array name is passed to printf() function.

Answer: B

Correct answer: 1, 2

The name of an array normally decays to the address of its first element - which is why passing it to scanf() or printf() gives the base address. There are two exceptions. With sizeof the name means the whole array, so sizeof(arr) gives the total number of bytes. With & it again means the whole array, so &arr has type “pointer to array”. Those are statements 1 and 2.

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