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 >

Tuesday, February 25, 2020

C Programming if statement predict output MCQs Set-1

C Programming if statement predict output Set-1

Predict output of following C programs based on if statements

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.

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

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

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

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

Check your Answer here...


Read more interesting C Programming MCQs here.



Sunday, February 23, 2020

C Programming for loop predict output MCQs Set - 1 Answers

C Programming for loop predict output (MCQs) Set - 1 Answers

1. Predict output of following C program:
#include <stdio.h>
void main()
{
    int c = 0;
    for (c)
        printf("Top C Practicals");
}
A. Compile time error
B. Top C Practicals
C. no output
D. none of these

ANSWER: A
Explanation: C programming for loop needs three expressions/statements inside for loop.

2. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i = 0;
    for ( ; i<5 ; i++)
        printf("%d ", i);
    return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. Compile time error
D. none of these

ANSWER: A
Explanation: Initial value of i will be considered as 0. Hence output will be 0 1 2 3 4

3. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i=0, j=5;
    printf("Hello ");
    for (i; i>j; i++)
        printf("%d ", i);
    printf("Students ");
    return 0;
}
A. Hello 0 1 2 3 4 Students
B. Hello Students
C. Hello 0 1 2 3 4
D. 0 1 2 3 4 5

ANSWER: B
Explanation: Initial value of i is 0 and j is 5; Hence i>j will be evaluated to false. Hence for loop will be executed zero times; thus answer is Hello Students.

4. Predict output of following C program:
#include <stdio.h>
int main()
{
   int i;
   for (i=0; i<10; i++)
       printf("%d ", ++i);
   return 0;
}
A. 1 3 5 7 9
B. 0 1 2 3 4 5 6 7 8 9
C. 2 4 6 8
D. 2 4 6 8 10

ANSWER: A
Explanation: Due to ++i (pre-increment) inside loop, the output will be 1 3 5 7 9

5. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i;
    for (i=65; i<70; i++)
        printf("%c ", i);
    return 0;
}
A. 65 66 67 68 69
B. A B C D E
C. Compile time error
D. Run time error

ANSWER: B
Explanation: %c in the printf will print character associated with the ASCII value of 65 to 69. 

* * * * *



Read more interesting C Programming MCQs here.


c programming for loop predict output MCQs set-1

C Programming for loop predict output (MCQs) Set - 1

1. Predict output of following C program:
#include <stdio.h>
void main()
{
    int c = 0;
    for (c)
        printf("Top C Practicals");
}
A. Compile time error
B. Top C Practicals
C. no output
D. none of these

2. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i = 0;
    for ( ; i<5 ; i++)
        printf("%d ", i);
    return 0;
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. Compile time error
D. none of these

3. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i=0, j=5;
    printf("Hello ");
    for (i; i>j; i++)
        printf("%d ", i);
    printf("Students ");
    return 0;
}
A. Hello 0 1 2 3 4 Students
B. Hello Students
C. Hello 0 1 2 3 4
D. 0 1 2 3 4 5

4. Predict output of following C program:
#include <stdio.h>
int main()
{
   int i;
   for (i=0; i<10; i++)
       printf("%d ", ++i);
   return 0;
}
A. 1 3 5 7 9
B. 0 1 2 3 4 5 6 7 8 9
C. 2 4 6 8
D. 2 4 6 8 10

5. Predict output of following C program:
#include <stdio.h>
int main()
{
    int i;
    for (i=65; i<70; i++)
        printf("%c ", i);
    return 0;
}
A. 65 66 67 68 69
B. A B C D E
C. Compile time error
D. Run time error


Check your answer here.
* * * * *



Read more interesting C Programming MCQs here.