October 10, 2023
October 10, 2023
October 10, 2023
###### Programming
October 10, 2023
 Question 18
Consider the following 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?
 A The program will compile successfully and output 13 when executed. B The program will compile successfully and output 10 when executed. C The program will compile successfully and output 8 when executed. D The program will not compile successfully.
Question 18 Explanation:

Input: count=0 , i=0 and j=-3

For(j = -3; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = -2; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = -1; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = 0; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=0+0 → Count=0

For(j = 1; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=0+1 → Count=1

For(j = 2; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=1+2 → Count=3

For(j = 3; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=3+3 → Count=6

For(j = 4; j <= 3; j++)   → Condition FALSE we are not entering the loop.

count=6+4 → We are given a condition as a post increment. So, “i” updates the next instruction.

The above code segment executes successfully and will print value=10.

Question 18 Explanation:

Input: count=0 , i=0 and j=-3

For(j = -3; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = -2; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = -1; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition fails because they are given logical AND.

So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.

For(j = 0; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=0+0 → Count=0

For(j = 1; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=0+1 → Count=1

For(j = 2; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=1+2 → Count=3

For(j = 3; j <= 3; j++)   → Condition TRUE then enters into the loop.

if((j >= 0) && (i++))   → Condition TRUE then enters into the loop.

count=3+3 → Count=6

For(j = 4; j <= 3; j++)   → Condition FALSE we are not entering the loop.

count=6+4 → We are given a condition as a post increment. So, “i” updates the next instruction.

The above code segment executes successfully and will print value=10.

Article Rating
Subscribe
Notify of
Inline Feedbacks
0