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

Control Instructions Questions and Answers

101.

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("This is c program.");
        case 1:
            printf("Case1");
            break;
        case 2:
            printf("Case2");
            break;
    }
return 0;
}

 

Answer: C

Correct answer: No Error and prints "Case1"

The printf before the first case is legal but unreachable: a switch transfers control straight to a matching label, so nothing before the first label ever runs. i is 1, so case 1 executes and the break ends the switch.

Please log in or register to report this question.

102.

What is the output of this C code?

void main()

    {

        int i = 0;

        if (i == 0)

        {

            printf("Hello");

            continue;

        }

    }

 

Answer: D

Correct answer: Compile time error

continue is meaningful only inside a loop: it asks for the next iteration, and here there is no loop to iterate. Written inside a bare if with no enclosing loop, it is a compile-time error.

Please log in or register to report this question.

103.

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int a = 10;
    switch(a)
    {
    }
    printf("This is c program.");
    return 0;
}

 

Answer: C

Correct answer: No Error

A switch body is just a compound statement, and an empty one is perfectly legal. Neither a case nor a default is required - the switch simply evaluates its controlling expression, finds no matching label, and does nothing. The program compiles and prints its message.

Please log in or register to report this question.

104.

In C++, which system - provided function is called when no handler is provided to deal with an exception?

Answer: A

Correct answer: terminate ( )

If an exception is thrown and no matching handler is found, C++ calls std::terminate(), which by default calls abort(). unexpected() belonged to the old dynamic exception specifications and is now removed; abort() is what terminate ends up calling rather than the function called first.

Please log in or register to report this question.

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.

106.

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.

107.

#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.

108.

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.

109.

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.

110.

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.

Loading…

Other Subjects

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