C Programming MCQ Questions and Answers on Conditional Statements 2

Image
C MCQ Questions from ExamTray.com

Learn C Programming MCQ Questions and Answers on Conditional Statements like Ternary Operator, IF, ELSE and ELSE IF statements. Easily attend exams after reading these Multiple Choice Questions.

Go through C Theory Notes on Conditional Operators before studying questions.



1) What is the output of C Program.?
int main()
{
    int x=1;
    float y = 1.0;
    if(x == y)
    {
        printf("Polo\n");
    }
    if( 1 == 1.0)
    {
        printf("Golf\n");
    }

    if( 1.0 == 1.0f )
    {
         printf("Boxing\n");
    }
    return 0;
}
A) No Output
B) Boxing
C) Golf Boxing
D) Polo Golf Boxing
Answer [=]
D
Explanation:

Integer is promoted to float or double automatically before comparison. So all are equal.

1 == 1.0 == 1.0f

2) What is the output of C Program.?
int main()
{
    int a=9;
    if(a=8)
    {
        printf("Kangaroo\n");
    }
    printf("Eggs\n");

    return 0;
}
A) No output
B) Eggs
C) Kangaroo Eggs
D) Compiler error
Answer [=]
C
Explanation:

a=8 is an assignment not comparison. IF( Non Zero) is always TRUE.

3) What is the output of C Program.?
int main()
{
    int a=9;
    if(a==5);
    {
        printf("Kangaroo\n");
    }
    printf("Eggs\n");

    return 0;
}
A) Eggs
B) Kangaroo Eggs
C) No output
D) Compiler error
Answer [=]
B
Explanation:

Notice a Semicolon at the end of IF(a==5);. So IF block ends without any statements. Also, { } is not part of IF now. So it is executed without checking for any condition.

4) What is the output of C Program.?
int main()
{
    int a=9;
    if(a==9);
    {
        printf("Ostrich\n");
    }
    elseif(a==8)
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}
A) White
B) Ostrich White
C) No Ouput
D) Compiler error
Answer [=]
D
Explanation:

Notice IFELSE statement. There should be one SPACE between IF and ELSE.

5) What is the output of C Program.?
int main()
{
    int a=9;
    if(a==9)
    {
        printf("Ostrich\n");
    }
    else;
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}
A) White
B) Ostrich White
C) Ostrich Eggs White
D) Compiler Error
Answer [=]
C
Explanation:

Notice a Semicolon (;) at the end of ELSE. So ELSE is terminated immediately. Eggs Printf block is not part of ELSE and is always called.

6) What is the output of C Program.?
int main()
{
    int a=9, b=5, c=8;
    a=b=c=10;
    if(a==9)
    {
        printf("Ostrich\n");
    }
    else
    {
        printf("Eggs\n");
    }
    
    printf("White");

    return 0;
}
A) Ostrich Eggs White
B) Ostrich White
C) Eggs White
D) Compiler error as you can not assign to more than two variables at once.
Answer [=]
C
7) What is the output of C Program.?
int main()
{
    int a=9, b=5, c=8;

    if(!(a==9))
    {
        printf("Bear\n");
    }
    else
    {
        printf("Elephant\n");
    }
    
    printf("Fox");

    return 0;
}
A) Bear Fox
B) Elephant Fox
C) Fox
D) Compiler error
Answer [=]
B
Explanation:

Logical Not Operator ( ! ) changes true to false and false to true. So IF(false) is not executed.



8) What is the Priority of C Logical Operators.? NOT (!), AND (&&) and OR (||)
A) NOT (!) > AND (&&)  > OR (||)
B) NOT (!) > AND (&&) = OR (||)
C) AND (&&) > OR (||) > NOT (!)
D) AND (&&) = OR (||) > NOT (!)
Answer [=]
A
Explanation:

Logical NOT Operator in C has the highest priority.

