Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

C Aptitude Questions And Answers -DECLARATIONS AND INITIALIZATION

------------------------------------- Test Your "C" Skill ---------------------------------------


DECLARATIONS AND INITIALIZATION:



1.
#include<stdio.h>  
void main()
  {   
   char var *s1,*s2;
   printf(""%d%d",sizeof(s1),sizeof(s2));
  }

 Outout:-
   4 2

2.
#include<stdio.h>  
int x=40;
void main()
{
int x=20;
printf("%d",x);
}

Outout:- 
            20
3.
#include<stdio.h> 
void main()
{
 int x=40;
  {
  int x=20;
  printf("%d",x0;
   }
 printf("%d",x);
}

Output:-
      20 40
4.
Is the following statement declaration or definition
  extern int x;

Output:-
Declaration

5.
#include<stdio.h>
 main()
  {
  extern int i;
  i=20;
  printf("%d",sizeof(i));
 }

Output:-
   Error,i undefined
   Because extern int i is a declaration and not definition

 6.Is it true that the global variable have many declarations but only one definition?

Output:-
yes

7.Is it true that the function may have many declarations but only one definition?

output:-
yes

8.what is the difference between declaration and definition of a variable

Output:-
Declaration:-only gives the type,status and nature of variable without reserving any space for the variable
 Definition:-actual space is reserved for the variable and some initial value is given.

9.if the definition of the external variable occurs in the source file before it's use in a
   particular function then there is no need for an external declaration in the function

Output:-
True

10.suppose the program is divided in three source files f1,f2,f3 and the variable is defined in   file f1 but used in f2 and f3. In such a case would we need the external declaration for the variable in files f2 and f3?

Output:-
yes


10.what is the difference between following declarations
  extern int fun()
  int fun();

 Output:-
Nothing except that that the first one gives us hint that function fun is probably in another file.


10. why  does the following program reports the re declaration error of function display()
#include<stdio.h>
   void main()
   {
      dispaly();
   }

 void dispaly()
 {
  printf("Hello world");
 }
Output:-
Here the function display() is called before it is declared .That is why the compiler assumes it to be declared as
     int display();
That accept unspecified no of arguments.i.e. undeclared function assumes to return int

On appearing the declaration the fun shows that it returns void hence the error

11.
void   main()
 {
  extern int fun(float);
  int a;
  a=fun(3.14);
  printf("%d",a);
 }

 int fun(aa)        /* K & R style of function definition*/
 float aa
 {
 return((int)aa);
}
Output:-
Error
     Because we have mixed the ANSI prototype with k & r style of function definition
     If we use an ANSI prototype and pass float to the function then it is promoted to           double  the function accepts it in to variable of type float hence the type mismatch         occurs To ready the situation define the function as
    int fun(float aa)
     {
       .....
     }

12.point error if any
  struct emp
  {
    char name[20];
     int age;
  }

fun(int aa)
 {
  int bb;
  bb=aa*aa;
  return(bb);
 }

main()
{
 int a;
a=fun(20);
printf("%d",a);
}

 Output:-
Missing semicolon at the end of struct due to which the function fun assumed to be returning void of type struct emp.
But it returns an int hence the error

13.Correct the error
   fun(struct emp);
     struct emp
       {
         char name[20];
         int age;
       };
   void  main()
     {
       struct emp e={"Mukesh",21}
        fun(e);
     }

   fun(struct emp ee)
      {
        printf("\n %s %d",ee.name,ee.age);
      }    
 Output:-
 Declare the structure before the prototype of fun().

14.What would be the output of the following program
 void main()
  {
    int a[5]={2,3}
     printf("\n %d %d %d",a[2],a[3],a[4]);
  }

 Output:-
 0 0 0
  If a automatic array is partially initialized then remaining elements are initialized by 0

15.void main()
 {
  struct emp
   {
     char name[20];
     int age;
     float sal;
   };

  struct emp e={"Mukesh"}
  printf("\n %d %f",e.age, e.sal);
 }

 Output:-
 0 0.000000
If an automatic structure is partially initialized then remaining elements are initialized by 0.
16.point out error
 void main()
 {
  int(*p)()=fun;
  (*p)();
 }
 fun()
  {
    printf("\n Hello World");
  }

 Output:-
 Here we are initializing function pointer to address of the function fun() but during the time of initialization the  function has not been defined. 
Hence an error To eliminate the error add the prototype of function fun() before the declaration of p, as shown bellow;
    extern int fun();       or 
    int fun();

17.point error if any
  void main()
  {
    union a
     {
       int i;
       char ch[2];
     };
    union a z=512;
    printf("%d %d",z.ch[0],z.ch[1]);
  }
 Output:-
In Pre-ANSI compiler union variable can not be initialized . ANSI compiler permits initialization of first member of the union 

18.What do you mean by the scope of the variable? what are the 4 different types of scopes that a variables can have?
Output:- 
Scope indicates the region over which the variable's declaration has an effect. The four kinds of scopes are: 
---file
---function
---block
---prototype.

19. what are different types of linkages?
Output:- 
There are three different types of linkages : external , internal , and none. 
---External linkage means global, non-static variables and functions,
---Internal linkage means static variables and functions with file scope
--- None linkage means local variables.

Previous                                                                                                         Next Page


No comments:

Post a Comment