Friday, 11 May 2012

Pointers


1. An element of structure can be a Pointer; Which Points to a structure of the same type is called ___.
referential pointer
structure pointer
pointer structure
Self-Referential Structure
Ans. : d
2.Consider the following statements:
    int *p;
    int i;
    int k;
    i = 42;
    k = i;
    p = &i;
After these statements, which of the following statements will change the value of i to75?
K = 75
*k = 75;
p = 75;
*p = 75;
Ans. : d, Indirection operator will yield the value of the variable i.
3.Consider the following statements:
   int i = 42;
   int j = 80;
   int *p1;
   int *p2;
   p1 = &i;
   p2 = &j;
   *p1 = *p2;
   printf(“%d%d",i,j);
  What numbers are printed by the output statement?
a.       42 and then another 42
b.      42 and then 80
c.       80 and then 42
d.      80 and then another 80
Ans. : d, since the last statement will change the values of i, as the *p2 which is actually j value will be copied to *p1 i.e. i.

4. .    void main()
            {
                        int *p1,*p2;
                        p1=(int *)malloc(sizeof(int));
                        p2=(int *)malloc(sizeof(int));
                        *p1=17;
                        *p2=153;
1st O/p             printf("\n%d.......%d..",*p1,*p2);
                        free( p1);
                        p1=p2;
2nd O/p             printf("\n%d.......%d..",*p1,*p2);
                        *p1=253;
3rd O/p             printf("\n%d.......%d..",*p1,*p2);
                         *p2=355;
4th O/p             printf("\n%d.......%d..",*p1,*p2);
            } 
What will be the O/p at O/p 4.
a.       17….153
b.      253….253
c.       253….353
d.      353….353
Ans. : d, Since both p1 and p2 point to the same location, so when indirection of p2 is modified, the actual value gets changed, and both the pointers indirection will yield the same value.
5.What is printed by these statements?
    int i = 1;
    int k = 2;
    int *p1;
    int *p2;
    p1 = &i;
    p2 = &k;
    p1 = p2;
    *p1 = 3;
    *p2 = 4;
    printf(“%d”,i);
a.       1
b.      2
c.       3
d.      4
Ans. : a, Since the line p1 = p2, makes both of the pointers to point at k, so the next two lines changes the value of k.
6.What is printed by these statements?
     int i = 1;
     int k = 2;
     int* p1;
     int* p2;
     p1 = &i;
     p2 = &k;
     p1 = p2;
     *p1 = 3;
     *p2 = 4;
     printf(“%d”,k);
a.       1
b.      2
c.       3
d.      4
Ans. : d,  Since the line p1 = p2, makes both of the pointers to point at k, so the next two lines changes the value of k and the last changed value is 4.
7.What is printed by these statements?
     int i = 1;
     int k = 2;
     int* p1;
     int* p2;
     p1 = &i;
     p2 = &k;
     *p1 = *p2;
     *p1 = 3;
     *p2 = 4;
     printf(“%d”,i);
a.       1
b.      2
c.       3
d.      4
Ans. : c, Last but one line changes the value stored in i to  3.
8.In which location do dynamic variables reside?
a.       The code segment.
b.      The data segment.
c.       The heap.
d.      The run-time stack.
Ans. : c, All the dynamic memory allocation is done from the heap of the program.
9.Suppose you have the following function prototype and variable declaration:
    void goop(int z[ ]);
    int x[10];
Which is the correct way to call the goop function with x as the argument:
a.       goop(x);
b.      goop(x[ ]);
c.       goop(x[10]);
d.      goop(&x);
e.       goop(&x[ ]);
Ans. : a,
10.  What's wrong with this code?
               char *p;
               *p = malloc(10);
               
a.       No Error
b.      Logical Error
c.       Syntax Error
d.      All of the above

Ans.: c, Memory is allocated to the pointer and not to indirection of the pointer.

11. Is there a problem with this code…if yes then what
          typedef struct {
                              char *item;
                                       NODEPTR next;
                               } *NODEPTR;
a.       No Problem
b.      The struct ptr cannot be defined inside the same struct
c.       Problem with typedef
d.      The struct has to be defined first
Ans. : c, the problem is with typedef,
                                              struct node{
                                                             char *item;
                                                             struct node *next;
                                                         } *NODEPTR; would be correct


12.  Does *p++ increment p, or what it points to
a.       equivalent to *(p++)
b.      equivalent to (*p)++
c.       Gives Error
d.      Equivalent to *(++p)
Ans. : a

13.Suppose that the goop function from the previous question changes the value of z[1]. Does this change effect the value of the actual argument?

Yes
No
Ans. : Yes, it will change.
14.Here is a function declaration:
 void goo(int* x)
    {
        *x = 1;
    }
Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you print *a after the function call goo(a)?
a.       0
b.      1
c.       address of a
d.      address of x
e.       None of the above
Ans. : b, The value is changed to 1 in the function by the indirection operator.

15.A variable b if represents its value, what does &b represent…

a.       Another way to access value of b
b.      Address of b
c.       Pointer to b
d.      Pointer on b

Ans.  :  b, The operator ‘&’ as such says at the address of, it is not a separate variable to be a pointer.

16. What is the problem if any…
         int array[5], i, *ip;
               for(i = 0; i < 5; i++) array[i] = i;
               ip = array;
               printf("%d\n", *(ip + 3 * sizeof(int)));

a.       No Error
b.      Prints 3
c.       Prints Garbage(Logical Error)
d.      Syntax Error
Ans.  : c, When ip+3 is calculated, since it is an int pointer it automatically traverses to the 3rd element in the array, size of is not required.

17.     What will go wrong if any,
            ……….<Executable stmts>
void *p1, *p2;
*p1++ …
*p2--…..
……….<Executable stmts>


a.       No error
b.      Pointers Cannot have Additions and subtractions
c.       Void Pointers cannot undergo any arithmetic operation
d.      Syntax Logic is correct But syntax is a problem
Ans. : c, Void pointers cannot undergo ANY because the size of the pointing object is unknown.
18.  In the statement
            Char * p1, p2;
a.       Both are pointers
b.      Only p1 is a char pointer but p2 is not
c.       Neither p1 nor p2 are pointers
d.      Syntax Error
Ans. : b.

19.  What is NULL…
a.       Practically = 0
b.      A Macro defined in stdio.h
c.       Some Garbage Value
d.      A pointer Pointing to Nothing
Ans. : b.


Fig  Pointers1.1
                                                            

  int     *i, *j, k;
k = 4;
j = &k;
i = &j;



20.   printf(“%d”,j);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : c


21.   printf(“%d”,*j);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : a
22 .   printf(“%d”,*i);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : c
23.   printf(“%d”,**i);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : a
24.   printf(“%d”,&k);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : c
25.  printf(“%d”,&j);
a.       4
b.      2006
c.       2008
d.      Error
Ans. : b

26.  printf(“%d”,&i);
a.       2004
b.      2006
c.       2008
d.      Error
Ans. : a
27. The pointer that may point to a structure of same type  as the structure is called
as______
Referential pointer
Self-Referential Pointer
Structure pointer
Pointer structure
Ans)B
28.   Assume Memory location = 100 (decimal). What happens when we write:
ptr + 1;(ptr is pointing to an integer variable)
a.       ptr points 101
b.      ptr point to 102
c.       ptr point to 104
d.      Gives an error
Ans. : b, when a pointer variable is incremented it’s incremented by the size of the data type it is pointing to, in this case int.
29.    What will *++p yield:
a.       1
b.      23
c.       Garbage
d.      Error
Ans. : b, Since it is ++p, it will be de-referencing to the second place directly.
30.     What will *p+1 yield:
a.       1
b.      23
c.       2
d.      Error
Ans. : c, Since it is p+1, first p will be de- referenced then the value got will be incremented, so 2.
31.     What will *(p+1) yield:
a.       1
b.      23
c.       Garbage
d.      Error
Ans. : b, The pointer value will be incremented first then it will be de – referenced so to the second location yields 23.

