Dynamic Memory Allocation QA
Ques 1. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *s;
s=malloc(26);
strcpy(s, "ptinstitute.in");
printf("%s\n",s);
return 0;
}
options:
A. ptinstitute.in
B. 0
C.compiler error
D. runtime error
Ques 2. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *s;
s=malloc(26);
strcpy(s, "good morning");
free(s);
printf("%s\n",s);
return 0;
}
options:
A. good morning
B. garbage string
C. comipler error
D. rutime error
Ques 3. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *s;
s=malloc(26);
s=NULL;
strcpy(s,"good");
printf("%s\n",s);
return 0;
}
options:
A. runtime error
B. good
C. 0
D. compiler error
Ques 4. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *s;
s='g';
printf("%s\n",s);
return 0;
}
options:
A. compiler error
B. 0
C. g
D. run time error
Ques 5. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int *foo(void)
{
int x=22;
return &x;
}
int main()
{
int *w;
*w=foo() ;
printf("\n %d", *w);
return 0;
}
options:
A. 0
B. 22
C. run time error
D. garbage value
Ques 6. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int foo(int *s)
{
int *x=s;
return *x;
}
int main()
{
int w[1]={23};
w[0]=foo(w) ;
printf("\n %d", *w);
return 0;
}
options:
A. run time error
B. 23
C. 0
D. garbage value
Ques 7. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int* foo(int *s)
{
int *x=s;
x++;
return x;
}
int main()
{
int w[4]={56,3,44,5};
int *p;
p=foo(&w[0]) ;
printf("\n %d", *p);
return 0;
}
options:
A. 44
B. 56
C. 3
D. run time error
Ques 8. what is the output for the below code?
int * foo(int *s)
{
s=(int *)malloc(4);
return s;
}
int main()
{
int *p;
p= foo(p);
*p=283;
printf("\n %d", *p);
return 0;
}
options:
A. 283
B. 284
C. run time error
D. compiler error
Ques 9. what is the output for the below code?
int main()
{
int *p;
p= (int *)malloc(4);
*p=283;
++*p;
printf("\n %d", *p++);
return 0;
}
options:
A. 281
B. 283
C. 285
D. 284
Ques 10. what is the output for the below code?
int main()
{
int *q;
q=calloc(12,1);
*q=54;
*(++q)=78;
printf("\n %d %d", *(--q), *(q));
return 0;
}
options:
A. run time error
B. 54 78
C. 55 79
D. 53 77
Ques 11. what is the output for the below code?
int main()
{
int *q[2];
q[0]=calloc(4,2);
q[1]=calloc(4,2);
**q=54;
*q[1]=58;
++(*q[1]);
printf("\n %d %d", **q, *q[1]);
return 0;
}
options:
A. 55 58
B. 54 58
C. 54 59
D. run time error
Ques 12. what is the output for the below code?
int main()
{
int *q[2];
q[0]=(int *)calloc(4,2);
q[0]=(int *)realloc(q[0], 20);
**q=54;
++**q;
*(q[0]+3) = 78;
--(*(q[0]+3));
printf("\n %d %d", **q, *(*q +3));
return 0;
}
options:
A. 54 78
B. 55 77
C. 54 78
D. run time error
Ques 13. what is the output for the below code?
int main()
{
int ****p;
int ***q;
int **r;
int *s;
s=(int *)malloc(4);
r=&s;
q=&r;
p=&q;
*s=47;
printf("\n %d %d %d %d", *s, **r, ***q, ****p);
return 0;
}
options:
A. run time error
B. 47 46 47 0
C. 46 45 0 0
D. 47 47 47 47
Ques 14. what is the output for the below code?
int main()
{
int ****p;
int ***q;
int **r;
int *s;
s=(int *)malloc(4);
r=&s;
q=&r;
p=&q;
*s=47;
++**r;
++***q;
++****p;
printf("\n %d %d %d %d", *s, **r, ***q, ****p);
return 0;
}
options:
A.47 47 48 47
B.50 50 50 50
C. 47 46 50 47
D. 47 48 49 59
Ques 15. what is the output for the below code?
int main()
{
int *s;
s=(int *)calloc(4, 5);
*s=45;
s++;
*s=56;
*s=++(*s);
printf("\n %d %d ", *s, *(s-1));
return 0;
}
options:
A. 45 57
B. 56 45
C. 57 45
D. 45 56
Ques 16. what is the output for the below code?
int main()
{
int *s;
s=(int *)calloc(4, 2);
s=realloc(s, sizeof(int)*4);
*s=45;
s++;
*s=56;
*s=++(*s);
*(++s) =78;
*(++s) =97;
printf("\n %d %d %d %d", *s, *(s-1), *(s-2), *(s-3));
return 0;
}
options:
A. 57 97 45 57
B. 78 97 45 57
C. 97 78 57 45
D. 45 57 78 97
Ques 17. what is the output for the below code?
int main()
{
int *s[1];
*s = (int *)malloc(4);
*s = realloc(*s, sizeof(int)*3);
**s = 45;
++(*s);
**s=56;
*(++(*s)) =78;
*(++(*s)) =97;
printf("\n %d %d %d %d", **s, *(*s-1), *(*s-2), *(*s-3));
return 0;
}
options:
A. 45 56 78 97
B. 97 78 56 45
C. 45 56 97 78
D. 56 45 78 97
Answers
1. A
2. B
3. A
4. D
5. C
6. B
7. C
8. A
9. D
10. B
11. C
12. B
13. D
14. B
15. C
16. C
17. B