Multiple Choice Questions and Answers for If-then-else Statements in C Language

1. The output of the code below is
  1.     #include 
  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) None of the mentioned
Answer:a 
2. The output of the code below is
  1.     #include 
  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) None of the mentioned
Answer:b 
3. Comment on the following code below
  1.     #include 
  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

Answer:b 

4. The output of the code below is
  1.     #include 
  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
Answer:d 
5. The output of the code below is
  1.     #include 
  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
Answer:c 
6. The output of the code below is(when 1 is entered)
  1.     #include 
  2.     void main()
  3.     {
  4.         double ch;
  5.         printf("enter a value btw 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
Answer:a 
7. The output of the code below is(When 1 is entered)
  1.     #include 
  2.     void main()
  3.     {
  4.         char *ch;
  5.         printf("enter a value btw 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
Answer:c 
8. When 1 is entered, The output of the code below is?
  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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
Answer:c 
9. When 2 is entered, The output of the code below is?
  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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
Answer:d 
10. When 1 is entered, The output of the code below is?
  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 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
Answer:b
11. What is the output of this C code?
  1.     #include 
  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
d) Compile time error
Answer:a 
12. What is the output of this C code?
  1.     #include 
  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
Answer:b 
13. What is the output of this C code?
  1.     #include 
  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
Answer:c 
14. What is the output of this C code?
  1.     #include 
  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
Answer:b 
15. What is the output of this C code?
  1.     #include 
  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
Answer:d 
16. if (a == 1||b == 2){} can be 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
Answer:d 
17. 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){}
Answer:a 
18. What is the output of this C code?
  1.     #include 
  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
Answer:a 
19. Comment on the output of this C code?
  1.     #include 
  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
Answer:d 
20. What is the output of this C code?
  1.     #include 
  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
Answer:d

Related

Multiple Choice Questions 8812235785481418961

Post a Comment

emo-but-icon

item