0% found this document useful (0 votes)
337 views11 pages

8.c Pointers

This document contains 19 multiple choice questions about pointers in C programming. The questions cover topics like pointer arithmetic, pointer to pointer, pointer to array, pointer to structure, and more. For each question, 4 possible answers are provided as multiple choice options.

Uploaded by

subash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views11 pages

8.c Pointers

This document contains 19 multiple choice questions about pointers in C programming. The questions cover topics like pointer arithmetic, pointer to pointer, pointer to array, pointer to structure, and more. For each question, 4 possible answers are provided as multiple choice options.

Uploaded by

subash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

‘C’ & ‘C’ only Sona Placement & Training Cell

8. Pointers
Week : 8 Duration : 60 mins

1.What is the output of this program?


main()
{ int i;
float *pf;
pf = (float *)&i;
*pf = 100.00;
printf(“\n %d”, i);
}
a) Runtime error. b) 100
c) Some Integer not 100 d) None of the above

2.What is the output of this program?


main()
{ int (*functable[2])(char *format, ...) ={printf, scanf};
int i = 100;
(*functable[0])(“%d”, i);
(*functable[1])(“%d”, i);
(*functable[1])(“%d”, i);
(*functable[0])(“%d”, &i);
}
a) 100, Runtime error.
b) 100, Random number, Random number, Random number.
c) Compile error
d) 100, Random number

3. Find the output of this program?


main()
{ int i, j, *p;
i = 25;
j = 100;
p = &i; // Address of i is assigned to pointer p
printf(“%f”, i/(*p) ); // i is divided by pointer p
}
a) Runtime error b) 1.00000 c) Compile error d) 0.00000

52
‘C’ & ‘C’ only Sona Placement & Training Cell

4.Predict the output of this program?


main()
{ char *p = “hello world”;
p[0] = ‘H’;
printf(“%s”, p);
}
a) Runtime error. b) “Hello world”
c) Compile error d) “hello world”

5.What is the output of this program?


main()
{ char * strA;
char * strB = I am OK;
memcpy( strA, strB, 6);
}
a) Runtime error. b) I am OK c) Compile error d) I am O

6.What is the output of this program?


struct Foo
{ char *pName;
};
main()
{ struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
strcpy(obj->pName,”Your Name”);
printf(“%s”, obj->pName);
}
a) Your Name b) compile error c) Name d) Runtime error

7.What is the output of this program?


struct Foo
{ char *pName;
char *pAddress;
};
main()
{ struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
obj->pName = malloc(100);
obj->pAddress = malloc(100);
strcpy(obj->pName,”Your Name”);
strcpy(obj->pAddress, “Your Address”);
free(obj);

53
‘C’ & ‘C’ only Sona Placement & Training Cell

printf(“%s”, obj->pName);
printf(“%s”, obj->pAddress);
}
a) Your Name, Your Address b) Your Address, Your Address
c) Your Name Your Name d) None of the above

8.What is the output of this program?


main()
{ char *a = “Hello “;
char *b = “World”;
clrscr();
printf(“%s”, strcat(a,b));
}
a) Hello b) Hello World c) HelloWorld

9.Find the output of this program?


main()
{ char *a = “Hello “;
char *b = “World”;
clrscr();
printf(“%s”, strcpy(a,b));
}
a) Hello b) Hello World c) HelloWorld d) None of the above

10.What would be the output of following program?


main( )
{ int arr[ ] = { 0, 1, 2, 3, 4 } ;
int *ptr ;
for ( ptr = arr + 4 ; ptr >= arr ; ptr-- )
printf ( “%d “, arr [ ptr - arr ] ) ;
}

11.What would be the output of following program


main( )
{ static int a[ ] = { 0, 1, 2, 3, 4 } ;
static int *p[ ] = { a, a + 1, a + 2, a + 3, a + 4 };
int **ptr = p ;
printf ( “\n%u %d”, a, *a ) ;
printf ( “%u %u %d”, p, *p, **p );
printf ( “\n%u %u %d”, ptr, *ptr, **ptr ) ;
}

54
‘C’ & ‘C’ only Sona Placement & Training Cell

12. What would be the output of following program?


