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

Control Instructions Questions and Answers

1.

The keyword ‘break’ cannot be simply used within:

Answer: B

Correct answer: if-else

break belongs to loops and switch: it exits the nearest enclosing for, while, do-while or switch. An if is not one of those, so a break written directly inside an if either escapes the loop containing the if or, if there is no such loop, fails to compile.

Please log in or register to report this question.

2.

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

 

Answer: B

Correct answer: 1

i++ yields the old value and then increments, so the loop keeps going while the old value is non-zero. Starting at 65000 the variable climbs to the maximum for an unsigned int and then wraps round to 0 - wrapping is defined behaviour for unsigned types. On that pass the test sees 0 and stops, but i has already been incremented once more, to 1.

Please log in or register to report this question.

3.

The output of the code below is

#include <stdio.h>
    void main()
    {
        char *ch;
        printf("enter a value btw 1 to 3:");
        scanf("%s", ch);
        switch (ch)
        {
        case "1":
            printf("1");
            break;
        case "2":
            printf("2");
            break;
        }
    }

 

Answer: C

Correct answer: Compile time error

Two things are wrong, and either alone is fatal. The controlling expression ch is a pointer, not an integer type; and the case labels "1" and "2" are string literals, which are not integer constants. (The scanf into an uninitialised pointer would also be undefined behaviour, but the program never gets that far.)

Please log in or register to report this question.

4.

Which loop is guaranteed to execute at least once?

Answer: C

Correct answer: do while

A do-while tests its condition after the body, so the body always runs at least once. for and while test first, and so may not run at all. This makes do-while the natural choice for reading input and then validating it.

Please log in or register to report this question.

5.

Which of the following statements are correct about an if-else statements in a C-program?

1. Every if-else statement can be replaced by an equivalent statements using   ?: operators

2. Nested if-else statements are allowed.

3. Multiple statements in an if block are allowed.

4. Multiple statements in an else block are allowed.

Answer: D

Correct answer: 2, 3, 4

2, 3 and 4 are true: if-else statements nest freely, and either branch may hold as many statements as you like inside braces.

1 is false. The conditional operator ?: combines expressions, not statements, so it cannot replace an if-else whose branches contain declarations, loops, or several statements. It replaces only the special case where both branches produce a value.

Please log in or register to report this question.

6.

Which of the following is correct with respect to “Jump Statements” in C?

Answer: D

Correct answer: All of the above.

C has four jump statements, and three of them are listed: goto, which transfers to a label in the same function; continue, which starts the next iteration; and break, which leaves a loop or switch. The fourth is return.

Please log in or register to report this question.

7.

Which of the following sentences are correct about a switch loop in a C program?

1. switch is useful when we wish to check the value of variable against a particular set of values.

2. switch is useful when we wish to check whether a value falls in different ranges.

3. Compiler implements a jump table for cases used in switch.

4. It is not necessary to use a break in every switch statement.

Answer: B

Correct answer: 1,3,4

1 is true - a switch compares the controlling expression against a set of discrete constants. 3 is true: where the labels are dense the compiler typically builds a jump table, which is why a switch can beat a chain of ifs. 4 is true: omitting break is legal and produces deliberate fall-through.

2 is false. A switch cannot test a range - each value in the range needs its own label - which is what if-else if is for.

Please log in or register to report this question.

8.

Consider the following pseudocode:

x:=1;
i:=1;
while (x ≤ 500)
begin
x := 2^x ;
i:=i+1;
end

What is the value of i at the end of the pseudocode?

Answer: B

Correct answer: 5

Each pass replaces x by \(2^x\), which grows extremely fast:

after pass-1234
x1241665536
i12345

The test \(x \le 500\) fails once x reaches 65536, and i is then 5.

Please log in or register to report this question.

Answer: B

Correct answer: Division, Multiplication, Addition, Subtraction

Multiplication and division sit at the same, higher precedence, and addition and subtraction at the same, lower one; within a level, evaluation runs left to right. The listed order reflects that grouping, which is what BODMAS and PEMDAS describe.

Please log in or register to report this question.

10.

What will be the output of the program?

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

 

Answer: A

Correct answer: 4, 3, 2, 1, 0, -1 4, 3, 2, 1, 0, -1

i-- >= 0 tests the old value and then decrements, so the value printed is always one less than the value tested. Starting at 5 that gives 4, 3, 2, 1, 0 and finally -1 - the pass where the test saw 0. The loop then leaves i at -2. After i is reset to 5 the second loop prints the same line. The third loop then starts with i at -2, so its very first test fails and it prints nothing.

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