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

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

C# Questions & Answers on Inheritance Implementation for Freshers

1. Select the correct output for the given set of code? class sample { public int i; void display() { Console.WriteLine(i); } } class sample1 : s...

C# Questions & Answers on Method Overloading for Freshers

1. The process of defining two or more methods within the same class that have same name but different parameters list? a) Method overloadingb) Method overridingc) Encapsulationd) None of the menti...

C# Questions & Answers on Fundamentals of Inheritance for Freshers

1. Which procedure among the following should be used to implement a ‘Has a’ or a ‘Kind of’ relationship between two entities? a) Polymorphismb) Inheritancec) Templatesd) All of the mentioned Answ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item