Home »
Aptitude Que. & Ans. »
C Aptitude Que. & Ans.
C If else - Aptitude Questions & Answers
C programming if else Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on condition statements – if else, nested if else, ladder if else, conditional operators etc.
1) What will be the output of following program ?
#include <stdio.h>
void main()
{
if(!printf(""))
printf("Okkk");
else
printf("Hiii");
}
- Okkk
- Hiii
- Error
- None
Correct Answer - 1
Okkk
2) What will be the output of following program ?
#include <stdio.h>
void main()
{
int x=22;
if(x=10)
printf("TRUE");
else
printf("FALSE");
}
- TRUE
- FALSE
- Error
- None
Correct Answer - 1
TRUE
if(x=10)... "=" is an assignment operator, so 10 will be assigned to x and
condition will be true due to if(10)..
3) What will be the output of following program ?
#include <stdio.h>
void main()
{
char val=1;
if(val--==0)
printf("TRUE");
else
printf("FALSE");
}
- FALSE
- Error
- TRUE
- None
Correct Answer - 1
FALSE
4) What will be the output of following program ?
#include <stdio.h>
void main()
{
float a=10.5;
printf("\n===FIRST CONDITION\n");
if(sizeof(a)==sizeof(10.5))
printf("Matched !!!");
else
printf("Not matched !!!");
printf("\n===SECOND CONDITION\n");
if(sizeof(a)==sizeof(10.5f))
printf("Matched !!!");
else
printf("Not matched !!!");
printf("\n===THIRD CONDITION\n");
if(sizeof((double)a)==sizeof(10.5))
printf("Matched !!!");
else
printf("Not matched !!!");
printf("\n===FOURTH CONDITION\n");
if(a==10.5f)
printf("Matched !!!");
else
printf("Not matched !!!");
printf("\n");
}
===FIRST CONDITION
Not matched !!!
===SECOND CONDITION
Matched !!!
===THIRD CONDITION
Matched !!!
===FOURTH CONDITION
Matched !!!
in this program a is a float variable and value 10.5 will consider as double.
5) What will be the output of following program ?
#include <stdio.h>
int main()
{
int a=10;
if(a==10)
{
printf("Hello...");
break;
printf("Ok");
}
else
{
printf("Hii");
}
return 0;
}
- Hello...
- Hello...OK
- OK
- Error
Correct Answer - 4
Error : misplaced break/ illegal break
A break statement can be used with looping and switch statements.
6) What will be the output of following program ?
#include <stdio.h>
int main()
{
if( (-100 && 100)||(20 && -20) )
printf("%s","Condition is true.");
else
printf("%s","Condition is false.");
return 0;
}
- Condition is true.
- Condition is false.
- No output
- ERROR
Correct Answer - 1
Condition is true.
Any non zero value is treated as true for conidion.
Consider the expressions: if( (-100 && 100)||(20 && -20) )
=if( (1) || (1) )
=if(1)
7) What will be the output of following program ?
#include <stdio.h>
#define TRUE 1
int main()
{
if(TRUE)
printf("1");
printf("2");
else
printf("3");
printf("4");
return 0;
}
- ERROR
- 1
- 12
- 2
Correct Answer - 1
ERROR : misplaced if/illegal else without matching if.
You can use only one statement within the if( )without parenthesis {...} .
8) What will be the output of following program ?
#include <stdio.h>
int main()
{
int pn=100;
if(pn>20)
if(pn<20)
printf("Heyyyyy");
else
printf("Hiiiii");
return 0;
}
- No output
- Hiiiii
- Heyyyyy
- HeyyyyyHiiiii
Correct Answer - 2
Hiiiii
printf("Hiiiii"); that is written after else , is treated as else part of inner if condition if(pn<20).
9) Which of the following are incorrect statements? If int a=10.
1) if( a==10 ) printf("IncludeHelp");
2) if( 10==a ) printf("IncludeHelp");
3) if( a=10 ) printf("IncludeHelp");
4) if( 10=a ) printf("IncludeHelp");
- 3 and 4.
- 3 only.
- 4 only.
- 2,3 and 4.
Correct Answer - 3
4 only.
Consider the following expressions:
if(a==10) => if(1) => correct.
if(10==a) => if(1) => correct.
if(a=10) => if(10) => correct (10 is a non zero value).
if(10=a) => incorrect, because value of a can not assign in 10, Lvalue required error is occurred.
10) What will be the output of following program ?
#include <stdio.h>
int main()
{
int a=10;
if(10L == a)
printf("10L");
else if(10==a)
printf("10");
else
printf("0");
return 0;
}
- 10.
- 10L.
- 10L10.
- ERROR.
Correct Answer - 2
10L.