C-Programming
Question 1 |
# 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?
The program will compile successfully and output 13 when executed. | |
The program will compile successfully and output 10 when executed. | |
The program will compile successfully and output 8 when executed. | |
The program will not compile successfully. |
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 2 |
strnstr() | |
strrchr() | |
strstr() | |
None of these is useful. Its better to use strcmp() |