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

Discussion

Consider the following function with line numbers.

1. Int Myfunction( )
2. {
3. int a = 24;
4. int b = 25;
5. int c;
6. if (a! = 24)
7. printf ("Is less than or not equal to 24");
8. c = a \(\lt\lt\) 2;
9. return c;
10. b = 24;
11. }

Which line numbers contain the dead code/dead variable/unreachable code in the above function definition ?

  • A.4, 6, 7
  • B.6, 7, 10
  • C.4, 6, 7, 8, 10
  • D.4, 6, 7, 10

Answer: D

The dead or unreachable code is on lines 4, 6, 7, 10.

  • Line 4: b is set to 25 but never used before the function returns, so it is a dead variable.
  • Lines 6-7: a is 24 and never changes, so the condition a != 24 is always false and the printf can never execute.
  • Line 10: it comes after the return on line 9, so it is unreachable.

Line 8 is live: it computes \(c = 24 \lt\lt 2 = 96\), which line 9 returns. So option C, which includes line 8, is wrong, and A and B each leave out a line that is dead.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.