1.
#include “stdio.h”
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int y = 20;
fun(&y);
printf(“%d”, y);
return 0;
}
(a) 20
(b) 30
(c) Compiler Error
(d) Runtime Error
2.
int main()
{
char *ptr = “ScaBhopal”;
printf(“%c\n”, *&*&*ptr);
return 0;
}
(a) Compiler Error
(b) Garbage Value
(c) Runtime Error
(d) S
3.
#include “stdio.h”
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf(“%d %d \n”, i, j);
getchar();
return 0;
}
(a) 2 2
(b) 2 1
(c) 0 1
(d) 0 2
Answer
/* p points to i and q points to j */
void f(int *p, int *q)
{
p = q; /* p also points to j now */
*p = 2; /* Value of j is changed to 2 now */}
4.
#include “stdio.h”
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
++*p;
p += 2;
printf(“%d”, *p);
return 0;
}
(a) 2
(b) 3
(c) 4
(d) Compiler
When p += 2 is done, p is changed to point to third element of array.
5.
void f(int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
void main()
{
int i=5, j=10;
f(&i, j);
printf(“%d”, i+j);
}
(a) 10
(b) 20
(c) 30
(d) 40
6.
#include “stdio.h”
void fun(int x)
{
x = 30;
}
int main()
{
int y = 20;
fun(y);
printf(“%d”, y);
return 0;
}
(a) 30
(b) 20
(c) Compiler
(d) Runtime
Note that everything is passed by value in C. We only get the effect of pass by reference using pointers.
7.
#include “stdio.h”
int main()
{
static char *a[] = {“ibm”, “wipro”, “accenture”, “aegis”};
char **ptr[] = {a+3, a+2, a+1, a}, ***p;
p = ptr;
++p;
printf(“%s”, **p+1);
return 0;
}
(a) Ibm
(b) Gis
(c) Pro
(d) ccenture
Answer
2nd line create in another array having reversed order of strings in array ‘a'(i.e. the first string in this array is “aegis”).
3rd line making **p to point to the same address as **ptr is pointing to.
4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string “accenture”).
5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now pointing to “accenture”).
8.
#include “stdio.h”
int main()
{
int a=3.2, *b, c;
b = &a;
printf(“%d\n”, a**b*a-*b);
return 0;
}
(a) 3
(b) 30
(c) 27
(d) 24
Answer
above line executed in following steps
1>a**b
3**3=9
2>a**b*a
9*3=27(9 from the previous step)
3>a**b*a-*b
27-*j=27-*j=27-3=24
9.
#include “stdio.h”
int main()
{
int i=5, *j, *k;
j=&i; /* Assume address of i is 500 and integer is 4 byte size */
k=j;
*j++=*k++;
i++;
printf(“i=%d, j=%d, k=%d\n”, i, j, k);
return 0;
}
(a) I=6, j=502,k=502
(b) I=6,j=500,k=500
(c) I=6,j=504,k=504
(d) I=6,j=498,k=498
Answer
now this expression *j++=*k++; means “whatever the the VALUE AT k++ ,assign it to the address of j++”
note:-here *j++ & *k++ both are post increment so…firstly
expression(*j++=*k++;) is performed.
so…value at k is assigned to j(i.e nothing but 5)
and after that both address is incremented by 4 bytes(according to
the question) so finally..
i++;
becomes 6 and j,k becomes 504,504
10.
#include
int main()
{
char s[20] = “bye”;
char *const p=s;
*p=’Mi’;
printf(“%s\n”, s);
return 0;
}
(a) Miye
(b) Iye
(c) Bye
(d) None
Answer