If else questions in c



If else or control flow objective type questions and answers with explanation in c language for written test or interview


1.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10,c=1;
    if(a&&b>c){
         printf("cquestionbank");
    }
    else{
         break;
    }
}

Choose all that apply:

(A)  cquestionbank
(B)  It will print nothing
(C)  Run time error
(D)   Compilation error
(E)  None of the above


Explanation:

Keyword break is not syntactical part of if-else statement. So we cannot use break keyword in if-else statement. This keyword can be use in case of loop or switch case statement.
Hence when you will compile above code compiler will show an error message: Misplaced break.

2.

What will be output when you will execute following c code?

#define PRINT printf("Star Wars");printf(" Psycho");
#include<stdio.h>
void main(){
    int x=1;
    if(x--)
         PRINT
    else
         printf("The Shawshank Redemption");
}

Choose all that apply:

(A)  Stars Wars Psycho
(B)  The Shawshank Redemption
(C)  Warning: Condition is always true
(D)  Warning: Condition is always false
(E)  Compilation error


Explanation:

PRINT is macro constant. Macro PRINT will be replaced by its defined statement just before the actual compilation starts.  Above code is converted as:

void main(){
    int x=1;
    if(x--)
         printf("Star Wars");
printf(" Psycho");
    else
         printf("The Shawshank Redemption");
}
   
If you are not using opening and closing curly bracket in if clause, then you can write only one statement in the if clause. So compiler will think:
(i)
if(x--)
    printf("Star Wars");

It is if statement without any else. It is ok.
(ii)
printf(" Psycho");

It is a function call. It is also ok
(iii)
else
         printf("The Shawshank Redemption");

You cannot write else clause without any if clause. It is cause of compilation error. Hence compiler will show an error message: Misplaced else 

3.

What will be output when you will execute following c code?

#define True 5==5
#include<stdio.h>
void main(){
    if(.001-0.1f)
         printf("David Beckham");
    else if(True)
         printf("Ronaldinho");
    else
        printf("Cristiano Ronaldo");
}

Choose all that apply:

(A)  David Beckham
(B)  Ronaldinho 
(C)  Cristiano Ronaldo
(D)  Warning: Condition is always true
(E)  Warning: Unreachable code


Explanation:

As we know in c zero represents false and any non-zero number represents true. So in the above code:

(0.001 – 0.1f) is not zero so it represents true. So only if clause will execute and it will print: David Beckham on console.
But it is bad programming practice to write constant as a condition in if clause. Hence compiler will show a warning message: Condition is always true

Since condition is always true, so else clause will never execute. Program control cannot reach at else part. So compiler will show another warning message:
Unreachable code

4.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=100;
    if(a>10)
         printf("M.S. Dhoni");
    else if(a>20)
         printf("M.E.K Hussey");
    else if(a>30)
           printf("A.B. de villiers");
}

Choose all that apply:

(A)  M.S. Dhoni
(B)  A.B. de villiers
(C)   

M.S Dhoni
M.E.K Hussey
A.B. de Villiers
(D)
 Compilation error: More than one 
conditions are true
(E)  None of the above


Explanation:

In case of if – if else – if else … Statement if first if clause is true the compiler will never check rest of the if else clause and so on. 

5.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int x=-1,y=-1;
    if(++x=++y)
         printf("R.T. Ponting");
    else
         printf("C.H. Gayle");
}

Choose all that apply:

(A)  R.T Ponting
(B)  C.H. Gayle
(C)
 Warning: x and y are assigned a
value that is never used
(D)  Warning: Condition is always true
(E)  Compilation error


Explanation:

Consider following statement:
++x=++y
As we know ++ is pre increment operator in the above statement. This operator increments the value of any integral variable by one and return that value. After performing pre increments above statement will be:

0=0
In C language it is illegal to assign a constant value to another constant. Left side of = operator must be a container i.e. a variable. So compiler will show an error message: Lvalue required

In c if you assign any value to variable but you don’t perform any operator or perform operation only using unary operator on the variable the compiler will show a warning message: Variable is assigned a value that is never  

6.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    if(sizeof(void))
         printf("M. Muralilidaran");
    else
         printf("Harbhajan Singh");
}

Choose all that apply:

(A)  M. Muralilidaran
(B)  Harbhajan Singh
(C)  Warning: Condition is always false
(D)  Compilation error
(E)  None of the above


Explanation:

It illegal to find size of void data type using sizeof operator. Because size of void data type is meaning less.

7.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int m=5,n=10,q=20;
    if(q/n*m)
         printf("William Gates");
    else
         printf(" Warren Buffet");
         printf(" Carlos Slim Helu");
}

Choose all that apply:
(A)  William Gates
(B)   Warren Buffet Carlos Slim Helu
(C)  Run time error
(D)  Compilation error
(E)  None of the above


