...
Programming
October 10, 2023
Programming
October 10, 2023
Programming
October 10, 2023
Programming
October 10, 2023

Programming

Question 2
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 2 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.

Correct Answer: B
Question 2 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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!