Test: Arrays - 2


15 Questions MCQ Test Learn to Program with C++: Beginner to Expert | Test: Arrays - 2


Description
This mock test of Test: Arrays - 2 for Back-End Programming helps you for every Back-End Programming entrance exam. This contains 15 Multiple Choice Questions for Back-End Programming Test: Arrays - 2 (mcq) to study with solutions a complete question bank. The solved questions answers in this Test: Arrays - 2 quiz give you a good mix of easy questions and tough questions. Back-End Programming students definitely take this Test: Arrays - 2 exercise for a better result in the exam. You can find other Test: Arrays - 2 extra questions, long questions & short questions for Back-End Programming on EduRev as well by searching above.
QUESTION: 1

What is a array?

Solution:
QUESTION: 2

An array elements are always stored in ________ memory locations.

Solution:
QUESTION: 3

What is the output of following program?

#include <stdio.h>
int main()
{
    int a[][] = {{1,2},{3,4}};
    int i, j;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            printf("%d ", a[i][j]);
    return 0;
}

Solution:

There is compilation error in the declaration " int a[][] = {{1,2},{3,4}};". Except the first dimension, every other dimension must be specified.
int arr[] = {5, 6, 7, 8} //valid
int arr[][5] = {}; //valid
int arr[][] = {}; //invalid
int arr[][10][5] = {}; //valid
int arr[][][5] = {}; //invalid

QUESTION: 4

Which of the following gives the memory address of the first element in array?

Solution:
QUESTION: 5

What will be printed after execution of the following code?
void main()
{
      int arr[10] = {1,2,3,4,5};
      printf("%d", arr[5]);
}

Solution:
QUESTION: 6

For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
t0 = i * 1024
t1 = j * 32
t2 = k * 4
t3 = t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statements about the source code for the C program is CORRECT?

Solution:

The final expression can be simplified in form ofi, j and k by following the intermediate code steps in reverse order
t5 = X[t4]
   = X[t3 + t2]
   = X[t1 + t0 + t2]
   = X[i*1024 + j*32 + k*4]
   = X + i*1024 + j*32 + k*4 
Since k is multiplied by 4, the array must be an int array. We are left with 2 choices (A and B) among the 4 given choices. X[i][j][k]'th element in one dimensional array is equivalent to X[i*M*L + j*L + k]'th element in one dimensional array (Note that multi-dimensional arrays are stored in row major order in C). So we get following equations
j*L*4 = j*32, we get L = 8 (4 is the sizeof(int))
i*1024 = i*M*L*4, we get M = 1024/32 = 32
Therefore option A is the only correct option as M and L are 32 and 8 respectively only in option A.

QUESTION: 7

What is the output of this program?

#include < stdio.h >

using namespace std;

int main()

{

char str[5] = "ABC";

cout << str[3];

cout << str;

return 0;

}

Solution:

We are just printing the values of first 3 values.

QUESTION: 8

What will be the output of the program ?

#include"stdio.h"
int main()
{
        int a[5] = {51, 1, 5, 20, 25};
        int x, y, z;
        x = ++a[1];
        y = a[1]++;
        z = a[x++];
        printf("%d, %d, %d", x, y, z);
    return 0;
}

Solution:
QUESTION: 9

Find out the correct statement for the following program.

#include "stdio.h"

int * arrPtr[5];

int main()

{

 if(*(arrPtr+2) == *(arrPtr+4))

 {

   printf("Equal!");

 }

 else

 {

  printf("Not Equal");

 }

 return 0;

}

Solution:

Here arrPtr is a global array of pointers to int. It should be noted that global variables such arrPtr are initialized to ZERO. That’s why all are elements of arrPtr are initialized implicitly to ZERO i.e. correct answer is b.

QUESTION: 10

What will be the output of the program ?

#include"stdio.h"
int main()
{
    int arr[5] = {10};
    printf("%d", 0[arr]);
    
    return 0;
}

Solution:
QUESTION: 11

Pick the best statement for the below program:

#include "stdio.h"
void fun(int n)
{
   int idx;
   int arr1[n] = {0};
   int arr2[n];
   for (idx=0; idx<n; idx++)
       arr2[idx] = 0;   
}

  int main()
{
   fun(4);
   return 0;
}

Solution:

There’s no issue with definition of arr1 and arr2. In definition of these arrays, the mention of array size using variable is ok as per C standard but these types of arrays can’t be initialized at the time of definition. That’s why initialization of arr1 is incorrect. But initialization of arr2 is done correctly. Right answer is C.

QUESTION: 12

What is right way to Initialize array?

Solution:
QUESTION: 13

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.

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.

Solution:
QUESTION: 14

Let x be an array. Which of the following operations are illegal?
I. ++x
II. x+1
III. x++
IV. x*2

Solution:
QUESTION: 15

Consider the C program given below :

#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 ("%dn", maxsum);

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

Solution:

If you look for loop carefully, you will notice that it assigns sum variable to some value in if condition and increments it in the else condition. On further thought, it would be clear that this loop stores sum of increasing subsequence of positive integers in sum variable and max of sum in maxsum. Hence, maxsum - maximum sum of increasing subsequence of positive integers will get printed out when this program is executed, which is 3 + 4 = 7. This solution is contributed by Vineet Purswani //output will be 3+4 =7 {for || if 1st argument is true 2nd argument will not be calculated, and if 1st argument is false, 2nd argument will be calculated}

Another Solution : When i=1 -> i==0 is false, but a[i]<0 is true so condition (1) is true.Now if (sum > maxsum) is true, since sum=2 and maxsum=0.So maxsum=2. sum = (a [i] > 0) ? a [i] : 0; , sum=0 since a[i]<0.
When i=2 -> i==0 is false, a[i]<0 is true and so condition (1) is true.Now if (sum > maxsum) is false, since sum=0 and maxsum=2.Since sum = (a [i] > 0) ? a [i] : 0; , sum=0 since a[i]<0.
When i=3 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is false so condition (1) is false. Now sum += a [i] = 3.
When i=4 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is false so condition (1) is false. Now sum += a [i] = 7.
When i=5 -> i==0 is false , a[i]<0 is false and a [i] < a [i – 1] is true so condition (1) is true. sum > maxsum is true, since sum=7 and maxsum=2,so maxsum=7.Since sum = (a [i] > 0) ? a [i] : 0, so sum=2 since a[5]>0.

Related tests