32. What’s wrong if any :
            struct abc{
                                    int a =40;
                                    struct abc *n;
                        };
a.       Nothings Wrong
b.      Error because of initialization
c.       Error because of the pointer
d.      Syntax Error
Ans. :  b, initialization is not allowed inside a structure
33.      What is true for calloc:
a.       calloc initializes memory after allocating it.
b.      calloc does not initialize after allocating it
c.       calloc is not used to allocate memory
d.      calloc takes in only one argument.
Ans. : a, One of the difference between calloc and malloc is that calloc initializes the allocated memory.
34.     What is False for calloc:
a.       Calloc takes in two arguments
b.      Calloc returns a pointer to the reserved memory
c.       Calloc is a function defined in alloc.c
d.      Calloc always initializes to 0.
Ans. : d, The second argument in calloc is the one which determines what the allocated space is supposed to be initialized to.
35.    What is true for malloc:
a.       Malloc initializes memory after allocating it.
b.      Malloc does not initialize after allocating it
c.       Malloc is not used to allocate memory
d.      Malloc takes in two arguments.
Ans. : b, One of the difference between calloc and malloc is that malloc does not initialize the allocated memory.
36.    What is False for Malloc:
a.       Malloc takes in one argument.
b.      Malloc returns a pointer to the reserved memory
c.       Malloc is a function defined in alloc.c
d.      Malloc always initializes memory to some value specified in the call.
Ans. : d, Malloc never initializes the memory in the first place.
37.    The command to free the memory reserved during the running of the program is:
a.       realloc()
b.      calloc()
c.       malloc()
d.      free()
Ans. : d, free actually frees the memory or returns the memory back to the heap.
38.    The ‘&’ is known as __________.
a.       Referencing Operator
b.      Dereferencing Operator
c.       Memory Allocation Operator
d.      Memory freeing operator
Ans. : a, Referencing Operator or at the address of operator
39.     The ‘*’ is known as ___________.
a.       Referencing Operator
b.      Dereferencing Operator
c.       Memory Allocation Operator
d.      Memory freeing operator
Ans. : b, Dereferencing Operator or value at operator
40.   Consider the code
               int A;
               int * ptr = &A;  
               int** ptp = &ptr;
