C Programming Questions and Answers – If-then-else Statements – 2

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “If-then-else Statements – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 1;
  5.         if (x > 0)
  6.             printf("inside if\n");
  7.         else if (x > 0)
  8.             printf("inside elseif\n");
  9.     }

a) inside if
b) inside elseif
c)

inside if
inside elseif
advertisement
advertisement

d) compile time error
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

2. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 0;
  5.         if (x++)
  6.             printf("true\n");
  7.         else if (x == 1)
  8.             printf("false\n");
  9.     }

a) true
b) false
c) compile time error
d) undefined behaviour
View Answer

Answer: b
Explanation: None.
advertisement

3. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 0;
  5.         if (x == 1)
  6.             if (x == 0)
  7.                 printf("inside if\n");
  8.             else
  9.                 printf("inside else if\n");
  10.         else
  11.             printf("inside else\n");
  12.     }

a) inside if
b) inside else if
c) inside else
d) compile time error
View Answer

Answer: c
Explanation: None.

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 0;
  5.         if (x == 0)
  6.             printf("true, ");
  7.         else if (x = 10)
  8.             printf("false, ");
  9.         printf("%d\n", x);
  10.     }

a) false, 0
b) true, 0
c) true, 10
d) compile time error
View Answer

Answer: b
Explanation: None.

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 0;
  5.         if (x == 1)
  6.             if (x >= 0)
  7.                 printf("true\n");
  8.             else
  9.                 printf("false\n");
  10.     }

a) true
b) false
c) Depends on the compiler
d) No print statement
View Answer

Answer: d
Explanation: None.

6. The C statement “”if (a == 1 || b == 2) {}”” can be re-written as ___________
a)

    if (a == 1)
    if (b == 2){}

b)

    if (a == 1){}
    if (b == 2){}

c)

    if (a == 1){}
    else if (b == 2){}

d) none of the mentioned
View Answer

Answer: d
Explanation: None.

7. Which of the following is an invalid if-else statement?
a) if (if (a == 1)){}
b) if (func1 (a)){}
c) if (a){}
d) if ((char) a){}
View Answer

Answer: a
Explanation: None.

8. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1;
  5.         if (a--)
  6.             printf("True");
  7.             if (a++)
  8.                 printf("False");
  9.     }

a) True
b) False
c) True False
d) No Output
View Answer

Answer: a
Explanation: None.

9. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1;
  5.         if (a)
  6.             printf("All is Well ");
  7.             printf("I am Well\n");
  8.         else
  9.             printf("I am not a River\n");
  10.     }

a) Output will be All is Well I am Well
b) Output will be I am Well I am not a River
c) Output will be I am Well
d) Compile time errors during compilation
View Answer

Answer: d
Explanation: None.

10. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         if (printf("%d", printf(")))
  5.             printf("We are Happy");
  6.         else if (printf("1"))
  7.             printf("We are Sad");
  8.     }

a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) compile time error
View Answer

Answer: d
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.