If Statement Quiz in C

If 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.

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.

If you are finding it difficult to answer these questions then you should first learn these:- If-else statement in C, Programs on if-else in C

Note:- In C, 0 is the false and the non-zero value (both positive and negative) is true.

Q1) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(0)
  {
     printf("x");
  }
  printf("y");
  printf("z");
  return 0;
}

a) xyz
b) Compile time error
c) yz
d) x

View Answer Answer:- c) yz

Q2) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a;
  a = 0, 1;
  if(a)
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) Compile time error
b) z
c) xy
d) xyz

View Answer Answer:- b) z

The comma operator evaluates to its rightmost operand. So, a=0 and condition become false.

Q3) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if("false")
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) z
b) Compile time error
c) xy
d) xyz

View Answer Answer:- d) xyz

The “false” is a valid string so condition becomes true, and if block executed.

Q4) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(5%5);
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) z
b) xyz
c) xy
d) Compile time error

View Answer Answer:- b) xyz

Note that there is a semicolon after if(5%5), so if block completed. printf(“x”); and printf(“y”) is not the part of if block statement.

Q5) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(0)
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) z
b) xyz
c) Compile time error
d) xy

View Answer Answer:- a) z

Q6) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a = 0, 1;
  if(a)
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) xy
b) Compile time error
c) xyz
d) z

View Answer Answer:- b) Compile time error

Compile-time error due to int a=0,1; Here invalid declaration of variable.

Q7) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if('a')
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) xy
b) xyz
c) z
d) Compile time error

View Answer Answer:- b) xyz

ASCII value of ‘a’ is 97. So, the condition becomes true and display xy from the if block then from outside of if block z is displayed.

Q8) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if('a')
  printf("x");
  printf("y");
  printf("z");
  return 0;
}

a) xy
b) z
c) xyz
d) Compile time error

View Answer Answer:- c) xyz

Q9) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if()
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) xyz
b) z
c) Compile time error
d) xy

View Answer Answer:- c) Compile time error

Compile-time error; In C language if is a conditional statement and without condition, it can’t be executed.

Q10) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a;
  a = (0, 1);
  if(a)
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) Compile time error
b) z
c) xy
d) xyz

View Answer Answer:- d) xyz

Here () is used in a = (0,1); so expressions evaluated from left to right, Hence a=1; The if condition becomes true and if block executed.

Q11) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if("")
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) xy
b) z
c) xyz
d) Compile time error

View Answer Answer:- c) xyz

The empty string has a null character which value is other than zero so if statement becomes true.

Q12) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(" ")
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) xy
b) x
c) Compile time error
d) xyz

View Answer Answer:- d) xyz

The value of string blank space is not zero so the condition of if block becomes true and displayed xy from if block. After that z is displayed from outside from the if block.

Q13) Find the output of the given C program.

#include<stdio.h>
int main()
{
  if(0)
  printf("x");
  printf("y");
  printf("z");
  return 0;
}

a) xyz
b) x
c) Compile time error
d) yz

View Answer Answer:- d) yz

Q14) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a=0;
  if(a++)
  {
    printf("x");
    printf("y");
  }
  printf("z");
  return 0;
}

a) z
b) Compile time error
c) xyz
d) xy

View Answer Answer:- a) z

Postfix increment operator so first value assigned and then increment. Hence condition becomes false.

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!

Leave a Comment

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