main( )
{ static int a[ ] = { 0, 1, 2, 3, 4 } ;
static int *p[ ] = { a, a + 1, a + 2, a + 3, a + 4 } ;
int **ptr = p ;
ptr++ ;
printf ( “\n%d %d %d”, ptr - p, *ptr - a, **ptr ) ;
*ptr++ ;
printf ( “\n%d %d %d”, ptr - p, *ptr - a, **ptr ) ;
*++ptr ;
printf ( “\n%d %d %d”, ptr - p, *ptr - a, **ptr ) ;
++*ptr ;
printf ( “\n%d %d %d”, ptr - p, *ptr - a, **ptr ) ;
}

13. What will be the output of the following program?


main( )
{ static int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
static int *ptr[3] = { a[0], a[1], a[2] } ;
int **ptr1 = ptr ;
int i ;
printf ( “\n” ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( “%d “, *ptr[i] ) ;
printf ( “\n” ) ;
for ( i = 0 ; i <= 2 ; i++ )
printf ( “%d “, *a[i] ) ;
printf ( “\n” ) ;
for ( i = 0 ; i <= 2 ; i++ )
{ printf ( “%d “, **ptr1 ) ;
ptr1++ ; }
}

14.What will be the output of the following program?


main( )
{ static char *s[ ] = {“ice”, “green”, “cone”, “please” } ;
staic char **ptr[ ] = { s + 3 , s + 2 , s + 1 , s } ;
char ***p = ptr ;
printf ( “\n%s” , **++p ) ;
printf ( “\n%s” , *--*++p + 3 ) ;
printf ( “\n%s” , *p[-2] + 3 ) ;
printf ( “\n%s” , p[-1][-1] + 1 ) ;
}

55
‘C’ & ‘C’ only Sona Placement & Training Cell

15. What will be the output of the following program?


main( )
{ struct s1
{ char *z ;
int i ;
struct s1 *p ;
};
static struct s1 a[ ] = { { “Nagpur”, 1, a + 1 }, { “Raipur”, 2, a + 2 },
{ “Kanpur”, 3, a } } ;
struct s1 *ptr = a ;
printf ( “\n%s %s %s”, a[0].z, ptr->z, a[2].p->z ) ;
}

16.What will be the output of the following program?


main( )
{ char str[ ] = “For your eyes only” ;
int i ;
char *p ;
for ( p = str, i = 0 ; p + i <= str + strlen ( str ) ; p++, i++ )
printf ( “%c”, *( p + i ) ) ;
}

17. What will be the output of the following program?


main()
{ char s[ ]=”man”;
int i;
for(i=0;s[ i ];i++)
printf(“\n%c%c%c%c”,s[ i ],*(s+i),*(i+s),i[s]);
}

18. What is the output of the following program?


main()
{ int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++)
{ printf(“ %d “,*c);
++q; }
for(j=0;j<5;j++)
{ printf(“ %d “,*p);
++p; }
}

56
‘C’ & ‘C’ only Sona Placement & Training Cell

19.What will be the output of the following program?


main()
{ char *p;
printf(“%d %d “,sizeof(*p),sizeof(p));
}

20.Find the output of the following program?


#include<stdio.h>
main()
{ char s[]={‘a’,’b’,’c’,’\n’,’c’,’\0’};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf(“%d”,++*p + ++*str1-32);
}

21. What will be the output of the following program?


main()
{ char *p=”hai friends”,*p1;
p1=p;
while(*p!=’\0’) ++*p++;
printf(“%s %s”,p,p1);
}

22. What will be the output of the following program?


void main()
{ char far *farther,*farthest;
printf(“%d..%d”,sizeof(farther),sizeof(farthest));
}

23. Predict the output of the following program?


main()
{ char *p;
p=”Hello”;
printf(“%c\n”,*&*p);
}

24. What will be the output of the following program?


#include<stdio.h>
main()
{ int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };

57
‘C’ & ‘C’ only Sona Placement & Training Cell

int *p,*q;
p=&a[2][2][2];
*q=***a;
printf(“%d----%d”,*p,*q);
}

25.Find the output of the following program?


main( )
{ int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}

8. Assignment

26. What will be the output of the following program?


main( )
{ int a[ ] = {10,20,30,40,50},j,*p;
for(j=0; j<5; j++)
{ printf(“%d” ,*a);
a++; }
p = a;
for(j=0; j<5; j++)
{ printf(“%d ” ,*p);
p++; }
}

