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

Control Instructions Questions and Answers

61.

Consider the following program fragment i=6720; j=4; while (i%j)==0 { i=i/j; j=j+1; } On termination j will have the value

Answer: C

Correct answer: 9

Each pass divides i by j and then increases j:

i672016803365681
j456789

At each step the division is exact, so the loop continues - until i is 1 and j is 9, where \(1 \bmod 9 = 1\) and the loop stops. j is then 9. (Note that 6720 = 4 × 5 × 6 × 7 × 8, which is what makes every division exact.)

Please log in or register to report this question.

Answer: D

Correct answer: “break” and “continue” can be used in “for”, “while” and “do-while” loop body. But only “break” can be used in “switch” body

Both statements work in all three loops. The difference is the switch: break is what ends a switch, while continue has no meaning there, since a switch has no iterations. A continue written inside a switch applies to the loop enclosing the switch, and fails to compile if there is none.

Please log in or register to report this question.

63.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("IndiaBIX\n");
    return 0;
}

 

Answer: B

Correct answer: Error: Lvalue required

= is assignment, not comparison, and its left operand must be an object that can be assigned to. i % 2 is a computed value, so the compiler reports that an lvalue is required. The author meant ==.

Please log in or register to report this question.

64.

How many times is GeeksQuiz printed?

#include <stdio.h>
int main()
{
    int i = -5;
    while (i <= 5)
    {
        if (i >= 0)
            break;
        else
        {
            i++;
            continue;
        }
        printf("GeeksQuiz");
    }
    return 0;
}

 

Answer: D

Correct answer: 0 times

Every path through the loop body reaches either break or continue before the printf. While i is negative the else runs, incrementing and continuing; the moment i reaches 0 the break fires. The printf is therefore unreachable, and a compiler will usually say so.

Please log in or register to report this question.

65.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("Hello\n");
        case 1:
            printf("Hi\n");
            break;
        case 2:
            printf("\nBye\n");
            break;
    }
    return 0;
}

 

Answer: C

Correct answer: Hi

A switch jumps directly to a matching label, so the printf("Hello") written before any case is unreachable. i is 1, so control lands on case 1, prints Hi, and the break ends the switch.

Please log in or register to report this question.

66.

When 1 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, ch + 1)
        {
        case 1:
            printf("1\n");
            break;
        case 2:
            printf("2");
            break;
        }
    }

 

Answer: B

Correct answer: 2

The controlling expression uses the comma operator: ch, ch + 1 evaluates ch, discards it, and yields ch + 1. With 1 entered, the switch is therefore performed on 2, so case 2 runs and prints 2.

Please log in or register to report this question.

67.

Which of the following statements are correct about the below program?

#include <stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("IndiaBIX\n");
        exit(0);
        main();
    }
    return 0;
}

 

Answer: B

Correct answer: The program prints 'IndiaBIX' one time

exit(0) terminates the program immediately. The recursive main() call written after it is never reached, so the message is printed exactly once. (Calling main recursively is legal in C, which is why the last option is wrong - it just never happens here.)

Please log in or register to report this question.

68.

Can we use a switch statement to switch on strings?

Answer: B

Correct answer: No

A switch requires an integer controlling expression, and a string in C is an array of characters - in an expression, a pointer. Comparing strings means comparing their contents, which == does not do, so a chain of if (strcmp(s, "...") == 0) is the usual alternative. (Java added string switches; C has not.)

Please log in or register to report this question.

69.

How many lines of output does the following C code produce?

#include <stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
    while (i/j > 0.001)
    {
        j+=j;
        sum=sum+(i/j);
        printf("%f\n", sum);
    }
}

 

Answer: D

Correct answer: 11

Each pass doubles j and prints, so the tested quantity \(i/j\) starts at 2 and halves every time: 2, 1, 0.5, 0.25, … The loop continues while it exceeds 0.001. Since \(2/2^{11} = 0.000977\) is the first value at or below the threshold, the test succeeds for \(j = 1, 2, 4, \ldots, 1024\) - eleven passes, and eleven lines.

Please log in or register to report this question.

70.

Point out the error, if any in the for loop.

#include <stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

 

Answer: D

Correct answer: No error

for(;;) is legal and idiomatic: all three clauses may be omitted, and an omitted condition is taken as true, giving an infinite loop. The two semicolons are required, since the header always has three parts. The loop here terminates properly through its break.

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