Exercise: Questions on Arrays


Questions on 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;
}


2002
2004
2020
lvalue required
Show Answer
Questions on Arrays : 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;
} 

2004 2020
2004 2004
2004 Garbage value
The program fails to compile because Address-of operator cannot be used with array name
Show Answer
Questions on Arrays : 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;
}

1, 2, 3, 4, 5, 6, 7, 8
Compiler Error
1
Run Time Error
Show Answer
Questions on Arrays : 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; 
}

1
2
6
Runtime Error
Show Answer
Questions on Arrays : 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

only (i)
only (ii)
either (i) or (ii) but not both
neither (i) nor (ii)
Show Answer
Questions on Arrays : 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)?

I, II, and IV only
II, III, and IV only
II and IV only
IV only
Show Answer
Questions on Arrays : 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)

4040
4050
5040
5050
Show Answer
Questions on Arrays : Question 8 :
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
Show Answer
Questions on Arrays : 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;
}

20 30 40
20 20 40
50 20 40
Compile-time error
Show Answer
Questions on Arrays : 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;
}

1 followed by four garbage values
1 0 0 0 0
1 1 1 1 1
0 0 0 0 0
Show Answer