My Report

C# Pointer Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. A structure pointer points to __________

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

class UnsafeCode
 {
     unsafe static void Main()
     { 
         int[] nums = new int[10];
         Console.WriteLine("Index pointer like array.");
         fixed (int* p = nums)
         {
             for (int i = 0 ;i <10 ;i++)
             p[i] = i;
             for (int i = 10 ;i >0 ;i--)
             Console.WriteLine("p[{0}]: {1} ", i, p[i]);
             Console.ReadLine();
         }
     }
 }

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

 class UnsafeCode
 {
     unsafe static void Main()
     {
         string str = "this is a test";
         
             fixed (char* p = str)
            {
                for (int i = str.Length-1 ;p[i] != 0 ;i--)
                Console.Write(p[i]);
            }
        Console.ReadLine();
    }
 }

4. Among the given pointers which of the following cannot be incremented?

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

 unsafe static void Main()
 {
     int a = 5;
     int b = 5;
     int c = 5;
     int*[] ptr = new int* [3];
     ptr[0] = &a;
     ptr[1] = &b;
     ptr[2] = &c;
     for (a = 0; a < 3; a++)
     {
         c += *ptr[a];
         Console.WriteLine(c);
     }
     Console.ReadLine();
 }

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

class UnsafeCode
{
    unsafe static void Main()
    {
        int* ptrs = stackalloc int[3];
        ptrs[0] = 1;
        ptrs[1] = 2;
        ptrs[2] = 3;
        for (int i = 2; i >= 0; --i)
        {
            ptrs[i] = ptrs[i]* 3;
            ptrs[i] = ptrs[i] + 4;
            Console.WriteLine(ptrs[i]);
        }
        Console.ReadLine();
    }
}

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

 class UnsafeCode
 {
     unsafe static void Main()
     {
         char[] arr = { 'A', 'B', 'C', 'D', 'E' };
         fixed (char* P = arr)
         {
             int i;
             for (i = 0 ;i < 5 ;i++)
             if (*P % 2 == 0)
             ++*P;
             else
             (*P)++;
             Console.WriteLine(arr);
         }
         Console.ReadLine();
     }
 }

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

 class UnsafeCode
 {
     unsafe static void Main()
     {
         int* p ;
         int ch = 13*5;
         p = &(ch);
         Console.WriteLine(Convert.ToChar(*p));
         if (*p == 'A')
         Console.WriteLine(Convert.ToBoolean(1));
         else
         Console.WriteLine(Convert.ToBoolean(0));
         Console.ReadLine();
     }
 }

9. What will be the declaration of the variable ptr as the pointer to array of 6 floats?

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

 class UnsafeCode
 {
     unsafe static void Main()
     {
         int m = 10;
         int *mptr = &m;
         int **ptr = &mptr;
         int n = 20;
         int *nptr = &n;
         int **prt = &nptr;
         m = **prt + *nptr;
         n = *mptr* **prt;
         Console.WriteLine(n + " " + m);
         Console.ReadLine();
     }
 }

 

Start practicing “1000 MCQs on C#”, and once you are ready, you can take tests on all topics by attempting our “C# Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.