C Programming Questions and Answers on Pointers and Addresses for Freshers

1. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *p = NULL;
  5.         char *q = 0;
  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 compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
Answer: a
2. What is 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 error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
Answer: a
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);
  7.         return 0;
  8.     }
a) Compile time error
b) Undefined behaviour
c) 10
d) 0.000000
Answer: d
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 returned
d) Undefined behaviour
Answer: a
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;
  11.         return &j;
  12.     }
a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
Answer: a
6. Comment on the following pointer declaration?
    int *ptr, p;

a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
Answer: a
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,10
b) 10,11
c) 11,10
d) 11,11
Answer: d
8. Comment on the following?
    const int *ptr;

a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) You May or maynot change the value pointed by ptr
d) You can change the pointer as well as the value pointed by it
Answer: a
9. Which is an indirection operator among the following?

a) &
b) *
c) ->
d) .
Answer: b
10. Which of the following does not initialize ptr to null (assuming variable declaration of a as int     a=0;?

a) int *ptr = &a;
b) int *ptr = &a – &a;
c) int *ptr = a – a;
d) All of the mentioned
Answer: a
11. 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 address
b) Different address
c) Compile time error
d) Varies
Answer: a
12. 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) Compile time error
c) 0xbfd605e8 0xbfd605ec
d) 0xbfd605e8 0xbfd605e8
Answer: b
13. 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);
  7.         ptr++;
  8.         printf("%p\n ", ptr);
  9.     }
a) 0xbfd605e8 0xbfd605ec
b) 0xbfd605e8 0cbfd60520
c) 0xbfd605e8 0xbfd605e9
d) Run time error
Answer: a
14. 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) Compile time error
Answer: d
15. 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
Answer: c

Related

C# Questions & Answers on Multithreaded Programming – 1 for Freshers

1. Select the type of multitasking methods that exist: a) process based b) thread based c) only process d) both process & thread based Answer: d Explanation: There are two distinct types of...

Linux Questions & Answers on sed Editor for Freshers

1. What is sed? a) a non-interactive stream editorb) an IDEc) a hex editord) none of the mentioned Answer: a 2. Sed maintains the hold space (a buffer) to a) copy the each line of inputb) save t...

C# Questions & Answers on Multithreaded Programming – 2 for Freshers

1. Which of these keywords are used to implement synchronization? a) synchronizeb) sync) synchd) synchronized Answer: d 2. Which keyword is used for using the synchronization features defined by ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item