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

Control Instructions Questions and Answers

51.

What will be the output of the program?

#include <stdio.h>
int main()
{
    unsigned int i = 65535; /* Assume 2 byte integer*/
    while(i++ != 0)
        printf("%d",++i);
    printf("\n");
    return 0;
}

 

Answer: A

Correct answer: Infinite loop

i++ tests the old value and then increments, and the body increments again with ++i. Starting from 65535, the test takes i to 0, the body takes it to 1, and thereafter the value seen by the test is always odd - 1, 3, 5, … Wrapping round at 65536 lands on 1 again, never on 0, so the condition is never false.

Please log in or register to report this question.

Answer: B

Correct answer: for (i = 0, j = 0;i < n, j < n; i++, j += 5)

The comma operator lets a single for carry more than one initialisation and more than one update, so both variables advance together in one loop. Note the trap in the condition: i < n, j < n evaluates both but yields only the last, so it is really j < n that controls the loop - a genuine condition on both would need &&.

Please log in or register to report this question.

53.

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++);
        printf("%d", i);
    return 0;
}

 

Answer: D

Correct answer: 6

The semicolon immediately after the for header is an empty body, so the loop does nothing but count. It exits when the test i <= 5 first fails, which is when i has reached 6 - and that is the value the printf, which is outside the loop, then prints once.

Please log in or register to report this question.

54.

Comment on the code below

#include <stdio.h>
    void main()
    {
        int x = 5;
        if (true);
            printf("hello");
    }

 

Answer: B

Correct answer: It will throw an error

true is not a keyword in C. It is a macro defined in <stdbool.h>, which this program does not include, so the identifier is undeclared and the program fails to compile. (Had it compiled, the semicolon after the if would have been the empty statement forming its branch, and hello would have printed unconditionally.)

Please log in or register to report this question.

55.

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.

57.

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.

58.

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.

59.

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.

60.

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.

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