This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pointers”.
Pre-requisite for C Pointers MCQ set: Video Tutorial on C Pointers.
1. What will be the output of the following C code?
#include <stdio.h>
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}
a) Compile time error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}
a) Compile time error
b) Undefined behaviour
c) 10
d) 0.000000
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}
a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
View Answer
Explanation: None.
5. What will be the output of the following C code?
#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return &j;
}
a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
View Answer
Explanation: We are returning address of a local variable which should not be done. In this specific instance, we are able to see the value of 10, which may not be the case if we call other functions before calling printf() in main().
6. Comment on the following pointer declaration.
int *ptr, p;
a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
a) 10,10
b) 10,11
c) 11,10
d) 11,11
View Answer
Explanation: None.
8. Comment on the following C statement.
const int *ptr;
a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) You May or may not change the value pointed by ptr
d) You can change the pointer as well as the value pointed by it
View Answer
Explanation: None.
More Pointers MCQs in C Programming:
- Pointers and Addresses
- Pointers and Function Arguments – 1
- Pointers and Function Arguments – 2
- Pointers and Arrays
- Pointers to Pointers – 1
- Pointers to Pointers – 2
- Pointers to Functions – 1
- Pointers to Functions – 2
- Character Pointers and Functions – 1
- Character Pointers and Functions – 2
- Initialization of Pointer Arrays – 1
- Initialization of Pointer Arrays – 2
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Check C Books
- Practice BCA MCQs
- Check Computer Science Books
- Apply for C Internship