12/12/2016

Interview-question-on-c-programming

Interview question on c programming
Set-1 

1.What is difference between a declaration and a definition?


Ans:There are two differences between a declaration and a definition:
In the definition of a variable space is reserved for the variable and some initial value is given to it ,whereas a declaration only identifies the type of the variable.Thus definition is the place where the variable is created or assigned storage,whereas declaration refers to place where the nature of the variable is stated but no storage is allocated.
     
       Secondly,redefinition is an error,whereas,redeclaration is not an error. 

2.What is more efficient  a switch statement or an if-else chain?

Ans:As far as efficiency is concerned there would hardly be any difference,if at all.If the cases in a switch are sparsely distributed the compiler may internally use the equivalent of an if-else chain instead of a compact jump table.However,one should use switch where one can.It is definitely a cleaner way to program and certainly is not any less efficient than the if-else chain.  
3.Which of the following is the correct output for the program       given below?


#include <stdio.h>
int main( )
{
       int a =0, b = 1, c = 3 ;
       *( (a) ?&b:&a ) = a c ;
       printf "%d %d %d/n", a, b, c, ) ;
       return 0 ;
}
A.   0 1 3
B.   1 2 3
C.   3 1 3
D.   1 3 1

Ans:  C
4.Which of the following is the correct output for the program       given below?
#include<stdio.h>
int main(){
int i=2;
printf("%d%d",++i,++i);
return 0;
}
a.3 4
b.4 3
c.4 4,
d.output  may vary from compiler to compiler.
Ans. D.
  The order of evaluation of the arguments passed to a function call is unspecified. Hence, some compilers would report the output as   4 3 and some as 4 4.

5.Are the following two statement  same?
   
a<=20?(b=10):(b=30);
b=a<=20?10:30;

Yes this two statement are same.


6.Point out the error if any, in the following code.

#include<stdio.h>
int main()
{
 int a=10;
void f();
a=f();
printf("%d\n",a);
return o;
}
void f(){
printf("\n HI");
}


Ans:In spite of defining the function f() as returning void, the program is trying to collect the value return by f() in the variable a.
7.Is it necessary that the header file should .h extension?

Ans:No . However, traditionally they have been given a .h extension to identify them as something different then the .c program files.

8.What do the header file contain?

Ans:Header files contain preprocessor directive like #define, structure,union and enum declarations, type def declarations,global variable declarations and external function declaration. you should not write the actual code (i.e-function bodies )in header files. The 
#include directive should be used to pull in header files,not other '.c' files.
9.What will be the output of the following program?


#include<stdio.h>
#define PRINT(int) printf("%d",int)
int main()
{
  int x=2,y=3,z=4;
PRINT(x);
PRINT(y);
PRINT(z);
printf("\n");
return 0;
}

Ans: 2 3 4

10.Is this a correct way fur null pointer assignment?

int i=0;
char *q=(char *)i;

Ans:No. The correct way is shown below:

       char *q=0;
or
char *q=(char *)0;

No comments:

Post a Comment