Explanation:

Consider the following expression:
q / n * m

In this expression there are two operators. They are:
/: Division operator
*: Multiplication operator
Precedence and associate of each operator is as follow:

Precedence
Operator
Associate
1
/ , *
Left to right
 
Precedence of both operators is same. Hence associate will decide which operator will execute first. Since Associate is left to right. So / operator will execute then * operator will execute.
= q / n * m
= 20 / 10 * 5
= 2 * 5
=10

As we know in c zero represents false and any non-zero number represents true. Since 10 is non- zero number so if clause will execute and print: William Gates

Since in else clause there is not any opening and closing curly bracket. So compiler will treat only one statement as a else part. Hence last statement i.e.
printf(" Carlos Slim Helu");

is not part of if-else statement. So at the end compiler will also print: Carlos Slim Helu  
So output of above code will be:
William Gates Carlos Slim Helu

8.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    if(!printf("Mukesh Ambani"))
    if(printf(" Lakashmi Mittal"));
}

Choose all that apply:

(A)  Mukesh Ambani
(B)   Lakashmi Mittal
(C)  It will print nothing
(D)  Mukesh Ambani Lakashmi Mittal
(E)  Compilation error: if statement without body


Explanation:

Return type of printf function is int. This function return a integral value which is equal to number of characters a printf function will print on console. First of all printf function will: Mukesh Ambani. Since it is printing 13 character so it will return 13. So,

!printf("Mukesh Ambani")
= !13
= 0
In c language zero represents false. So if(0) is false so next statement which inside the body of first if statement will not execute.

9.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    if("ABC") printf("Barack Obama\n");
    if(-1)    printf("Hu Jintao\n");
    if(.92L)  printf("Nicolas Sarkozy\n");
    if(0)     printf("Ben Bernanke\n");
    if('W')   printf("Vladimir Putin\n");
}

Choose all that apply:


 (A)
It will print nothing



(B)
Barack Obama
Hu Jintao
Nicolas Sarkozy
Vladimir Putin

(C)
Barack Obama
Hu Jintao
Nicolas Sarkozy
Ben Bernanke
Vladimir Putin


(D)
Hu Jintao
Nicolas Sarkozy
Vladimir Putin

 (E)
Compilation error


Explanation:

“ABC”: It is string constant and it will always return a non-zero memory address.
0.92L: It is long double constant.
‘W’: It is character constant and its ASCII value is  

As we know in c language zero represents false and any non-zero number represents true. In this program condition of first, second, third and fifth if statements are true.

10.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    if(0xA)
         if(052)
             if('\xeb')
                 if('\012')
                      printf("Tom hanks");
                 else;
             else;
         else;
    else;
}

Choose all that apply:

(A)  Tom hanks
(B)  Compilation error: Misplaced else
(C)  Compilation error: If without any body
(D)  Compilation error: Undefined symbol
(E)  Warning: Condition is always true


Explanation:

oxA: It is hexadecimal integer constant.
052: It octal integer constant.
‘\xeb’: It is hexadecimal character constant.
‘\012’: It is octal character constant.

As we know in c zero represents false and any non-zero number represents true. All of the above constants return a non-zero value. So all if conditions in the above program are true.

In c it is possible to write else clause without any body.

11.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=10;
    if(printf("%d",a>=10)-10)
         for(;;)
             break;
    else;
}

Choose all that apply:

(A)  It will print nothing
(B)  0
(C)  1
(D)  Compilation error: Misplaced else
(E)  Infinite loop


Explanation:

Return type of printf function is int. This function return a integral value which is equal to number of charcters printf function will print on console.

Operator >= will return 1 if both operands are either equal or first operand is grater than second operand. So a>=10 will return 1 since a is equal to 10.Thus printf function will print 1. Since this function is printing only one character so it will also return 1.    So, printf("%d",a>=10) - 10
= 1 - 10
= -9

Since -9 is non-zero number so if(-9) is true condition hence if clause will execute which contains an infinite loop but due to break keyword it will come out of loop.

12.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10;
    if(++a||++b)
         printf("%d  %d",a,b);
    else
         printf("John Terry");
}

Choose all that apply:

(A)  5 10
(B)  6 11
(C)  6 10
(D)  5 11
(E)  John Terry


Explanation:

Consider the following expression:
++a || ++b

In this expression || is Logical OR operator. Two important properties of this operator are:
Property 1:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise it || operator returns 1.

Property 2:

To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return zero.

In this program initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute and if condition will be true and if clause will be executed.

13.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    static int i;
    for(;;)
    if(i+++"The Matrix")
          printf("Memento");
    else
         break;
}

Choose all that apply:

(A)  It will print Memento at one time
(B)  It will print Memento at three times
(C)  It will print Memento at ten times
(D)  It will print Memento at infinite times
(E)  Compilation error: Unknown operator +++