ptp will yield: 
a.       The same value as in ptr
b.      Value of A
c.       The address of ptr
d.      Will give an error, a pointer cannot be pointed
Ans. : c, &ptr will return the address of variable ptr.
41.    int a = 77;
         int *d,*f,*i,*g,*t;
         d = &a;
          f = &a;         
          i = &a;
          g = &a;
          t =  &a;
a.       Error not more than one pointer can point to a single location.
b.      d,f,i,g,t will have the same address of 77
c.       d,f,i,g,t will have the value 77
d.      Error due to assignment operator
Ans. : b,  It is absolutely fine with as many number of links to the same address.
42.   int a = 77;
        int *g,*u;
        g = &a;
        u = g;
a.       Error due to copying to two pointers
b.      g will have the address of a and u will have the value of a
c.       g and u will have the same address
d.      u will have the address of g
Ans. : b, pointers can be copied the contents that is the address will be copied
43.   int a = 60, b = 70;
        int *i,*j,*k;
        *k = i * j / 10;
a.       K will have the value 4200
b.      Error, multiplication is not possible on pointers
c.       Syntax error
d.      K will have the product of the addresses held in i and j
Ans. : b, pointers can only undergo addition and subtraction not multiplication and division.
44.   void *i;
        int k = 55;
        i = &k;
a.       Creates an error
b.      No problem with this
c.       Logical error
d.      It is ok, but pointer arithmetic cannot be performed                
Ans. : a, void pointer is a generic pointer, it cannot be directly pointed to any variable, since in a void pointer the size of the datatype it will be pointing to is unknown.






