Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C Arrays - Aptitude Questions and Answers
C programming Arrays (One-D Array, Two-D Array) Aptitude Questions and Answers : In this section you will find C Aptitude Questions and Answers on One Dimensional (1D) and Two Dimensional (2D) array.
List of C programming Array (One, Two Dimensional) Aptitude Questions and Answers
1) What will be the output of following program ?
#include <stdio.h>
int main()
{
static int var[5];
int count=0;
var[++count]=++count;
for(count=0;count<5;count++)
printf("%d ",var[count]);
return 0;
}
- 0 1 0 0 0
- 0 2 0 0 0
- 0 0 2 0 0
- 0 0 0 0 0
Correct Answer - 3
0 0 2 0 0
2) What will be the output of following program ? (for 32 bits compiler)
#include <stdio.h>
int main()
{
int MAX=10;
int array[MAX];
printf("size of array is = %d",sizeof(array);
return 0;
}
- size of array is = 20
- size of array is = 40
- size of array is = 4
- Error
Correct Answer - 2
size of array is = 40
3) What will be the output of following program ?
#include <stdio.h>
#define MAX 10
int main()
{ int array[MAX]={1,2,3},tally;
for(tally=0;tally< sizeof(array)/sizeof(int);tally+=1)
printf("%d ",*(tally+array));
return 0;
}
- Error
- 1 3 4 5 6 7 8 9 10 11
- 1 2 3 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0
Correct Answer - 3
1 2 3 0 0 0 0 0 0 0.
You can also access the array elements using *(counter_variable+array_name).
4) What will be the output of following program ?
#include <stdio.h>
int main()
{ static int x[]={'A','B','C','D','E'},tally;
for(tally=0;tally< sizeof(x)/sizeof(int) ; tally+=1)
printf("%c,%c,%c\n",*(x+tally)+1,x[tally]+1,*(tally+x)+1);
return 0;
}
- Error
- A,A,A
B,B,B
C,C,C
D,D,D
E,E,E
- B,B,B
C,C,C
D,D,D
E,E,E
F,F,F
- E,E,E
D,D,D
C,C,C
B,B,B
A,A,A
Correct Answer - 3
B,B,B
C,C,C
D,D,D
E,E,E
F,F,F
5) What will be the output of following program ?
#include <stdio.h>
int main()
{ static int array[]={10,20,30,40,50};
printf("%d...%d",*array,*(array+3)* *array);
return 0;
}
- Error
- 10...40
- 10...300
- 10....400
Correct Answer - 4
10...400
In expression printf("%d...%d",*array,*(array+3)* *array);, *array is 10, *(array+3) is 40.
6) What will be the output of following program ?
#include <stdio.h>
int main()
{ int a[5]={1,2,3,4,5},b[5]={10,20,30,40,50},tally;
for(tally=0;tally< 5;++tally)
*(a+tally)=*(tally+a)+ *(b+tally);
for(tally=0;tally< 5;tally++)
printf("%d ",*(a+tally));
return 0;
}
- 1 2 3 4 5
- 10 20 30 40 50
- 11 22 33 44 55
- Error
Correct Answer - 3
11 22 33 44 55
This is a simple program to add elements of two arrays, you can access array elements using
*(tally+a) Or *(b+tally) Or a[tally] .
7) What will be the output of following program ?
#include <stdio.h>
int main()
{ int a[5]={0x00,0x01,0x02,0x03,0x04},i;
i=4;
while(a[i])
{
printf("%02d ",*a+i);
--i;
}
return 0;
}
- 00 01 02 03 04
- 04 03 02 01 00
- 04 03 02 01
- 01 02 03 04
Correct Answer - 3
04 03 02 01
0x00,0x01,0x02,0x03,0x04,0x05 are hex values of 0,1,2,3,4,5.
while(a[i]) will be terminated by a[0], becuase value of a[0] is 0 hence, 04,03,03,01 will print.
8) What will be the output of following program ?
#include <stdio.h>
int main()
{
char X[10]={'A'},i;
for(i=0; i<10; i++)
printf("%d ",X[i]);
return 0;
}
- A 0 0 0 0 0 0 0 0 0
- A
- A 32 32 32 32 32 32 32 32 32
- ERROR
Correct Answer - 1
A 0 0 0 0 0 0 0 0 0
char X[10]={'A'}; 0th index of X is assigned by 'A' and rest of elements
is assigned by 0.
9) Which is an incorrect declaration of one dimensional array ?
- int x[5];
- int x[5]={1,2,3,4,5};
- int x[5]={1,2};
- int x[];
Correct Answer - 4
int x[];
You can ignore value within the subscript [] when you are initialising array with elements, but here no initialisation found.