C Array
C Array
h>
int main()
{
int arr[5];
return 0;
}
A 2002
B 2004
2020
lvalue required
Explanation:
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;
}
2004 2020
2004 2004
2004 Garbage value
The program fails to compile because
Address-of operator cannot be used with
array name
Explanation:
Name of array in C gives the address(except in sizeof operator) of the first element. Adding 1
to this address gives the address plus the sizeof type the array has. Applying the Address-of
operator before the array name gives the address of the whole array. Adding 1 to this address
gives the address plus the sizeof whole array.
What is output?
# include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr);
return 0;
}
1, 2, 3, 4, 5, 6, 7, 8
B Compiler Error
12
D Run Time Error
C Arrays
Discuss it
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
6
D Runtime Error
Explanation:
&a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] +
sizeof(a)”. And this value is typecasted to int *. So ptr points the memory just after 6 is
stored. ptr is typecasted to "int *" and value of *(ptr-1) is printed. Since ptr points memory
after 6, ptr – 1 points to 6.
Question 5
Wrong
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++];
}
Which of the following condition(s) hold(s) after the termination of the while loop? (GATE
CS 2006) (i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n (ii) i < n, k = m+i-1, and b[m-1] <=
a[i] if j = m
A only (i)
only (ii)
either (i) or (ii) but not both
D neither (i) nor (ii)
C Arrays
Discuss it
Question 5 Explanation:
The function xyz() is similar to merge() of mergeSort(). The condition (i) is true if the last
inserted element in c[] is from a[] and condition (ii) is true if the last inserted element is from
b[].
Question 6
Wrong
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)?
I, II, and IV only
B II, III, and IV only
II and IV only
D IV only
C Arrays
Discuss it
Question 6 Explanation:
See following for explanation.
int main()
{
int *A[10], B[10][10];
int C[] = {12, 11, 13, 14};
/* No problem with below statement as A[2] is a pointer
and we are assigning a value to pointer */
A[2] = C;
Question 7 Explanation:
Address of a[40][50] = Base address + 40*100*element_size + 50*element_size
= 0 + 4000*1 + 50*1
= 4050
Based on row major or column major
if row major then the result will be 4050
if column major then
Address of a[40][50] = Base address + 50*100*element_size +
40*element_size
= 0 + 5000*1 + 40*1
= 5040
Question 8
Wrong
Which of the following is true about arrays in C.
A For every type T, there can be an array of T.
For every type T except void and function type, there can be an array of T.
When an array is passed to a function, C compiler creates a copy of array.
D 2D arrays are stored in column major form
C Arrays
Discuss it
Question 8 Explanation:
In C, we cannot have an array of void type and function types. For example, below program
throws compiler error
int main()
{
void arr[100];
}
But we can have array of void pointers and function pointers. The below program works fine.
int main()
{
void *arr[100];
}
See examples of function pointers for details of array function pointers.
Question 9
Correct
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
50 20 40
D Compile-time error
C Arrays
Discuss it
Question 9 Explanation:
The crux of the question lies in the expression: *arr += *(arr + n - 1) += 10; The composite
operator (here +=) has right to left associativity. First 10 is added to the last element of the
array. The result is then added to the first element of the array.
Question 10
Correct
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;
}
1 followed by four garbage
A
values
10000
C 11111
D 00000
C Arrays
Discuss it
Question 10 Explanation:
In C/C++, if we initialize an array with fewer members, all remaining members are
automatically initialized as 0. For example, the following statement initializes an array of size
1000 with values as 0.
int arr[1000] = {0};
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;
}
Compiler Error: Array index out of bound.
B The always prints 0 five times followed by garbage value
C The program always crashes.
The program may print 0 five times followed by garbage
value, or may crash if address (arr+5) is invalid.
C Arrays
Discuss it
Question 12
Wrong
#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 1234
Compiler Error in line " int a[][] =
{{1,2},{3,4}};"
C 4 garbage values
4321
C Arrays
Discuss it
Question 12 Explanation:
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 13
Correct
#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);
printf("%d",*(*(*(a+5)+2)+1));
D None of these
C Arrays
Discuss it
Question 14
Wrong
#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
6
9
D None of the above
C Arrays
Discuss it
Question 15
Wrong
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?
X is declared as “int X[32][32][8]”.
B X is declared as “int X[4][1024][32]”.
X is declared as “char X[4][32][8]”.
D X is declared as “char X[32][16][2]”.
C Arrays GATE-CS-2014-(Set-2)
Discuss it
Question 15 Explanation:
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 16
Correct
What’s the meaning of following declaration in C language?
int (*p)[5];
It will result in compile error because there shouldn't be any parenthesis i.e.
A
“int *p[5]” is valid.
B p is a pointer to 5 integers.
C p is a pointer to integer array.
D p is an array of 5 pointers to integers.
p is a pointer to an array of 5 integers
C Arrays C Quiz - 106
Discuss it
Question 16 Explanation:
Here p is basically a pointer to integer array of 5 integers. In case of “int *p[5]”, p is array of
5 pointers to integers.
Question 17
Wrong
For the following declaration of a function in C, pick the best statement
int [] fun(void (*fptr)(int *));
It will result in compile error.
No compile error. fun is a function which takes a function pointer fptr as
B
argument and return an array of int.
No compile error. fun is a function which takes a function pointer fptr as
C argument and returns an array of int. Also, fptr is a function pointer
which takes int pointer as argument and returns void.
No compile error. fun is a function which takes a function pointer fptr as
argument and returns an array of int. The array of int depends on the
body of fun i.e. what size array is returned. Also, fptr is a function
pointer which takes int pointer as argument and returns void.
C Arrays C Quiz - 107
Discuss it
Question 17 Explanation:
As per C standard, a function can’t have an explicit array as return type. That’s why the
above would result in compile error. There’re indirect ways if we need an array as an output
of a function call. For example, a pointer can be returned by function by return statement
while providing the size of array via other means. Alternatively, function argument can be
used for this.
Question 18
Correct
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
FALSE
C Arrays C Quiz - 108
Discuss it
Question 18 Explanation:
First thing first, sizeof() operator works at compile time. So usage of sizeof on arr in
sourcefile2.c won’t work because arr in sourcefile2.c is an incomplete type. Please note that
arr in sourcefile1.c is a complete type because size of array got determined at compile time
due to initialization.
Question 19
Correct
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
It’ll always print Equal.
C It’ll always print Not Equal.
Since elements of arrPtr aren’t initialized in the program,
D
it’ll print either Equal or Not Equal.
C Arrays C Quiz - 109
Discuss it
Question 19 Explanation:
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 20
Wrong
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};
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) */
Pick the correct statements.
Only (i) is correct.
Only (i) and (ii) are correct.
C Only (i), (ii) and (iii) are correct.
D All (i), (ii), (iii) and (iv) are correct.
C Arrays C Quiz - 110
Discuss it
Question 20 Explanation:
First of all, C language doesn’t provide any true support for 2D array or multidimensional
arrays. A 2D array is simulated via 1D array of arrays. So a 2D array of int is actually a 1D
array of array of int. Another important point is that array size can be derived from its
initialization but that’s applicable for first dimension only. It means that 2D array need to
have an explicit size of 2nd dimension. Similarly, for a 3D array, 2nd and 3rd dimensions
need to have explicit size. That’s why only (i) and (ii) are correct. But array2D[2][] and
array2D[][] are of incomplete type because their complete size can’t derived even from the
initialization.
You have completed 20/37 questions .
Question 21 Explanation:
In C, initialization of array can be done for selected elements as well. By default, the
initializer start from 0th element. Specific elements in array can be specified by []. It should
be noted that the remaining elements (i.e. the ones not mentioned in array initialization)
would be initialized to 0. For example, “int arr[10] = {100, [5]=100,[9]=100}” is also legal in
C. This initializes arr[0], arr[5] and arr[9] to 100. All the remaining elements would be 0.
Question 22
Wrong
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;
}
Definition of both arr1 and arr2 is incorrect because variable is
used to specify the size of array. That’s why compile error.
Apart from definition of arr1 arr2, initialization of arr1 is also
B incorrect. arr1 can’t be initialized due to its size being specified as
variable. That’s why compile error.
Initialization of arr1 is incorrect. arr1 can’t be initialized due to its
size being specified as variable. That’s why compile error.
No compile error. The program would define and initializes both
D
arrays to ZERO.
C Arrays C Quiz - 111
Discuss it
Question 22 Explanation:
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 23
Wrong
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;
}
No compile error and it’ll print “Initialized to ZERO”.
B 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”
D or “Not initialized to ZERO” depending on what value is
present at arr[0] at a particular run of the program.
C Arrays C Quiz - 111
Discuss it
Question 23 Explanation:
An array whose size is specified as variable can’t be defined out any function. It can be
defined only inside a function. So putting arr[size] outside main() would result in compile
error. Answer is C.
Question 24
Wrong
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");
Question 24 Explanation:
Please see the link below for full explanation http://www.geeksforgeeks.org/find-a-pair-with-
the-given-difference/
Question 25
Wrong
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
Question 26 Explanation:
#include<iostream>
int max(int *p, int n)
{
int a=0, b=n-1;
while (a!=b)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];
}
int main()
{
int arr[] = {10, 5, 1, 40, 30};
int n = sizeof(arr)/sizeof(arr[0]);
std::cout << max(arr, 5);
}
Question 27
Correct
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?
A9
B8
7
D6
C Arrays C Quiz - 113 Gate IT 2007
Discuss it
Question 27 Explanation:
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. This
solution is contributed by nirmal Bharadwaj
Question 28
Correct
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
Null String
C dlrld
D worow
C Arrays C Quiz - 113 Gate IT 2008
Discuss it
Question 28 Explanation:
As at the base address or starting of the string "Null" is placed, so while reading array if Null
comes it assumes that this is the end of array, so it terminates here only.
Question 29
Wrong
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
2, 4
3, 2
D 3, 3
C Arrays C Quiz - 113 Gate IT 2008
Discuss it
Question 29 Explanation:
Be careful about the scope of i,
there are two variables named: i, with different scope. There are 2 main points to consider
while solving this question. Scope of variable i and integer division. First for loop will run for
i = 0, 2 and 4 as i is incremented twice inside loop and resultant array will be a = 2, 2, 4, 4, 5,
6, 7, 8 (Loop will terminate at i = 4) After that i value is 3 as there is a decrement operation
after for loop. Next for loop is running for j = 7, 6 and 5 and corresponding i values which is
a local variable inside for loop will be 3 (7/2), 3 (6/2) and 2 (5/2). Array after this for loop
will be a = 2, 2, 3, 2, 5, 6, 7, 8 After the for loop, current i value is 3 and element at a[3] = 2.
This solution is contributed by Pranjul Ahuja.
Question 30
Wrong
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?
ab
cd
ef
ad
be
cf
ac
Ceb
df
ae
Ddc
bf
C Arrays C Quiz - 113 Gate IT 2008
Discuss it
Question 30 Explanation:
*p= a[0][0]
*(p+2) = a[0][1] *(p+4) = a[0][2] *(p+1) = a[1][0] *(p+3) = a[1][1] *(p+5) = a[1][2]