27. Predict the output of the following program?


main( )
{ static int a[ ] = {0,1,2,3,4};
int *p[ ] = {a,a+1,a+2,a+3,a+4};
int **ptr = p;
ptr++;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
*ptr++;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
*++ptr;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
++*ptr;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
}

58
‘C’ & ‘C’ only Sona Placement & Training Cell

28. What will be the output of the following program?


main( )
{ char *q;
int j;
for (j=0; j<3; j++) scanf(“%s” ,(q+j));
for (j=0; j<3; j++) printf(“%c” ,*(q+j));
for (j=0; j<3; j++) printf(“%s” ,(q+j));
}

29.Find the output of the following program?


main( )
{ void *vp;
char ch = ‘g’, *cp = “goofy”;
int j = 20;
vp = &ch;
printf(“%c”, *(char *)vp);
vp = &j;
printf(“%d”,*(int *)vp);
vp = cp;
printf(“%s”,(char *)vp + 3);
}

30. What will be the output of the following program


main ( )
{ static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s”,*--*++p + 3);
}

31. Find the output of the following program?


main()
{ int i, n;
char *x = “girl”;
n = strlen(x);
*x = x[n];
for(i=0; i<n; ++i)
{ printf(“%s\n”,x);
x++;
}
}

59
‘C’ & ‘C’ only Sona Placement & Training Cell

32. What will be the output of the following program


main()
{ int *j;
{ int i=10;
j=&i;
}
printf(“%d”,*j);
}

33. Predict the output of the following program?


main()
{ char *p;
int *q;
long *r;
p=q=r=0;
p++;
q++;
r++;
printf(“%p...%p...%p”,p,q,r);
}

34. What will be the output of the following program?


# include <stdio.h>
int one_d[]={1,2,3};
main()
{ int *ptr;
ptr=one_d;
ptr+=3;
printf(“%d”,*ptr);
}

35.What will be the output of the following program?


# include<stdio.h>
aaa()
{ printf(“hi”); }
bbb()
{ printf(“hello”); }
ccc()
{ printf(“bye”); }
main()
{ int (*ptr[3])();
ptr[0]=aaa;

60
‘C’ & ‘C’ only Sona Placement & Training Cell

ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}

36.In the following program add a statement in the function fun such that the address
of ‘a’ gets stored in ‘j’.
main()\
{ int * j;
void fun(int **);
fun(&j);
}
void fun(int **k)
{ int a =0;
/* add a stmt here*/ }

37. What will be the output of the following program?


main()
{ char *p;
p=”%d\n”;
p++;
p++;
printf(p-2,300);
}

38. What will be the output of the following program?


void main()
{ void *v;
int integer=2;
int *i=&integer;
v=i;
printf(“%d”,(int*)*v);
}

39. Is the following statement a declaration/definition. Find what does it mean?


int (*x)[10];

40. What will be the output of the following program?


int swap(int *a,int *b)
{
*a=*a+*b;*b=*a-*b;*a=*a-*b;
}

61
‘C’ & ‘C’ only Sona Placement & Training Cell

main()
{ int x=10,y=20;
swap(&x,&y);
printf(“x= %d y = %d\n”,x,y);
}

41. What will be the output of the following program?


main()
{ har *p = “ayqm”;
printf(“%c”,++*(p++));
}

42. What will be the output of the following program?


main()
{ char *p = “ayqm”;
char c;
c = ++*p++;
printf(“%c”,c);
}

43. What will be the output of the following program?


int aaa() {printf(“Hi”);}
int bbb(){printf(“hello”);}
int ccc(){printf(“bye”);}
main()
{ int ( * ptr[3]) ();
ptr[0] = aaa;
ptr[1] = bbb;
ptr[2] =ccc;
ptr[2]();
}

44. Find the output of the following program?


void ( * abc( int, void ( *def) () ) ) ();

45. Predict the output of the following program?


void main()
{ int *mptr, *cptr;
mptr = (int*)malloc(sizeof(int));
printf(“%d”,*mptr);
int *cptr = (int*)calloc(sizeof(int),1);
printf(“%d”,*cptr);
}

62

You might also like