Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following C-program fragment in ... Start Learning for Free
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);
Q. 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)
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Consider the following C-program fragment in which i, j and n are inte...
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.
View all questions of this test
Most Upvoted Answer
Consider the following C-program fragment in which i, j and n are inte...
Explanation:
The given program fragment is a for loop that initializes two variables, i and j. It then executes the loop as long as i is greater than 0, and after each iteration, it divides i by 2 and assigns its value to j.

The loop starts with i = n and j = 0. In each iteration, i is divided by 2 and its value is assigned to j. So, j takes the values of n, n/2, n/4, n/8, and so on until i becomes 0.

Let's analyze the values of j at each iteration:

- At the first iteration, j = n.
- At the second iteration, j = n/2.
- At the third iteration, j = n/4.
- At the fourth iteration, j = n/8.
- And so on.

We can observe that in each iteration, j is divided by 2. So, after k iterations, j will be equal to n divided by 2^k. We need to find the number of iterations required for i to become 0.

The condition for the loop termination is i > 0. In each iteration, i is divided by 2. So, the number of iterations required for i to become 0 is equal to the number of times i can be divided by 2 until it becomes less than or equal to 0.

To find this number of iterations, we can calculate the logarithm of n to the base 2, which gives us the exponent to which 2 must be raised to obtain n. Let's denote this value as k.

So, after k iterations, j will be equal to n divided by 2^k, which is equal to n divided by n, which simplifies to 1.

Therefore, val(j) = 1.

Conclusion:
The value stored in the variable j after termination of the for loop is 1, which is independent of the value of n.

Hence, the correct answer is option C) val(j) = n.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

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);Q. 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)Correct answer is option 'C'. Can you explain this answer?
Question Description
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);Q. 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)Correct answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for 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);Q. 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)Correct answer is option 'C'. Can you explain this answer?.
Solutions for 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of 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);Q. 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)Correct answer is option 'C'. Can you explain this answer?, a detailed solution for 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? has been provided alongside types of 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice 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);Q. 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)Correct answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev