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

«
»
This section on C programming questions and answers focuses on “If-then-else Statements”. One shall practice these questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming 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.     void main()
  3.     {
  4.         int x = 5;
  5.         if (x < 1)
  6.             printf("hello");
  7.         if (x == 5)
  8.             printf("hi");
  9.         else
  10.             printf("no");
  11.     }

a) hi
b) hello
c) no
d) error
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

  1.     #include <stdio.h>
  2.     int x;
  3.     void main()
  4.     {
  5.         if (x)
  6.             printf("hi");
  7.         else
  8.             printf("how are u");
  9.     }

a) hi
b) how are you
c) compile time error
d) error
View Answer

Answer: b
Explanation: None.
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5;
  5.         if (true);
  6.             printf("hello");
  7.     }

a) It will display hello
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 0;
  5.         if (x == 0)
  6.             printf("hi");
  7.         else
  8.             printf("how are u");
  9.             printf("hello");
  10.     }

a) hi
b) how are you
c) hello
d) hihello
View Answer

Answer: d
Explanation: None.

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

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5;
  5.         if (x < 1);
  6.             printf("Hello");
  7.  
  8.     }

a) Nothing
b) Run time error
c) Hello
d) Varies
View Answer

Answer: c
Explanation: None.

6. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         double ch;
  5.         printf("enter a value between 1 to 2:");
  6.         scanf("%lf", &ch);
  7.         switch (ch)
  8.         {
  9.            case 1:
  10.              printf("1");
  11.              break;
  12.            case 2:
  13.              printf("2");
  14.              break;
  15.         }
  16.     }

a) Compile time error
b) 1
c) 2
d) Varies
View Answer

Answer: a
Explanation: None.

7. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *ch;
  5.         printf("enter a value between 1 to 3:");
  6.         scanf("%s", ch);
  7.         switch (ch)
  8.         {
  9.            case "1":
  10.               printf("1");
  11.               break;
  12.            case "2":
  13.               printf("2");
  14.               break;
  15.         }
  16.     }

a) 1
b) 2
c) Compile time error
d) No Compile time error
View Answer

Answer: c
Explanation: None.

8. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch)
  8.         {
  9.            case 1:
  10.               printf("1\n");
  11.            default:
  12.               printf("2\n");
  13.         }
  14.     }

a) 1
b) 2
c) 1 2
d) Run time error
View Answer

Answer: c
Explanation: None.

9. What will be the output of the following C code? (Assuming that we have entered the value 2 in the standard input)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch)
  8.         {
  9.            case 1:
  10.               printf("1\n");
  11.               break;
  12.               printf("Hi");
  13.            default:
  14.               printf("2\n");
  15.         }
  16.     }

a) 1
b) Hi 2
c) Run time error
d) 2
View Answer

Answer: d
Explanation: None.

10. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch, ch + 1)
  8.         {
  9.            case 1:
  10.               printf("1\n");
  11.               break;
  12.            case 2:
  13.               printf("2");
  14.               break;
  15.         }
  16.     }

a) 1
b) 2
c) 3
d) Run time error
View Answer

Answer: b
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