C Miscellaneous - Study Mode

[#76] Determine Output: #include<stdio.h>
void main()
{
char s[]={'a','b','c','n','c','x00'}
char *p, *str, *str1
p=&s[3]
str=p
str1=s
printf("%c", ++*p + ++*str1-32)
}
Correct Answer

(C) M

Explanation

Solution: p is pointing to character '
'. str1 is pointing to character 'a'. ++*p: "p is pointing to '
' and that is incremented by one." the ASCII value of '
' is 10. then it is incremented to 11. the value of ++*p is 11. ++*str1: "str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Both 11 and 98 is added and result is subtracted from 32. i.e. (11+98-32) = 77("M").

[#77] 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)
}
Correct Answer

(D) ck

Explanation

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'.

[#78] What will be the output of the following C code? #include<stdio.h>
main()
{
int n
n=f1(4)
printf("%d",n)
}
f1(int x)
{
int b
if(x==1)
return 1
else
b=x*f1(x-1)
return b
}
Correct Answer

(A) 24

[#79] What is the output of this C code? #include <stdio.h>
main()
{
if (sizeof(int) > -1)
printf("True")
else
printf("False")
}
Correct Answer

(B) False

[#80] Suppose we have: int a=100
(signed by default). If we want to convert this to an unsigned long integer, we can do this by making the following small change:
Correct Answer

(B) int a= 100ul