Professional Documents
Culture Documents
Coding
Coding
Determine Output:
void main()
{
static char *s[] = {"black", "white", "yellow", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s",*--*++p + 3);
}
A. te
B. ow
C. et
D. ck
Answer: Option D
Solution:
In this problem we have an array of char pointers "s" pointing to start of 4 strings. Then we have ptr which is a
pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p holds the
initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the
printf statement the expression is evaluated as follows: *++p causes gets value s+1 then the pre decrement is
executed and we get s+1-1 = s which is pointing to the word "black". The indirection operator now gets the value
from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the
output is 'ck'.
-------------------------------------------
1) How many times this loop will execute?
int main()
{
inti;
for(i=0;i<10;i++)
{
printf("%d\n",i++);
i=++i;
}
return 0;
}
1. 10
2. 5
3. 4
4. None of these
In this code, i initialize by ‘0’ and terminate condition is ‘i less than 10’ and there is 3
increment conditions is used, first in for loop, second in print statement, third in the
body of the forloop. For single iteration, there is 3 increment operation perform. That’s
way loop execute 4 times for 0,3,6,9.
const MAX=10;
int main()
{
inti=1;
for(;i<MAX;i=i/i)
{
printf("%d\n",i);
}
return 0;
}
1. 10
2. 5
3. 4
4. Infinite
Infinite
In this code, for loop never achieve the termination condition, because iteration
condition reset the variable of the loop(i) to an initial value that is ‘1’.
Correct Answer - 2
In this code, Loop can run infinitely if condition is set to TRUE always or no condition is
specified, i.e. for(;;)but in this condition, we specify terminating in if statement and by
break statement we exit the for loop.
1. 1 2 3 4 END
2. 1 2 3 4 5 END
3. 5 END
4. Error
Correct Answer - 3
5 END
In this code, semicolon at end of the for loop signifies that there is no body exists for
this loop. So, for variable initialized with ‘0’ and terminate with ‘5’.
int main()
{
inti=5;
for (; 0; i--)
{
printf("%d\n",i);
}
return 0;
}
1. 54321
2. No output
3. Compile time error
4. Run time error
Correct Answer - 2
No output
Variables ‘i’ is initialized before for loop to ‘5’, condition is FALSE always as ‘0’ is
provided that causes NOT to execute loop statement, iteration is decrement of counter
variable ‘i’.
int main()
{
int i,k;
for (i=0, k=0; (i< 5 && k < 3); i++, k++)
{
;
}
printf("%d\n",i);
return 0;
}
1. 5
2. 3
3. 4
4. No output
Correct Answer - 2
Variables ‘i’ and ‘k’ are initialized to 0; condition is to execute loop when ‘i’ is lesser than
‘5’and ‘k’ is lesser than ‘ 3’; iteration is increment of counter variables ‘i’ and ‘k’.