Saturday, January 28, 2012

Puzzle code of statement and condition

Q1) Output of the program
#include "stdio.h"
#include "conio.h"
main()
{
  if(!printf("hello"))
   printf("hello");
  else
   printf("world");
   getch();
}  
Ans:- helloworld
Discussion:-within if condition printf() function prints hello and return 5 into if condition and within if condition !5 expression produce 0,so if condition became false.

Q2)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  if( !(10<20)*1*0)
   printf("C");
  else
   printf("C++");
   getch();
}  
Ans:-C++
Discussion:-int if condition the expression produce 0.because 10<20 the statement is true so it produce 1 and !1=0 and 0*1*0=0.so if condition is false.

Q3)
#include "stdio.h"
#include "conio.h"
main()
{
 switch(5)
 {
          default:printf("default");
          case 1:printf("one");
          case 2:printf("two");
 };
 getch();
}  
Ans:-defaultonetwo
Discussion:-Here break statement is missing in case statement. so all statement within switch case is executed.
Q4)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  int a=100,b=200;
  if(a<b)
   return a+b;
  printf("Return statement execute");
  getch();
}

Ans:-Nothing will print.
Discussion:-Return statement is considered as the last statement of function. Since main() is a function so after execution it will return value to the calling environment ,i.e operating system. So printf() function will not execute.

Q5)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
 int b=5;
 printf("%d",++--++--b);
 getch();
}  

Ans:-5
Discussion:-Here value of b will increment and decrement twice. So value of b remain unchanged.

Q6)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
 0>1?printf("one"):printf("two");
 getch();
}  
Ans:-two
Discussion:-Here 0>1 expression is false ,so it produce zero. so two is printed.






Q7) Output of the program.
#include "stdio.h"
#include "conio.h"
#define PRINT printf("Hello")
main()
{
 int a=0.0;
 float b=0.0;
 if(a==b)
 PRINT;
 getch();
}
Ans:-Hello
Discussion:- Here printf() function is defined as pre processor directive.

Q8)Output of program.
#include "stdio.h"
#include "conio.h"
main()
{
int a=10,b=20,c;
c=(a==b);
printf("%d",c);
getch();
}     
Ans:-0
Discussion:-Here the expression a==b is false. So it return 0 into c.

Q9)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
if(!printf(""))
 printf("C");
else
 printf("C++");
getch();
}  
Ans:-C
Discussion:-Here printf(“%d”) function will print the garbage value and ! operator turns into 0.so if condition is true.


Q10) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a=10,b=20,c=30;
if(a>b>c)
 printf("C");
else
 printf("C++");
getch();
}
Ans:-C++
Discussion:-The the expression a>b is false so produce 0.And again the expression 0>30 is also false and produce 0.So the value of total expression a>b>c is 0.And if condition is false.

Q11)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  static int a=5;
  printf("%d",--a);
  if(a)
  main();
  getch();
}
Ans:-4 3 2 1 0
Discussion:-Static variable is capable to retain it’s value. and main() function will call until if condition is false.

Q12)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  char a[]="\0";
  if(a)
   printf("C");
  else
   printf("C++");
  getch();
}
Ans:-C

Q13) Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
  int x=(2<0)?printf("one"):printf("two");
  printf("%d",x);
  getch();
}
Ans:-two3
Discussion:-The expression 2<0 is false so printf(“two”) will exhecute. And  printf function return 3.

Q14)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
 for(int i=0;i<=15;i+=3);
 printf("%d",i);
 getch();
}
Ans:-In DEV C++ compiler the code is erroneous. But in TC++ it produces 15.
Discussion:-Dev C++ do not support null statement in for loop.


Q15)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
int a;
if(a=0)
 printf("C");
else
 printf("C++");
getch();
}
Ans:-C++
Discussion:-The expression a=0 return 0 in if condition.



No comments:

Post a Comment