6.arrays - 37
6.arrays - 37
int main()
{
int arr[5];
return 0;
}
A 2002
B 2004
C 2020
D lvalue required
ANS :-D
Question 2
Predict the output of below program:
#include <stdio.h>
int main()
{
int arr[5];
// Assume base address of arr is 2000 and size of integer is 32 bit
printf("%u %u", arr + 1, &arr + 1);
return 0;
}
A 2004 2020
B 2004 2004
C 2004 Garbage value
D The program fails to compile because Address-of operator cannot be
used with array name
ANS :- A
Question 3
What is output?
# include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr);
return 0;
}
A 1, 2, 3, 4, 5, 6, 7, 8
B Compiler Error
C 1 2
D Run Time Error
ANS :- C
Question 4
Output of following program?
#include<stdio.h>
int main()
{
int a[] = {1, 2, 3, 4, 5, 6};
int *ptr = (int*)(&a+1);
printf("%d ", *(ptr-1) );
return 0;
}
A 1
B 2
C 6
D Runtime Error
ANS :- C
Question 5
Consider the following C-function in which a[n] and b[m] are two sorted
integer arrays and c[n + m] be another integer array.
void xyz(int a[], int b [], int c[])
{
int i, j, k;
i = j = k = O;
while ((i<n) && (j<m))
if (a[i] < b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}
A only (i)
B only (ii)
C either (i) or (ii) but not both
D neither (i) nor (ii)
ANS :- C
Question 6
Assume the following C variable declaration
int *A [10], B[10][10];
Of the following expressions I A[2] II A[2][3] III B[1] IV B[2][3] which will
not give compile-time errors if used as left
hand sides of assignment statements in a C program (GATE CS 2003)?
ANS :- A
Question 7
Consider the following declaration of a �two-dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is
stored starting from memory address 0, the address of a[40][50] is (GATE CS 2002)
A 4040
B 4050
C 5040
D 5050
ANS :- B
Question 8
Which of the following is true about arrays in C.
A For every type T, there can be an array of T.
B For every type T except void and function type, there can be an array
of T.
C When an array is passed to a function, C compiler creates a copy of
array.
D 2D arrays are stored in column major form
ANS :- B
Question 9
Predict the output of the below program:
#include <stdio.h>
#define SIZE(arr) sizeof(arr) / sizeof(*arr);
void fun(int* arr, int n)
{
int i;
*arr += *(arr + n - 1) += 10;
}
int main()
{
int arr[] = {10, 20, 30};
int size = SIZE(arr);
fun(arr, size);
printArr(arr, size);
return 0;
}
A 20 30 40
B 20 20 40
C 50 20 40
D Compile-time error
ANS :- C
Question 10
Predict output of following program
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
ANS :- B
Question 11
Does C perform array out of bound checking? What is the output of the
following program?
int main()
{
int i;
int arr[5] = {0};
for (i = 0; i <= 5; i++)
printf("%d ", arr[i]);
return 0;
}
ANS :- D
Question 12
#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;
}
A 1 2 3 4
B Compiler Error in line " int a[][] = {{1,2},{3,4}};"
C 4 garbage values
D 4 3 2 1
ANS :- B
Question 13
#include<stdio.h>
int main()
{
int a[10][20][30] = {0};
a[5][2][1] = 2;
return 0;
}
Which of the following will print the value 2 for the above code?
A printf("%d",*(((a+5)+2)+1));
B printf("%d",***((a+5)+2)+1);
C printf("%d",*(*(*(a+5)+2)+1));
D None of these
ANS :- C
Question 14
#include <stdio.h>
int main()
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%dn", p);
return 0;
}
A 5
B 6
C 9
D None of the above
ANS :- C
Question 15
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?
ANS :- A
Question 16
What�s the meaning of following declaration in C language?
int (*p)[5];
ANS :- E
Question 17
For the following declaration of a function in C, pick the best statement
int [] fun(void (*fptr)(int *));
ANS :- A
Question 18
In a C file (say sourcefile1.c), an array is defined as follows. Here, we
don�t need to mention arrary arr size explicitly in [] because the size would be
determined by the number of elements used in the initialization.
int arr[] = {1,2,3,4,5};
In another C file (say sourcefile2.c), the same array is declared for usage
as follows:
extern int arr[];
In sourcefile2.c, we can use sizeof() on arr to find out the actual size of
arr.
A TRUE
B FALSE
ANS :- B
Question 19
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;
}
A Compile Error
B It�ll always print Equal.
C It�ll always print Not Equal.
D Since elements of arrPtr aren�t initialized in the program, it�ll
print either Equal or Not Equal.
ANS :- B
Question 20
In C, 1D array of int can be defined as follows and both are correct.
int array1D[4] = {1,2,3,4};
int array1D[] = {1,2,3,4};
Run on IDE
But given the following definitions (along-with initialization) of 2D arrays
int array2D[2][4] = {1,2,3,4,5,6,7,8}; /* (i) */
int array2D[][4] = {1,2,3,4,5,6,7,8}; /* (ii) */
int array2D[2][] = {1,2,3,4,5,6,7,8}; /* (iii) */
int array2D[][] = {1,2,3,4,5,6,7,8}; /* (iv) */
ANS :- B
Question 21
Pick the best statement for the below:
int arr[50] = {0,1,2,[47]=47,48,49};
ANS :- B
Question 22
Pick the best statement for the below program:
#include "stdio.h"
void fun(int n)
{
int idx;
int arr1[n] = {0};
int arr2[n];
int main()
{
fun(4);
return 0;
}
ANS :- C
Question 23
Pick the best statement for the below program:
#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;
}
ANS :- C
Question 24
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");
ANS :- B
Question 25
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
a[i] = b [i] => c[2i] = a [i]
a[i] = b [i] => c[2i] = b [i]
a[i] = b [i] => c[2i] = a [i]
a[i] = b [i] => c[2i] = b [i]
Which of the following is TRUE?
A only I and II
B only I and IV
C only II and III
D only III and IV
ANS :- C
Question 26
The following function computes the maximum value contained in an integer
array p[] of size n (n >= 1)
int max(int *p, int n)
{
int a=0, b=n-1;
while (__________)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];
}
A a != n
B b != 0
C b > (a + 1)
D b != a
ANS :- D
Question 27
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);
A 9
B 8
C 7
D 6
ANS :- C
Question 28
What is the output printed by the following C code?
# include <stdio.h>
int main ()
{
char a [6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a [i++] = a [j--]);
printf ("%sn", a);
}
/* Add code here. Remove these lines if not writing code */
A dlrow
B Null String
C dlrld
D worow
ANS :- B
Question 29
Consider the C program given below. What does it print?
#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 */
A 2, 3
B 2, 4
C 3, 2
D 3, 3
ANS :- C
Question 30
C program is given below:
# include <stdio.h>
int main ()
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
}
/* Add code here. Remove these lines if not writing code */
What should be the contents of the array b at the end of the program?
A
a b
c d
e f
B
a d
b e
c f
C
a c
e b
d f
D
a e
d c
b f
ANS :- B
Question 31
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 ?
A 50
B 25
C 75
D 100
ANS :- A
Question 32
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 ?
A N-1
B N*(N-1)/2
C N
D Not possible to determine the bit array
ANS :- C
Question 33
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] ?
A 15i+ j+ 84
B 15j+ i+ 84
C 10i+ j+ 89
D 10j+ i+ 89
ANS :- A
Question 34
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.
Question 35
Let A(1:8, -5:5, -10:5) be a three dimensional array. How many elements are
there in the array A?
A 1200
B 1408
C 33
D 1050
ANS :- B
Question 36
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?
A 1
B 2
C N/2
D 2N-1
ANS :- A
Question 37
A one dimensional array A has indices 1....75. Each element is a string and
takes up three memory words. The array is stored at location 1120 decimal.
The starting address of A[49] is
A 1267
B 1164
C 1264
D 1169
ANS :- C