Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | arockia-raj |
View: | 222 times |
Download: | 0 times |
of 114
8/10/2019 pointers1.docx
1/114
8/10/2019 pointers1.docx
2/114
(a)Invalid statement
(b) ptr is a function that accepts an argument which is a pointer to a integer returns an integer
quantity
(c)ptr is a function pointer
(d)None of these
[Q006] Several declarations involving pointers are shown below. Pick the correct
solution.
int *ptr(int *a);
(a)Invalid statement
(b)ptr is a function pointer that accepts an argument which is a pointer to a integer returns
an integer quantity
(c) ptr is a function that accepts an argument which is a pointer to a integer returns a pointer to an
integer quantity
(d)None of these
[Q007] Several declarations involving pointers are shown below. Pick the correct solution.
int (*ptr)(char *a);
(a)Invalid statement
(b) ptr is a pointer to a function that accepts an argument which is a pointer to a character retur ns
an integer quantity
(c)ptr is a function that accepts an argument which is a pointer to a character returns a
pointer to an integer quantity
(d)None of these
[Q008] Several declarations involving pointers are shown below. Pick the correct solution.
int (*ptr(int *b))[5];
(a)Invalid statement(b)ptr is a pointer to a function that accepts pointer to an integer returns a 5 -element
integer array
(c) ptr is a function that accepts an argument which is a pointer to a integer returns a pointer to a 5 -
element integer array
(d)None of these
8/10/2019 pointers1.docx
3/114
[Q009] Several declarations involving pointers are shown below. Pick the correct solution.
char ptr(int (*a)[]);
(a)Invalid statement
(b)ptr is a function that acce pts an argumen t which is a pointer to a integer array returns an
character quantity
(c)ptr is a function that accepts an argument which is an array of pointers to integers
returns an character quantity
(d)None of these
[Q010] Several declarations involving pointers are shown below. Pick the correct solution.char ptr(int *a[]);
(a)Invalid statement
(b)ptr is a function that accepts an argument which is a pointer to a integer array returns
an character quantity
(c) ptr is a function that accepts an argument which is an array of pointers to integers returns an
character quantity
(d)None of these
[Q011] Several declarations involving pointers are shown below. Pick the correct solution.
int *ptr(char a[]);
(a)ptr is a function pointer
(b)ptr is a function that accepts an argument which is a character array returns an integer
(c) ptr is a function that accepts an argument which is a character array returns a pointer to an
integer quantity
(d)None of these
[Q012] Several declarations involving pointers are shown below. Pick the correct solution.
int *ptr(char *a[]);
(a)ptr is a function pointer
(b)ptr is a function that accepts an argument which is a pointer to a character array
8/10/2019 pointers1.docx
4/114
returns a pointer to an integer quantity
(c) ptr is a function that accepts an argument which is a array of pointers to characters returns a
pointer to an integer quantity
(d)None of these
[Q013] Several declarations involving pointers are shown below. Pick the correct solution.
int (*ptr)(char (*a)[]);
(a)ptr is a function that accepts an argument which is a pointer to a character array
returns a pointer to an integer quantity
(b)p tr is a pointer to a function that accepts an argument which is a pointer to a character
array returns an integer quantity
(c)ptr is a pointer to a function that accepts an argument which is a array of pointers to
characters returns an integer quantity (d)None of these
[Q014] Several declarations involving pointers are shown below. Pick the correct solution.
int *(*ptr)(char (*a)[]);
(a)ptr is a function that accepts an argument which is a pointer to a character array
returns a pointer to an integer quantity
(b )ptr is a pointer to a function that accepts an argument which is a pointer to a character array
returns a pointer to an integer quantity
(c)ptr is a pointer to a function that accepts an argument which is a array of pointers to
characters returns a pointer to an integer quantity
(d)None of these
[Q015] Several declarations involving pointers are shown below. Pick the correct solution.
int *(*ptr)(char (*a)[]);
(a)ptr is a function that accepts an argument which is a pointer to a character array
returns a pointer to an integer quantity
(b)ptr is a pointer to a function that accepts an argument which is a pointer to a character
array returns a pointer to an integer quantity
(c)ptr is a pointer to a function t hat accepts an argument which is an array of pointers to
characters returns a pointer to an integer quantity
8/10/2019 pointers1.docx
5/114
(d)None of these
[Q016] Several declarations involving pointers are shown below. Pick the correct solution.
int (*ptr[10])(void);
(a)Invalid statement
(b)ptr is a function that returns an integer quantity
(c) ptr is a 10-element array of pointers to functions. Each function returns an integer quantity.
(d)ptr is pointer to a function that returns a pointer to a 10-element integers
[Q017] Several declarations involving pointers are shown below. Pick the correct solution.
int (*ptr[10])(float a);
(a)Invalid statement
(b)ptr is a function that returns an integer quantity
(c) ptr is a 10-element array of pointers to functions. Each function accepts an argument which is a
float and returns an integer quantity.
(d)ptr is pointer to a function that accepts an argument which is a float and returns a
pointer to a 10-element integers
[Q018] Several declarations involving pointers are shown below. Pick the correct solution.
int *(*ptr[10])(float a);
(a)Invalid statement
(b)ptr is a function that returns an integer quantity
(c)ptr is a 10-element array of pointers to functions. Each function accepts an argument
which is a float and returns an integer quantity.
(d) ptr is a 10-element array of pointers to functions. Each function accepts an argument which is a
float and returns a pointer to an integer quantity.
[Q019] Several declarations involving pointers are shown below. Pick the correct solution.
int *(*ptr[10])(float *a);
8/10/2019 pointers1.docx
6/114
8/10/2019 pointers1.docx
7/114
(a) 1234 1234 (b)1235 1235 (c)1234 1235 (d)1235 1234
[Q003] What will be the output of the following program :
int main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,++*ptr);
return(0);
}
(a)1234 1234 (b) 1235 1235 (c)1234 1235 (d)1235 1234
[Q004] What will be the output of the following program :
int main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",val,(*ptr)++);
return(0);
}
(a)1234 1234 (b)1235 1235 (c)1234 1235 (d) 1235 1234
[Q005] What will be the output of the following program :
int main()
{
int val=1234;
int *ptr=&val;
printf("%d %d",++val,(*(int *)ptr)--);
return(0);
8/10/2019 pointers1.docx
8/114
}
(a)1234 1233 (b)1235 1234 (c) 1234 1234 (d)None of these
[Q006] What will be the output of the following program :
int main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",++a,--b,*ptr++);
return(0);}
(a)Compile-Time Error (b)555 554 555 (c) 556 554 555 (d)557 554 555
[Q007] What will be the output of the following program :
int main()
{int a=555,b=*ptr,*ptr=&a;
printf("%d %d %d",++a,--b,*ptr++);
return(0);
}
(a) Compile-Time Error (b)555 554 555 (c)556 554 555 (d)557 554 555
[Q008] What will be the output of the following program :
int main()
{
int a=555,*ptr=&a,b=*ptr;
printf("%d %d %d",a,--*&b,*ptr++);
return(0);
8/10/2019 pointers1.docx
9/114
}
(a)Compile-Time Error (b) 555 554 555 (c)556 554 555 (d)557 554 555
[Q009] What will be the output of the following program :
int main()
{
int a=555,*ptr=&a,b=*ptr=777;
printf("%d %d",--*&b,*(int *)&b);
return(0);
}
(a)Compile-Time Error (b) 776 777 (c)554 555 (d)None of these
[Q010] What will be the output of the following program :
int main()
{
int a=5u,*b,**c,***d,****e;
b=&a;
c=&b;
d=&c;
e=&d;
printf("%u %u %u %u",*b-5,**c-11,***d-6,65535+****e);
return(0);
}
(a)Compile-Time Error (b) 0 65530 65535 4
(c)0 65530 65535 65539 (d)0 -6 -1 -2
[Q011] What will be the output of the following program :
int main()
8/10/2019 pointers1.docx
10/114
{
float val=5.75;
int *ptr=&val;
printf("%.2f %.2f",*(float *)ptr,val);
return(0);
}
(a)Compile-Time Error (b)5.75 5.75 (c)5.00 5.75 (d)None of these
[Q012] What will be the output of the following program :
int main()
{
int val=50;const int *ptr1=&val;
int const *ptr2=ptr1;
printf("%d %d %d",++val,*ptr1,*ptr2);
*(int *)ptr1=98;
printf("\n%d %d %d",++val,*ptr1,*ptr2);
return(0);
}
(a)Compile-Time Error
(b)51 50 5099 98 98
(c)Run-Time Error
(d)None of these
[Q013] What will be the output of the following program :
int main()
{
int val=77;const int *ptr1=&val;
int const *ptr2=ptr1;
printf("%d %d %d",--val,(*ptr1)++,*ptr2);
return(0);
}
8/10/2019 pointers1.docx
11/114
(a)Compile-Time Error (b)77 78 77 (c)76 77 77 (d)77 77 77
[Q014] What will be the output of the following program :
int main()
{
int a=50,b=60;
int* const ptr1=&a;
printf("%d %d",--a,(*ptr1)++);
ptr1=&b;
printf("\n%d %d",++b,(*ptr1)++);
return(0);
}(a)Compile-Time Error
(b)49 50
61 60
(c)50 50
62 60
(d)None of these
[Q015] What will be the output of the following program :
int main()
{
int a=50;
const int* const ptr=&a;
printf("%d %d",*ptr++,(*ptr)++);
return(0);
}
(a)Compile-Time Error (b)51 51 (c)51 50 (d)None of these
[Q016] What will be the output of the following program :
int main()
{
8/10/2019 pointers1.docx
12/114
int val=77;
const int const *ptr=&val;
printf("%d",*ptr);
return(0);
}
(a)Compile-Time Error (b)Run-Time Error (c)77 (d)None of these
[Q017] What will be the output of the following program :
int main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;printf("%d %d",--*ptr+1,1+*--ptr);
return(0);
}
(a)Compile-Time Error (b)1 2 (c)2 3 (d)1 3
[Q018] What will be the output of the following program :
int main()
{
int a[]={1,2,3,4,5,6};
int *ptr=a+2;
printf("%d %d",*++a,--*ptr);
return(0);
}
(a)Compile-Time Error (b)2 2 (c)3 2 (d)4 2
[Q019] What will be the output of the following program :
int main()
8/10/2019 pointers1.docx
13/114
{
int matrix[2][3]={{1,2,3},{4,5,6}};
printf("%d %d %d\n",*(*(matrix)),*(*(matrix+1)+2),*(*matrix+1));
printf("%d %d %d",*(matrix[0]+2),*(matrix[1]+1),*(*(matrix+1)));
return(0);
}
(a)Compile-Time Error
(b)1 5 2
6 3 4
(c)1 6 2
3 5 4
(d)1 6 2
3 4 5
[Q020] What will be the output of the following program :
int main()
{
int (*a)[5];
printf("%d %d",sizeof(*a),sizeof(a));
return(0);
}
(a)Compile-Time Error
(b)2 5
(c)10 4
(d)None of these
1. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *p = NULL ;
5. char *q = 0;
8/10/2019 pointers1.docx
14/114
6. if (p )
7. printf (" p " );
8. else
9. printf ("nullp" );
10. if (q ) 11. printf ("q \n ");
12. else
13. printf (" nullq \n ");
14. }
a) nullp nullq b) Depends on the compilerc) x nullq where x can be p or nullp depending on the value of NULLd) p q
2. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int i = 10;
5. void *p = &i ;
6. printf ("%d \n ", (int)*p );
7. return 0;
8. }
a) Compile time errorb) Segmentation fault/runtime crashc) 10d) Undefined behaviour
3. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int i = 10;
5. void *p = &i ;
6. printf ("%f \n ", *(float *)p );
8/10/2019 pointers1.docx
15/114
7. return 0;
8. }
a) Compile time errorb) Undefined behaviourc) 10d) 0.000000
4. What is the output of this C code?
1. #include
2. int *f ();
3. int main ()
4. {
5. int *p = f ();
6. printf ("%d \n ", *p );
7. }
8. int *f ()
9. {
10. int *j = (int*)malloc (sizeof (int));
11. *j = 10 ;
12. return j ;
13. }
a) 10 b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returnedd) Undefined behaviour
5. What is the output of this C code?
1. #include
2. int *f ();
3. int main ()
4. { 5. int *p = f ();
6. printf ("%d \n ", *p );
7. }
8. int *f ()
9. {
10. int j = 10 ;
8/10/2019 pointers1.docx
16/114
11. return &j ;
12. }
a) 10b) Compile time errorc) Segmentation fault/runtime crashd) Undefined behaviour
6. Comment on the following pointer declaration?int *ptr, p;
a) ptr is a pointer to integer, p is notb) ptr and p, both are pointers to integerc) ptr is a pointer to integer, p may or may not bed) ptr and p both are not pointers to integer
7. What is the output of this C code?1. #include
2. int main ()
3. {
4. int *ptr , a = 10;
5. ptr = &a ;
6. *ptr += 1;
7. printf ("%d,%d/n" , *ptr , a );
8. }
a) 10,10b) 10,11c) 11,10d) 11,11
8. Comment on the following?const int *ptr;
a) You cannot change the value pointed by ptrb) You cannot change the pointer ptr itselfc) Both (a) and (b)
d) You can change the pointer as well as the value pointed by it
8/10/2019 pointers1.docx
17/114
Pointers and Addresses
1. Which is an indirection operator among the following?a) & b ) * c) -> d) .
2. Which of the following does not initialize ptr to null (assuming variable declaration of a asint a=0;?
a) i nt *ptr = &a; b) int *ptr = &a &a; c) int *ptr = a a; d) All of the mentioned
3. What is the output of this C code?
1. #include
2. int x = 0;
3. void main ()
4. {
5. int *ptr = &x ;
6. printf ("%p \n ", ptr );
7. x ++;
8. printf ("%p \n ", ptr );
9. }
a) Same addr e ss b) Different address c) Compile time error d) Varies
4. What is the output of this C code?
1. #include
2. int x = 0;
3. void main () 4. {
5. int *const ptr = &x ;
6. printf ("%p \n ", ptr );
7. ptr ++;
8. printf ("%p \n ", ptr );
9. }
a) 0 1 b) Comp ile time error c) 0xbfd605e8 0xbfd605ec d) 0xbfd605e8 0xbfd605e8
5. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int x = 0;
5. int *ptr = &x ;
6. printf ("%p \n ", ptr );
8/10/2019 pointers1.docx
18/114
7. ptr ++;
8. printf ("%p \n ", ptr );
9. }
a) 0xbfd605e8 0xbfd6 05e c b) 0xbfd605e8 0cbfd60520 c) 0xbfd605e8 0xbfd605e9
d) Run time error
6. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int x = 0;
5. int *ptr = &5;
6. printf ("%p \n ", ptr );
7. }
a) 5 b) Address of 5 c) Nothing d) C om pile time error
7. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int x = 0;
5. int *ptr = &x ;
6. printf ("%d \n ", *ptr );
7. }
a) Address of x b) Junk value c) 0 d) Run time error
Pointers and Function Arguments:
1. Which of the following can never be sent by call-by-value?
a) Variable b) Ar ra y c) Structures d) Both (b) and (c)2. Which type of variables can have same name in different function:a) global variables b) static variables c) Function argumentsd) Both (b) a nd (c) 3. Arguments that take input by user before running a program are called?a) main function arguments b) main argumentsc) Command-Line argumen ts d) Parameterized arguments
8/10/2019 pointers1.docx
19/114
4. The maximum number of arguments that can be passed in a single function are_____________a) 127 b) 2 53 c) 361 d) No limits in number of arguments5. What is the output of this C code?
1. #include
2. void m (int *p , int *q )
3. {
4. int temp = *p ; *p = *q ; *q = temp ;
5. }
6. void main ()
7. {
8. int a = 6, b = 5;
9. m(&a , &b );
10. printf ("%d %d \n ", a , b );
11. }
a) 5 6 b) 6 5 c) 5 5 d) 6 66. What is the output of this C code?
1. #include
2. void m (int *p )
3. {
4. int i = 0;
5. for (i = 0;i < 5; i ++)
6. printf ("%d \t ", p [i ]);
7. }
8. void main () 9. {
10. int a [5] = {6, 5, 3};
11. m(&a );
12. }
a) 0 0 0 0 0 b) 6 5 3 0 0 c) Run time error d) 6 5 3 junk junk7. What is the output of this C code?
1. #include
2. void m (int p , int q )
3. {
4. int temp = p ;
5. p = q ;
6. q = temp ;
7. }
8. void main ()
9. {
8/10/2019 pointers1.docx
20/114
8/10/2019 pointers1.docx
21/114
6. foo ((&i )++);
7. }
8. void foo (int *p )
9. {
10. printf ("%d\n
", *p ); 11. }
a) 10b) Some garbage valuec) Compile time errord) Segmentation fault/code crash
Answer:c
2. What is the output of this C code?
1. #include
2. void foo (int*);
3. int main ()
4. {
5. int i = 10, *p = &i ;
6. foo (p ++);
7. }
8. void foo (int *p )
9. {
10. printf ("%d \n ", *p );
11. }
a) 10b) Some garbage valuec) Compile time errord) Segmentation fault
Answer:a
3. What is the output of this C code?
1. #include
2. void foo (float *);
3. int main ()
4. {
5. int i = 10, *p = &i ;
6. foo (&i );
8/10/2019 pointers1.docx
22/114
7. }
8. void foo (float *p )
9. {
10. printf ("%f \n ", *p );
11. } a) 10.000000b) 0.000000c) Compile time errord) Undefined behaviour
Answer:b
4. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int i = 97, *p = &i ;
5. foo (&i );
6. printf ("%d " , *p );
7. }
8. void foo (int *p )
9. {
10. int j = 2;
11. p = &j ;
12. printf ("%d " , *p );
13. }
a) 2 97b) 2 2c) Compile time errord) Segmentation fault/code crash
Answer:a
5. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int i = 97, *p = &i ;
5. foo (&p );
8/10/2019 pointers1.docx
23/114
6. printf ("%d " , *p );
7. return 0;
8. }
9. void foo (int **p )
10. { 11. int j = 2;
12. *p = &j ;
13. printf ("%d " , **p );
14. }
a) 2 2b) 2 97c) Undefined behaviourd) Segmentation fault/code crash
Answer:a
6. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int i = 11 ;
5. int *p = &i ;
6. foo (&p );
7. printf ("%d " , *p );
8. }
9. void foo (int *const *p )
10. {
11. int j = 10 ;
12. *p = &j ;
13. printf ("%d " , **p );
14. }
a) Compile time errorb) 10 10c) Undefined behaviourd) 10 11
Answer:a
7. What is the output of this C code?
8/10/2019 pointers1.docx
24/114
1. #include
2. int main ()
3. {
4. int i = 10;
5. int *p = &i ; 6. foo (&p );
7. printf ("%d " , *p );
8. printf ("%d " , *p );
9. }
10. void foo (int **const p )
11. {
12. int j = 11 ;
13. *p = &j ;
14. printf ("%d " , **p );
15. }
a) 11 11 11b) 11 11 Undefined-valuec) Compile time errord) Segmentation fault/code-crash
Answer:b
8. What is the output of the code below?
1. #include
2. int main ()
3. {
4. int i = 10;
5. int *const p = &i ;
6. foo (&p );
7. printf ("%d \n ", *p );
8. }
9. void foo (int **p )
10. { 11. int j = 11 ;
12. *p = &j ;
13. printf ("%d \n ", **p );
14. }
a) 11 11b) Undefined behaviour
8/10/2019 pointers1.docx
25/114
c) Compile time errord) Segmentation fault/code-crash
Answer:a
9. Which of the following are correct syntaxes to send an array as a parameter to function:a) func(&array);b) func(array);c) func(*array);d) func(array[size]);View Answer
Answer:a & b
Pointers to array
1. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int a [3] = {1, 2, 3};
5. int *p = a ;
6. printf ("%p \t %p" , p , a );
7. }
a) Same address is printed.b) Different address is printed.c) Compile time errord) Nothing
Answer:a
2. What is the output of this C code?
1. #include
2. void main () 3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%p \t %p" , p , s );
7. }
8/10/2019 pointers1.docx
26/114
a) Different address is printedb) Same address is printedc) Run time errord) NothingView Answer
Answer:b
3. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , p [0], s [1]);
7. }
a) Run time errorb) h hc) h ed) h lView Answer
Answer:c
4. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , *(p + 3), s [1]);
7. }
a) h eb) l lc) l od) l eView Answer
Answer:d
8/10/2019 pointers1.docx
27/114
8/10/2019 pointers1.docx
28/114
8/10/2019 pointers1.docx
29/114
2. int main ()
3. {
4. double *ptr = (double *)100 ;
5. ptr = ptr + 2;
6. printf ("%u" , ptr ); 7. }
a) 102b) 104c) 108d) 116View Answer
Answer:d
2. Comment on the output of this C code?
1. #include
2. int main ()
3. {
4. int *p = (int *)2;
5. int *q = (int *)3;
6. printf ("%d" , p + q );
7. }
a) 2
b) 3c) 5d) Compile time errorView Answer
Answer:d3. Which of the following operand can be applied to pointers p and q?
(Assuming initialization as int *a = (int *)2; int *b = (int *)3;)a) a + bb) a bc) a * bd) a / bView Answer
Answer:b4. What is the size of *ptr in a 32-bit machine, (assuming initialization as int *ptr = 10;)?a) 1b) 2c) 4
8/10/2019 pointers1.docx
30/114
d) 8View Answer
Answer:c5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)a) a | bb) a ^ bc) a & bd) None of the mentionedView Answer
Answer:d
6. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , *(p + 1), s [1]);
7. }
a) h eb) e l
c) h hd) e eView Answer
Answer:d
7. What is the output of this C code?
1. #include
2. void main ()
3. { 4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , *p , s [1]);
7. }
a) e hb) Compile time error
8/10/2019 pointers1.docx
31/114
8/10/2019 pointers1.docx
32/114
2. What is the output of this C code?
1. #include
2. int main ()
3. { 4. const int ary [4] = {1, 2, 3, 4};
5. int *p ;
6. p = ary + 3;
7. *p = 5;
8. printf ("%d \n ", ary [3]);
9. }
a) 4b) 5c) Compile time errord) 3View Answer
Answer:b3. Different ways to initialize an array with all elements as zero area) int array[5] = {};b) int array[5] = {0};c) int a = 0, b = 0, c = 0;
int array[5] = {a, b, c};d) All of the mentioned
View Answer Answer:d4. The elements in the array of the following code are
int array[5] = {5};a) 5, 5, 5, 5, 5b) 5, 0, 0, 0, 0c) 5, (garbage), (garbage), (garbage), (garbage)d) (garbage), (garbage), (garbage), (garbage), 5View Answer
Answer:b
5. Which of the following declaration is illegal?a) int a = 0, b = 1, c = 2;
int array[3] = {a, b, c};b) int size = 3;
int array[size];c) int size = 3;
int array[size] = {1, 2, 3};
8/10/2019 pointers1.docx
33/114
8/10/2019 pointers1.docx
34/114
8/10/2019 pointers1.docx
35/114
c) 4d) 8View Answer
Answer:c5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)a) a | bb) a ^ bc) a & bd) None of the mentionedView Answer
Answer:d
6. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , *(p + 1), s [1]);
7. }
a) h e
b) e lc) h hd) e eView Answer
Answer:d
7. What is the output of this C code?
1. #include
2. void main () 3. {
4. char *s = "hello" ;
5. char *p = s ;
6. printf ("%c \t %c" , *p , s [1]);
7. }
8/10/2019 pointers1.docx
36/114
a) e hb) Compile time errorc) h hd) h eView Answer
Answer:d
8. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *n = "cjn" ;
6. char *p = s + n ;
7. printf ("%c \t %c" , *p , s [1]);
8. }
a) h eb) Compile time errorc) c od) h nView Answer
Answer:b
1. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *s = "hello" ;
5. char *p = s * 3;
6. printf ("%c \t %c" , *p , s [1]); 7. }
a) h eb) l ec) Compile time errord) l hView Answer
8/10/2019 pointers1.docx
37/114
8/10/2019 pointers1.docx
38/114
5. int a [4] = {1, 2, 3, 4};
6. p = &a [3];
7. int *ptr = &a [2];
8. int n = (int*)p - ptr ;
9. printf ("%d\n
", n ); 10. }
a) 1b) Compile time errorc) Segmentation faultd) 4View Answer
5. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int a [4] = {1, 2, 3, 4};
5. int b [4] = {1, 2, 3, 4};
6. int n = &b [3] - &a [2];
7. printf ("%d \n ", n );
8. }
a) -3
b) 5c) 4d) Compile time errorView Answer
6. What is the output of this C code?
1. #include
2. int main ()
3. { 4. int a [4] = {1, 2, 3, 4};
5. int *p = &a [1];
6. int *ptr = &a [2];
7. ptr = ptr * 1;
8. printf ("%d \n ", *ptr );
9. }
8/10/2019 pointers1.docx
39/114
8/10/2019 pointers1.docx
40/114
d) Depends on the compilerView Answer
Character Pointers and Functions
1. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *str = "hello, world \n ";
5. char *strc = "good morning \n ";
6. strcpy (strc , str );
7. printf ("%s \n ", strc ); 8. return 0;
9. }
a) hello, worldb) Crash/segmentation fault c) Undefined behaviourd) Run time error
2. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *str = "hello world" ;
5. char strc [] = "good morning india \n ";
6. strcpy (strc , str );
7. printf ("%s \n ", strc );
8. return 0;
9. }
a) hello world
b) hello worldg indiac) Compile time errord) Undefined behaviour
3. What is the output of this C code?
8/10/2019 pointers1.docx
41/114
1. #include
2. int main ()
3. {
4. char *str = "hello, world!! \n ";
5. char strc [] = "good morning\n
"; 6. strcpy (strc , str );
7. printf ("%s \n ", strc );
8. return 0;
9. }
a) hello, world!!b) Compile time errorc) Undefined behaviourd) Segmenation fault
4. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *str = "hello, world \n ";
5. str [5] = '.';
6. printf ("%s \n ", str );
7. return 0;
8. }
a) hello. worldb) hello, worldc) Compile errord) Segmentation fault
5. What is the output of this C code?
1. #include
2. int main () 3. {
4. char str [] = "hello, world" ;
5. str [5] = '.';
6. printf ("%s \n ", str );
7. return 0;
8. }
8/10/2019 pointers1.docx
42/114
a) hello. worldb) hello, worldc) Compile errord) Segmentation fault
6. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *str = "hello world" ;
5. char strary [] = "hello world" ;
6. printf ("%d %d \n ", sizeof (str ), sizeof (strary ));
7. return 0;
8. }
a) 11 11b) 12 12c) 4 12 d) 4 11
7. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *str = "hello world" ;
5. char strary [] = "hello world" ;
6. printf ("%d %d \n ", strlen (str ), strlen (strary ));
7. return 0;
8. }
a) 11 11 b) 12 11c) 11 12d) x 11 where x can be any positive integer.
8/10/2019 pointers1.docx
43/114
8. What is the output of this C code?
1. #include
2. void f (char *k )
3. { 4. k ++;
5. k [2] = 'm';
6. printf ("%c \n ", *k );
7. }
8. void main ()
9. {
10. char s [] = "hello" ;
11. f (s );
12. }
a) lb) ec) hd) o
9. What is the output of this C code?
1. #include
2. void fun (char *k )
3. {
4. printf ("%s" , k );
5. }
6. void main ()
7. {
8. char s [] = "hello" ;
9. fun (s );
10. }
a) hellob) Run time errorc) Nothingd) h
1. Comment on the output of this C code?
8/10/2019 pointers1.docx
44/114
1. #include
2. int main ()
3. {
4. char *str = "This" //Line 1
5. char *ptr = "Program\n
"; //Line 2 6. str = ptr ; //Line 3
7. printf ("%s, %s \n ", str , ptr ); //Line 4
8. }
a) Memory holding this is cleared at line 3 b) Memory holding this loses its reference at line 3 c) You cannot assign pointer like in Line 3d) Output will be This, ProgramView Answer
Answer:b
2. What type initialization is needed for the segment ptr[3] = 3; to work? a) char *ptr = Hello!; b) char ptr[] = Hello!; c) Both (a) and (b)d) None of the mentionedView Answer
Answer:b
3. The syntax for constant pointer to address (i.e., fixed pointer address) is:
a ) const *
b ) * const
c ) const *
d ) Both (a ) and (c ) View Answer
Answer:b
4. Comment on the output of this C code?
1. #include
2. int add (int a , int b )
3. {
4. return a + b ;
5. }
6. int main ()
7. {
8/10/2019 pointers1.docx
45/114
8. int (*fn_ptr )(int, int);
9. fn_ptr = add ;
10. printf ("The sum of two numbers is: %d" , (int)fn_ptr (2, 3));
11. }
a) Compile time error, declaration of a function inside main.b) Compile time error, no definition of function fn_ptr.c) Compile time error, illegal application of statement fn_ptr = add.d) No Run time error, output is 5.View Answer
Answer:d5. The correct way to declare and assign a function pointer is done by:
(Assuming the function to be assigned is int multi(int, int);) a) int (*fn_ptr)(int, int) = multi;b) int *fn_ptr(int, int) = multi;
c) int *fn_ptr(int, int) = &multi;d) Both (b) & (c)View Answer
Answer:a6. Calling a function f with a an array variable a[3] where a is an array, is equivalent toa) f(a[3])b) f(*(a + 3))c) f(3[a])d) All of the mentionedView Answer
Answer:d
7. What is the output of this C code?
1. #include
2. void f (char *k )
3. {
4. k ++;
5. k [2] = 'm';
6. }
7. void main ()
8. {
9. char s [] = "hello" ;
10. f (s );
11. printf ("%c \n ", *s );
8/10/2019 pointers1.docx
46/114
12. }
a) hb) ec) md) o;View Answer
Answer:a
8.What is the output of this C code?
1. #include
2. void main ()
3. {
4. char s [] = "hello" ;
5. s ++;
6. printf ("%c \n ", *s );
7. }
a) Compile time errorb) hc) ed) oView Answer
Answer:a
Pointers to Pointers
1. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int k = 5; 5. int *p = &k ;
6. int **m = &p ;
7. printf ("%d%d%d \n ", k , *p , **m);
8. }
a) 5 5 5b) 5 5 junk value
8/10/2019 pointers1.docx
47/114
c) 5 junk junkd) Run time errorView Answer
Answer:a
2. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int k = 5;
5. int *p = &k ;
6. int **m = &p ;
7. printf ("%d%d%d \n ", k , *p , **p );
8. }
a) 5 5 5b) 5 5 junk valuec) 5 junk junkd) Compile time errorView Answer
Answer:d
3. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int k = 5;
5. int *p = &k ;
6. int **m = &p ;
7. **m = 6;
8. printf ("%d \n ", k );
9. } a) 5b) Compile time errorc) 6d) JunkView Answer
Answer:c
8/10/2019 pointers1.docx
48/114
8/10/2019 pointers1.docx
49/114
Answer:d
7. What is the output of this C code?
1. #include 2. int main ()
3. {
4. int a = 1, b = 2, c = 3;
5. int *ptr1 = &a , *ptr2 = &b , *ptr3 = &c ;
6. int **sptr = &ptr1 ; //-Ref
7. *sptr = ptr2 ;
8. }
a) ptr1 points to ab) ptr1 points to bc) sptr points to ptr2d) None of the mentionedView Answer
Answer:b
8. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int a [3] = {1, 2, 3};
5. int *p = a ;
6. int **r = &p ;
7. printf ("%p %p" , *r , a );
8. }
a) Different address is printedb) 1 2c) Same address is printed.
d) 1 1View Answer
Answer:c
1. What substitution should be made to //-Ref such that ptr1 points to variable C?
8/10/2019 pointers1.docx
50/114
1. #include
2. int main ()
3. {
4. int a = 1, b = 2, c = 3;
5. int *ptr1 = &a ; 6. int **sptr = &ptr1 ;
7. //-Ref
8. }
a) *sptr = &c;b) **sptr = &c;c) *ptr1 = &c;d) None of the mentioned.View Answer
Answer:a
2. Which of the following declaration throw run-time error?a) int **c = &c;b) int **c = &*c;c) int **c = **c;d) None of the mentionedView Answer
Answer:d
3. Comment on the output of this C code?
1. #include
2. int main ()
3. {
4. int a = 10;
5. int **c -= && a ;
6. }
a) You cannot apply any arithmetic operand to a pointer.b) We dont have address of an address operator c) Both (a) and (b)d) None of the mentioned.View Answer
Answer:b
4. What is the output of this C code?
8/10/2019 pointers1.docx
51/114
1. #include
2. void main ()
3. {
4. int k = 5;
5. int *p = &k ; 6. int **m = &p ;
7. printf ("%d%d%d \n ", k , *p , **m);
8. }
a) 5 5 5b) 5 5 junk valuec) 5 junk junkd) Compile time errorView Answer
Answer:a
5. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int k = 5;
5. int *p = &k ;
6. int **m = &p ;
7. printf ("%d%d%d \n ", k , *p , **p );
8. }
a) 5 5 5b) 5 5 junk valuec) 5 junk junkd) Compile time errorView Answer
Answer:d
6. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int k = 5;
5. int *p = &k ;
8/10/2019 pointers1.docx
52/114
8/10/2019 pointers1.docx
53/114
7. for (j = 0; j < 3; j ++)
8. printf ("%d" , a [i ][j ]);
9. }
a) 1 2 3 4 5 0
b) 1 2 3 4 5 junkc) 1 2 3 4 5 5d) Run time errorView Answer
Answer:a
2. What is the output of this C code?
1. #include
2. void main ()
3. {
4. int a [2][3] = {1, 2, 3, , 4, 5};
5. int i = 0, j = 0;
6. for (i = 0; i < 2; i ++)
7. for (j = 0; j < 3; j ++)
8. printf ("%d" , a [i ][j ]);
9. }
a) 1 2 3 junk 4 5b) Compile time error
c) 1 2 3 0 4 5d) 1 2 3 3 4 5View Answer
Answer:b
3. What is the output of this C code?
1. #include
2. void f (int a [][ 3])
3. { 4. a [0][1] = 3;
5. int i = 0, j = 0;
6. for (i = 0; i < 2; i ++)
7. for (j = 0; j < 3; j ++)
8. printf ("%d" , a [i ][j ]);
9. }
8/10/2019 pointers1.docx
54/114
10. void main ()
11. {
12. int a [2][3] = {0};
13. f (a );
14. } a) 0 3 0 0 0 0b) Junk 3 junk junk junk junkc) Compile time errord) All junk valuesView Answer
Answer:a
4. What is the output of this C code?
1. #include
2. void f (int a [][])
3. {
4. a [0][1] = 3;
5. int i = 0, j = 0;
6. for (i = 0;i < 2; i ++)
7. for (j = 0;j < 3; j ++)
8. printf ("%d" , a [i ][j ]);
9. }
10. void main ()
11. {
12. int a [2][3] = {0};
13. f (a );
14. }
a) 0 3 0 0 0 0b) Junk 3 junk junk junk junkc) Compile time errord) All junk values
View Answer Answer:c
5. What is the output of this C code?
1. #include
2. void f (int a [2][])
8/10/2019 pointers1.docx
55/114
3. {
4. a [0][1] = 3;
5. int i = 0, j = 0;
6. for (i = 0;i < 2; i ++)
7. for (j = 0;j < 3; j ++) 8. printf ("%d" , a [i ][j ]);
9. }
10. void main ()
11. {
12. int a [2][3] = {0};
13. f (a );
14. }
a) 0 3 0 0 0 0b) Junk 3 junk junk junk junkc) Compile time errord) All junk valuesView Answer
Answer:c6. Comment on the following statement:
int (*a)[7];a) An array a of pointers. b) A pointer a to an array. c) A ragged array.
d) None of the mentionedView Answer Answer:b7. Comment on the 2 arrays regarding P and Q:
int *a1[8];int *(a3[8]);P. Array of pointersQ. Pointer to an array
a) a1 is P, a2 is Qb) a1 is P, a2 is P
c) a1 is Q, a2 is Pd) a1 is Q, a2 is QView Answer
Answer:b8. Which of the following is not possible statically in C?a) Jagged Arrayb) Rectangular Array
8/10/2019 pointers1.docx
56/114
c) Cuboidal Arrayd) Multidimensional ArrayView Answer
Answer:a
Here is a listing of C multiple choice questions on Multidimensional Arrays along with answers,
explanations and/or solutions:
1. What is the correct syntax to send a 3-dimensional array as a parameter?(Assuming declaration int a[5][4][3];)
a) func(a);b) func(&a);c) func(*a);
d) func(**a);View Answer
Answer:a2. Applications of multidimensional array are?a) Matrix-Multiplicationb) Minimum Spanning Treec) Finding connectivity between nodesd) All of the mentionedView Answer
Answer:d
3. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int ary [2][3];
5. foo (ary );
6. }
7. void foo (int *ary [])
8. {
9. int i = 10, j = 2, k ;
10. ary [0] = &i ;
11. ary [1] = &j ;
12. *ary [0] = 2;
8/10/2019 pointers1.docx
57/114
13. for (k = 0;k < 2; k ++)
14. printf ("%d \n ", *ary [k ]);
15. }
a) 2 2
b) Compile time errorc) Undefined behaviourd) 10 2View Answer
Answer:a
4. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int ary [2][3];
5. foo (ary );
6. }
7. void foo (int (*ary )[3])
8. {
9. int i = 10, j = 2, k ;
10. ary [0] = &i ;
11. ary [1] = &j ;
12. for (k = 0;k < 2; k ++)
13. printf ("%d \n ", *ary [k ]);
14. }
a) Compile time errorb) 10 2c) Undefined behaviourd) segmentation fault/code crashView Answer
Answer:a
5. What is the output of this C code?
1. #include
2. int main ()
3. {
4. foo (ary );
8/10/2019 pointers1.docx
58/114
8/10/2019 pointers1.docx
59/114
6. printf ("%d \n ", ary [1][0]);
7. }
a) Compile time errorb) 4c) 1d) 2View Answer
Answer:a
Here is a listing of C quiz on Initialization of Pointer Arrays along with answers, explanations and/or
solutions:
1. To declare a 3 dimension array using pointers, which of the following is the correct syntax:a) char *a[][];b) char **a[];c) char ***a;d) All of the mentionedView Answer
Answer:a
2. Comment on the output of this C code?
1. #include 2. int main ()
3. {
4. char *a = {"p" , "r" , "o" , "g" , "r" , "a" , "m" };
5. printf ("%s" , a );
6. }
a) Output will be programb) Output will be pc) No output
d) Compile-time errorView Answer Answer:b3. An array of strings can be initialized by:a) char *a[] = {Hello, World}; b) char *a[] = {Hello, Worlds}; c) char *b = Hello;
8/10/2019 pointers1.docx
60/114
char *c = World; char *a[] = {b, c};
d) All of the mentioned.View Answer
Answer:d
4. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *a [10] = {"hi" , "hello" , "how" };
5. int i = 0;
6. for (i = 0;i < 10; i ++)
7. printf ("%s" , *(a [i ])) ;
8. }
a) Segmentation faultb) hi hello how followed by 7 null valuesc) 10 null valuesd) depends on compilerView Answer
Answer:a
5. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char *a [10] = {"hi" , "hello" , "how" };
5. int i = 0, j = 0;
6. a [0] = "hey" ;
7. for (i = 0;i < 10; i ++)
8. printf ("%s \n ", a [i ]); 9. }
a) hi hello how Segmentation faultb) hi hello how followed by 7 null valuesc) hey hello how Segmentation faultd) Depends on compilerView Answer
8/10/2019 pointers1.docx
61/114
8/10/2019 pointers1.docx
62/114
6. for (i = 0;i < 10; i ++)
7. printf ("%s" , a [i ]);
8. }
a) hi hello how Segmentation fault
b) hi hello how nullc) hey hello how Segmentation faultd) hi hello how followed by 7 nullsView Answer
Answer:d
1. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char *p [1] = {"hello" };
5. printf ("%s" , (p )[0]);
6. return 0;
7. }
a) Compile time errorb) Undefined behaviourc) hello
d) None of the mentionedView Answer
2. What is the output of this C code?
1. #include
2. int main ()
3. {
4. char **p = {"hello" , "hi" , "bye" };
5. printf ("%s" , (p )[0]); 6. return 0;
7. }
a) Compile time errorb) Undefined behaviourc) hello
8/10/2019 pointers1.docx
63/114
8/10/2019 pointers1.docx
64/114
1. #include
2. int main ()
3. {
4. int i = 0, j = 1;
5. int *a [] = {&i , &j }; 6. printf ("%d" , (*a )[1]);
7. return 0;
8. }
a) Compile time errorb) Undefined behaviourc) 1d) Some garbage valueView Answer
Answer:d
6. Which of the following are generated from char pointer?a) char *string = Hello.; b) char *string;
scanf(%s, string); c) char string[] = Hello.; d) Both (a) and (c).View Answer
Answer:a7. Which of the following declaration are illegal?a) int a[][] = {{1, 2, 3}, {2, 3, 4, 5}};b) int *a[] = {{1, 2, 3}, {2, 3, 4, 5}};c) int a[4][4] = {{1, 2, 3}, {2, 3, 4, 5}};d) Both (a) and (b).View Answer
Answer:a
Here is a listing of C programming questions on Pointers Vs. Multi- dimensional Arrays along with answers,
explanations and/or solutions:
1. int a[10][20]; which is true for aa) a is true two-dimensional arrayb) 200 int-sized locations have been set asidec) The conventional rectangular subscript calculation 20 * row + col is used to find the elementa[row, col]
8/10/2019 pointers1.docx
65/114
d) All of the mentionedView Answer
Answer:d2. int *b[10]; which is true for ba) The definition only allocates 10 pointers and does not initialize themb) Initialization must be done explicitlyc) Both a and bd) ErrorView Answer
Answer:c
3. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char a [10][5] = {"hi" , "hello" , "fellows" };
5. printf ("%s" , a [2]);
6. }
a) fellowsb) fellowc) fellod) fell
View Answer Answer:c
4. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char a [10][5] = {"hi" , "hello" , "fellows" };
5. printf ("%p \n ", a ); 6. printf ("%p" , a [0]);
7. }
a) Same address is printedb) Different address is printedc) hello
8/10/2019 pointers1.docx
66/114
d) hi hello felloView Answer
Answer:a
5. What is the output of this C code?
1. #include
2. void main ()
3. {
4. char a [10][5] = {"hi" , "hello" , "fellows" };
5. printf ("%d" , sizeof (a [1])) ;
6. }
a) 2b) 4c) 5d) 10View Answer
Answer:c
6. What is the output of the code given below?
1. #include
2. int main ()
3. {
4. char a [1][5] = {"hello" };
5. printf ("%s" , a [0]);
6. return 0;
7. }
a) Compile time errorb) helloc) Undefined behaviourd) hellon
View Answer Answer:c
7. What is the output of the code given below?
1. #include
2. int main ()
8/10/2019 pointers1.docx
67/114
3. {
4. char *a [1] = {"hello" };
5. printf ("%s" , a [0]);
6. return 0;
7. } a) Compile time errorb) helloc) Undefined behaviourd) hellonView Answer
Answer:b8. Which of the following statements are true?
P. Pointer to ArrayQ. Multi-dimensional array
a) P are static, Q are staticb) P are static, Q are dynamicc) P are dynamic, Q are staticd) P are dynamic, Q are dynamicView Answer
Answer:c
Here is a listing of C interview questions on Command Line Arguments along with answers, explanations
and/or solutions:
1. What does argv and argc indicate in command-line arguments?(Assuming: int main(int argc, char *argv[]) )
a) argument count, argument variableb) argument count, argument vectorc) argument control, argument variabled) argument control, argument vectorView Answer
Answer:b
2. Which of the following syntax is correct for command-line arguments?a) int main(int var, char *varg[])b) int main(char *argv[], int argc)c) int main()
{int argv, char *argc[];
}
8/10/2019 pointers1.docx
68/114
d) Both (a) and (b)View Answer
Answer:a3. In linux, argv[0] by command-line argument can be occupied bya) ./a.outb) ./testc) ./fun.out.outd) All of the mentionedView Answer
Answer:d4. What type of array is generally generated in Command-line argument?a) Single dimension arrayb) 2-Dimensional Square Arrayc) Jagged Array
d) 2-Dimensional Rectangular ArrayView Answer 5. What would be the output if we try to execute following segment of code (assuming the
following input cool brother in city)? printf(%s \n, argv[argc]);
a) (null)b) Cityc) InD. Segmentation FaultView Answer
Answer:a6. The first argument in command line arguments isa) The number of command-line arguments the program was invoked with;b) A pointer to an array of character strings that contain the argumentsc) Nothingd) Both a & bView Answer 7. The second (argument vector) in command line arguments isa) The number of command-line arguments the program was invoked with;b) A pointer to an array of character strings that contain the arguments,one per string.c) Nothingd) Both a & bView Answer
Answer:b8. argv[0] in command line arguments, isa) The name by which the program was invokedb) The name of the files which are passed to the program
8/10/2019 pointers1.docx
69/114
c) Count of the arguments in argv[] vectord) Both a & bView Answer
Answer:a
1. A program that has no command line arguments will have argca) Zerob) Negativec) Oned) TwoView Answer
Answer:c2. The index of the last argument in command line arguments isa) argc 2
b) argc + 1c) argcd) argc 1View Answer
Answer:d
3. What is the output of this C code (if run with no options or arguments)?
1. #include
2. int main (int argc , char *argv [])
3. {
4. printf ("%d \n ", argc );
5. return 0;
6. }
a) 0b) 1c) Depends on the platformd) Depends on the compilerView Answer
Answer:b
4. What is the output of this C code (run without any commandline arguments)?
1. #include
2. int main (int argc , char *argv [])
8/10/2019 pointers1.docx
70/114
3. {
4. while (argc --)
5. printf ("%s \n ", argv [argc ]);
6. return 0;
7. } a) Compile time errorb) Executablefilenamec) Segmentation faultd) UndefinedView Answer
Answer:b
5. What is the output of this C code (run without any commandline arguments)?
1. #include
2. int main (int argc , char *argv [])
3. {
4. printf ("%s \n ", argv [argc ]);
5. return 0;
6. }
a) Segmentation fault/code crashb) Executable file namec) Depends on the platform
d) Depends on the compilerView Answer
Answer:a
6. What is the output of this C code (run without any commandline arguments)?
1. #include
2. int main (int argc , char *argv [])
3. {
4. while (*argv ++ != NULL ) 5. printf ("%s \n ", *argv );
6. return 0;
7. }
a) Segmentation fault/code crashb) Executable file namec) Depends on the platform
8/10/2019 pointers1.docx
71/114
d) Depends on the compilerView Answer
Answer:a
7. What is the output of this C code (run without any command line arguments)?
1. #include
2. int main (int argc , char *argv [])
3. {
4. while (*argv != NULL )
5. printf ("%s \n ", *(argv ++));
6. return 0;
7. }
a) Segmentation fault/code crashb) Executable file namec) Depends on the platformd) Depends on the compilerView Answer
Answer:b
8. What is the output of this C code(run without any command line arguments)?
1. #include
2. int main (int argc , char *argv [])
3. {
4. while (argv != NULL )
5. printf ("%s \n ", *(argv ++));
6. return 0;
7. }
a) Segmentation fault/code crashb) Executable file namec) Depends on the platform
d) Depends on the compilerView Answer
Answer:a
Here is a listing of C quiz on Pointer to Functions along with answers, explanations and/or solutions:
8/10/2019 pointers1.docx
72/114
1. Which function is not called in the following program?
1. #include
2. void first ()
3. { 4. printf ("first" );
5. }
6. void second ()
7. {
8. first ();
9. }
10. void third ()
11. {
12. second ();
13. }
14. void main ()
15. {
16. void (*ptr )() ;
17. ptr = third ;
18. ptr ();
19. }
a) Function firstb) Function second
c) Function thirdd) None of the mentionedView Answer
Answer:d2. How to call a function without using the function name to send parameters?a) typedefsb) Function pointerc) Both (a) and (b)d) None of the mentionedView Answer
Answer:b3. Correct syntax to pass a Function Pointer as an argumenta) void pass(int (*fptr)(int, float, char)){}b) void pass(*fptr(int, float, char)){}c) void pass(int (*fptr)){}d) void pass(*fptr){}View Answer
8/10/2019 pointers1.docx
73/114
Answer:a4. Which of the following is not possible in C?a) Array of function pointerb) Returning a function pointerc) Comparison of function pointerd) None of the mentionedView Answer
Answer:d
5. What is the output of this C code?
1. #include
2. void first ()
3. {
4. printf ("Hello World" );
5. }
6. void main ()
7. {
8. void *ptr () = first ;
9. ptr ++
10. ptr ();
11. }
a) Illegal application of ++ to void data type
b) pointer function initialized like a variablec) Both (a) and (b)d) None of the mentionedView Answer
Answer:c
6. What is the output of this C code?
1. #include
2. int mul (int a , int b , int c ) 3. {
4. return a * b * c ;
5. }
6. void main ()
7. {
8. int (*function_pointer )(int, int, int);
8/10/2019 pointers1.docx
74/114
9. function_pointer = mul ;
10. printf ("The product of three numbers is:%d" ,
11. function_pointer (2, 3, 4));
12. }
a) The product of three numbers is:24b) Run time errorc) Nothingd) VariesView Answer
Answer:a
7. What is the output of this C code?
1. #include
2. int mul (int a , int b , int c )
3. {
4. return a * b * c ;
5. }
6. void main ()
7. {
8. int (function_pointer )(int, int, int);
9. function_pointer = mul ;
10. printf ("The product of three numbers is:%d" ,
11. function_pointer (2, 3, 4));
12. }
a) The product of three numbers is:24b) Compile time errorc) Nothingd) VariesView Answer
Answer:b
8. What is the output of this C code?
1. #include
2. void f (int (*x )(int));
3. int myfoo (int);
4. int (*fooptr )(int);
5. int ((*foo (int)))( int);
8/10/2019 pointers1.docx
75/114
6. int main ()
7. {
8. fooptr = foo (0);
9. fooptr (10);
10. } 11. int ((*foo (int i )))( int)
12. {
13. return myfoo ;
14. }
15. int myfoo (int i )
16. {
17. printf ("%d \n ", i + 1);
18. }
a) 10b) 11c) Compile time errord) Undefined behaviourView Answer
Answer:b
1. What is the output of this C code?
1. #include
2. int mul (int a , int b , int c )
3. {
4. return a * b * c ;
5. }
6. void main ()
7. {
8. int *function_pointer ;
9. function_pointer = mul ;
10. printf ("The product of three numbers is:%d" , 11. function_pointer (2, 3, 4));
12. }
a) The product of three numbers is:24b) Compile time errorc) Nothing
8/10/2019 pointers1.docx
76/114
d) VariesView Answer
Answer:b
2. What is the output of this C code?
1. #include
2. int sub (int a , int b , int c )
3. {
4. return a - b - c ;
5. }
6. void main ()
7. {
8. int (*function_pointer )(int, int, int);
9. function_pointer = &sub ;
10. printf ("The difference of three numbers is:%d" ,
11. (*function_pointer )(2, 3, 4));
12. }
a) The difference of three numbers is:1b) Run time errorc) The difference of three numbers is:-5d) VariesView Answer
Answer:c3. One of the uses for function pointers in C isa) Nothingb) There are no function pointers in cc) To invoke a functiond) To call a function defined at run-time.View Answer
4. What is the output of this C code?
1. #include
2. void f (int);
3. void (*foo )() = f ;
4. int main (int argc , char *argv [])
5. {
6. foo (10);
8/10/2019 pointers1.docx
77/114
7. return 0;
8. }
9. void f (int i )
10. {
11. printf ("%d\n
", i ); 12. }
a) Compile time errorb) 10c) Undefined behaviourd) None of the mentionedView Answer
Answer:b
5. What is the output of this C code?
1. #include
2. void f (int);
3. void (*foo )(void ) = f ;
4. int main (int argc , char *argv [])
5. {
6. foo (10);
7. return 0;
8. }
9. void f (int i )
10. {
11. printf ("%d \n ", i );
12. }
a) Compile time errorb) 10c) Undefined behaviourd) None of the mentionedView Answer
Answer:a
6. What is the output of this C code?
1. #include
2. void f (int);
3. void (*foo )(float ) = f ;
8/10/2019 pointers1.docx
78/114
4. int main ()
5. {
6. foo (10);
7. }
8. void f (int i ) 9. {
10. printf ("%d \n ", i );
11. }
a) Compile time errorb) 10c) 10.000000d) Undefined behaviourView Answer
Answer:d
7. What is the output of this C code?
1. #include
2. void f (int (*x )(int));
3. int myfoo (int i );
4. int (*foo )(int) = myfoo ;
5. int main ()
6. {
7. f (foo (10));
8. }
9. void f (int (*i )(int))
10. {
11. i (11 );
12. }
13. int myfoo (int i )
14. {
15. printf ("%d \n ", i );
16. return i ; 17. }
a) Compile time errorb) Undefined behaviourc) 10 11d) 10 Segmentation faultView Answer
8/10/2019 pointers1.docx
79/114
Answer:d
8. What is the output of this C code?
1. #include 2. void f (int (*x )(int));
3. int myfoo (int);
4. int (*foo )() = myfoo ;
5. int main ()
6. {
7. f (foo );
8. }
9. void f (int(*i )(int ))
10. { 11. i (11 );
12. }
13. int myfoo (int i )
14. {
15. printf ("%d \n ", i );
16. return i ;
17. }
a) 10 11b) 11c) 10d) Undefined behaviourView Answer
Answer:b
Here is a listing of C question bank on Complicated Declarations along with answers, explanations and/or
solutions:
1. What is the output of this C code?
1. #include
2. void main ()
3. {
4. struct student
5. {
8/10/2019 pointers1.docx
80/114
8/10/2019 pointers1.docx
81/114
3. {
4. int no = 5;
5. char name [20];
6. };
7. void main () 8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. }
a) Nothingb) Compile time errorc) hellod) VariesView Answer
Answer:b
4. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no ;
5. char name [20];
6. };
7. void main ()
8. {
9. student s ;
10. s. name = "hello" ;
11. printf ("hello" );
12. }
a) Nothing
b) helloc) Compile time errord) VariesView Answer
Answer:c
5. What is the output of this C code?
8/10/2019 pointers1.docx
82/114
1. #include
2. void main ()
3. {
4. struct student
5. { 6. int no ;
7. char name [20];
8. };
9. struct student s ;
10. s. no = 8;
11. printf ("%s" , s. name );
12. }
a) Nothingb) Compile time errorc) Junkd) 8View Answer
Answer:c
6. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no ;
5. char name [20];
6. };
7. struct student s ;
8. void main ()
9. {
10. s. no = 8;
11. printf ("%s" , s. name );
12. } a) Nothingb) Compile time errorc) Junkd) 8View Answer
Answer:a
8/10/2019 pointers1.docx
83/114
7. What is the output of this C code?
1. #include
2. int main ()
3. { 4. int *((*x )())[ 2];
5. x ();
6. printf ("after x \n ");
7. }
8. int *((*x )())[ 2]
9. {
10. int **str ;
11. str = (int*)malloc (sizeof (int)* 2);
12. return str ;
13. }
a) Compile time errorb) Undefined behaviourc) After xd) None of the mentionedView Answer
Answer:a
8. What is the output of this C code?
1. #include
2. void (*(f )())( int, float );
3. void (*(*x )())( int, float ) = f ;
4. void ((*y )(int, float ));
5. void foo (int i , float f );
6. int main ()
7. {
8. y = x ();
9. y (1, 2); 10. }
11. void (*(f )())( int, float )
12. {
13. return foo ;
14. }
15. void foo (int i , float f )
8/10/2019 pointers1.docx
84/114
8/10/2019 pointers1.docx
85/114
8/10/2019 pointers1.docx
86/114
8/10/2019 pointers1.docx
87/114
1. #include
2. struct student
3. {
4. int no ;
5. char name [20]; 6. }
7. void main ()
8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. }
a) Compile time errorb) Nothingc) hellod) VariesView Answer
Answer:a
7. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no = 5;
5. char name [20];
6. };
7. void main ()
8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. } a) Nothingb) Compile time errorc) hellod) VariesView Answer
Answer:b
8/10/2019 pointers1.docx
88/114
8. What is the output of this C code?
1. #include
2. struct student
3. { 4. int no ;
5. char name [20];
6. };
7. void main ()
8. {
9. student s ;
10. s. no = 8;
11. printf ("hello" );
12. }
a) Nothingb) helloc) Compile time errord) VariesView Answer
Answer:c
9. What is the output of this C code?
1. #include
2. void main ()
3. {
4. struct student
5. {
6. int no ;
7. char name [20];
8. };
9. struct student s ;
10. s. no = 8; 11. printf ("%d" , s. no);
12. }
a) Nothingb) Compile time errorc) Junk
8/10/2019 pointers1.docx
89/114
d) 8View Answer
Answer:d10. Is the below declaration legal?
int* ((*x)())[2];a) trueb) falsec) Undefined behaviourd) Depends on the standardView Answer
Answer:b
Here is a listing of C question bank on Complicated Declarations along with answers, explanations and/or
solutions:
1. What is the output of this C code?
1. #include
2. void main ()
3. {
4. struct student
5. {
6. int no ; 7. char name [20];
8. };
9. struct student s ;
10. no = 8;
11. printf ("%d" , no );
12. }
a) Nothingb) Compile time errorc) Junkd) 8View Answer
Answer:b
2. What is the output of this C code?
8/10/2019 pointers1.docx
90/114
1. #include
2. struct student
3. {
4. int no ;
5. char name [20]; 6. };
7. void main ()
8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. }
a) Run time errorb) Nothingc) hellod) VariesView Answer
Answer:c
3. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no = 5;
5. char name [20];
6. };
7. void main ()
8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. } a) Nothingb) Compile time errorc) hellod) VariesView Answer
Answer:b
8/10/2019 pointers1.docx
91/114
4. What is the output of this C code?
1. #include
2. struct student
3. { 4. int no ;
5. char name [20];
6. };
7. void main ()
8. {
9. student s ;
10. s. name = "hello" ;
11. printf ("hello" );
12. }
a) Nothingb) helloc) Compile time errord) VariesView Answer
Answer:c
5. What is the output of this C code?
1. #include
2. void main ()
3. {
4. struct student
5. {
6. int no ;
7. char name [20];
8. };
9. struct student s ;
10. s. no = 8; 11. printf ("%s" , s. name );
12. }
a) Nothingb) Compile time errorc) Junk
8/10/2019 pointers1.docx
92/114
d) 8View Answer
Answer:c
6. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no ;
5. char name [20];
6. };
7. struct student s ;
8. void main ()
9. {
10. s. no = 8;
11. printf ("%s" , s. name );
12. }
a) Nothingb) Compile time errorc) Junkd) 8View Answer
Answer:a
7. What is the output of this C code?
1. #include
2. int main ()
3. {
4. int *((*x )())[ 2];
5. x ();
6. printf ("after x \n "); 7. }
8. int *((*x )())[ 2]
9. {
10. int **str ;
11. str = (int*)malloc (sizeof (int)* 2);
12. return str ;
8/10/2019 pointers1.docx
93/114
13. }
a) Compile time errorb) Undefined behaviourc) After xd) None of the mentionedView Answer
Answer:a
8. What is the output of this C code?
1. #include
2. void (*(f )())( int, float );
3. void (*(*x )())( int, float ) = f ;
4. void ((*y )(int, float ));
5. void foo (int i , float f );
6. int main ()
7. {
8. y = x ();
9. y (1, 2);
10. }
11. void (*(f )())( int, float )
12. {
13. return foo ;
14. }
15. void foo (int i , float f )
16. {
17. printf ("%d %f \n ", i , f );
18. }
a) 1 2.000000b) 1 2c) Compile time errord) Segmentation fault/code crash
View Answer Answer:a9. What does this declaration say?
int (*(*y)())[2];a) y is pointer to the function which returns pointer to integer arrayb) y is pointer to the function which returns array of pointersc) y is function which returns function pointer which in turn returns pointer to integer array
8/10/2019 pointers1.docx
94/114
d) y is function which returns array of integersView Answer
Answer:a
10. What is the output of this C code?
1. #include
2. void (*(f )())( int, float );
3. typedef void (*(*x )())( int, float );
4. void foo (int i , float f );
5. int main ()
6. {
7. x = f ;
8. x ();
9. }
10. void (*(f )())( int, float )
11. {
12. return foo ;
13. }
14. void foo (int i , float f )
15. {
16. printf ("%d %f \n ", i , f );
17. }
a) Compile time errorb) Undefined behaviourc) 1 2.000000d) NothingView Answer
Answer:a
11. What is the output of this C code?
1. #include 2. void (*(f )())( int, float );
3. typedef void (*(*x )())( int, float );
4. void foo (int i , float f );
5. int main ()
6. {
7. x p = f ;
8/10/2019 pointers1.docx
95/114
8. p ();
9. }
10. void (*(f )())( int, float )
11. {
12. return foo ; 13. }
14. void foo (int i , float f )
15. {
16. printf ("%d %f \n ", i , f );
17. }
a) Compile time errorb) Undefined behaviourc) 1 2.000000d) NothingView Answer
Answer:d
Here is a listing of C multiple choice questions on Complicated Declarations along with answers,
explanations and/or solutions:
1. Read the following expression?void (*ptr)(int);
a) ptr is pointer to int that converts its type to voidb) ptr is pointer to function passing int returning voidc) ptr is pointer to void that converts its type to intd) ptr is pointer to function passing void returning intView Answer
Answer:b2. Which of the following expression is true for the following?
ptr is array with 3 elements of pointer to function returning pointer of inta) int **ptr[3]();b) int *(*ptr[3])();c) int (*(*ptr[3])());d) None of the mentionedView Answer
Answer:b3. What do the following declaration denote?
int **ptr;a) ptr is a function pointer that returns pointer to int type
8/10/2019 pointers1.docx
96/114
b) ptr is a pointer to an int pointerc) ptr is a pointer to pointer to type intd) None of the mentionedView Answer
Answer:b4. What do the following declaration denote?
char *str[5];a) str is an array of 5 element pointer to type charb) str is a pointer to an array of 5 elementsc) str is a function pointer of 5 elements returning chard) None of the mentionedView Answer
Answer:a5. Comment on the following declaration?
int (*ptr)(); // i)char *ptr[]; // ii)
a) Both i) and ii) and cannot exist due to same nameb) i) is legal, ii) is illegalc) i) is illegal, ii) is legald) Both i) and ii) will work legal and flawlesslyView Answer
Answer:d
6. What is the output of this C code?
1. #include
2. struct student
3. {
4. int no ;
5. char name [20];
6. }
7. void main ()
8. {
9. struct student s ;
10. s. no = 8;
11. printf ("hello" );
12. }
a) Compile time errorb) Nothingc) hello
8/10/2019 pointers1.docx
97/114
8/10/2019 pointers1.docx
98/114
a) Nothingb) helloc) Compile time errord) VariesView Answer
Answer:c
9. What is the output of this C code?
1. #include
2. void main ()
3. {
4. struct student
5. {
6. int no ;
7. char name [20];
8. };
9. struct student s ;
10. s. no = 8;
11. printf ("%d" , s. no);
12. }
a) Nothingb) Compile time error
c) Junkd) 8View Answer
Answer:d10. Is the below declaration legal?
int* ((*x)())[2];a) trueb) falsec) Undefined behaviourd) Depends on the standardView Answer
Answer:b
[Q001] What will be the output of the following program :
struct {
int i;
8/10/2019 pointers1.docx
99/114
float f;
}var;
int main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f",var.i,var.f);
return(0);
}
(a)Compile-Time Error (b)5 9.76723 (c)5 9.76 (d)5 9.77
[Q002] What will be the output of the following program :int main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",val,*ptr++);
return(0);
}
(a)Compile-Time Error (b) 1235 1235
[Q003] What will be the output of the following program :
struct values {
int i;
float f;
};
int main()
{
struct values var={555,67.05501};
printf("%2d %.2f",var.i,var.f);
return(0);
}
8/10/2019 pointers1.docx
100/114
8/10/2019 pointers1.docx
101/114
[Q006] What will be the output of the following program :
struct first {
int a;
float b;
}s1={32760,12345.12345};
typedef struct {
char a;
int b;
}second;
struct my_struct {
float a;
unsigned int b;
};
typedef struct my_struct third;
int main()
{
static second s2={'A',- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf("%d %.2f\n%c %d\n%.2f %u",(s1.a)--,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--
s3.b);
return(0);
}(a)Compile-Time Error (b)32760 12345.12
A 4
1 -5
(c)32760 12345.13
a -5
0.00 65531
(d)32760 12345.13
a 5
0.00 65530
[Q007] What will be the output of the following program :
struct {
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
8/10/2019 pointers1.docx
102/114
int main()
{
printf("%d %d %d\n",var.i,vptr->i,(*vptr).i);
printf("%d %d %d %d %d %d",var.val[4],*(var.val+4),vptr->val[4],*(vptr-
>val+4),(*vptr).val[4],*((*vptr).val+4));
return(0);
}
(a)Compile-Time Error (b)1 1 1
6 6 6 6 6 6
(c)1 1 1
5 5 5 5 5 5
(d)None of these
[Q008] What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;ptr->f=y;
}
int main()
{
temp a={111,777.007};
printf("%d %.2f\n",a.i,a.f);
alter(&a,222,666.006);
printf("%d %.2f",a.i,a.f);
return(0);
}
(a)Compile-Time error
(b)111 777.007
222 666.006
(c)111 777.01
222 666.01
8/10/2019 pointers1.docx
103/114
(d)None of these
[Q009] What will be the output of the following program :
typedef struct {
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;}
int main()
{
temp a={111,777.007};
printf("%d %.3f\n",a.i,a.f);
a=alter(a,222,666.006);
printf("%d %.3f",a.i,a.f);
return(0);
}(a)Compile-Time error
(b)111 777.007
222 666.006
(c)111 777.01
222 666.01
(d)None of these
[Q010] What will be the output of the following program :typedef struct {
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
8/10/2019 pointers1.docx
104/114
temp tmp=*ptr;
printf("%d %.2f\n",tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
int main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf("%d %.2f",a.i,a.f);
return(0);
}
(a)Compile-Time error
(b)65535 777.777
-1 666.666
(c)65535 777.78
-1 666.67
(d)-1 777.78
-1 666.67
[Q011] What will be the output of the following program :
struct my_struct1 {
int arr[2][2];
};
typedef struct my_struct1 record;
struct my_struct2 {
record temp;
}list[2]={1,2,3,4,5,6,7,8};
int main()
{
int i,j,k;for (i=1; i>=0; i--)
for (j=0; j=0; k--)
printf("%d",list[i].temp.arr[j][k]);
return(0);
}
8/10/2019 pointers1.docx
105/114
(a)Compile-Time Error
(b)Run-Time Error
(c)65872143
(d)56781243
[Q012] What will be the output of the following program :
struct my_struct {
int i;
unsigned int j;
};
int main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf("%d %u",temp2.i,temp2.j);
return(0);
}
(a)32767 -1
(b)-32769 -1
(c)-32769 65535
(d)32767 65535
[Q013] What will be the output of the following program :
struct names {
char str[25];
struct names *next;
};
typedef struct names slist;
int main()
{
slist *list,*temp;
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,"Hai");
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,"Friends");
8/10/2019 pointers1.docx
106/114
temp->next=list;
list=temp;
while (temp != NULL)
{
printf("%s",temp->str);
temp=temp->next;
}
return(0);
}
(a)Compile-Time Error
(b)HaiFriends
(c)FriendsHai
(d)None of these
[Q014] What will be the output of the following program :
(i) struct A {
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B {
int b;
struct B *next;
};
struct A {
int a;
struct B tempB;
struct A *next;};
(iii) struct B {
int b;
}tempB;
struct {
8/10/2019 pointers1.docx
107/114
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
(a) (iv) Only
(b) (iii) Only
(c)All of the these
(d)None of these
[Q015] What will be the output of the following program :
union A {
char ch;
int i;
float f;
}tempA;
int main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
return(0);
}
(a)Compile-Time Error(b)12345
(c)Erroneous output
(d)777
8/10/2019 pointers1.docx
108/114
8/10/2019 pointers1.docx
109/114
Size=4 bytes
(d)None of these
[Q018] What are the largest values that can be assigned to each of the bit fields defined in
[Q017] above.
(a)a=0 b=2 c=3 d=4
(b)a=1 b=2 c=7 d=15
(c)a=1 b=3 c=7 d=15
(d)None of these
[Q019] What will be the output of the following program :
int main()
{
struct sample {
unsigned a:1;
unsigned b:4;
}v={0,15};
unsigned *vptr=&v.b;
printf("%d %d",v.b,*vptr);
return(0);
}
(a)Compile-Time Error
(b)0 0
(c)15 15
(d)None of these
[Q020] What will be the output of the following program :
int main()
{
static struct my_struct {
unsigned a:1;
int i;
unsigned b:4;
8/10/2019 pointers1.docx
110/114
unsigned c:10;
}v={1,10000,15,555};
printf("%d %d %d %d",v.i,v.a,v.b,v.c);
printf("\nSize=%d bytes",sizeof v);
return(0);
}
(a)Compile-Time Error
(b)1 10000 15 555
Size=4 bytes
(c)10000 1 15 555
Size=4 bytes
(d)10000 1 15 555
Size=5 bytes
[Q001]
Ans. (d) Though both and are optional, one of
the two must appear. In the above program, i.e. var is used. (2
decimal places or) 2-digit precision of 9.76723 is 9.77
[Q002]
Ans. (d) Both and are optional. Thus the
structure defined in the above program has no use and program executes in the normal
way.
[Q003]
Ans. (c) The members of a structure variable can be assigned initial values in much the
same manner as the elements of an array. The initial values must appear in order in which
they will be assigned to their corresponding strucutre members, enclosed in braces and
separated by commas.
8/10/2019 pointers1.docx
111/114
[Q004]
Ans. (c) In the above program, values is the user-defined structure type or the new user-
defined data type. Structure variables can then be defined in terms of the n ew data type.
[Q005]
Ans. (a) C language does not permit the initialization of individual structure members
within the template. The initialization must be done only in the declaration of the actual
variables. The correct way to initialize the values is shown in [Q003] or [Q004].
[Q006]
Ans. (d) Illustrating 3 different ways of declaring the structres : first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also an expression of
the form ++variable.member is equivalent to ++(variable.member), i.e. ++ operator will
apply to the structure member, not the entire structure variable.
[Q007]
Ans. (b) Since value of the member 'i' can be accessed using var.i, vptr->i and
(*vptr).i Similarly 5th value of the member 'val' can be accessed using var.val[4],
*(var.val+4), vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
[Q008]
Ans. (c) This program illustrates the transfer of a structure to a function by passing the
structure's address (a pointer) to the function.
8/10/2019 pointers1.docx
112/114
8/10/2019 pointers1.docx
113/114
components. Rather, the data structure can expand or contract in size as required.]
[Q014]
Ans. (d) Since all the above structure declarations are valid in C.
[Q015]
Ans. (c) The above program produces erroneous output (which is machine dependent). In
effect, a union creates a storage location that can be used by any one of its members at a
time. When a different member is assigned a new value, the new value supercedes the
previous member's value. [NOTE : The compiler allocates a piece of storage that is large
enough to hold the largest variable type in the union i.e. all members share the same
address.]
[Q016]
Ans. (b) Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union is int (2
bytes) is equal to (8 bytes). Also the total number of bytes the array 'temp2' requires :
(8 bytes) * (5 bytes) = (40 bytes).
[Q017]
Ans. (b) The four fields within 'v' require a total of 10 bits and these bits can be
accomodated within the first word(16 bits). Unnamed fields can be used to control the
alignment of bit fields within a word of memory. Such f ields provide padding within the
word. [NOTE : Some compilers order bit-fields from righ-to-left (i.e. from lower-order bits
to high- order bits) within a word, whereas other compilers order the fields from left-to-
8/10/2019 pointers1.docx
114/114