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

Control Instructions Questions and Answers

11.

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.

13.

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.

14.

Which of the following cannot be checked in a switch-case statement?

Answer: C

Correct answer: Float

A switch requires an integer controlling expression, so char, int and enumeration constants are all fine. float is not: exact equality against a case label would be unreliable for values that cannot be represented exactly in binary.

Please log in or register to report this question.

15.

How many times the while loop will get executed if a short int is 2 byte wide?

#include <stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d\n", j, j);
        j++;
    }
    return 0;
}

 

Answer: B

Correct answer: 255 times

j runs from 1 and the loop continues while j <= 255, so it executes for j equal to 1, 2, …, 255 - 255 passes. The width of a short is a red herring: j is declared int, and 255 is nowhere near the limit of either type.

Please log in or register to report this question.

16.

What is the output of this C code?

void main()

    {

        int k;

        for (k = -3; k < -5; k++)

            printf("Hello");

    }

 

Answer: D

Correct answer: Nothing

A for tests its condition before the first pass. With k at -3, the test k < -5 is already false, so the body never runs and nothing is printed. (Had it been a do-while, the body would have run once.)

Please log in or register to report this question.

17.

What is the output of the following C code?

#include <stdio.h>
int main()
{
int index;
for(index=1; index<=5; index++)
{
printf("%d", index);
if (index==3)
continue;
}
}

 

Answer: B

Correct answer: 12345

The continue is the last statement in the body, so it skips nothing - the next thing after it would have been the end of the iteration anyway. Every pass prints its index, giving 12345.

Please log in or register to report this question.

18.

A char variable can store either an ASCII character or a Unicode character.

Answer: A

Correct answer: False

In C a char is one byte - sizeof(char) is 1 by definition - and on ordinary implementations that is eight bits, giving 256 possible values. That is ample for ASCII, which uses only the codes 0 to 127, but Unicode defines over a million code points, so a single char cannot hold an arbitrary Unicode character. C provides other types for that work: wchar_t for wide characters, and since C11 char16_t and char32_t for UTF-16 and UTF-32 code units, declared in <uchar.h>. What a char array can hold is a Unicode character encoded in UTF-8, but that takes one to four char elements per character, not one - the character is spread across several bytes rather than stored in a single variable. The confusion usually comes from Java, where char is a 16-bit UTF-16 code unit and the statement would be much closer to true. In C it is false.

Please log in or register to report this question.

19.

A short integer is at least 16 bits wide and a long integer is at least 32 bits wide.

Answer: A

Correct answer: True

The standard sets minimum widths, not exact ones: short must be at least 16 bits and long at least 32, with int at least 16 and long long at least 64. It also requires the ordering char ≤ short ≤ int ≤ long ≤ long long. Actual widths vary by platform, which is why <stdint.h> offers fixed-width types.

Please log in or register to report this question.

20.

#include <stdio.h>
int i;
int main()
{
    if (i);
    else
        printf("Else");
    return 0;
}

What is correct about the above program?

Answer: B

Correct answer: Else block is executed

i is a global, so it is zero-initialised - not indeterminate, which is what makes the outcome predictable. The condition is therefore false. The semicolon after if (i) is the empty statement that forms the if branch, and the else attaches to it perfectly legally, so the else branch runs.

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