© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

Ex - 5: What is the output?

 main(){
  int a= 0;int b = 20;char x =1;char y =10;
  if(a,b,x,y)
        printf("hello");
 }
Answer:
hello
Explanation:

The comma operator has associatively from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if becomes true so, "hello" will be printed.

Ex - 6:  What is the output?
main()
{
      int i=10;
      void pascal f(int,int,int);
f(i++,i++,i++);
      printf(" %d",i);
}
void pascal f(integer :i,integer:j,integer :k)
{
write(i,j,k);
}
Answer:
Compiler error:  unknown type integer
Compiler error:  undeclared function write
Explanation:

Pascal keyword doesn’t mean that pascal code can be used. It means that the function follows Pascal argument passing mechanism in calling the functions.

Ex - 7:  What is the output?
#include<conio.h>
main()
{
      int x,y=2,z,a;
      if(x=y%2) z=2;
      a=2;
      printf("%d %d ",z,x);
}
Ans:
Garbage-value 0
Explanation:

The value of y%2 is 0. This value is assigned to x. The condition reduces to if (x) or in other words if(0) and so z goes uninitialized. Thumb Rule: Check all control paths to write bug free code.


1 >> 2 >> 3 >> 4
SLogix Student Projects
bottom