C Programming MCQ : Conditional Operators (Multiple Choice Questions)


Question 1
Guess the output of the following program ?
#include<stdio.h>
int main()
{
  int num1 = 30;
  int num2 = 40;
  int ans = num1 > num2;
  printf("%d",ans);
  return(0);
}
A
30
B
0
C
1
D
40
Question 2
How many times printf will be executed ?
#include<stdio.h>
int main()
{
    int x;
    for(x=0; x<=15; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("c4learn.com");
    }
    return 0;
}
A
15 times
B
0 times
C
5 times
D
16 times
Question 2 Explanation: 
Continue statement will take control back to the beginning of the loop.
Question 3
How many times "c4learn.com" gets printed ?
#include<stdio.h>
int main()
{
    int x;
    for(x=0; x<=15; x++);
    {
        printf("c4learn.com");
    }
    return 0;
}
A
16 times
B
0 times
C
15 times
D
1 times
Question 3 Explanation: 
Note semicolon at the end of for loop. Semicolon at the end of for loop will be equivalent to -
for(x=0; x<=15; x++)
 {
 }
above program will be equivalent to -
#include<stdio.h>
int main()
{
    int x;
    for(x=0; x<=15; x++)
    {
    }
    {
        printf("c4learn.com");
    }
    return 0;
}
Question 4
Guess the output of the following program ?
#include<stdio.h>
int main()
{
    int x;
    for(x=0; x<=3; x++);
    {
        printf("x = %d",x);
    }
    return 0;
}
A
x = 0
x = 1
x = 2
x = 3
x = 4
B
x = 4
C
x = 0
x = 1
x = 2
x = 3
D
x = 3
Question 4 Explanation: 
Note the semicolon at the end of for loop. Loop will be executed 5 times and final value of x will be 4.
Question 5
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++);
        printf("%d,", i);
    return 0;
}
A
1, 2, 3, 4
B
5
C
0, 1, 2, 3, 4, 5
D
6
Question 5 Explanation: 
Noe the semicolon at the end of for loop. Above program is equivalent to -
#include<stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++)
    {
    } 
    printf("%d,", i);
    return 0;
}
There are 5 questions to complete.