C# Questions & Answers – Pointers Operation – 1

This section of our 1000+ C# MCQs focuses on operation on pointers in C# Programming Language.

1. What will be the output of the following C# code snippet?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          int m = 10;
  6.          int *mptr = &m;
  7.          int **ptr = &mptr;
  8.          int n = 20;
  9.          int *nptr = &n;
  10.          int **prt = &nptr;
  11.          m = **prt + *nptr;
  12.          n = *mptr* **prt;
  13.          Console.WriteLine(n + " " + m);
  14.          Console.ReadLine();
  15.      }
  16.  }

a) 20 200
b) 40 200
c) 800 40
d) 40 800
View Answer

Answer: c
Explanation: None.
Output: 800 40
advertisement

2. What will be the output of the following C# code snippet?

  1.  unsafe static void Main()
  2.  {
  3.      int a = 5;
  4.      int b = 5;
  5.      int c = 5;
  6.      int*[] ptr = new int* [3];
  7.      ptr[0] = &a;
  8.      ptr[1] = &b;
  9.      ptr[2] = &c;
  10.      for (a = 0; a < 3; a++)
  11.      {
  12.          c += *ptr[a];
  13.          Console.WriteLine(c);
  14.      }
  15.      Console.ReadLine();
  16.  }

a) 5 10
b) 10 20
c) Compile time error
d) 5 10 20
View Answer

Answer: d
Explanation: None.
Output:5 10 20
Free 30-Day C++ Certification Bootcamp is Live. Join Now!

3. What will be the output of the following C# code snippet?

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int* ptrs = stackalloc int[3];
  6.         ptrs[0] = 1;
  7.         ptrs[1] = 2;
  8.         ptrs[2] = 3;
  9.         for (int i = 2; i >= 0; --i)
  10.         {
  11.             ptrs[i] = ptrs[i]* 3;
  12.             ptrs[i] = ptrs[i] + 4;
  13.             Console.WriteLine(ptrs[i]);
  14.         }
  15.         Console.ReadLine();
  16.     }
  17. }

a) 20, 10, 7
b) 13, 10, 7
c) 6, 9, 3
d) Compile time error
View Answer

Answer: b
Explanation: None.
Output: 13, 10, 7
advertisement

4. Among the given pointers which of the following cannot be incremented?
a) int
b) char
c) float
d) void
View Answer

Answer: d
Explanation: None.

5. A structure pointer points to __________
a) first member of structure
b) first two members of structure
c) whole structure
d) only to the last member of structure
View Answer

Answer: c
Explanation: None.

6. What will be the declaration of the variable ptr as the pointer to array of 6 floats?
a) float *ptr[6]
b) float [6]*ptr
c) float(*ptr)[6]
d) float(*ptr)(6).
View Answer

Answer: c
Explanation: None.

7. What will be the output of the following C# code snippet?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          char[] arr = { 'A', 'B', 'C', 'D', 'E' };
  6.          fixed (char* P = arr)
  7.          {
  8.              int i;
  9.              for (i = 0 ;i < 5 ;i++)
  10.              if (*P % 2 == 0)
  11.              ++*P;
  12.              else
  13.              (*P)++;
  14.              Console.WriteLine(arr);
  15.          }
  16.          Console.ReadLine();
  17.      }
  18.  }

a) ACCEE
b) FBCDE
c) BBDDF
d) BBCEE
View Answer

Answer: b
Explanation: None.
Output:FBCDE

8. What will be the output of the following C# code snippet?

  1. class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      { 
  5.          int[] nums = new int[10];
  6.          Console.WriteLine("Index pointer like array.");
  7.          fixed (int* p = nums)
  8.          {
  9.              for (int i = 0 ;i <10 ;i++)
  10.              p[i] = i;
  11.              for (int i = 10 ;i >0 ;i--)
  12.              Console.WriteLine("p[{0}]: {1} ", i, p[i]);
  13.              Console.ReadLine();
  14.          }
  15.      }
  16.  }

a) p[10] :0, p[9] :9, p[8] :8…..p[1]:1
b) p[10] : 1, p[9] :2, p[8] :3…..p[1] :0
c) p[1] : 1, p[2] :2, p[3] :3…..p[10] :0
d) Compile time error
View Answer

Answer: a
Explanation: None.
Output:Index pointer like array:
p[10] :0, p[9] :9, p[8] :8…p[1]:1

9. What will be the output of the following C# code snippet?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          string str = "this is a test";
  6.  
  7.              fixed (char* p = str)
  8.             {
  9.                 for (int i = str.Length-1 ;p[i] != 0 ;i--)
  10.                 Console.Write(p[i]);
  11.             }
  12.         Console.ReadLine();
  13.     }
  14.  }

a) test a is this
b) compile time error
c) tset a si siht
d) run time error
View Answer

Answer: c
Explanation: Reversal of string using pointers.
Output:tset a si siht

10. What will be the output of the following C# code snippet?

  1.  class UnsafeCode
  2.  {
  3.      unsafe static void Main()
  4.      {
  5.          int* p ;
  6.          int ch = 13*5;
  7.          p = &(ch);
  8.          Console.WriteLine(Convert.ToChar(*p));
  9.          if (*p == 'A')
  10.          Console.WriteLine(Convert.ToBoolean(1));
  11.          else
  12.          Console.WriteLine(Convert.ToBoolean(0));
  13.          Console.ReadLine();
  14.      }
  15.  }

a) 65 False
b) 65 1
c) A True
d) A 1
View Answer

Answer: c
Explanation: Convert.Tochar(*p) = A
Convert.ToBoolean(1) = True
Output: A
True

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.