Question 18Asked in DSSSB TGT Computer Science (Technical), 1 Aug 2021, Shift 3

Consider the following segment of C++ code. int j, n;

Consider the following segment of C++ code. int j, n; for(j = 1; j <= n;) j = j*2; What is the number of comparisons made in the execution of the loop for any n > 0?
  1. An + 1
  2. B(n + 1)/2
  3. Clog₂(n + 1)
  4. Dlog₂n + 1
Show answerHide answer

Correct answer: (D) log₂n + 1

Explanation

j takes the values 1, 2, 4, ... while j ≤ n, so the loop condition is checked about log₂n + 1 times, which is the keyed answer. Strictly, the final failing test makes it ⌊log₂n⌋ + 2.

From DSSSB TGT Computer Science PYQ (Technical), 1 Aug 2021, Shift 3

This is question 18 of DSSSB TGT Computer Science PYQ (Technical), 1 Aug 2021, Shift 3. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.