Pages

Wednesday, February 26, 2020

C Programming if statement predict output MCQs Set-1 Answer

C PROGRAMMING IF STATEMENT PREDICT OUTPUT SET-1 ANSWER


Program - 1

#include<stdio.h>
int main()
{
  int score =  100;
  if ( score != 100 )
    printf("You win ");
  else
    printf("You lose ");

  printf("best prize.");

  return 0;
}
A. You win
B. You lose
C. You lose best prize.
D. You win best prize.

ANSWER: C
Explanation: Condition (score != 100) is evaluated to false, hence code inside else part will be executed. Also, last printf is not a part of if..else statement as we are not using any { }, hence final output becomes "You lose best prize."

Program - 2

#include<stdio.h>
int main()
{
    int num1=5, num2=4, num3=3;

    if(num1 > num2 && num1 > num3)
    {
         printf("Number1.");
    }
    if(num2 > num1 || num2 > num3)
    {
         printf("Number2.");
    }
    if(num3 > num1 && num3 > num2)
    {
         printf("Number3.");
    }
    return 0;
}
A. Number1.
B. Number2.
C. Number3.
D. Number1.Number2
E. Number1.Number2.Number3

ANSWER: D

Explanation:
  • Observe conditions of first if -> num1>num2 is TRUE, num1>num3 is also TRUE; TRUE && TRUE results in TRUE, hence first if blok will be executed.
  • Observe second if -> num2 > num1 is FALSE, num2 > num3 is TRUE; FALSE || TRUE results in TRUE, hence second if block will be executed.
  • Observe third if -> num3>num1 is FALSE, num3>num2 is FALSE; FALSE && FALSE results in FALSE, hence third if block will not be executed.
  • Hence final result will be "Number1.Number2"

Program - 3

#include<stdio.h>

int main()
{
    int time=0;
 
    if(time < 12)
    {
        printf("Good Morning...");
    }
    printf("Good Day...");
    return 0;
}
A. Good Morning...
B. Good Day...
C. Good Morning...Good Day...
D. Syntax error

ANSWER: C
Explanation: Condition inside if statement, i.e. time < 12 is true, hence both the printf statements will be executed resulting in output as "Good Morning...Good Day..."

Program - 4

#include<stdio.h>
int main()
{
    int num1=15, num2=10;

    if(num1 > num2)
    {
        printf("Num1 is big..");
    }
    if(num2 > num1)
    {
        printf("Num2 is big..");
    }
    if(num1 = num2)
    {
        printf("Num1 and Num2 are equal.");
    }
    return 0;
}
A. Num1 is big.
B. Num2 is big.
C. Num1 and Num2 are equal.
D. Num1 is big..Num1 and Num2 are equal.
E. Syntax error

ANSWER: D
Explanation: 
  • Condition in first if TRUE, which prints "Num1 is big.."
  • Condition in second if is FALSE, hence second if block will be skipped.
  • Statement inside third if (assignment statement) will be executed, which returns non-zero number. In C, any non-zero number is considered as TRUE, hence code inside if will be executed; which results in final output as "Num1 is big..Num1 and Num2 are equal."
Program - 5

#include<stdio.h>
void main()
{
  int number=0;

  if(number > 0)
     printf("Number is positive.");

  if(number => 0)
     printf("Number is not positive.");
}
A. Number is positive.
B. Number is not positive.
C. Number is positive.Number is not positive.
D. Syntax error

ANSWER: D

Explanation: In second if statement, operator used is "=>" which results in syntax error. The correct operator is "<=".


Read more interesting C Programming MCQs here.

< Back to Home >

No comments:

Post a Comment