Thursday, 21 December 2017

C MCQ - Function and pointer

1) What will be output of the following program

int main()
{
    void fun();

    printf("Hii");

    fun();
}

void fun()
{
   printf("Hello");
}

a) Hello Hii
b) Hii Hello
c) Hii Hello Hii
d) Hello Hii Hello

Ans--> Here, void fun(); is function declaration , fun(); is function calling and void fun() {  } is function definition. We known that program execution starts from main(), so firstly Hii will be printed then fun() is called and then Hello is printed.

2) What will be output of the following program

int main()
{
   void fun(), fun1();
     
    fun1();
}

void fun1()
{
        printf("Hello");
        fun();
}

void fun()
{
      printf("World");
}

a) Hello World
b) World Hello
c)  Error
d) None of these

3) What will be output of the following program

void fun();

int main()
{
    void fun();
   
    fun();

return 0;
}

void fun()
{
    printf("Welcome");
}

a) Welcome
b) Program will crash
c) Error
d) None of these

Ans--> Function can be declared any number of times, but can be defined only once. Inside the main(), fun() function is called, thus Welcome will be printed.

4) What will be output of the following program

void fun();

int main()
{
    void fun(int);
    fun();
return 0;
}

void fun()
{
   printf("Hii");
}

a) Error
b) No output
c) Hii
d) Compile Time Error


5) What will be output of the following program

void fun();

void fun1()
{
     fun();
}

void main()
{
   void fun()
     {
         printf("Hello");
     }
}

a) Hello
b) Compile Time Error
c) No output
d) None of these

Ans-->In C, a function can't be defined inside the body of another function. Here, fun() is defined inside main(), so it reports compile time error.

6) What will be output of the following program.

void main()
{
    static int x=4;
    x++;
  if(x<=6)
    {
        printf("Hii");
        main();
    }
}

a) Hii Hii
b) Hii Hii Hii
c) Hii Hii Hii Hii
d) None of These

Ans--> Static variable is initialized only once. For the first time, when x=4 Then it is incremented by 1 and then compared with 6 and Hii will be printed. 
Again, main() is called, then x will be not re-initialized, it will be incremented to 6 and condition will be satisfied and again Hii will be printed and for the next time when main() is called, x will become 7. Thus if condition will not be satisfied.

7) What will be output of the following program

void main()
{
    int r=fun();
    printf("%d", r);
}

void fun()
{
    printf("Hello World");
}

a) Hello World
b) Hello World 5
c) Compile Time Error
d) None of these

Ans-->Before calling any function, its prototype must be declared and In order to return any value, fun should have return type as int.

8) What will be output of the following program

#include<stdio.h>

void fun()
{
    return 1;
}

void main()
{
   int r=0;
   r=fun();
  printf("%d",r);
}

a) Compile Time Error
b) 1
c) No output
d) 0

Ans-->In C, A function with same name cannot have different signature,  different return types and different number of parameters. Thus, For returning any value, fun() should have return type as int. Thus, it gives Compile Time Error.

9) What will be output of the following program

int *fun()
{
   int *r=5;
   return r;
}

void main()
{
    int *m=fun();
    printf("%d",m);
}

a) Error
b) 5
c) No output
d) None of these

10) If the return type is not specified in function definition, then default return type is ______

a) void
b) int
c) long int
d) float

11) What will be output of the following program

int main()
{
    char *m=NULL;
    char *n=0;
   
    if(m)
        printf("Welcome ");
   else
        printf("Hii ");
   
   if(n)
       printf("Hello\n");
   else
       printf("World\n");
}

a) Hii World
b) Welcome World
c) Hii Hello
d) Welcome Hello

Ans-->A pointer variable can be initialized to NULL i.e. 0. So, m=0 and n=0.
and In C, any non-zero value means true whereas zero value represent false.
Thus, Hii World will be printed.

12) What will be output of the following program where &k=65432

int main()
{
  int k=11;
  
  void *l=&k;

  printf("%d\n",(int)*l);

return 0;

}

a) Compile Time Error
b) 11
c) Program crashes
d) 65432

Ans-->void pointer can be assigned to any type without typecasting but vice versa is not true.

13) What is the difference between 

const int *ptr and int *const ptr

Ans--> In, const int * ptr, we cannot change the value stored at the address pointed by ptr whereas In int * const ptr, we cannot the address pointed by ptr.

14) What will be output of the following program

void main()
{
   int m=0;
   int *k=&9;
   printf("%p\n",k);
}

a) 65324
b) No output
c) Program crashes
d) Compile Time Error

Ans--> We cannot assign address of a constant to a pointer. Thus, gives compile Time Error.

15) What will be output of the following program

void fun(int *)

int main()
{
   int k=9;
  
  fun((&k)++);
}

void fun(int *m)
{
   printf("%d\n", m);
}

a) 9
b) No output
c) Compile Time Error
d) None of these

Ans--> Here, we are trying to increment the address of k, which is not possible. Thus, gives compile Time Error.

16) What will be output of the following program

int main()
{
  int k=9;

  int *p=&k;

  fun(&p);

  printf("%d", *p);

  printf("%d", *p);

}

void fun(int **const p)
{
    int j=11;

    *p=&j;

    printf("%d",**p);
}

a) 11, 10, 10
b) 11, Undefined Value
c) 11, 11, 11
d) No output

Ans-->Here, p is assigned address of j. So, now **p gives 11, but when control goes back to main() p holds the address of variable that is not defined. So, Gives undefined value.

17) The maximum number of arguments that can be passed in a single function are _________

a) 256
b) 253
c) 252
d) None of these

18) What will be output of the following program

void fun(int *k)
{
   int i=0;
  
   for(i=0;i<5;i++)
      printf("%d\t",k[i]);
}

void main()
{
   int a[5]={1,2,3};
   fun(&a);
}

a)1 2 3 0 0 
b) Error
c) 1 2 3
d) No output

Ans-->Here base address is passed and stored in k. K[0] can be written as 
*(k+0), thus value stored at k[0] is 1. K[1] can be viewed as *(k+1), k is holding base address of array a, So, k+1 means the next address i.e. address of 2. Thus, *(k+1) or k[1] = 2. Similarly, k[4]=0 ( as, when an array is initialized at the time of declaration, and if number of value initialized is less than dimension of the array, then rest of the value is initialized to zero) and k[5]=0.

19) What Will be output of the following program

int main()
{
   void *p;
   
   int a[4]={4,6,9,8};

   p=&a[3];
  
   int *ptr=&a[2];

   int n=(int *)p-ptr;

   printf("%d\n",n);
}

a) 4
b) 8
c) 9
d) 6

Ans--> p is pointer to a void. So, it can be type-casted to int by writing         (int *)p. Now, p holds address of array 4th element and ptr holds the address of array 3rd element. so n=1, indicate number of elements between the two address.


20) What will be output of the following program

int main()
{
   double *fun=(double *)200;

   fun=fun+2;

   printf("%u",fun);
}

a) 216
b) 224
c) 214
d) 210

Ans-->Since, double is 8 bytes. So, fun=200+(2*8)
                                                          =216

21) What will be output of the following program

int main()
{
   void *p;
    
   int a[4]={1,2,3,2};

   p=&a[3];

  int *ptr=&a[2];

  int n=p-ptr;

  printf("%d\n",n);
}

a) 1
b) 2
c) No output
d) Compile Time Error

Ans-->Here, p is a pointer of type void and ptr is pointer to integer. So, It will be Compile Time Error.













No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...