Friday 30 October 2015

Array Aptitude Questions in C with Answers

  Array Aptitude Questions in C with Answers

1.What is the output of the following code?
 #include<stdio.h>
void main()
{
      int arr[];
      arr[1]=10;
       printf(“%d”,arr[0]);
}
Answers:-Error
Explanation:- in Declaration of array size must be required in specify.

2.What is the output of the following code?
    #include<stdio.h>
    void main()
   {
      int arr[2]={10,20,30,40};
      printf(“%d”,*(arr+2) );
   }
Answers:-Error
Explanation:- In Array initialization we can’t initialize more then array size elements.

3.What is the output of the following code?
#include<stdio.h>
 void main()
{
   int arr[2]={10,20};
   arr[2]=30;
   arr[3]=40;
   printf(“%d”,*(arr+3));
}
Answers:-40
Explanation:- In c language array upper boundary checking is not consider.  


4.What is the output of the following code?
void main()
{
   int arr[2]={10,20};
   *arr=30;
   *(arr+1)=40;
   printf(“%d %d”,arr[0],1[arr]);
}
Answers: 30 40
                                       arr[0] ——->*(arr+0)
                                        1[arr] ——>*(1+arr)

 5. What is the output of the following code?
void main()
{
    int a=1;
   void xyz(int,int);
    xyz(++a,a++);
    xyz(a++,++a);
    printf(“n%d”,a);
}
void xyz( int x,int y)
{
    printf(“n%d %d”,x,y);
 }
Answers: 3 1
                     4 3
                      5
Explanation:- Default calling conversion of any function is cdecl.

6.What is the output of the following code?
void abc()
{
    ++a;
}
void main()
{
  int a=1;
  abc();
  printf(“n%d”,a);
}
Answers: Error
Explanation:- By default any kind of variable storage class specifier is auto and scope of auto variable is restricted in body.

7.What is the output of the following code?
void display(int a)
{
    printf(“%d “,a);
    if(–a)
    display(a);
    printf(“%d “,a);
}
void main()
{
    display(4);
}
Answers: 4 3 2 1 0 1 2 3

8.What is the output of the following code?
void xyz(int x)
{
    printf(“n%d”,x);
    if(x)
    xyz(x-1);
    printf(“n%d”,x);
}
void abc(int a)
{
    printf(“n%d”,a);
    xyz(a);
    if(a)
    abc(a-1);
    printf(“n%d”,a);
}
void main()
{
    abc(2);
}
Answers: 

9.What is the output of the following code?
#include <stdio.h>
void main()
{  
      static int a=1;
      printf(“n%d”,++a);
      if(a<=3)
        main();
      printf(“n%d”,a);
}
Answers: 2 3 4 4 4 4

10.What is the output of the following code?
#include <stdio.h>
void main()
   int a=32767,b;
   char *ptr;
   ptr=&a;
   b=*ptr;
   printf(“n%d %d”,a,b);
}
 Answers: 32767 -1
Explanation:- on integer variable when we are using pointer to char it can access low order bye data only.


 11. What is the output of the following code?
void main()
{
   int a=10;
   int *p=&a;
   int **pp=&p;
   ++*p;
   ++**pp;
   a*=a-2;
   printf(“n%d “,a);
}
 Answers: 100

12.What is the output of the following code?
void change(int p1,int *p2)
    ++p1;
    ++*p2;
}
void main()
       int x=10,y=20;
       change(x,&y);
        printf(“n%d %d”,x,y);
}
Answers: 10 21

13.What is the output of the following code?
void main()
{
   int a[]={7,18,29};
   int *p;
   p=a;
   ++p;
   printf(“%d %d %d”,p[-1],p[0],p[1]);
}
Answers: 7 18 29

14.What is the output of the following code?
void xyz(int *ptr)
{
   ++*ptr;
   if(*ptr<=10)
   xyz(++ptr);
   ++*ptr;
}
void main()
{
   int arr[]={2,4,8,14,16};
   int i;
   xyz(arr+1);
   for(i=0;i<5;i++)
   printf(“%d “,arr[i]);
}
Answers: 2 5 10 17 16

15.What is the output of the following code?
 void main()
{
   int arr[4][8]={10,20,30};
   printf(“n%d “,sizeof(arr));
   printf(“n%d “,sizeof(arr[1]));
   printf(“n%d “,sizeof(arr[1][0]));
}
Answers: 64 16 2

16.What is the output of the following code?
          void main()
          {
                int arr[4][8]={10,20,30};
                printf(“n%d “,&arr[3][4]-&arr[1][2]);
           }
Answers: 18

17.What is the output of the following code?
          void main()
   {
   int arr[3][]={
                                  {7,8,9},
                                  {3,13},
                                 {10,20,30}
                        };
                printf(“n%d “,arr[1][1]);
             }
Answers: Error
Explanation:- in initialization of 2D-array column size must be required.

18.What is the output of the following code?
           void main()
   {
   int arr[][4]={
                {7,8,9},
                {3,13},
                {10,20,30}
         };
   printf(“n%d “,arr[1][1]);
}
Answers: 13
Explanation:- in initialization of 2D-array row size is optional.

19.What is the output of the following code?
void main()
{
   int arr[5]={10,20};
   printf(“n%d %d “,1[arr],arr[0]);
}
Answers: 20 10

20.What is the output of the following code?
void main()
{
   int arr1[5]={10,20};
   int arr2[5];
   printf(“n%d %d “,arr1[3],arr2[3]);
}
Answers: 0 gr/junk

21.What is the output of the following code?
 void main()
{
    int a[2];
    int i;
   for(i=0;i<10;i++)
     a[i]=i*i;
    printf(“%d %d “,a[3],a[4]);
}
Answers: 9 16


22.What is the output of the following code?
#include <stdio.h>
void main()
{
  int a[5]={10,20,30,40,50};
  int b[5]={3,13};
  b=a;
  printf(“%d”,b[1]);
}
Answers: Error
Explanation:- we can’t assign address of array to another array because it is constant pointer.

23.What is the output of the following code?
          void main()
  {
int a[5]={6,16,26};
++*a;
++*(a+1);
++a[1];
printf(“%d %d”,a[0],a[1]);
   }
Answers: 7 18

24.What is the output of the following code?
void main()
{
int a[5]={ 10,20,30,40,50};
int * ptr;
ptr=a+1;
printf(“%d %d %d”, ++*ptr,*ptr++,*++ptr);
}
Answers: 41 30 30

1 comment: