retagged by
9,008 views
19 votes
19 votes

​​​​​Consider the following$\text{ ANSI C}$ program.

#include <stdio.h>
int main()
{
    int i, j, count;
    count=0;
    i=0;
    for (j=-3; j<=3; j++)
    {
        if (( j >= 0) && (i++))
            count = count + j;
    }
    count = count +i;
    printf("%d", count);
    return 0;
}

Which one of the following options is correct?

  1. The program will not compile successfully
  2. The program will compile successfully and output $10$ when executed
  3. The program will compile successfully and output $8$ when executed
  4. The program will compile successfully and output $13$ when executed
retagged by

4 Answers

Best answer
16 votes
16 votes
for (j=-3; j<=3; j++)
{
    if (( j >= 0) && (i++))
        count = count + j;
}

From the above loop code, we can see that the loop iterates $7$ times for $j \in \{-3,-2,-1,0,1,2,3\}.$

Now, we have an $\text{“if”}$ condition and inside it, we have a logical AND operator (&&). In $C$ language we have the following short-circuit rule for binary logical operators

  1. The second operand of logical $\text{OR}$ operator || is ignored if the first operand is non-zero.
  2. The second operand of logical $\text{AND}$ operator (&&) is ignored if the first operand is $0$.

So, for $j \in \{-3,-2,-1\}$ the first operand of && operator (j >= 0) will be $0,$ and hence the second operand (i++) will be ignored. 

For $j \in \{0,1,2,3\}$ the first operand of && operator (j >= 0) will be $1,$ and hence the second operand (i++) will get evaluated $4$ times and final value of $i = 4.$

Initial value of $i = 0.$ 

The postincrement operator i++, returns the original value of $i$ and then increments $i.$ So, when the first time i++ happens, the second operator of logical $\text{AND}$ operator is $0$ and hence the $\text{“if”}$ condition fails. So, count = count +j happens only for $j \in \{1,2,3\}$ and we get $\text{count} = 0 + 1 + 2 + 3 = 6.$

After the loop, we have count = count + i, which makes $\text{count} = 6 + 4 = 10.$

So, the correct option is B.

Reference: https://gateoverflow.in/62409/what-is-the-output

edited by
5 votes
5 votes
It should be Option B

In for loop till value of  j  does not reach to 0   first  if condition reamin false and so second condition won’t be evaluated and  no change will happen in value of  i  (it will remain 0 )  

Then for  j from 0 to 3 loop will execute 4 times

 to increase value of i  to 4

and count = count+j will cause value to  6

count+i =10  

program will compile successfully to output 10 (no reason to not compile successfully)
1 votes
1 votes
It is possible that few  people will be confused for i value to be 7 in the end but condition will not increment for ( j=-3,-2,-1 ) as j>=0 will be false so further condition will not be checked hence at last i value will be 4 and count =6=(count+j)

count+i = 6+4=10

Correct option is (b).
0 votes
0 votes

Answer 

Answer:

Related questions

6 votes
6 votes
1 answer
2
Arjun asked Feb 18, 2021
4,923 views
$$\begin{array}{|c|c|c|c|} \hline \textbf{Items} & \textbf{Cost} & \textbf{Profit %} & \textbf{Marked Price} \\ & \text{(₹)} & & \text{(₹)} \\\hline P &5,400 & -&5,86...
14 votes
14 votes
1 answer
3
Arjun asked Feb 18, 2021
10,817 views
There are five bags each containing identical sets of ten distinct chocolates. One chocolate is picked from each bag.The probability that at least two chocolates are iden...