Explanation:

Think yourself

14.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int x=1;
    if(x--)
         printf("The Godfather");
         --x;
    else
         printf("%d",x);
}

Choose all that apply:

(A)  The Godfather
(B)  1
(C)  0
(D)  Compilation error
(E)  None of the above


Explanation:

If you are not using { and } in if clause then you can write only one statement. Otherwise it will cause of compilation error: Misplace else

15.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    if('\0');
    else if(NULL)
         printf("cquestionbank");
    else;
}

Choose all that apply:

(A)  cquestionbank
(B)  It will print nothing
(C)  Warning: Condition is always true
(D)  Warning: Unreachable code
(E)  Compilation error: if statement without any body


Explanation:

‘\0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check it else if clause.
NULL is macro constant which has been defined in stdio.h which also returns zero.     

16.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10;
    clrscr();
    if(a<++a||b<++b)
         printf("%d  %d",a,b);
    else
         printf("John Terry");
}

Choose all that apply:

(A)  5 10
(B)  6 11
(C)  6 10
(D)  Compilation error
(E)  John Terry


Explanation:

Consider the following expression:

a<++a||b<++b

In the above expression || is logical OR operator. It divides any expression in the sub expressions. In this way we have two sub expressions:
(1)   a<++a
(2)   b<++b
In the expression: a< ++a
There are two operators. There precedence and associate are:

Precedence
Operator
Associate
1
++
Right to left
2
< 
Left to right

From table it is clear first ++ operator will perform the operation then < operator.
One important property of pre-increment (++) operator is: In any expression first pre-increment increments the value of variable then it assigns same final value of the variable to all that variables. So in the expression: a < ++a
Initial value of variable a is 5.
Step 1: Increment the value of variable a in whole expression. Final value of a is 6.
Step 2: Now start assigning value to all a in the expression. After assigning 6 expression will be:
6 < 6
Since condition is false .So second expression i.e. b<++b will be evaluated. Again 11 < 11 is false. So || will operator will return zero and else clause will execute. 

17.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int x=1,y=2;
    if(--x && --y)
         printf("x=%d  y=%d",x,y);
    else
         printf("%d %d",x,y);
}

Choose all that apply:

(A)  1 2
(B)  x=1 y=2
(C)  0 2
(D)  x=0 y=1
(E)  0 1


Explanation:

Consider the following expression:
--x && --y

In this expression && is Logical AND operator. Two important properties of this operator are:
Property 1:
(Expression1) && (Expression2)
&& operator returns 1 if and only if both expressions return a non-zero value other wise it && operator returns 0.

Property 2:

To optimize the execution time there is rule, Expression2 will only evaluate if and only if Expression1 return a non-zero value.

In this program initial value of x is 1. So –x will be zero. Since -–x is returning zero so -–y will not execute and if condition will be false. Hence else part will be executed.

18.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    signed int a=-1;
    unsigned int b=-1u;
    if(a==b)
         printf("The Lord of the Rings");
    else
         printf("American Beauty");
}

Choose all that apply:

(A)  The Lord of the Rings
(B)  American Beauty
(C)
Compilation error: Cannot compare signed 
number with unsigned number
(D)  Compilation error: Undefined symbol -1u
(E)  Warning: Illegal operation


Explanation:

Read following tutorial:
Data type tutorial

19.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char c=256;
    char *ptr="Leon";
    if(c==0)                                
         while(!c)
             if(*ptr++)
                 printf("%+u",c);
             else
                 break;
}

Choose all that apply:

(A)  +256+256+256+256
(B)  0000
(C)  +0+0+0+0
(D)  It will print +256 at infinite times
(E)  Compilation error


Explanation:

In the above program c is signed (default) char variable. Range of signed char variable in Turbo c is from -128 to 127. But we are assigning 256 which is beyond the range of variable c. Hence variable c will store corresponding cyclic value according to following diagram:

Since 256 is positive number move from zero in clock wise direction. You will get final value of c is zero.









if(c==0)

It is true since value of c is zero.

Negation operator i.e. ! is always return either zero or one according to following rule:

!0 = 1
!(Non-zero number) = 0
 So,
!c = !0 =1
As we know in c zero represents false and any non-zero number represents true. So
while(!c) i.e. while(1) is always true.
In the above program prt is character pointer. It is pointing to first character of string “Leon” according to following diagram:
In the above figure value in circle represents ASCII value of corresponding character.
Initially *ptr means ‘L’. So *ptr will return ASCII value of character constant ‘L’ i.e. 76
if(*ptr++) is equivalent to : if(‘L’) is equivalent to: if(76) . It is true so in first iteration it will print +0. Due to ++ operation in second iteration ptr will point to character constant ‘e’ and so on. When ptr will point ‘\0’ i.e. null character .It will return its ASCII value i.e. 0. So if(0) is false. Hence else part will execute. 

