• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Arrays

Question 21

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 22

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 23

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 24

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 25

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 26

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 27

    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

    Question 28

    In an array of 2N elements that is both 2-ordered and 3-ordered, what is the maximum number of positions that an element can be from its position if the array were 1-ordered?
    • 1
    • 2
    • N/2
    • 2N-1

    Question 29

    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 30

    Consider the C program given below. What does it print? C
    #include <stdio.h>
    int main ()
    {
            int i, j;
            int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
            for(i = 0; i < 3; i++) {
                 a[i] = a[i] + 1;
                 i++;
            }
            i--;
            for (j = 7; j > 4; j--) {
                  int i = j/2;
                  a[i] = a[i] - 1;
            }
            printf (\"%d, %d\", i, a[i]);
    }
     /* Add code here. Remove these lines if not writing code */ 
    
    • 2, 3
    • 2, 4
    • 3, 2
    • 3, 3

    There are 37 questions to complete.

    Last Updated :
    Take a part in the ongoing discussion