45.   void *i;
        int k = 55;
        (int *) i =  &k;
a.       Creates an error
b.      No problem with this
c.       It will create a logical error since void pointer.
d.      The address will be stored but the pointer will anyways be of one byte.
Ans. : b, No problem with this because although a generic pointer but is type casted.
46.    int func(int a, float b);
               This is a prototype of which argument passing type,
a.       Pass by Value
b.      Pass by Reference
c.       Call by Reference
d.      It generates an Error.
Ans. : a, This is an example of pass by value
47.   int func(int *a, float *b);
               This is a prototype of which argument passing type,
a.       Pass by Value
b.      Pass by Reference
c.       Call by Reference
d.      It generates an Error.
Ans. : b, This is an example of pass by value
48. The term pointee in pointer assignment is used to indicate    __________.
a.       The pointed location
b.      The location of the pointer
c.       There is no term as pointee
d.      The term does not deal with pointers.
               Ans. : a, pointee is basically the location which the pointer is pointing to.
Refn. : http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
49.  What are two pointers pointing to the same location called.
a.       Multiple Pointers
b.      Sharing Pointers
c.       Sharing Pointees
d.      Multiple Pointees
Ans. : b, two pointers sharing the same pointee are known as sharing pointers.
Refn. : http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
50.   What is the solution called if two functions share the same copy of memory without
        actually copying the memory into their respective space allocated.(In ‘C’)
a.       Pass by reference
b.      Pass by value
c.       Deep Copying
d.      Shallow Copying
Ans. : d, Shallow copying is a technique in which more than one function refer to the same location by using a pointer each, both of which point to the same pointee. Refn. : <Same .pdf used in as previous one.>
51.   What is the technique known as in which the complete memory location is passed to 
         the functions, where each function has its own copy of the memory it wants to 
         access and is even free to edit, which was not available with shallow copying.
a. Pass by reference
b. Pass by value
c. Deep Copying
d.Shallow Copying
Ans. : c, Deep Copying, Refn. Is the same pdf as above.
52.   What is a bad pointer?
a.       A pointer which do not have any pointee, initially.
b.      It’s similar to dangling pointer
c.       No term as bad pointer exists
d.      A pointer pointing to a corrupt location
Ans. : a, Every pointer when initialized is actually a bad pointer, because it does not point to any location and bad pointers differ from dangling pointers by, the fact that dangling pointers first points to a location and then without dereferencing the location if deleted, the pointer becomes dangling.
53.   What is a dangling pointer?                                                        
a.       A pointer which do not have any pointee, initially.
b.      It’s similar to bad pointer
c.       No term as dangling pointer exists
d.      A pointer pointing to a location, which does not exist anymore.
Ans. : d, Explanation is same as of the bad pointer.
54.   Consider the code snippet,
               void eg( )
                           {
                              int *p;
                               *p = 45;
                               printf(“%d”,*p);
                            }
a.       Program has a syntax error.
b.      It generates an error on runtime.
c.       It works perfectly fine with 45 being printed on the screen.
d.      The pointer does not get dereference in the printf statement 
Ans. : b, it generates a runtime error, pointer cannot point to data as such, it can only point to locations.
55. What will be the o/p
void main()
{
          int *x,*y;
          x = (int *)malloc(sizeof(int));
          *x = 42;
          y = x;
          *y = 24;
          printf("%d",*x);
}
a.       Generates a syntax error
b.      Generates a runtime error
c.       Prints 42 on console
d.      Prints 24 on console
Ans. : d, prints 24 on console.
56.    Consider the code..
                                     void main()
{
          int *x,*y;
          x = (int *)malloc(sizeof(int));
          *x = 42;
          y = x;
          printf("%d",*y);
}
a.       Generates a syntax error
b.      Generates a runtime error
c.       Prints 42 on console
d.      Prints 24 on console
Ans. d,Prints 42 on console.
57.   Consider the code..
                  void main()
{
          int *x,*y;
          x = malloc(sizeof(int));
          *x = 42;
          y = x;
          *y = 24;
          printf("%d",*y);
}
a.       Generates a syntax error
b.      Generates a runtime error
c.       Prints 42 on console
d.      Prints 24 on console
Ans. : a, Generates a syntax error, malloc will return a void pointer, it has to be typecasted.