20.

What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=2;
    if(a--,--a,a)
         printf("The Dalai Lama");
    else
         printf("Jim Rogers");
}

Choose all that apply:

(A)  The Dalai Lama
(B)  Jim Rogers
(C)  Run time error
(D)
Compilation error: Multiple parameters in 
if statement
(E)  None of the above


Explanation:

Consider the following expression:
a-- , --a , a
In c comma is behaves as separator as well as operator.In the above expression comma is behaving as operator.Comma operator enjoy lest precedence in precedence table andits associatively is left to right. So first of all left most comma operator will perform operation then right most comma will operator in the above expression.

After performing a-- : a will be 2
After performing --a : a will be 0
a=0

As we know in c zero represents false and any non-zero number represents true. Hence else part will execute.




Looping questions
Precedence and Associativity table of operators in c



If you have any queries or suggestions on above If else or control flow question please suggest us.

27 comments:

Anonymous said...

very good collection...keep it up..bravo n thanxxxx :)

Unknown said...

how to connect dos promt through c-language

Unknown said...

good collection...

Unknown said...

nice collection 2 increase d knowledge of bigner...
thank u.

manish kumar said...

thanks for these collection .

Anonymous said...

thnq.. its really helpful...

Anonymous said...

nice collection of question it helps to increase the knowledge

Anonymous said...

nice collection....it helped me a lot

Lakshmi said...

plese explain that 13th question

vin said...

19th example should be like this.. i mean format spacifier shoud be %d not %u ... If it is %u, we can't print + mark in visual c++

#include
void main(){
char c=256;
char *ptr="Leon";
if(c==0)
while(!c)
if(*ptr++)
printf("%+d",c);
else
break;
}

Anonymous said...

very nice collection and really very helpful..

Anonymous said...

#include
int main()
{
int a = 2 , b = 5 ;
char x = 1 , y = 10;
if (a,b,x,y)
{
printf(" TGF");
}
return 0;
}

Anonymous said...

What's the meaning of if(a,b,x,y) ?
I tried in TC.It gives following warnings:

a is assigned a value that is never used
b is assigned a value that is never used
x is assigned a value that is never used

-Ravi

Anonymous said...

ITS VERY USEFUL....
NICE TQ...

Gaurav Kr Garg said...

GAURAV GARG :
if you compile this in gcc then this code is not giving error
in gcc sizeof(void) = 1


#include
void main(){
if(sizeof(void))
printf("M. Muralilidaran");
else
printf("Harbhajan Singh");
}

Alampally Dilipkumar said...

please explain 13th question...........

Anonymous said...

this is very helpful


gebrehiwet
mekele ethiopia

Anonymous said...

this is very helpful. you have a good job. i wanna to appreciate for the owner of this site, this cqestions bank help to know the mystery of the c programming. even the beginner program learner , they can be easily understand.

by GEBREHIWET ABRHAM
MU=MEKELE UNIVERSITY
MEKELE INSTITUTE OF TECHNOLOGY(MIT)
DEPARTMENT BIOLOGICAL AND CHEMICAL ENGINEERING

coolsinu said...

very tricky questions.. Thanks a lot.. Keep it up

Anonymous said...

in gcc we can use sizeof(void)

Unknown said...

sizeof(void ) iam getting 1.. in dev c++ alo getting 1

CoronaMovies said...

Really Nice Question... Thankx For Sharing.... More Question Please......

Unknown said...

thanxs 4 sharing....really gud questions...

Unknown said...

That all question is help to the clear the all concept .....THANKS

Unknown said...

very good and thanks for help in improvement of our knowledge

Unknown said...

#include
#include
void main()
{
int a,b,c;
char d;
clrscr();
printf("Enter any two numbers for calculation\n");
scanf("%d%d",&a,&b);
printf("Guidance: if you enter + result is addition and like that all\n");
printf("Enter operators '+' '-' '*' '/' '%' which one you required\n");
scanf("%s",&d);/* when i used %c it not take operator */

if(d=='+')
{
c=a+b;
printf("result = %d",c);
}
else if(d=='-')
{
c=a-b;
printf("result = %d",c);
}
else if(d=='*')
{
c=a*b;
printf("result = %d",c);
}
else if(d=='/')
{
c=a/b;
printf("result = %d",c);
}
else if(d=='%')
{
c=a%b;
printf("result = %d",c);
}
else
{
printf("invalid operator\n");
}
getch();
}


problem is :
it take two number but
when i used %c it goes last "else" option and print invalid operator
when i used %s it give answer but this answer is that number which is input first but it does not perform operation
please someone tell me where i'am wrong

Unknown said...

please explain question no 13.plzzzzzzzzzzzzzzzzzzzzzzz