Search Tutorials


C If .. Else MCQ Questions and Answers | JavaInUse

C If .. Else MCQ Questions and Answers

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int num = 5;
        if (num > 10)
        {
            printf("Number is greater than 10.");
        }
        else
        {
            printf("Number is less than or equal to 10.");
        }
        return 0;
    }
    
A. Number is greater than 10.
B. Number is less than or equal to 10.
C. Compilation error
D. Runtime error

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int x = 10;
        if (x > 5)
            printf("x is greater than 5");
        else
            printf("x is less than or equal to 5");
        return 0;
    }
    
A. x is greater than 5
B. x is less than or equal to 5
C. Compilation error
D. Runtime error

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int a = 3, b = 7;
        if (a > b)
        {
            printf("a is greater");
        }
        else if (a < b)
        {
            printf("b is greater");
        }
        else
        {
            printf("a and b are equal");
        }
        return 0;
    }
    
A. a is greater
B. b is greater
C. a and b are equal
D. No output

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int score = 85;
        char grade;
        if (score >= 90)
            grade = 'A';
        else if (score >= 80)
            grade = 'B';
        else if (score >= 70)
            grade = 'C';
        else
            grade = 'D';
        printf("Grade: %c", grade);
        return 0;
    }
    
A. Grade: A
B. Grade: B
C. Grade: C
D. Grade: D

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int num = 10;
        if (num % 2 == 0)
        {
            printf("%d is even.", num);
        }
        else
        {
            printf("%d is odd.", num);
        }
        return 0;
    }
    
A. 10 is even.
B. 10 is odd.
C. Compilation error
D. Runtime error

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int age = 18;
        if (age <= 18)
        {
            printf("You are a teenager.");
        }
        else
        {
            printf("You are an adult.");
        }
        return 0;
    }
    
A. You are a teenager.
B. You are an adult.
C. Compilation error
D. Runtime error

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int num = 15;
        if (num % 2 == 0)
        {
            printf("%d is even.", num);
        }
        else if (num % 2 != 0)
        {
            printf("%d is odd.", num);
        }
        return 0;
    }
    
A. 15 is even.
B. 15 is odd.
C. Compilation error
D. Runtime error

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int num = 5;
        if (num < 10)
        {
            printf("Number is less than 10.");
        }
        else if (num > 10)
        {
            printf("Number is greater than 10.");
        }
        else
        {
            printf("Number is equal to 10.");
        }
        return 0;
    }
    
A. Number is less than 10.
B. Number is greater than 10.
C. Number is equal to 10.
D. Compilation error





Q. What is the purpose of the else if statement in C?

A. To provide an alternative condition to be evaluated if the previous condition is false.
B. To combine multiple if statements into one.
C. To handle errors or exceptions.
D. To repeat a block of code multiple times.

Q. How can you rewrite the following code using a single if-else statement?

    #include <stdio.h>
    int main()
    {
        int x = 5;
        if (x > 0)
        {
            printf("x is positive");
        }
        if (x < 0)
        {
            printf("x is negative");
        }
        return 0;
    }
    
A.
    #include <stdio.h>
    int main()
    {
        int x = 5;
        if (x > 0)
        {
            printf("x is positive");
        }
        else
        {
            printf("x is negative");
        }
        return 0;
    }
    

B.
    #include <stdio.h>
    int main()
    {
        int x = 5;
        if (x >= 0)
        {
            printf("x is positive");
        }
        else
        {
            printf("x is negative");
        }
        return 0;
    }
    

C.
    #include <stdio.h>
    int main()
    {
        int x = 5;
        if (x < 0)
        {
            printf("x is negative");
        }
        else
        {
            printf("x is positive");
        }
        return 0;
    }
    

D.
    #include <stdio.h>
    int main()
    {
        int x = 5;
        if (x < 0)
        {
            printf("x is positive");
        }
        else if (x > 0)
        {
            printf("x is negative");
        }
        return 0;
    }
    

