GATE 2007
October 14, 2023
Database-Management-System
October 14, 2023
GATE 2007
October 14, 2023
Database-Management-System
October 14, 2023

GATE 2007

Question 15

Consider the following segment of C-code:

  int j, n;
  j = 1;
  while (j <= n)
        j = j*2;

The number of comparisons made in the execution of the loop for any n > 0 is: Base of Log is 2 in all options.

A
⌈log2n⌉ + 1
B
n
C
⌈log2n⌉
D
⌊log2n⌋ + 1
Question 15 Explanation: 
Let us consider n=6, then
1<=6 (✔️)
2<=6 (✔️)
4<=6 (✔️)
8<=6 (❌)
4 comparisons required
Option A:
[log n]+1
[log 6]+1
3+1 = 4 (✔)
Option B:
n=6 (❌)
Option C:
[log n]
[log 6]=3 (❌)
Option D:
[log2n]+1
[log26]+1 = 2+1 = 3 (❌)

Correct Answer: A
Question 15 Explanation: 
Let us consider n=6, then
1<=6 (✔️)
2<=6 (✔️)
4<=6 (✔️)
8<=6 (❌)
4 comparisons required
Option A:
[log n]+1
[log 6]+1
3+1 = 4 (✔)
Option B:
n=6 (❌)
Option C:
[log n]
[log 6]=3 (❌)
Option D:
[log2n]+1
[log26]+1 = 2+1 = 3 (❌)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!