0 votes 0 votes Consider the following pseudo-code fragment, where $m$ is a non-negative integer that has been initialized: $p=0$ $k=0$ while $(k<m)$ $p=p+2^k$ $k=k+1$ end while Which of the following is a loop invariant for the while statement? (Note: a loop invariant for a while statement is an assertion that is true each time the guard is evaluated during the execution of the while statement). $p=2^k-1$ and $0 \leq k < m$ $p=2^{k+1}-1$ and $0 \leq k < m$ $p=2^k-1$ and $0 \leq k \leq m$ $p=2^{k+1}-1$ and $0 \leq k \leq m$ Unknown Category ugcnetcse-dec2018-paper2 + – Arjun asked Jan 2, 2019 • edited Jun 22, 2020 by soujanyareddy13 Arjun 6.5k views answer comment Share Follow See all 12 Comments See all 12 12 Comments reply Show 9 previous comments Shaik Masthan commented Mar 13, 2019 reply Follow Share mam, why you checking after executing the line p = p + 2$^k$ ? check before that line tooo... then p=0,k=0 ==> option c is matching 2 votes 2 votes srestha commented Mar 13, 2019 reply Follow Share ok, thank u that means, loop invariant will be executed before any condition will be evaluated right? 0 votes 0 votes Shaik Masthan commented Mar 13, 2019 reply Follow Share before any statement of the loop evaluated 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes $k=0,p=0$ After $first$ iteration, $p=0+2^0=1,\,k=1$ After $n^{th}$ iteration, $p=2^0+2^1+...+2^{n-1}=2^{n}-1,k=n$ As, the loop will go for $m$ iterations, so, $0\leq k\leq m$ So, $(C)$ should be the answer Shobhit Joshi answered Jan 3, 2019 Shobhit Joshi comment Share Follow See all 0 reply Please log in or register to add a comment.