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

«
»
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone preparing for Infosys and other companies C interviews. One should practice these 1000+ interview questions and answers continuously for 2-3 months to clear Infosys interviews on C Programming language.

Here is a listing of C test questions on “If-then-else Statements” along with answers, explanations and/or solutions:

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

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

2. 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++)
  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?

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

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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter