C Technical Interview Questions on Arrays

These C Technical Interview Questions on Arrays post providing basic, intermediate and Hard level examples in C Arrays. The Practice of these examples makes you good in C programming.


What is the output of following C program?
#include <stdio.h>
int main()
{
int a[3] = {10, 20, 30};
int *p = a;
printf(“%u\t%u”,p,&a[0]);
return 0;
}

  1. Print same address 2 times
  2. Print 2 consecutive memory locations
  3. Memory location , 10
  4. 10, 10

Answer :

Ans: 1 , Both are pointing to base address of array memory block

What is the output of following C program?
#include <stdio.h>
int main()
{
int a[3] = {10, 20, 30};
int *p = a + 2;
printf(“%d\n”, p[-2]);
return 0;
}

  1. 20
  2. Compile time error
  3. 30
  4. 10

Answer :

Ans: 4, Initially ‘p’ points to 30, but we are accessing p[-2]. hence it access 10

What is the output of following C program?
#include <stdio.h>
int main()
{
int a[3] = {10, 20, 30};
int *p = a + 1;
printf(“%d %d\n”, p[-1], a[*p-20]+1);
return 0;
}

  1. 20, 20
  2. 20, 30
  3. 10,11
  4. 20,11

Answer :

Ans: 3

What is the output of following C program?
#include<stdio.h>
int main()
{
int size = 3;
int a[size] = {1, 2, 3};
printf(“%d \n”, a[2] + a[0]);
return 0 ;
}

  1. 4
  2. Garbage value
  3. Compile time error
  4. None of the above

Answer :

Ans: 3 , we cannot specify the size of array unless it is constant.

What is the output of following C program?
#include <stdio.h>
int main()
{
int a[2][2] = {10,20,30,40};
printf(“%d”, *(*(a+1)+0));
return 0;
}

  1. 30
  2. 10
  3. Garbage value
  4. Compile time error

Answer :

Ans: 1 , It is pointing to second row first column

What is the output of following C program?
#include <stdio.h>
void demo(int a[][2])
{
int i=0, j=1;
a[0][1] = 20;
printf(“%d”, a[i][0]+a[i][j]);
}
int main()
{
int a[2][2] = {10};
demo(a);
return 1;
}

  1. 10
  2. Compile time error
  3. 30
  4. Garbage value

Answer :

Ans: 3

What is the output of following C program?
#include<stdio.h>
int main()
{
int arr[5], i=0;
while(i<5)
arr[i] = i++ + 2;
for(i=0; i<5; i++)
printf(“%d\t”, arr[i]);
return 0;
}

  1. 2 to 6
  2. 1 to 6
  3. 2 to 5
  4. 2 to 5 and Garbage value

Answer :

Ans: 1

Arrays – Technical – Part 2

Share this