Arrays
Question 1
CORRECT
|
Predict the output of below program:
#include <stdio.h> int main() { int arr[5]; // Assume that base address of arr is 2000 and size of integer // is 32 bit arr++; printf ( "%u" , arr); return 0; } |
2002
| |
2004
| |
2020
| |
lvalue required
|
Discuss it
Question 1 Explanation:
Array name in C is implemented by a constant pointer. It is not possible to apply increment and decrement on constant types.
Question 2
CORRECT
|
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; } |
2004 2020
| |
2004 2004
| |
2004 Garbage value
| |
The program fails to compile because Address-of operator cannot be used with array name
|
Discuss it
Question 2 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 theAddress-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.
Question 3
WRONG
|
What is output?
# include <stdio.h> void print( int arr[]) { int n = sizeof (arr)/ sizeof (arr[0]); int i; for (i = 0; i < n; i++) printf ( "%d " , arr[i]); } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; print(arr); return 0; } |
1, 2, 3, 4, 5, 6, 7, 8
| |
Compiler Error
| |
1
| |
Run Time Error
|
Discuss it
Question 3 Explanation:
See http://www.geeksforgeeks.org/using-sizof-operator-with-array-paratmeters/ for explanation.
Question 4
CORRECT
|
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; } |
1
| |
2
| |
6
| |
Runtime Error
|
Discuss it
Question 4 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.
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
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++]; } |
only (i)
| |
only (ii)
| |
either (i) or (ii) but not both
| |
neither (i) nor (ii)
|
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
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)?
int *A [10], B[10][10]; |
I, II, and IV only
| |
II, III, and IV only
| |
II and IV only
| |
IV only
|
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; /* No problem with below statement also as array style indexing can be done with pointers*/ A[2][3] = 15; /* Simple assignment to an element of a 2D array*/ B[2][3] = 15; printf("%d %d", A[2][0], A[2][3]); return 0; }
Question 7
CORRECT
|
Consider the following declaration of a ‘two-dimensional array in C:
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)
char a[100][100]; |
4040
| |
4050
| |
5040
| |
5050
|
Discuss it
Question 7 Explanation:
Address of a[40][50] = Base address + 40*100*element_size + 50*element_size = 0 + 4000*1 + 50*1 = 4050
Question 8
WRONG
|
Which of the following is true about arrays in C.
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.
| |
2D arrays are stored in column major form
|
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; } void printArr( int * arr, int n) { int i; for (i = 0; i < n; ++i) printf ( "%d " , arr[i]); } int main() { int arr[] = {10, 20, 30}; int size = SIZE(arr); fun(arr, size); printArr(arr, size); return 0; } |
20 30 40
| |
20 20 40
| |
50 20 40
| |
Compile-time error
|
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
WRONG
|
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 values
| |
1 0 0 0 0
| |
1 1 1 1 1
| |
0 0 0 0 0
|
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};
Question 11
CORRECT
|
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.
| |
The always prints 0 five times followed by garbage value
| |
The program always crashes.
| |
The program may print 0 five times followed by garbage value, or may crash if address (arr+5) is invalid.
|
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; } |
1 2 3 4
| |
Compiler Error in line " int a[][] = {{1,2},{3,4}};"
| |
4 garbage values
| |
4 3 2 1
|
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
WRONG
|
#include<stdio.h> int main() { int a[10][20][30] = {0}; a[5][2][1] = 2; return 0; } |
printf("%d",*(((a+5)+2)+1));
| |
printf("%d",***((a+5)+2)+1);
| |
printf("%d",*(*(*(a+5)+2)+1));
| |
None of these
|
Discuss it
Question 14
CORRECT
|
#include <stdio.h> int main() { char p; char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8}; p = (buf + 1)[5]; printf ( "%d\n" , p); return 0; } |
5
| |
6
| |
9
| |
None of the above
|
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]”.
| |
X is declared as “int X[4][1024][32]”.
| |
X is declared as “char X[4][32][8]”.
| |
X is declared as “char X[32][16][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*4Since 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*j*k + j*k + 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*k*4 = j*32, we get k = 8 i*1024 = i*j*k*4, we get j = 1024/32 = 32Therefore option A is the only correct option as j and k are 32 and 8 only in option A.
No comments:
Post a Comment