Thursday, March 1, 2012

if-else set 1

(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: D

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:E

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:a,d,e

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:a

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:c,e

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 complier 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:d

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:e

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:a

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:b

“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:a,e

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.

No comments:

Post a Comment