58.   Consider the code…
void main()
{
                                    int *x,*y;
                                    x = (int *)malloc(sizeof(int));
                        *x = 42;
                                    y = x;
                        *y = 24;
                        printf("%d,%d”,x,y);
                        }
a.       42, 24
b.      24 , 24
c.       409863, 409863
d.      409863, 409865
Ans. : c, both will print the same address on the console, that in this case is 409863.
59.    What is wrong with the code if any .
            int *  tab()
                        {
                        int temp;
                        temp = 24;
                        return (&temp);
                        }
            void victim()
                        {
                        int *ptr;
                        ptr = tab();
                        printf(“%d”,*ptr);
                        } 
a.       Generates a syntax Error
b.      Generates a Runtime Error
c.       Prints 24 on the console.
d.      Prints Garbage.
Ans. : b, Generates a runtime Error, since temp is only a local pointee.
60.   What happens when a memory is allocated in a pointer and then without de-allocating the function exits…
a.       Nothing Happens
b.      A runtime Error occurs
c.       Memory Leak
d.      Automatic de allocation happens
Ans. : c, memory leak will occur, i.e. the function will exit for that time but the memory will not be allocated for any further requirements.
61.  Memory to the pointer is allocated from
a.       Program Heap allocated to the program
b.      Borrowed from secondary Memory
c.       Borrowed from primary Memory
d.      No memory is allocated, since it does not store any value
Ans. : a, memory newly allocated is always allocated from the program heap.
Consider the piece of code :
void main()
            {
                        int *p1,*p2;
                        p1=(int *)malloc(sizeof(int));
                        p2=(int *)malloc(sizeof(int));
                        *p1=17;
                        *p2=153;
1st O/p             printf("\n%d.......%d..",*p1,*p2);
                        free( p1);
                        p1=p2;
2nd O/p             printf("\n%d.......%d..",*p1,*p2);
                        *p1=253;
3rd O/p             printf("\n%d.......%d..",*p1,*p2);
                         *p2=355;
4th O/p             printf("\n%d.......%d..",*p1,*p2);
            } 
62.    What will be the output at O/p 1.
a.       17 ……153
b.      17…….<Garbage>
c.       <Garbage>….153
d.      <Garbage>…….<Garbage>
Ans. : a, It at no time on the 1st output give garbage value, both the pointers have been assigned proper values.
63.        What will be the O/p at O/p 2.
a.       17……153
b.      153……153
c.       Results into a runtime error, because p1 has been freed
d.      17…….17
Ans. : b, there wont be any error because the pointer has been freed but has not been eliminated from the program, so on p1, p2’s address is copied, resulting in p1 and p2 both pointing to the same location.


64.        What will be the O/p at O/p 3.
a.       17…..153
b.      153…..153
c.       253…..253
d.      253…..153
Ans. : c, Since both p1 and p2 point to the same location, so when indirection of p1 is modified, the actual value gets changed, and both the pointers indirection will yield the same value.
65. Assume Memory location = 2022 (decimal). What happens when we write:
ptr + 1;(ptr is pointing to an double variable)
a.       ptr points 2023
b.      ptr point to 2024
c.       ptr point to 2030
d.      ptr points to some garbage location
Ans. : c, when a pointer variable is incremented it’s incremented by the size of the data type it is pointing to, in this case double, so 8.


66.  Consider this code
int a[] = {1,23,17,4,-5,100};
int *p = a;
   What will *p++ yield :
a.       1
b.      23
c.       Garbage
d.      Error
Ans. : a, first de-referencing will take place then increments  will happen. Since it is p++, the value of the pointer will be incremented in the next stmt.
  









0 comments:

Post a Comment