Q. What is the output of the following C code snippet?

    #include <stdio.h>
    int main()
    {
        int num = 12;
        if (num % 2 == 0)
        {
            printf("%d is divisible by 2.", num);
        }
        else if (num % 3 == 0)
        {
            printf("%d is divisible by 3.", num);
        }
        else
        {
            printf("%d is not divisible by 2 or 3.", num);
        }
        return 0;
    }
    
A. 12 is divisible by 2.
B. 12 is divisible by 3.
C. 12 is not divisible by 2 or 3.
D. Compilation error

Q. What will be the output of the following code snippet?

int x = 10;
if (x > 5) {
    x++;
} else {
    x--;
}

if (x < 10) {
    x = x * 2;
} else {
    x = x / 2;
}

printf("The value of x is: %d", x);
A.
The value of x is: 11
B.
The value of x is: 20
C.
The value of x is: 22
D.
The value of x is: 5

Q. How can you rewrite the following if-else statement using a ternary operator?

int score = 85;
if (score >= 90) {
    printf("A");
} else {
    printf("B");
}
A.
printf(score >= 90) ? "A" : "B");
B.
printf(score < 90) ? "B" : "A");
C.
printf(score == 90) ? "A" : "B");
D.
printf(score >= 90 ? "A" : "B");

Q. What will be the output of the following code snippet?

int num = 5;
char grade;

if (num >= 90) {
    grade = 'A';
} else if (num >= 80) {
    grade = 'B';
} else if (num >= 70) {
    grade = 'C';
} else if (num >= 60) {
    grade = 'D';
} else {
    grade = 'F';
}

printf("Grade: %c", grade);
A.
Grade: B
B.
Grade: C
C.
Grade: D
D.
Grade: F

Q. What will be the output of the following code snippet?

int x = 10;
int y = 20;

if (x > y) {
    printf("X is greater");
} else if (x < y) {
    printf("Y is greater");
} else {
    printf("X and Y are equal");
}
A.
X is greater
B.
Y is greater
C.
X and Y are equal
D.
None of the above

Q. What will be the output of the following code snippet?

int num = 10;

if (num % 2 == 0) {
    printf("Even");
} else if (num % 2 == 1) {
    printf("Odd");
} else {
    printf("Neither even nor odd");
}
A.
Even
B.
Odd
C.
Neither even nor odd
D.
None of the above

Q. What will be the output of the following code snippet?

int x = 5;

if (x < 10) {
    printf("X is less than 10");
    x++;
}

if (x < 10) {
    printf("Now X is greater than or equal to 10");
} else {
    printf("X remains less than 10");
}
A.
X is less than 10
Now X is greater than or equal to 10
B.
X is less than 10
X remains less than 10
C.
X is less than 10
D.
None of the above

Q. What will be the output of the following code snippet?

int x = 10;
int y = 20;

if (x > y) {
    x = x + y;
} else {
    y = x + y;
}

printf("X: %d, Y: %d", x, y);
A.
X: 30, Y: 20
B.
X: 10, Y: 30
C.
X: 20, Y: 30
D.
X: 30, Y: 10

Q. What will be the output of the following code snippet?

int num = 5;

if (num % 2 == 0) {
    printf("Even");
} else {
    printf("Odd");
}
A.
Even
B.
Odd
C.
Neither even nor odd
D.
None of the above

Q. What will be the output of the following code snippet?

int x = 10;

if (x > 5) {
    printf("X is greater than 5");
}

if (x < 5) {
    printf("X is less than 5");
}

if (x == 5) {
    printf("X is equal to 5");
}
A.
X is greater than 5
B.
X is greater than 5
X is equal to 5
C.
X is greater than 5
X is less than 5
D.
None of the above

Q. What will be the output of the following code snippet?

int x = 10;

if (x < 5) {
    printf("X is less than 5");
} else if (x > 15) {
    printf("X is greater than 15");
} else {
    printf("X is between 5 and 15");
}
A.
X is between 5 and 15
B.
X is less than 5
C.
X is greater than 15
D.
None of the above