Machine-translated by Google. Questions are authored in English.
Top

Control Instructions Questions and Answers

Answer: A

Correct answer: if(n){c = a;}else{c = b;}

The conditional operator evaluates its first operand as a condition: if it is true the result is the second operand, otherwise the third. So c = n ? a : b; assigns a when n is non-zero and b when it is zero - exactly what the first if-else does.

Please log in or register to report this question.

102.

A switch statement accepts a controlling expression of type

Answer: D

Correct answer: All of the above

The controlling expression must be of integer type, and int, char and long all qualify - as do short and enumerations, with the smaller types promoted to int. Only the floating-point types are excluded.

Please log in or register to report this question.

103.

#include <stdio.h>
int main()
{
    int i = 3;
    while (i--)
    {
        int i = 100;
        i--;
        printf("%d ", i);
    }
    return 0;
}

 

Answer: B

Correct answer: 99 99 99

The inner int i = 100; shadows the outer i for the whole of the loop body, so i-- and the printf refer to the new variable, which is created afresh at 100 on every pass and printed as 99. The outer i is untouched inside the body and is decremented only by the while condition, so the loop still runs three times.

Please log in or register to report this question.

104.

What is the output of this C code?

int main()

    {

        printf("before continue ");

        continue;

        printf("after continue\n");

    }

 

Answer: D

Correct answer: Compile time error

continue must appear inside a loop. In a function body with no loop around it there is no next iteration to jump to, so the compiler rejects the program - neither printf ever runs.

Please log in or register to report this question.

105.

Predict the output of the below program:

#include <stdio.h>
int main()
{
    int i = 3;
    switch(i)
    {
        printf("Outside ");
        case 1: printf("Geeks");
            break;
        case 2: printf("Quiz");
            break;
        defau1t: printf("GeeksQuiz");
    }
    return 0;
}

 

Answer: C

Correct answer: Nothing gets printed

Two things happen here. The printf("Outside ") sits inside the switch body but before any label, so control never reaches it - a switch jumps straight to a matching label. And defau1t is spelt with the digit one, so it is an ordinary goto label, not the default case. With i equal to 3 nothing matches and there is no real default, so the switch does nothing at all.

Please log in or register to report this question.

106.

Point out the error, if any in the program.

#include <stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d\n", b);
    return 0;
}

 

Answer: C

Correct answer: Error: L value required for b

Assignment binds more loosely than the conditional operator, so the line parses as

(a >= 5 ? b = 100 : b) = 200;

and the left-hand side is now a conditional expression, which is a value rather than an object - so it cannot be assigned to. Written as b = a >= 5 ? 100 : 200; it would compile and give 100.

Please log in or register to report this question.

107.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int x = 10, y = 20;
    if(!(!x) && x)
        printf("x = %d\n", x);
    else
        printf("y = %d\n", y);
    return 0;
}

 

Answer: C

Correct answer: x = 10

!x is 0 since x is non-zero, and !(!x) is therefore 1 - the double negation normalises any non-zero value to 1. The condition 1 && 10 is true, so the first branch runs and prints the value of x, which the logical operators never altered: 10.

Please log in or register to report this question.

108.

What will be the output of the program?

#include <stdio.h>
int main()
{
    char ch;
    if(ch = printf(""))
        printf("It matters\n");
    else
        printf("It doesn't matters\n");
    return 0;
}

 

Answer: B

Correct answer: It doesn't matters

printf("") writes nothing and therefore returns 0 - the count of characters written. That 0 is assigned to ch, and the value of the assignment is also 0, which is false. So the else branch runs.

Please log in or register to report this question.

Loading…
Tags: Control Instructions Questions and Answers || Control Instructions MCQ Questions and Answers || Control Instructions GK Questions and Answers || Control Instructions GK MCQ Questions || Control Instructions Multiple Choice Questions and Answers || Control Instructions GK || GK on Control Instructions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming