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

C and C++ Interview Questions

Ex - 1: What is the output?

  main()
{
      unsigned int i=65000;
      while(i++!=0);
      printf("%d",i);
}
Ans:
 1
Explanation:

Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1.

Ex - 2:  What is the output?
void main()
{
      while(1){
            if(printf("%d",printf("%d")))
                  break;
            else
                  continue;
      }
}
Answer:
Garbage values
Explanation:

The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf  prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement.

Ex - 3:  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.

Ex - 4:  What is the output?
int i;
            main(){
int t;
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i))
                  printf("%d--",t--);
                  }
      // If the inputs are 0,1,2,3 find the o/p
Ans:
      4--0
      3--1
       2--2
Explanation:

Let us assume some x= scanf("%d",&i)-t the values during execution will be,
          t        i       x
          4       0      -4
          3       1      -2
          2       2       0


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