C Arrays

Last Updated :
Discuss
Comments

Question 1

Consider the array A[]= {6,4,8,1,3} apply the insertion sort to sort the array . Consider the cost associated with each sort is 25 rupees , what is the total cost of the insertion sort when element 1 reaches the first position of the array  ?
  • 50
  • 25
  • 75
  • 100

Question 2

What is the output printed by the following C code?

C
# include <stdio.h>
int main ()
{
    char a [6] = "world";
    int i, j;
    for (i = 0, j = 5; i < j; a [i++] = a [j--]);
    printf ("%s\\n", a);
}
 /* Add code here. Remove these lines if not writing code */ 
  • dlrow

  • Null String

  • dlrld

  • worow

Question 3

Pick the best statement for the below program: C
#include "stdio.h"

int size = 4;
int arr[size];

int main()
{
 if(arr[0])
  printf("Initialized to ZERO");
 else
  printf("Not initialized to ZERO");

 return 0;
}
  • No compile error and it’ll print “Initialized to ZERO”.
  • No compile error and it’ll print “Not initialized to ZERO”.
  • Compile error because size of arr has been defined using variable outside any function.
  • No compile error and it’ll print either “Initialized to ZERO” or “Not initialized to ZERO” depending on what value is present at arr[0] at a particular run of the program.

Question 4

Let a be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0.

C
i = 0;
j = 1;
while (j < n )
{
    if (E) j++;
    else if (a[j] - a[i] == S) break;
    else i++;
}
if (j < n)
    printf("yes")
else
   printf ("no");

Choose the correct expression for E.

  • a[j] - a[i] > S

  • a[j] - a[i] < S

  • a[i] - a[j] < S

  • a[i] - a[j] > S

Question 5

Let a and b be two sorted arrays containing n integers each, in non-decreasing order. Let c be a sorted array containing 2n integers obtained by merging the two arrays a and b. Assuming the arrays are indexed starting from 0, consider the following four statements
  1. a[i] ≥ b [i] => c[2i] ≥ a [i]
  2. a[i] ≥ b [i] => c[2i] ≥ b [i]
  3. a[i] ≥ b [i] => c[2i] ≤ a [i]
  4. a[i] ≥ b [i] => c[2i] ≤ b [i]

Which of the following is TRUE?

  • only I and II

  • only I and IV

  • only II and III

  • only III and IV

Question 6

Consider the C program given below :

C
 #include <stdio.h>
int main ()    {
    int sum = 0, maxsum = 0,  i,  n = 6;
    int a [] = {2, -2, -1, 3, 4, 2};
    for (i = 0; i < n; i++)    {
            if (i == 0 || a [i]  < 0  || a [i] < a [i - 1])  {
                     if (sum > maxsum) maxsum = sum;
                     sum = (a [i] > 0) ? a [i] : 0;
            }
            else sum += a [i];
    }
    if (sum > maxsum) maxsum = sum ;
    printf ("%d\\n", maxsum);

} 

What is the value printed out when this program is executed?

  • 9

  • 8

  • 7

  • 6

Question 7

You are given an array A[] having n random bits and a function OR(i,j) which will take two indexes from an array as parameters and will return the result of ( A[i] OR A[j] ) i.e bitwise OR. What is the minimum no of OR calls required so as to determine all the bits inside the array i.e. to determine every index of A[] whether it has 0 or 1 ?
  • N-1
  • N*(N-1)/2
  • N
  • Not possible to determine the bit array

Question 8

Let A be a two dimensional array declared as follows:
A: array [1 ... 10] [1 ... 15] of integer;
Assuming that each integer takes one memory location, the array is stored in row-major order and the first element of the array is stored at location 100, what is the address of the element a[i][j] ?
  • 15i+ j+ 84
  • 15j+ i+ 84
  • 10i+ j+ 89
  • 10j+ i+ 89

Question 9

An array A contains n≥1 positive integers in the locations A[1], A[2],... A[n]. The following program fragment prints the length of a shortest sequence of consecutive elements of A, A[i], A[i+1],...A[j] such that the sum of their values is ≥M, a given positive number. It prints ‘n+1’ if no such sequence exists. Complete the program by filling in the boxes. In each case use the simplest possible expression. Write only the line number and the contents of the box. ccc

    Question 10

    Let A(1:8, -5:5, -10:5) be a three dimensional array. How many elements are there in the array A?

    • 1200

    • 1408

    • 33

    • 1050

    Tags:

    There are 37 questions to complete.

    Take a part in the ongoing discussion