This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Address Arithmetic – 2”.
Pre-requisite for C Address Arithmetic MCQ set: Video Tutorial on C Pointers.
1. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
3. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
4. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
5. What will be the output of the following C code?
#include <stdio.h>
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) Can’t say
View Answer
Explanation: It depends on how the spaces for local variables are allocated on the runtime stack by the compiler. In some compiler, the answer will be 5, whereas in other compiler the answer will be -3. So, it is compiler dependent.
6. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
7. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
8. What will be the output of the following C code?
#include <stdio.h>
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
View Answer
Explanation: None.
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 Computer Science Books
- Apply for Computer Science Internship
- Check C Books
- Apply for C Internship