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

Control Instructions Questions and Answers

51.

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.

52.

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.

53.

In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.

Answer: D

Correct answer: Loop jamming

Loop jamming (loop fusion) merges two loops with the same trip count into one, which is safe only if neither depends on the other’s results. The gain is one set of loop overheads instead of two, and better use of the cache, since the data is walked once. Loop unrolling repeats the body to cut the number of tests; strength reduction replaces an expensive operation with a cheaper one.

Please log in or register to report this question.

54.

In C++, which system - provided function is called when no handler is provided to deal with an exception?

Answer: A

Correct answer: terminate ( )

If an exception is thrown and no matching handler is found, C++ calls std::terminate(), which by default calls abort(). unexpected() belonged to the old dynamic exception specifications and is now removed; abort() is what terminate ends up calling rather than the function called first.

Please log in or register to report this question.

55.

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.

56.

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.

57.

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.

58.

Which of the following is not a logical operator?

Answer: A

Correct answer: &

& is the bitwise AND: it combines the operands bit by bit and yields an integer. The logical operators are &&, || and !, which treat their operands as true or false, yield 0 or 1, and - unlike & - short-circuit.

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.

60.

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.

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