Here is a listing of C programming questions on “Address Arithmetic” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s = "hello";
-
char *p = s * 3;
-
printf("%ct%c", *p, s[1]);
-
}
a) h e
b) l e
c) Compile time error
d) l h
Answer: c
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s= "hello";
-
char *p = s + 2;
-
printf("%ct%c", *p, s[1]);
-
}
a) l e
b) h e
c) l l
d) h l
Answer: a
Clarification: None.
3. What will be the output of the following 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("%dn", n);
-
}
a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer: b
Clarification: None.
4. What will be the output of the following 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("%dn", n);
-
}
a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer: a
Clarification: None.
5. What will be the output of the following 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("%dn", n);
-
}
a) -3
b) 5
c) 4
d) Compile time error
Answer: a
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int a[4] = {1, 2, 3, 4};
-
int *p = &a[1];
-
int *ptr = &a[2];
-
ptr = ptr * 1;
-
printf("%dn", *ptr);
-
}
a) 2
b) 1
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int a[4] = {1, 2, 3, 4};
-
int *ptr = &a[2];
-
float n = 1;
-
ptr = ptr + n;
-
printf("%dn", *ptr);
-
}
a) 4
b) 3
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.
8. What will be the output of the following 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("%dn", n);
-
}
a) 1
b) 4
c) Compile time error
d) Depends on the compiler
Answer: b
Clarification: None.