Consider the following C-program fragment in which i, j and n are integer variables.
for (i = n, j = 0; i >0; i /= 2, j += i); |
chevron_right
filter_none
Let val(j) denote the value stored in the variable j after termination of the for loop. Which one of the following is true?
(A) val(j) =
(logn)
(B) vaI(j) =
(sqrt(n))
(C) val(j) =
(n)
(D) val(j) =
(nlogn)
(A) A
(B) B
(C) C
(D) D
Answer: (C)
Explanation: The variable j is initially 0 and value of j is sum of values of i. i is initialized as n and is reduced to half in each iteration.
j = n/2 + n/4 + n/8 + .. + 1 = Θ(n)
Note the semicolon after the for loop, so there is nothing in the body.
Same as question 1 of https://www.geeksforgeeks.org/c-language-set-6/
Quiz of this Question
Recommended Posts:
- GATE | GATE CS 1999 | Question 36
- GATE | GATE CS Mock 2018 | Question 39
- GATE | GATE CS 2013 | Question 1
- GATE | GATE CS 2013 | Question 3
- GATE | GATE CS 2013 | Question 4
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 6
- GATE | GATE CS 2013 | Question 7
- GATE | GATE CS 2013 | Question 8
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 11
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 17
- GATE | GATE CS 2013 | Question 18
- GATE | GATE CS 2013 | Question 19
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 22
- GATE | GATE CS 2013 | Question 23

