C Miscellaneous - Study Mode

[#71] Determine Output: void main()
{
char *p
p="Hello"
printf("%c", *&*p)
}
Correct Answer

(B) H

Explanation

Solution: * is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H.

[#72] Determine Output: void main()
{
int i=1
while(i<=5)
{
printf("%d", i)
if(i>2)
goto here
i++
}
}
fun()
{
here: printf("PP")
}
Correct Answer

(C) Compiler Error

Explanation

Solution: Compiler error: Undefined label 'here' in function main Labels have functions scope, in other words The scope of the labels is limited to functions. The label 'here' is available in function fun() Hence it is not visible in function main.

[#73] Determine Output: void main()
{
char *p
p="%d
"
p++
p++
printf(p-2, 300)
}
Correct Answer

(B) 300

Explanation

Solution: The pointer points to % since it is incremented twice and again decremented by 2, it points to ' %d
' and 300 is printed.

[#74] Determine Output: void main()
{
int c[] = {2.8,3.4,4,6.7,5}
int j, *p=c, *q=c
for(j=0
j<5
j++){
printf(" %d ", *c)
++q
}
for(j=0
j<5
j++){
printf(" %d ", *p)
++p
}
}
Correct Answer

(D) 2 2 2 2 2 2 3 4 6 5

Explanation

Solution: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

[#75] Determine Output: void main()
{
int a[] = {10,20,30,40,50}, j, *p
for(j=0
j<5
j++){
printf("%d" ,*a)
a++
}
p = a
for(j=0
j<5
j++){
printf("%d" ,*p)
p++
}
}
Correct Answer

(C) Error

Explanation

Solution: Compiler error: lvalue required Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue.