1. Question: 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");
    }

    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

    Note: “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.
    1. Report
  2. Question: 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;
    }

    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

    Note: 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.
    1. Report
  3. Question: 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;
    }

    A
    It will print nothing

    B
    0

    C
    1

    D
    Compilation error: Misplaced else

    E
    Infinite loop

    Note: 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.
    1. Report
  4. Question: 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");
    }

    A
    5 10

    B
    6 11

    C
    6 10

    D
    5 11

    E
    John Terry

    Note: 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.
    1. Report
  5. Question: 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;
    }

    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 +++

    Note: Think yourself.
    1. Report
  6. Question: 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);
    }

    A
    The Godfather

    B
    1

    C
    0

    D
    Compilation error

    E
    None of the above

    Note: 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
    1. Report
  7. Question: 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

    Note: 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.
    1. Report
  8. Question: 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

    Note: 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.
    1. Report
  9. Question: 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

    Note: 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
    1. Report
  10. Question: 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

    Note: 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.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd