C Programming Questions and Answers on Address Arithmetic for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_16.html
1. What is the output of this C code?
#include
int main()
{
double *ptr = (double *)100;
ptr = ptr + 2;
printf("%u", ptr);
}
a) 102
b) 104
c) 108
d) 116
b) 104
c) 108
d) 116
Answer: d
2. Comment on the output of this 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
b) 3
c) 5
d) Compile time error
Answer: d
3. Which of the following operand can be applied to pointers p and q?
(Assuming initialization as int *a = (int *)2; int *b = (int *)3;)
(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
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
5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)
(Assuming initialization int *a = 2; int *b = 3;)
a) a | b
b) a ^ b
c) a & b
d) None of the mentioned
Answer: d
6. What is the output of this C code?
#include
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *(p + 1), s[1]);
}
a) h e
b) e l
c) h h
d) e e
b) e l
c) h h
d) e e
Answer: d
7. What is the output of this C code?
#include
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *p, s[1]);
}
a) e h
b) Compile time error
c) h h
d) h e
b) Compile time error
c) h h
d) h e
Answer: d
8. What is the output of this C code?
#include
void main()
{
char *s = "hello";
char *n = "cjn";
char *p = s + n;
printf("%c\t%c", *p, s[1]);
}
a) h e
b) Compile time error
c) c o
d) h n
b) Compile time error
c) c o
d) h n
Answer: b
9. What is the output of this C code?
#include
void main()
{
char *s = "hello";
char *p = s * 3;
printf("%c\t%c", *p, s[1]);
}
a) h e
b) l e
c) Compile time error
d) l h
b) l e
c) Compile time error
d) l h
Answer: c
10. What is the output of this C code?
#include
void main()
{
char *s= "hello";
char *p = s + 2;
printf("%c\t%c", *p, s[1]);
}
a) l e
b) h e
c) l l
d) h l
b) h e
c) l l
d) h l
Answer: a
11. What is the output of this C code?
#include
int main()
{
void *p;
int a[4] = {1, 2, 3, 8};
p = &a[3];
int *ptr = &a[2];
int n = p - ptr;
printf("%d\n", n);
}
a) 1
b) Compile time error
c) Segmentation fault
d) 4
b) Compile time error
c) Segmentation fault
d) 4
Answer: b
12. What is the output of this C code?
#include
int main()
{
void *p;
int a[4] = {1, 2, 3, 4};
p = &a[3];
int *ptr = &a[2];
int n = (int*)p - ptr;
printf("%d\n", n);
}
a) 1
b) Compile time error
c) Segmentation fault
d) 4
b) Compile time error
c) Segmentation fault
d) 4
Answer: a
13. What is the output of this C code?
#include
int main()
{
int a[4] = {1, 2, 3, 4};
int b[4] = {1, 2, 3, 4};
int n = &b[3] - &a[2];
printf("%d\n", n);
}
a) -3
b) 5
c) 4
d) Compile time error
b) 5
c) 4
d) Compile time error
Answer: a
14. What is the output of this C code?
#include
int main()
{
int a[4] = {1, 2, 3, 4};
int *p = &a[1];
int *ptr = &a[2];
ptr = ptr * 1;
printf("%d\n", *ptr);
}
a) 2
b) 1
c) Compile time error
d) Undefined behaviour
b) 1
c) Compile time error
d) Undefined behaviour
Answer: c
15. What is the output of this C code?
#include
int main()
{
int a[4] = {1, 2, 3, 4};
int *ptr = &a[2];
float n = 1;
ptr = ptr + n;
printf("%d\n", *ptr);
}
a) 4
b) 3
c) Compile time error
d) Undefined behaviour
b) 3
c) Compile time error
d) Undefined behaviour
Answer: c
16. What is the output of this C code?
#include
int main()
{
int a[4] = {1, 2, 3, 4};
void *p = &a[1];
void *ptr = &a[2];
int n = 1;
n = ptr - p;
printf("%d\n", n);
}
a) 1
b) 4
c) Compile time error
d) Depends on the compiler
b) 4
c) Compile time error
d) Depends on the compiler
Answer: b