My Report

C# Pointer Test – 3


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. What will be the output of the following C# code snippet?

class UnsafeCode
{
    unsafe static void Main()
    {
        int[] nums = new int[10];
        fixed (int* p = &nums[0], p2 = nums)
        {
            if (p == p2)
            Console.WriteLine("p and p2 point to same address.");
            Console.ReadLine();
        }
    }
}

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

 unsafe struct FixedBankRecord
 {
     public fixed byte Name[80]; 
     public double Balance;
     public long ID;
 }
 class UnsafeCode
 {
     unsafe static void Main()
     {
         Console.WriteLine("Size of FixedBankRecord is " + sizeof(FixedBankRecord));
         Console.ReadLine();
     }
 }

3. Choose the statement which defines the Nullable type Correctly:

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

class UnsafeCode
{
    struct MyStruct
    {
        public int a;
        public int b;
        public int Sum() 
       { 
           return a * b; 
       }
   }
   unsafe static void Main()
   {
       MyStruct o = new MyStruct();
       MyStruct* p; 
       p = &o;
       p->a = 10; 
       p->b = 20; 
       Console.WriteLine("Value is " + p->Sum());
       Console.ReadLine();
   }
}

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

class UnsafeCode
{
    struct MyStruct
    {
        public int a;
        public int b;
        public int Sum() 
       { 
           return a / b; 
       }
   }
   unsafe static void Main()
   {
       MyStruct o = new MyStruct();
       MyStruct* p; 
       p = &o;
       p->a = 60; 
       p->b = 15; 
       int c = 30;
       Console.WriteLine("Value is : " + p->Sum()*c);
       Console.ReadLine();
   }
}

6. Which operator is commonly used to find the size of the type of C#?

7. 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--)
        Console.WriteLine(ptrs[i]);
        Console.ReadLine();
    }
}

8. What does the following code depicts?

   i. System.Nullable count;
   ii. bool? done;

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

class UnsafeCode
{
    static void Main()
    {
        int? count = null;
        int? result = null;
        int incr = 10;
        result = count + incr;
        if (result.HasValue)
            Console.WriteLine("result has this value: " + result.Value);
        else
            Console.WriteLine("result has no value");
        Console.ReadLine();
    }
}

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

class UnsafeCode
{
    static void Main()
    {
        int count = 100;
        int? result = null;
        int incr = 10;
        result = count + incr;
        if (result.HasValue)
            Console.WriteLine("result has this value: " + result.Value);
        else
            Console.WriteLine("result has no value");
        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.