GATE CSE 2018 Q42: Value returned by nested halving loops for input 2^40
- Programming and Data Structures
- 2 marks
- MCQ
- −2/3 if wrong
Syllabus topic: Programming in C
Question
Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}The value returned when we call fun with the input is
Try it: multiple choice: exactly one correct option
Show answer and explanation
Answer: B (official GATE 2018 final answer key)
Concept used: A loop that halves its variable until it reaches 1 runs about of the start value times, so two such loops compute roughly .
First step: Find after the first loop for , then trace the integer halvings of in the second loop, counting increments of sum.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Programming in C
Every PYQ on this topic →Share this question
Source: official GATE 2018 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15