• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Arrays

Question 1

Predict the output of below program: C
 #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

Question 2

Predict the output of below program: C
#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

Question 3

What is the output for a 64-bit Compiler?

C
# 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 2

  • Run Time Error

Question 4

Output of following program? C
#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

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. 

C
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)

Question 6

Assume the following C variable declaration C
 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

Question 7

Consider the following declaration of a ‘two-dimensional array in C: 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

Question 8

Predict the output of the below program: C
#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

Question 9

Predict output of following program C
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

Question 10

Does C perform array out of bound checking? What is the output of the following program? 

C
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.

There are 37 questions to complete.

Last Updated :
Take a part in the ongoing discussion