9) What is the output of C Program.?
int main()
{
    int a=9, b;
    
    b = (a==9) ? (printf("CAT\n");printf("DOG\n")) : (printf("FOX"));

    return 0;
}
A) CAT DOG
B) FOX
C) CAT DOG FOX
D) Compiler error
Answer [=]
D
Explanation:

You can not put more than 1 statement inside expression1 or expression2 inside Ternary Operator.

(Condition)?(expression1):(expression2)

10) What is the output of C Program.?
int main()
{
    int a=9, b=6;
    if(a==9 && b==6)
    {
        printf("Hockey");
    }
    else
    {
        printf("Cricket");
    }
    

    return 0;
}
A) Cricket Football
B) Hockey Football
C) Football
D) Compiler error
Answer [=]
B
Explanation:

== operator has more priority than &&. Logical && AND operator returns true only if both expressions are true.

11) What is the output of C Program.?
int main()
{
    int a=9, b=6;
    if(a!=9 || b==6)
    {
        printf("Hockey\n");
    }
    else
    {
        printf("Cricket\n");
    }
    
    printf("Football");

    return 0;
}
A) Cricket Football
B) Hockey Football
C) Football
D) Compiler error
Answer [=]
B
Explanation:

Logical OR || operator returns true if any one expression is true.

12) Choose a correct C Operator Priority.? Items in one group ( ) has same priority.
A) ( ! ) < (*, /, %) < (+, -) < ( <, <=, >, >=) < (==, !=) < (&&) < (||) < (=)
B) (( ! ) , (*, /, %) , (+, -)) < ( <, <=, >, >=) < (==, !=) < (&&) < (||) < (=)
C) ( ! ) > (*, /, %) > (+, -) > ( <, <=, >, >=) > (==, !=) > (&&) > (||) > (=)
D) (( ! ) , (*, /, %) , (+, -)) > ( <, <=, >, >=) > (==, !=) > (&&) > (||) > (=)
Answer [=]
C
Explanation:

( ! Logical NOT ) > (*, /, % Arithmetic) > (+, - Arithmetic) > ( <, <=, >, >= Relational) > (==, != Relational) > (&& Logical AND) > (|| Logical OR) > (= Assignment).

13) What is the output of C Program.?
int main()
{
    int a=5, b=8;
    
    if( a==5 && (b=9) )
    {
        printf("Gorilla Glass=");
    }
    printf("%d %d", a, b);

    return 0;
}
A) 5 8
B) 5 9
C) Gorilla Glass=5 8
D) Gorilla Glass=5 9
Answer [=]
D
Explanation:

In IF( a==5 && (b=9) ), && Operator checks both expressions for true or Non Zero value. So after checking a==5, b=9 is checked. Here b==9 is checking, but b=9 is assignment. Any NON-Zero value is true only. So b=9 now.

14) What is the output of C Program.?
int main()
{
    int a=5, b=8;
    
    if( a==5 || (b=9) )
    {
        printf("Gorilla Glass=");
    }
    printf("%d %d", a, b);

    return 0;
}
A) 5 8
B) 5 9
C) Gorilla Glass=5 8
D) Gorilla Glass=5 9
Answer [=]
C
Explanation:

Logical OR || checks wants only one TRUE condition. First expression (a==5) or even (a=5) is true. So second expression (b=9) is not evaluated and assignment not done. So b=8 only.



15) Choose a correct C Statement.
A) Nesting of ? : operator is possible.
B)
int main()
{
    int a=5, b=8;
    
    if( a>=5 || (b=9) )
    {
        printf("Gorilla Glass");
    }

    return 0;
}
//OUTPUT: Gorilla Glass
C)
int main()
{
    int a=5, b=8;
    
    if( a >= 5 && b <= 9 )
    {
        printf("Gorilla Glass");
    }

    return 0;
}
//OUTPUT: Gorilla Glass
D) All the above
Answer [=]
D