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

Code Optimization Questions and Answers

Answer: A

Removing the call overhead is only part of the gain; the larger benefit is that the body can then be optimised in the context of its caller. The cost is a larger program. Constants passed as arguments often propagate deep into the inlined body.

Please log in or register to report this question.

42.

Assertion (A): Aggressive function inlining can make a program slower.
Reason (R): The enlarged code may no longer fit in the instruction cache, so execution stalls waiting for memory.

Answer: B

Correct answer: Both A and R are true, and R is the correct explanation of A

Both are true and the reason gives the mechanism. Inlining removes call overhead and exposes context for further optimisation, but it duplicates the body at every site. Once the working set no longer fits the instruction cache, the misses cost far more than the calls saved - which is why inlining heuristics are based on size as much as on call frequency.

Please log in or register to report this question.

Answer: C

Because nothing remains to be done after the call, the frame can be reused. This turns tail recursion into iteration and removes the risk of stack exhaustion. Some languages require the optimisation rather than merely permitting it.

Please log in or register to report this question.

Answer: D

The call graph must be built and information propagated around it. Separate compilation also limits what is visible, which is why link time optimisation exists. The call graph itself may be incomplete when function pointers are used.

Please log in or register to report this question.

Answer: A

Deferring some optimisation until every module is present restores the whole program view that separate compilation gives up. The cost is a much slower link step. Large projects often reserve it for release builds only.

Please log in or register to report this question.

Answer: B

Correct answer: measurements from earlier runs to guide the transformations

A static heuristic can only guess which branch is taken and which loop runs hot. A profile from a representative run supplies the actual frequencies, so inlining, block layout and register allocation can all be directed at the code that matters. The weakness is that the profile must resemble real use, or the optimisation is aimed at the wrong code.

Please log in or register to report this question.

Answer: C

Correct answer: place the frequently executed path in consecutive locations

Laying the hot path out contiguously means the processor fetches ahead along it, branches are usually not taken, and cache lines are filled with code that will be executed. The cold paths are moved out of line. Nothing is removed from the program; what changes is where it sits.

Please log in or register to report this question.

48.

A loop executes 1000 times and contains an invariant computation costing 3 instructions. How many instructions does hoisting it save?

Answer: D

The computation runs once instead of a thousand times, so 999 executions of 3 instructions are saved, that is 2997. The hoisted copy still costs 3 instructions in the preheader. The saving grows in direct proportion to the trip count of the loop.

Please log in or register to report this question.

49.

A loop body of 8 instructions is unrolled by a factor of 4. Ignoring the prologue, how many instructions does one unrolled iteration contain, excluding the loop control?

Answer: A

Correct answer: 32

\[ 8 \times 4 = 32\ \mathrm{instructions}. \] The point of the unroll is that one copy of the loop control now serves four bodies instead of one, so the per-iteration overhead falls to a quarter and the four bodies can be scheduled together. The cost is code size, which is the conflict with optimising for size.

Please log in or register to report this question.

50.

A procedure has 12 basic blocks and 15 edges in its flow graph. What is its cyclomatic complexity, computed as edges minus nodes plus two?

Answer: B

Fifteen minus twelve plus two gives five. The measure counts the independent paths through the procedure and is often used as a rough guide to how hard the code will be to test. It also bounds the number of paths a data flow analysis must account for.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Code Optimization Questions and Answers || Code Optimization MCQ Questions and Answers || Code Optimization GK Questions and Answers || Code Optimization GK MCQ Questions || Code Optimization Multiple Choice Questions and Answers || Code Optimization GK || GK on Code Optimization || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design