C# Question & Answers – Pointers Operation – 2

This set of C# Questions and Answers for Freshers focuses on “Pointers Operation – 2”.

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

  1. class UnsafeCode
  2. {
  3.     struct MyStruct
  4.     {
  5.         public int a;
  6.         public int b;
  7.         public int Sum() 
  8.        { 
  9.            return a * b; 
  10.        }
  11.    }
  12.    unsafe static void Main()
  13.    {
  14.        MyStruct o = new MyStruct();
  15.        MyStruct* p; 
  16.        p = &o;
  17.        p->a = 10; 
  18.        p->b = 20; 
  19.        Console.WriteLine("Value is " + p->Sum());
  20.        Console.ReadLine();
  21.    }
  22. }

a) Compile time error
b) Run time error
c) 200
d) 30
View Answer

Answer: c
Explanation: A pointer can point to an object of a structure type as long as the structure does not contain reference types. When we access a member of a structure through a pointer, we must use the arrow operator, which is –>, rather than the dot (.) operator.
Output : 200
advertisement
advertisement

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

  1. class UnsafeCode
  2. {
  3.     struct MyStruct
  4.     {
  5.         public int a;
  6.         public int b;
  7.         public int Sum() 
  8.        { 
  9.            return a / b; 
  10.        }
  11.    }
  12.    unsafe static void Main()
  13.    {
  14.        MyStruct o = new MyStruct();
  15.        MyStruct* p; 
  16.        p = &o;
  17.        p->a = 60; 
  18.        p->b = 15; 
  19.        int c = 30;
  20.        Console.WriteLine("Value is : " + p->Sum()*c);
  21.        Console.ReadLine();
  22.    }
  23. }

a) Compile time error
b) 120
c) Run time error
d) 4
View Answer

Answer: b
Explanation: None.
Output :

120

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

advertisement
  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int[] nums = new int[10];
  6.         fixed (int* p = &nums[0], p2 = nums)
  7.         {
  8.             if (p == p2)
  9.             Console.WriteLine("p and p2 point to same address.");
  10.             Console.ReadLine();
  11.         }
  12.     }
  13. }

a) Run time error
b) Compile time error
c) p and p2 point to the same address
d) Only p2
View Answer

Answer: c
Explanation: None.
Output:

advertisement
p and p2 point to same address

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

  1. class UnsafeCode
  2. {
  3.     static void Main()
  4.     {
  5.         int? count = null;
  6.         int? result = null;
  7.         int incr = 10;
  8.         result = count + incr;
  9.         if (result.HasValue)
  10.             Console.WriteLine("result has this value: " + result.Value);
  11.         else
  12.             Console.WriteLine("result has no value");
  13.         Console.ReadLine();
  14.     }
  15. }

a) Run time error
b) 0
c) Result has no value
d) Compile time error
View Answer

Answer: c
Explanation: A nullable object can be used in expressions that are valid for its underlying type. When non-nullable and nullable types are mixed in an operation, the outcome is a nullable value.
Output:

result has no value

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

  1. class UnsafeCode
  2. {
  3.     static void Main()
  4.     {
  5.         int count = 100;
  6.         int? result = null;
  7.         int incr = 10;
  8.         result = count + incr;
  9.         if (result.HasValue)
  10.             Console.WriteLine("result has this value: " + result.Value);
  11.         else
  12.             Console.WriteLine("result has no value");
  13.         Console.ReadLine();
  14.     }
  15. }

a) Run time error
b) 110
c) Result has no value
d) Compile time error
View Answer

Answer: b
Explanation: None.
Output: result has this value :

110

6. Choose the statement which defines the Nullable type Correctly:
a) A special version of a value type that is represented by a structure
b) A nullable type can also store the value null
c) Nullable types are objects of System.Nullable, where T must be a non nullable value type
d) All of the mentioned
View Answer

Answer: d
Explanation: A nullable type is a special version of the value type that is represented by a structure. In addition to the values defined by the underlying type, a nullable type can also store the value null. Thus, a nullable type has the same range and characteristics as its underlying type. It simply adds the ability to represent a value which indicates that a variable of that type is unassigned. Nullable types are objects of System.Nullable<T>, where T must be a nonnullable value type.

7. What does the following code depicts?

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

a) Code i declares the objects of nullable of type Nullable<T> defined in the System namespace
b) Code ii declares a nullable type in much shorter and in more commonly used way using ‘?’
c) Both Code i declares the objects of nullable of type Nullable<T> defined in the System namespace & Code ii declares a nullable type in much shorter and in more commonly used way using ‘?’
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

8. Which operator is commonly used to find the size of the type of C#?
a) size()
b) sizeof(type)
c) both size() & sizeof(type)
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

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

  1.  unsafe struct FixedBankRecord
  2.  {
  3.      public fixed byte Name[80]; 
  4.      public double Balance;
  5.      public long ID;
  6.  }
  7.  class UnsafeCode
  8.  {
  9.      unsafe static void Main()
  10.      {
  11.          Console.WriteLine("Size of FixedBankRecord is " + sizeof(FixedBankRecord));
  12.          Console.ReadLine();
  13.      }
  14.  }

a) Run time error
b) 80
c) 96
d) Compile time error
View Answer

Answer: c
Explanation: The purpose of a fixed-size buffer is to allow the creation of a struct in which the array of elements that make up the buffer are contained within the struct. By using a fixed-size buffer, we let the entire array to be contained within the struct. The overall size of FixedBankRecord is 96, which is the sum of its members.
Output :

96

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

a) 3 2 1
b) 1 2 3
c) None of the mentioned
d) Run time error
View Answer

Answer: a
Explanation: Allocates memory from the stack by using stackalloc. Here, ptrs is a pointer that receives the address of the memory that is large enough to hold size of number of objects of type ‘int’. Here, type ‘int’ is a non reference type. Finally, stackalloc can be used only in an unsafe context.
Output :

3 2 1

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

Here’s the list of Best Books in C# Programming Language.

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
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.