C Programming Questions and Answers on If-then-else Statement for Freshers

https://www.computersprofessor.com/2017/11/c-programming-questions-and-answers-on_26.html
1. The output of the code below is
#include
void main()
{
int x = 5;
if (x < 1)
printf("hello");
if (x == 5)
printf("hi");
else
printf("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
#include
int x;
void main()
{
if (x)
printf("hi");
else
printf("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
#include
void 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
#include
void main()
{
int x = 0;
if (x == 0)
printf("hi");
else
printf("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
#include
void 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)
#include
void 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)
#include
void 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?
#include
void 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?
#include
void 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?
#include
void 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?
#include
int 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?
#include
int 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?
#include
int main()
{
int x = 0;
if (x == 1)
if (x == 0)
printf("inside if\n");
else
printf("inside else if\n");
else
printf("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?
#include
int 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?
#include
int main()
{
int x = 0;
if (x == 1)
if (x >= 0)
printf("true\n");
else
printf("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?
#include
int 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?
#include
int main()
{
int a = 1;
if (a)
printf("All is Well ");
printf("I am Well\n");
else
printf("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?
#include
int 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