If-Else C Programming Mock Test – 2


Question 1
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a == 5);
    {
        printf("Mock Test ");
    }
    printf("practicepaper.in");
    return 0;
}
A
Mock Test
B
practicepaper.in
C
Mock Test practicepaper.in
D
Compiler Error
   C Programming
Question 1 Explanation: 
In the above program, if statement is with (;) at the end.
(;) at the end of if statement indicates empty body of if block.
Therefore above program can be rewritten as
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a == 5)
    {//empty body of if due to (;)
    }
    {
        printf("Mock Test ");
    }
    printf("practicepaper.in");
    return 0;
}
In this code, both printf statements always executes irrespective of condition becomes true or false.
OUTPUT : Mock Test practicepaper.in
Question 2
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a == 5);
    {
        printf("Mock Test");
    }
    else
    {
        printf("practicepaper.in");
    }
    return 0;
}
A
Mock Test
B
practicepaper.in
C
Mock Test practicepaper.in
D
Compiler Error
   C Programming
Question 2 Explanation: 
In the above program, if statement is with (;) at the end.
(;) at the end of if statement indicates empty body of if block.
Therefore above program can be rewritten as
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a == 5)
    {//empty body of if due to (;)
    }
    {
        printf("Mock Test");
    }
    else
    {
        printf("practicepaper.in");
    }
    return 0;
}
In this code, condition of block of if must be above the else block violated.
Hence, Compiler report error as "else without a previous if".
OUTPUT : Compiler Error


Question 3
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a = 0)
    {
        printf("IITB");
    }
    else
    {
        printf("IISC");
    }
    return 0;
}
A
IITB
B
IISC
C
IITB IISC
D
Compiler Error
   C Programming
Question 3 Explanation: 
In the above program, statement if(a=0) evaluated by compiler as 0 transfer to a and expression a=0 return 0.
Compiler consider 0 as false. Hence, condition part of if statement become false.
Therefore, else block gets executed by compiler and print IISC.
OUTPUT : IISC
Question 4
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a = 2;
    if(a = -1)
    {
        printf("IITB");
    }
    else
    {
        printf("IISC");
    }
    return 0;
}
A
IITB
B
IISC
C
IITB IISC
D
Compiler Error
   C Programming
Question 4 Explanation: 
In the above program, statement if(a=-1) evaluated by compiler as (-1) transfer to a and expression a=-1 return -1.
Coiler consider (-1) as true.
NOTE: Compiler evaluate any non-zero value as true.
Hence, condition part of if statement become true.
Therefore, if block gets executed by compiler and print IITB.
OUTPUT : IITB
Question 5
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=2;
    if(5 == a)
    {
        printf("Mock Test ");
    }
    printf("practicepaper.in");
    return 0;
}
A
Mock Test
B
practicepaper.in
C
Mock Test practicepaper.in
D
Compiler Error
   C Programming
Question 5 Explanation: 
In the above program, statement if(5 == a) evaluated by compiler as if(0) as expression 5==a is false and return 0.
Zero value is considered as false.
So, condition part of if statement becomes false.
OUTPUT : practicepaper.in


There are 5 questions to complete.

Leave a Comment