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

Control Instructions Questions and Answers

21.

Output?

#include <stdio.h>
int main()
{
    int c = 5, no = 10;
    do {
        no /= c;
    } while(c--);
  
    printf ("%d\n", no);
    return 0;
}

 

Answer: B

Correct answer: Runtime Error

The loop divides no by c and then tests c--, so c falls 5, 4, 3, 2, 1, 0. On the pass after the test that saw c equal to 1, c is 0 - and no /= 0 is integer division by zero, which the hardware traps. The compiler cannot see it, so it is a run-time fault.

Please log in or register to report this question.

22.

What is the output of the below program?

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

 

Answer: C

Correct answer: GeeksQuiz

'0' is a character constant, and its value is the ASCII code 48, not the number 0. Likewise '1' is 49. The controlling expression is 0, which matches neither, so control falls to default.

Please log in or register to report this question.

23.

When 2 is entered, The output of the code below is?

#include <stdio.h>
    void main()
    {
        int ch;
        printf("enter a value btw 1 to 2:");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("1\n");
            break;
            printf("Hi");
        default:
            printf("2\n");
        }
    }

 

Answer: D

Correct answer: 2

ch is 2, which matches no case, so control goes straight to default and prints 2. The printf("Hi") is unreachable in any event: it sits immediately after a break and before the next label, so no path can reach it.

Please log in or register to report this question.

24.

Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3) ?

Answer: D

Correct answer: macros

The three parts of a for are ordinary expressions, so anything assignable can appear on the left of an assignment in them - a variable, or the result of a function returning a modifiable lvalue. A macro is only text, replaced before compilation, so what appears in the code is whatever it expands to; it is not itself an object and cannot be assigned to.

Please log in or register to report this question.

Answer: B

Correct answer: Both int and char can be used in “switch” control expression.

The controlling expression must have an integer type. char qualifies - it is a small integer type and is promoted to int - as do short, long and enumerations. float and double do not, because equality against a case label would be unreliable for values that cannot be represented exactly. The expression also cannot be omitted.

Please log in or register to report this question.

26.

The following code ‘for(;;)’ represents an infinite loop. Which statement exits the loop itself?

Answer: A

Correct answer: break

break is the statement that ends the loop: control passes to the statement after it and the program carries on. exit(0) and abort() would also stop the loop running, but only by ending the whole program, which is a different thing from leaving the loop.

Please log in or register to report this question.

27.

Which keyword is used to come out of a loop only for that iteration?

Answer: B

Correct answer: continue

continue abandons the current iteration and jumps to the next one - to the update expression in a for, or to the condition test in a while. break leaves the loop altogether, and return leaves the function.

Please log in or register to report this question.

28.

Point out the error, if any in the program.

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

 

Answer: D

Correct answer: No Error

case 1*2+4 is a constant expression, which the compiler evaluates to 6 - that is allowed, and 6 does not clash with case 1. A default label is optional. So the program compiles and, with i equal to 1, prints Case1.

Please log in or register to report this question.

Answer: B

Correct answer: GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times

i starts at 1 and rises by 2, so it takes only the odd values 1, 3, 5, 7, 9, 11, … and never equals 10. The test i != 10 is therefore never false. This is why < is safer than != in a loop condition whose variable moves by more than one.

Please log in or register to report this question.

30.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}

 

Answer: A

Correct answer: This is default This is case 1

i is 4, which matches no case, so control jumps to default - and default need not be last. Having no break, execution then falls through into case 1, whose break finally ends the switch. So two lines are printed.

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