1. This program will allocate the memory of ___ bytes for pointer “ptr”.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
ptr = realloc(0,sizeof(int)*10);
return 0;
}
a) 0
b) 10
c) 40
d) none of the mentioned
View Answer
Explanation: If the first argument of realloc() is NULL, then it behaves just like malloc().
2. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
free(ptr);
return 0
}
a) this program will print nothing after execution
b) segmentation fault
c) Aborted (core dumped)
d) none of the mentioned
View Answer
Explanation: This prgram is trying to free the memory which is not available in the heap segment.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
*** glibc detected *** ./san: free(): invalid pointer: 0x4a77cff4 ***
======= Backtrace: =========
/lib/libc.so.6[0x4a6489f2] ./san[0x80483c9] /lib/libc.so.6(__libc_start_main+0xf3)[0x4a5e96b3] ./san[0x8048321] ======= Memory map: ========
08048000-08049000 r-xp 00000000 fd:01 394194 /home/sanfoundry/san
08049000-0804a000 rw-p 00000000 fd:01 394194 /home/sanfoundry/san
09233000-09254000 rw-p 00000000 00:00 0 [heap] 4a5ab000-4a5cc000 r-xp 00000000 fd:01 785334 /lib/ld-2.14.90.so
4a5cc000-4a5cd000 r–p 00020000 fd:01 785334 /lib/ld-2.14.90.so
4a5cd000-4a5ce000 rw-p 00021000 fd:01 785334 /lib/ld-2.14.90.so
4a5d0000-4a77a000 r-xp 00000000 fd:01 789110 /lib/libc-2.14.90.so
4a77a000-4a77b000 —p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77b000-4a77d000 r–p 001aa000 fd:01 789110 /lib/libc-2.14.90.so
4a77d000-4a77e000 rw-p 001ac000 fd:01 789110 /lib/libc-2.14.90.so
4a77e000-4a781000 rw-p 00000000 00:00 0
4a7e0000-4a7fc000 r-xp 00000000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
4a7fc000-4a7fd000 rw-p 0001b000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1
b7724000-b7725000 rw-p 00000000 00:00 0
b773d000-b773f000 rw-p 00000000 00:00 0
b773f000-b7740000 r-xp 00000000 00:00 0 [vdso] bfc83000-bfca4000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped)
[root@localhost sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
struct st{
int a;
char b;
};
int main()
{
struct st *st_ptr;
st_ptr = malloc(sizeof(struct st));
printf("%d\n",sizeof(struct st));
return 0;
}
a) 8
b) 5
c) 0
d) none of the mentioned
View Answer
Explanation: Maximum size of the data type is 4 byte(int) in the structure.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
8
[root@localhost sanfoundry]#
4. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
ptr = (char *)malloc(sizeof(char)*11);
ptr = "sanfoundry";
printf("%s\n",*ptr);
return 0;
}
a) sanfoundry
b) segmentation fault
c) syntax error
d) none of the mentioned
View Answer
Explanation: value of a string can not be assign to a pointer.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#
5. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *ptr;
memcpy(ptr,"sanfoundry",11);
printf("%s\n",ptr);
return 0;
}
a) sanfoudry
b) segmentation fault
c) syntax error
d) none of the mentioned
View Answer
Explanation: Memory must be allocated to pointer “ptr”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Segmentation fault (core dumped)
[root@localhost sanfoundry]#
6. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *ptr;
ptr = (char*)malloc(sizeof(char)*11);
strcpy(ptr,"sanfoundry");
printf("%d\n",*ptr);
return 0;
}
a) s
b) sanfoundry
c) 115
d) segmentation fault
View Answer
Explanation: This program will print the equivalent decimal value at location pointed by “ptr”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
115
[root@localhost sanfoundry]#
7. Which one of the following in true about this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *ptr;
printf("%p\n",ptr);
ptr = (char *)malloc(sizeof(char));
printf("%p\n",ptr);
return 0;
}
a) this program will give segmentation fault
b) this program will print two same values
c) this program has some syntax error
d) none of the mentioned
View Answer
Explanation: This program will print two different values.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0x4a77cff4
0x980c008
[root@localhost sanfoundry]#
8. In this program the two printed memory locations has the difference of ___ bytes.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
ptr = (int*)malloc(sizeof(int)*2);
printf("%p\n",ptr);
printf("%p\n",ptr+1);
return 0;
}
a) 1
b) 4
c) can not be determined
d) none of the mentioned
View Answer
Explanation: Pointer will increment by 4 bytes because it is the types of integer.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0x9b4e008
0x9b4e00c
[root@localhost sanfoundry]#
9. What is the output of this program?
#include<stdio.h>
#inlcude<stdlib.h>
int main()
{
int *ptr;
double *ptr;
printf("%d\n",sizeof(ptr));
return 0;
}
a) 4
b) 8
c) the compiler will give the error
d) segmentaion fault
View Answer
Explanation: Just see the output carefully.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:8:10: error: conflicting types for ‘ptr’
san.c:7:7: note: previous declaration of ‘ptr’ was here
[root@localhost sanfoundry]#
10. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int ptr;
ptr = (int)malloc(sizeof(int)*10);
return 0;
}
a) syntax error
b) segmentaion fault
c) run time error
d) none of the mentioned
View Answer
Explanation: The memory has been allocated but we can not access rest of the memory other than 4 bytes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]#
Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.
To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for Programming Internship
- Check Linux Books
- Practice Programming MCQs
- Check Information Technology Books