Quiz on Arrays and Pointers

1 What is the output of the following program?

#include <stdio.h>

void foo(int a[])
{
a[0]=10;
printf("%d",a[0]);
return ;
}

int main()
{
int a[]={1,2,3};
foo(a);
printf("%d",a[0]);
return 0;
}
 
 
 
 


ans 1010
2 What is the output of the following program?


#include<stdio.h>
int main()
{
    int size, i;
    size=5;
    int arr[size]={1,2,3,4,5};
    for(i=1; i<=size;i++)
    {
        printf("%d", arr[i]);
    }
    return 0;
}

 
 
 
 



ans. none of these

3 In C, if you pass an array as an argument to a function, 
what actually gets passed?
 
 
 
 

ans . base address of array

4 What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    char arr[16]="The African Queen";
    printf("%s",arr);
}
 
 
 
 


ans. error

5 What is the valid statement that you can make about the 
output of the following code?

#include<stdio.h>
void fun(int * a)
{
      int ** x=&a;
      printf("%u\n",x);
      return ;
}
int main(){
    int a[5]={1,2,3,4,5};
    int * b = a;
    int ** y=&b;
    printf("%u\n",y);
    fun(b);
    return 0;  
}
 
 
 
 


ans. both the printed values may be same

6 What will be output if you will execute following C code?
#include<stdio.h>
void main(){
    char arr[20]="MysticRiver";
    printf("%d",sizeof(arr));   
}

 
 
 
 


ans.20

7 What is the output of the following code?


#include<stdio.h>
int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}
 
 
 
 

ans. 3 2 15

8 Which of the following are correct ways to initialize an array?
 (There may be multiple answers.)
 
 
 
 


ans. A&C

9 What will be the output of the following program?
#include<stdio.h>
int main(){
   int a = 330;
   char *p;
   p =( char *)&a;
   printf("%d ",*p);
   return 0;
}

 
 
 
 

Labels: , ,