Session 9
o Pointers
o Dynamic memory allocation
o Scope, binding
1. What is the output of this 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
Answer: a
2. What is (void*)0?
A.Representation of NULL pointer
B.Representation of void pointer
C.Error
D.None of above
Answer: Option A
3. Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
A. char p = *malloc(100);
B. char *p = (char) malloc(100);
C. char *p = (char*)malloc(100);
D. char *p = (char *)(malloc*)(100);
Answer: Option C
4. In which header file is the NULL macro defined?
A. stdio.h
B. stddef.h
C. stdio.h and stddef.h
D. math.h
Answer: Option C
Explanation: The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.
5. How many bytes are occupied by near, far and huge pointers (DOS)?
A. near=2 far=4 huge=4
B. near=4 far=8 huge=8
C. near=2 far=4 huge=8
D. near=4 far=4 huge=8
Answer: Option A
Explanation: near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long
6. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?
A. .
B. &
C. *
D. ->
Answer: Option D
7. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
A. ((((a+i)+j)+k)+l)
B. *(*(*(*(a+i)+j)+k)+l)
C. (((a+i)+j)+k+l)
D. ((a+i)+j+k+l)
Answer: Option B
8. A pointer is
A. A keyword used to create variables
B. A variable that stores address of an instruction
C. A variable that stores address of other variable
D. All of the above
Answer: Option C
9. The operator used to get value at address stored in a pointer variable is
A. *
B. &
C. &&
D. ||
Answer: Option A
10. What will be the output of the program ?
#include<stdio.h>
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}
A. ink
B. ack
C. ite
D. Let
Answer: A
Explanation: ptr = {pointer to "violet", pointer to "pink", pointer to "white", pointer to "black"}
p = ptr --> *p = pointer to "violet"
++p --> *p = pointer to "pink"
This implies that:
*p = {'p','i','n','k','\0'}
Which means:
**p = 'p'
**p + 1 = 'i'
so **p + 1 is a pointer to this string: {'i', 'n', 'k', '\0'}, which is simply "ink"
11. What will be the output of the program ?
#include<stdio.h>
int main()
{ int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}
[A]. 30
[B]. 27
[C]. 9
[D]. 3
Answer: Option A
Explanation:
i**j*i+*j
above line executed in following steps
1>i**j
3**3=9
2>i**j*i
9*3=27(9 from the previous step)
3>i**j*i+*j
27+*j=27+*j=27+3=30
12. What will be the output of the program ?
#include<stdio.h>
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
[A]. x=31, y=502, z=502
[B]. x=31, y=500, z=500
[C]. x=31, y=498, z=498
[D]. x=31, y=504, z=504
Answer: Option D
Explanation:
As x=30; y=z= 500;
*z++=504= *y++;
x++=31;
So the answer is 31, 504, 504.
13. What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
A. Mello
B. Hello
C. HMello
D. MHello
Answer: Option A
Explanation: Here p contains the base address and that value H is replaced by M.
14. What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>
int main()
{
int ***r, **q, *p, i=8;
p = &i;
q = &p;
r = &q;
printf("%d, %d, %d\n", *p, **q, ***r);
return 0;
}
A. 8, 8, 8
B. 4000, 4002, 4004
C. 4000, 4004, 4008
D. 4000, 4008, 4016
Answer: Option A
Explanation: Here each * means address holding it i.e. 'p' holds address of 'i', *p value at i = 8.
'q' holds address of p, p holds address of 'i'. So with two ** we are back to value at i = 8.
Similarly, three times *** means jump from r to q to p that holds i = 8.
15. Are the expression *ptr++ and ++*ptr are same?
A. True
B. False
Answer: Option B
Explanation:
*ptr++ increments the pointer and not the value, whereas the ++*ptr increments the value being pointed by ptr
16. Will the program compile?
#include<stdio.h>
int main()
{
char str[5] = "IndiaBIX";
return 0;
}
A. True
B. False
Answer: Option A
Explanation: C doesn't do array bounds checking at compile time, hence this compiles.
But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.
GCC would give you a warning.
17. The following program reports an error on compilation.
#include<stdio.h>
int main()
{
float i=10, *j;
void *k;
k=&i;
j=k;
printf("%f\n", *j);
return 0;
}
A. True
B. False
Answer: Option B
Explanation: This program will NOT report any error. (Tested in Turbo C under DOS and GCC under Linux)
The output: 10.000000
18. Are the three declarations char **apple, char *apple[], and char apple[][] same?
A. True
B. False
Answer: Option B
Explanation: char **apple - It is a double pointer
char *apple[] -It is an array of pointers
char apple[][]-It id 2-d array
19. What will be the output of the program ?
#include<stdio.h>
int main()
{
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}
A. Error
B. No output
C. K
D. %s
Answer: Option C
Explanation: The statement printf(str, "K\n"); is replaced with printf("%s" , "K\n");
// since str = "%s";
So it will print K.
20. What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>
int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof(""));
return 0;
}
A. 2, 1
B. 2, 2
C. 4, 1
D. 4, 2
Answer: Option C
Explanation: In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform. But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes. This difference is due to the platform dependency of C compiler.
21. What will be the output of the program ?
#include<stdio.h>
int main()
{
void *vp;
char ch=74, *cp="JACK";
int j=65;
vp=&ch;
printf("%c", *(char*)vp);
vp=&j;
printf("%c", *(int*)vp);
vp=cp;
printf("%s", (char*)vp+2);
return 0;
}
A. JCK
B. J65K
C. JAK
D. JACK
Answer: Option D
Explanation: Pointer always store integer value so cp will store the memory address of location where string "jack " is stored.
Step 1 : vp = &ch;
/*Will store address of ch in vp so while we print content in printf it will print asccii value of 74 i.e "J"*/
Step 2 : vp = &j;
/* It will assign address of j to vp again it will print ascii value of 65 as "A"*/
Step 3 : vp = cp;
/* In this step cp is pointing to memory locatioon where string jack is stored and we r incrementing it by two so it will point to "C" from sring "JACK" and since we hava given %S in printf so it will print content from c onwords ie "CK"*/
So final combined output will be JACK
22. Point out the compile time error in the program given below.
#include<stdio.h>
int main()
{
int *x;
*x=100;
return 0;
}
A. Error: invalid assignment for x
B. Error: suspicious pointer conversion
C. No error
D. None of above
Answer: Option C
Explanation: While reading the code there is no error, but upon running the program having an uninitialized variable can cause the program to crash (Null pointer assignment).
23. Point out the error in the program
#include<stdio.h>
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a);
a++;
}
return 0;
}
A. Error: Declaration syntax
B. Error: Expression syntax
C. Error: LValue required
D. Error: Rvalue required
Answer: Option C
Explanation: Lvalue and Rvalue means left hand side and right hand side respectively, required while doing the assignment operation.
It can be retified by
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j,*ptr;
ptr=&a;
for(j=0; j<5; j++)
{
printf("%d\n", *ptr);
ptr++;
}
return 0;
}
24. What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}
A. 8, 10
B. 10, 2
C. 8, 1
D. Garbage values
Answer: Option A
Explanation: Here the 3-D array the value can be store as:
a[2][2][2]={
{
10,2
3,4 // First Block
}
{
5,6
7,8 //Second Block
}
}
a[ 0 ] [ 0 ] [ 0 ] =10;
| | |
Block Rows Columns
a [ 1 ] [ 1 ] [ 1 ] = 8;
| | |
2'Block 2'Row 2'column
and the *arr represent the first number of a array is 10.
Then hence the number is (8,10).
25. What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}
A. 448, 4, 4
B. 520, 2, 2
C.1006, 2, 2
D. Error
Answer: Option C
Explanation: The array begins at the location 1002 and each integer occupies 4bytes. a[0]+1 =(1002+1(is nothing but 4 bytes)) = 1006
*(a[0]+1): This is nothing but the value of the 1st element +1(since * operator is used we are accessing the value). So 1+1=2.
26. What will be the output of the program?
#include<stdio.h>
int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
A. 2, 3
B. 2, 0
C. 2, Garbage value
D. 0, 0
Answer: Option B
27. What will be the output of the program ?
#include<stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
A. No output
B. 30
C. 3
D. 300
Answer: Option D
28. What will be the output of the program ?
#include<stdio.h>
int main()
{
printf("%c\n", 7["Helloworld"]);
return 0;
}
A. Error: in printf
B. Nothing will print
C. print "r"
D. print "7"
Answer: Option C
Explanation:
myarray[5] == 5[myarray]
It is printing the 7th character of the string "IndiaBIX".
29. Which of the following statements correctly declare a function that receives a pointer to pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?
A. float **fun(float***);
B. float *fun(float**);
C. float fun(float***);
D. float ****fun(float***);
Answer: Option D
Explanation: From the given question,
"... receives a pointer to pointer to a pointer to a float... " = ***
"... returns a pointer to a pointer to a pointer to a pointer..." = ****
Therefore, float ****fun (float ***) is the correct answer.
30. Which of the statements is correct about the program?
#include<stdio.h>
int main()
{
int i=10;
int *j=&i;
return 0;
}
A. j and i are pointers to an int
B. i is a pointer to an int and stores address of j
C. j is a pointer to an int and stores address of i
D. j is a pointer to a pointer to an int and stores address of i
Answer: Option C
31. Which of the statements is correct about the program?
#include<stdio.h>
int main()
{
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
A. It prints ASCII value of the binary number present in the first byte of a float variable a.
B. It prints character equivalent of the binary number present in the first byte of a float variable a.
C. It will print 3
D. It will print a garbage value
Answer: Option A
32. In the following program add a statement in the function fun() such that address of a gets stored in j?
#include<stdio.h>
int main()
{
int *j;
void fun(int**);
fun(&j);
return 0;
}
void fun(int **k)
{
int a=10;
/* Add a statement here */
}
A. **k=a;
B. k=&a;
C. *k=&a
D. &k=*a
Answer: Option C
33. Which of the following statements correct about k used in the below statement?
char ****k;
A. k is a pointer to a pointer to a pointer to a char
B. k is a pointer to a pointer to a pointer to a pointer to a char
C. k is a pointer to a char pointer
D. k is a pointer to a pointer to a char
Answer: Option B
34. What will be the output of the program ?
#include<stdio.h>
int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}
A. peace
B. eace
C. ace
D. ce
Answer: Option D
Explanation:
printf("%s\n", s++ +3);
In this,
first s+3 will get executed and printed. Which means s points to c. Hence, ce gets printed.
Then, s++ executes. That makes s to point to e but will not have any effect on outputed print.
35. What will be the output of the program ?
#include<stdio.h>
int main()
{
char *p;
p="hello";
printf("%s\n", *&*&p);
return 0;
}
A. llo
B. hello
C. ello
D. h
Answer: Option B
Explanation: Because & is reference operator and * is a dereference operator
so *(&p) converted into value at the address
this processes again...
and eventually we would get output as
--> hello
36. What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>
int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}
A. 1002, 2004, 4008, 2
B. 2004, 4008, 8016, 1
C. 1002, 1002, 1002, 1
D. Error
Answer: Option C
Explanation: Here a ,*a,and **a are same .....they return starting address of the array.
***a means that a[0][0][0]. so it returns the value 1.
37. Which of the statements is correct about the program?
#include<stdio.h>
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d\n", *(*(*(arr))));
return 0;
}
A. Output: Garbage value
B. Output: 1
C. Output: 3
D. Error: Invalid indirection
Answer: Option D
38. Which statement will you add to the following program to ensure that the program outputs "IndiaBIX" on execution?
#include<stdio.h>
int main()
{
char s[] = "Helloworld";
char t[25];
char *ps, *pt;
ps = s;
pt = t;
while(*ps)
*pt++ = *ps++;
/* Add a statement here */
printf("%s\n", t);
return 0;
}
A. *pt='';
B. pt='\0';
C. pt='\n';
D. *pt='\0';
Answer: Option D
Explanation:
pt=t;
So here pt having the same address as t, but in order to treat as a string pt must be null terminated so for we are putting
*pt='\0'; explicitly.
39. Which header file should be included to use functions like malloc() and calloc()?
A. memory.h
B. stdlib.h
C. string.h
D. dos.h
Answer: Option B
40. What function should be used to free the memory allocated by calloc() ?
A. dealloc();
B. malloc(variable_name, 0)
C. free();
D. memalloc(variable_name, 0)
Answer: Option C
41. Specify the 2 library functions to dynamically allocate memory?
A. malloc() and memalloc()
B. alloc() and memalloc()
C. malloc() and calloc()
D. memalloc() and faralloc()
Answer: Option C
42. What will be the output of the program (16-bit platform)?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p = (int *)malloc(20);
printf("%d\n", sizeof(p));
free(p);
return 0;
}
A. 4
B. 2
C. 8
D. Garbage value
Answer: Option B
Explanation: P is integer pointer and it stores 2 bytes so sizeof P is 2 bytes
43. Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[3];
a = (int*) malloc(sizeof(int)*3);
free(a);
return 0;
}
A. Error: unable to allocate memory
B. Error: We cannot store address of allocated memory in a
C. Error: unable to free memory
D. No error
Answer: Option B
Explanation: We should store the address in a[i]
44. Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
*ptr = (char)malloc(30);
strcpy(ptr, "RAM");
printf("%s", ptr);
free(ptr);
return 0;
}
A. Error: in strcpy() statement.
B. Error: in *ptr = (char)malloc(30);
C. Error: in free(ptr);
D. No error
Answer: Option B
Explanation:
ptr = (char*)malloc(30);
45. malloc() allocates memory from the heap and not from the stack.
A. True
B. False
Answer: Option A
46. malloc() returns a NULL if it fails to allocate the requested memory.
A. True
B. False
Answer: Option A
47. Which of the following statement is correct prototype of the malloc() function in c ?
A. int* malloc(int);
B. char* malloc(char);
C. unsigned int* malloc(unsigned int);
D. void* malloc(size_t);
Answer: Option D
48. Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct ex
{
int i;
float j;
char *s
};
struct ex *p;
p = (struct ex *)malloc(sizeof(struct ex));
p->s = (char*)malloc(20);
return 0;
}
A. free(p); , free(p->s);
B. free(p->s); , free(p);
C. free(p->s);
D. free(p);
Answer: Option B
49. Point out the correct statement which correctly allocates memory dynamically for 2D array following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p, i, j;
/* Add statement here */
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
p[i*4+j] = i;
printf("%d", p[i*4+j]);
}
}
return 0;
}
A. p = (int*) malloc(3, 4);
B. p = (int*) malloc(3*sizeof(int));
C. p = malloc(3*4*sizeof(int));
D. p = (int*) malloc(3*4*sizeof(int));
Answer: Option D
50. Can we increase the size of dynamically allocated array?
A. Yes
B. No
Answer: Option A
Explanation:
Use realloc(variable_name, value);
Previous Next