Here is a listing of C questions and puzzles on “Address Arithmetic” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
double *ptr = (double *)100;
-
ptr = ptr + 2;
-
printf("%u", ptr);
-
}
a) 102
b) 104
c) 108
d) 116
Answer: d
Clarification:None
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int *p = (int *)2;
-
int *q = (int *)3;
-
printf("%d", p + q);
-
}
a) 2
b) 3
c) 5
d) Compile time error
Answer: d
Clarification: None.
3. Which of the following arithmetic operation can be applied to pointers a and b?
(Assuming initialization as int *a = (int *)2; int *b = (int *)3;)
a) a + b
b) a – b
c) a * b
d) a / b
Answer: b
Clarification: None.
4. What is the size of *ptr in a 32-bit machine (Assuming initialization as int *ptr = 10;)?
a) 1
b) 2
c) 4
d) 8
Answer: c
Clarification: None.
5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)
a) a | b
b) a ^ b
c) a & b
d) None of the mentioned
Answer: d
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s = "hello";
-
char *p = s;
-
printf("%ct%c", *(p + 1), s[1]);
-
}
a) h e
b) e l
c) h h
d) e e
Answer: d
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s = "hello";
-
char *p = s;
-
printf("%ct%c", *p, s[1]);
-
}
a) e h
b) Compile time error
c) h h
d) h e
Answer: d
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s = "hello";
-
char *n = "cjn";
-
char *p = s + n;
-
printf("%ct%c", *p, s[1]);
-
}
a) h e
b) Compile time error
c) c o
d) h n
Answer: b
Clarification: None.