Arrays


Question 1
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;
}

A
2002
B
2004
C
2020
D
lvalue required
Arrays    
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
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
Arrays    
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 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.
Question 3
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;
}
A
1, 2, 3, 4, 5, 6, 7, 8
B
Compiler Error
C
1
D
Run Time Error
Arrays    
Discuss it


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
Arrays    
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
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)
B
only (ii)
C
either (i) or (ii) but not both
D
neither (i) nor (ii)
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
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)?
A
I, II, and IV only
B
II, III, and IV only
C
II and IV only
D
IV only
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;  
  
  /* 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
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
Arrays    
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
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
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
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;
}
A
20 30 40
B
20 20 40
C
50 20 40
D
Compile-time error
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
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;
}
A
1 followed by four garbage values
B
1 0 0 0 0
C
1 1 1 1 1
D
0 0 0 0 0
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};  
There are 33 questions to complete.

Company Wise Coding Practice    Topic Wise Coding Practice