If-Else C Programming Mock Test – 3

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
Question 6
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 6 Explanation: 
In the above program, statement if(5=a) evaluated by compiler as value of a transfer to 5
Compiler report error as lvalue required as left operand of assignment. NOTE : we can not transfer value of variable in to constant.
OUTPUT : Compiler Error
Question 7
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=20,b=15,c=10;
    if(a>b>c)
    {
        printf("IITB ");
    }
    else
    {
    printf("IISC");
    }
    return 0;
}
A
IITB
B
IISC
C
IITB IISC
D
Compiler Error
   C Programming
Question 7 Explanation: 
In the above program, for the statement if(a>b>c), compiler first evaluates the expression a>b>c.
Here, associativity of (>) is left to right. So, first a>b gets evaluated by compiler as true (or 1). After that, expression becomes 1>c, which is false.
So, condition part of if statement results as false.
Therefore, else part of program executed by compiler.
OUTPUT : IISC
Question 8
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=3,b=2,c=0;
    if(a+=b+=c+=-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 8 Explanation: 
In the above program, for the statement if(a+=b+=c+=-5), compiler first evaluates the expression a+=b+=c+=-5.
Here, associativity of (+=) is right to left. So, first c+= -5 gets evaluated by compiler as c=c+(-5)
c = -5 and expression return -5.
So, remaining expression become a+=b+= -5,
Next, compiler evaluate b+= -5 as b=b+(-5) which store -3 in b and whole expression return -3.
Remaining expression as a+= -3
Compiler evaluate it as a=a+(-3)
a=3-3=0.
So, 0 stored in a and whole expression return 0.

So finally, statement becomes if(0), here 0 is considered as false, which execute else block.
OUTPUT : practicepaper.in
Question 9
What is the output of the following program?
#include < stdio.h > 
int main()
{
 int a;
 if(sizeof(char)>-1 )
  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 9 Explanation: 
In the above program, for the statement if(sizeof(char)>-1), compiler first evaluates the expression sizeof(char)>-1
Sizeof(char) return 1 as size of char data type is 1 Byte.
NOTE: Return type of sizeof is unsigned int.

Therefore, compiler evaluate expression sizeof(char)>-1 as both operands are unsigned.
By default, compiler use 2's compliment method to store negative number
So, -1 is stored as binary 11111111 11111111 in 2's complement form.
Now, for expression sizeof(char)>-1
-1 is considered as unsigned number which is higher than 1 return by sizeof(char).
Therefore, sizeof(char)>-1 return false.
So, else block gets executed by compiler.
OUTPUT : practicepaper.in
Question 10
What is the output of the following program?
#include < stdio.h > 
int main()
{
 int a,b,c;
 a=b=c=5;
 if(a==b==c)
  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 10 Explanation: 
In the above program, for the statement if(a==b==c), compiler first evaluates the expression (a==b==c)
Here, associativity of (==) is left to right. So, first a==b gets evaluated by compiler which return true (or 1)
So, expression become 1==c, which is false.
Therefore, finally expression (a==b==c) evaluated as false. So, else block gets executed.
OUTPUT : practicepaper.in
There are 10 questions to complete.

Leave a Comment

Like this FREE website? Please share it among all your friends and join the campaign of FREE Education to ALL.