If Else Statement Quiz in C

If Else Statement Quiz in C | The if-else statement in C is a selection statement. When dealing with selection statements, there are generally three versions: one-way, two-way, and multi-way. You can check your knowledge through this If else statement quiz in C.

If you are finding it difficult to answer these questions then you should first learn these:- If-else statement in CPrograms on if-else in C. Also See:- If Statement Quiz in C.

One way decision statements do a particular thing or they do not. Two-way decision statements can do one thing or do another. Multi-way decision statements can do one of many different things depending on the value of an expression.

In if block, there was only one-way i.e. when the condition is true then the if block will be executed. Now, in the if-else block, we will provide a piece of optional information to the end-user when the condition has failed. When the program is having two options and only one option can be true then we use an if-else statement. When the condition is true then the if block is executed otherwise else block is executed. Now, let us start solving the If else statement quiz in C.

If Else Statement Quiz

Q1) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(-5)
  printf("a");
  printf("b");
  else
  printf("c");
  printf("d");
  return 0;
}

a) abd
b) cd
c) Compile time error
d) ab

View Answer Answer:- c) Compile time error

Only printf(“a”); is inside if block and if block ended. printf(“b”); is outside of if block. After that else part is started but the compiler doesn’t find if block for the else block, So it gives error: ‘else’ without a previous ‘if’. In C language, else part for if statement is optional. We can write a C program without else when we use if statement, But if block is compulsory for else block. We can’t write any else statement without if block.

Q2) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(-5)
  {
    printf("a");
    printf("b");
  }
  else
  printf("c");
  printf("d");
  return 0;
}

a) abd
b) ab
c) cd
d) Compile time error

View Answer Answer:- a) abd

The condition is -5 (other than zero) so, condition becomes true. Now, due to {} printf(“a”); and printf(“b”); are inside if block. Only, printf(“c”); is inside else block so, it is skipped, and next line printf(“c”); executed.

Q3) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(-5)
  {
    printf("a");
    printf("b");
  }
  else
  {
    printf("c");
    printf("d");
  }
  return 0;
}

a) abd
b) Compile time error
c) ab
d) cd

View Answer Answer:- c) ab

Q4) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(-5);
  {
    printf("a");
    printf("b");
  }
  else
  {
    printf("c");
    printf("d");
  }
  return 0;
}

a) Compile time error
b) cd
c) ab
d) abd

View Answer Answer:- a) Compile time error

Compile-time error; There is a semicolon after if, so if block ended and there is no any if block for else part. Hence error: ‘else’ without a previous ‘if’

Q5) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(-5)
  {
    printf("a");
    printf("b");
  }
  else;
  {
    printf("c");
    printf("d");
  }
  return 0;
}

a) cd
b) ab
c) abcd
d) Compile time error

View Answer Answer:- c) abcd

There is a semicolon after else so else part ended. Now, printf(“c”); and printf(“d”); are outside of else part.

Q6) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if("true") printf("a");
  else printf("b");
  return 0;
}

a) error
b) a
c) b
d) ab

View Answer Answer:- b) a

The condition “true” is a string, so if condition will be executed.

Q7) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(sizeof('a')==sizeof("abc"))
  printf("a");
  else printf("b");
  return 0;
}

a) error
b) b
c) ab
d) a

View Answer Answer:- d) a

The sizeof(‘a’) is 4 bytes because ASCII value ‘a’ is 97 and the sizeof(“abc”) is also 4 bytes because ‘a’, ‘b’, ‘c’ and, ‘\0’ takes 4 bytes memory. Hence condition becomes true.

Q8) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(5 && 5 == 5) printf("true");
  else printf("false");
  return 0;
}

a) error
b) true
c) None of these
d) false

View Answer Answer:- b) true

Relational operator have more priority than logical operator. So,
(5 && (5 == 5))
Or, (5 && 1)
Or, 1
Hence condition becomes true and printf(“true”); executed.

Q9) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if((5 && 5) == 5) printf("true");
  else printf("false");
  return 0;
}

a) false
b) error
c) None of these
d) true

View Answer Answer:- a) false

((5 && 5) == 5)
Or, (1 == 5)
Or, 0
Hence, the condition becomes false.

Q10) Find the output of the given C program.

#include<stdio.h>
int main()
{
  float a = 1.2;
  if(1.2==a) printf("true");
  else printf("false");
  return 0;
}

a) false
b) error
c) true
d) None of these

View Answer Answer:- a) false

The value of variable a is of float data type but literal 1.2 (in if statement condition) is of the double data type. Compiler by default treat the floating-point number as of double data type. float(1.2) and double(1.2) are not same. Due to this condition becomes false.

Q11) Find the output of the given C program.

#include<stdio.h>
int main()
{
  float a = 1.0;
  if(1.0==a) printf("true");
  else printf("false");
  return 0;
}

a) None of these
b) false
c) true
d) error

View Answer Answer:- c) true

float a = 1.0 and, double 1.0 stores same value.

Q12) Find the output of the given C program.

#include<stdio.h>
int main()
{
  float a = 1.3;
  if(1.3f==a) printf("true");
  else printf("false");
  return 0;
}

a) false
b) None of these
c) true
d) error

View Answer Answer:- c) true

Suffix f converted 1.3 value from double to float and condition becomes true.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

3 thoughts on “If Else Statement Quiz in C”

Leave a Comment

Your email address will not be published. Required fields are marked *