Multiple Choice Questions and Answers for If-then-else Statements in C Language
https://www.computersprofessor.com/2017/09/multiple-choice-questions-and-answers_56.html
1. The output of the code below is
#includevoid main()
{int x = 5;
if (x < 1)
printf("hello");
if (x == 5)
printf("hi");
elseprintf("no");
}
a) hi
b) hello
c) no
d) None of the mentioned
b) hello
c) no
d) None of the mentioned
Answer:a
2. The output of the code below is
#includeint x;
void main()
{if (x)
printf("hi");
elseprintf("how are u");
}
a) hi
b) how are you
c) Compile time error
d) None of the mentioned
b) how are you
c) Compile time error
d) None of the mentioned
Answer:b
3. Comment on the following code below
#includevoid main()
{int x = 5;
if (true);
printf("hello");
}
a) It will display hello
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent
Answer:b
4. The output of the code below is
#includevoid main()
{int x = 0;
if (x == 0)
printf("hi");
elseprintf("how are u");
printf("hello");
}
a) hi
b) how are you
c) hello
d) hihello
b) how are you
c) hello
d) hihello
Answer:d
5. The output of the code below is
#includevoid main()
{int x = 5;
if (x < 1);
printf("Hello");
}
a) Nothing
b) Run time error
c) Hello
d) Varies
b) Run time error
c) Hello
d) Varies
Answer:c
6. The output of the code below is(when 1 is entered)
#includevoid main()
{double ch;
printf("enter a value btw 1 to 2:");
scanf("%lf", &ch);
switch (ch)
{case 1:
printf("1");
break;
case 2:
printf("2");
break;
}}
a) Compile time error
b) 1
c) 2
d) Varies
b) 1
c) 2
d) Varies
Answer:a
7. The output of the code below is(When 1 is entered)
#includevoid main()
{char *ch;
printf("enter a value btw 1 to 3:");
scanf("%s", ch);
switch (ch)
{case "1":
printf("1");
break;
case "2":
printf("2");
break;
}}
a) 1
b) 2
c) Compile time error
d) No Compile time error
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?
#includevoid main()
{int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch)
{case 1:
printf("1\n");
default:
printf("2\n");
}}
a) 1
b) 2
c) 1 2
d) Run time error
b) 2
c) 1 2
d) Run time error
Answer:c
9. When 2 is entered, The output of the code below is?
#includevoid main()
{int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch)
{case 1:
printf("1\n");
break;
printf("Hi");
default:
printf("2\n");
}}
a) 1
b) Hi 2
c) Run time error
d) 2
b) Hi 2
c) Run time error
d) 2
Answer:d
10. When 1 is entered, The output of the code below is?
#includevoid main()
{int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch, ch + 1)
{case 1:
printf("1\n");
break;
case 2:
printf("2");
break;
}}
a) 1
b) 2
c) 3
d) Run time error
b) 2
c) 3
d) Run time error
Answer:b
11. What is the output of this C code?
#includeint main()
{int x = 1;
if (x > 0)
printf("inside if\n");
else if (x > 0)
printf("inside elseif\n");
}
a) inside if
b) inside elseif
c) inside if
inside elseif
d) Compile time error
b) inside elseif
c) inside if
inside elseif
d) Compile time error
Answer:a
12. What is the output of this C code?
#includeint main()
{int x = 0;
if (x++)
printf("true\n");
else if (x == 1)
printf("false\n");
}
a) true
b) false
c) Compile time error
d) Undefined behaviour
b) false
c) Compile time error
d) Undefined behaviour
Answer:b
13. What is the output of this C code?
#includeint main()
{int x = 0;
if (x == 1)
if (x == 0)
printf("inside if\n");
elseprintf("inside else if\n");
elseprintf("inside else\n");
}
a) inside if
b) inside else if
c) inside else
d) Compile time error
b) inside else if
c) inside else
d) Compile time error
Answer:c
14. What is the output of this C code?
#includeint main()
{int x = 0;
if (x == 0)
printf("true, ");
else if (x = 10)
printf("false, ");
printf("%d\n", x);
}
a) false, 0
b) true, 0
c) true, 10
d) Compile time error
b) true, 0
c) true, 10
d) Compile time error
Answer:b
15. What is the output of this C code?
#includeint main()
{int x = 0;
if (x == 1)
if (x >= 0)
printf("true\n");
elseprintf("false\n");
}
a) true
b) false
c) Depends on the compiler
d) No print statement
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?
#includeint main()
{int a = 1;
if (a--)
printf("True");
if (a++)
printf("False");
}
a) True
b) False
c) True False
d) No Output
b) False
c) True False
d) No Output
Answer:a
19. Comment on the output of this C code?
#includeint main()
{int a = 1;
if (a)
printf("All is Well ");
printf("I am Well\n");
elseprintf("I am not a River\n");
}
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
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?
#includeint main()
{if (printf("%d", printf(")))
printf("We are Happy");
else if (printf("1"))
printf("We are Sad");
}
a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) Compile time error
b) 1We are Happy
c) 1We are Sad
d) Compile time error
Answer:d
