Here is a listing of C quiz on “Pointers and Arrays” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int a[3] = {1, 2, 3};
-
int *p = a;
-
printf("%pt%p", p, a);
-
}
a) Same address is printed
b) Different address is printed
c) Compile time error
d) Nothing
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s = "hello";
-
char *p = s;
-
printf("%pt%p", p, s);
-
}
a) Different address is printed
b) Same address is printed
c) Run time error
d) Nothing
Answer: b
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s= "hello";
-
char *p = s;
-
printf("%ct%c", p[0], s[1]);
-
}
a) Run time error
b) h h
c) h e
d) h l
Answer: c
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s= "hello";
-
char *p = s;
-
printf("%ct%c", *(p + 3), s[1]);
-
}
a) h e
b) l l
c) l o
d) l e
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *s= "hello";
-
char *p = s;
-
printf("%ct%c", 1[p], s[1]);
-
}
a) h h
b) Run time error
c) l l
d) e e
Answer: d
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
void foo( int[] );
-
int main()
-
{
-
int ary[4] = {1, 2, 3, 4};
-
foo(ary);
-
printf("%d ", ary[0]);
-
}
-
void foo(int p[4])
-
{
-
int i = 10;
-
p = &i;
-
printf("%d ", p[0]);
-
}
a) 10 10
b) Compile time error
c) 10 1
d) Undefined behaviour
Answer: c
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int ary[4] = {1, 2, 3, 4};
-
int *p = ary + 3;
-
printf("%dn", p[-2]);
-
}
a) 1
b) 2
c) Compile time error
d) Some garbage value
Answer: b
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int ary[4] = {1, 2, 3, 4};
-
int *p = ary + 3;
-
printf("%d %dn", p[-2], ary[*p]);
-
}
a) 2 3
b) Compile time error
c) 2 4
d) 2 somegarbagevalue
Answer: d
Clarification: None.