C# Questions & Answers – Unsafe code & Pointers Basics

This set of C# Problems focuses on “Unsafe code & Pointers Basics”.

1. Pointer variable is used to hold the _________ of the variable.
a) Value
b) Address
c) Value and Address
d) Name of the variable
View Answer

Answer: b
Explanation: By definition.

2. Which among the given operators is referred to as ‘address of’ operator?
a) *
b) ^
c) &
d) ~
View Answer

Answer: c
Explanation: The ‘&’ is a unary operator that returns the memory address of its operand.
For example,

                           int* ip;
                           int num = 10;
                           ip = #

puts into ip the memory address of the variable num. This address is the location of the variable in the computer’s internal memory.

advertisement
advertisement

3. Choose the correct statement among the given statements?
a) Use of return statement is necessary in every function
b) Return statement may not be followed by a parenthesis
c) A program may contain more than one return statement
d) Return statement may not return a value
View Answer

Answer: a
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. What is the size of a char pointer?
a) 1 byte
b) 2 byte
c) 3 byte
d) 4 byte
View Answer

Answer: b
Explanation:

             class UnsafeCode
             {
                 unsafe static void Main()
                 {
                  char ch;
                  Console.WriteLine(sizeof(char));
                  Console.ReadLine();
                 }
            }

The sizeof() method helps in calculating size of char pointer.

advertisement

5. After incrementing a float pointer ptr by 1 it would be incremented by __________
a) 1 byte
b) 2 bytes
c) 3 bytes
d) 4 bytes
View Answer

Answer: d
Explanation: None.
advertisement

6. Which of the following job is done by the instruction ++*p for an integer pointer p?
a) increment value contained at address p
b) increment address contained in p
c) Both increment value contained at address p and increment address contained in p
d) neither increment value contained at address p nor increment address contained in p
View Answer

Answer: a
Explanation:

             class UnsafeCode
             {
                 unsafe static void Main()
                 {
                      int n = 10;
                      int* p = &n;
                      Console.WriteLine(*p);
                 }
             }

Output :

10 +  1 = 11.

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int a = 2;
  6.         int b = 4;
  7.         int *a1 = &a;
  8.         int *b1 = &b;
  9.         Console.WriteLine(*a1 + *b1);
  10.     }
  11. }

a) 6
b) print garbage value
c) print -6
d) print address of b + a
View Answer

Answer: a
Explanation: The (*) operator prints the value stored at address (&) of ‘a’.
Output :

 4 + 2 = 6

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.          int n = 10;
  6.          void* p = &n;
  7.          Console.WriteLine(*p);
  8.          Console.ReadLine();
  9.      }
  10.  }

a) The program will print 10
b) Run time error
c) Compile time error
d) Output is the address contained in p
View Answer

Answer: c
Explanation: The program will result in compile time error because void pointer cannot point anywhere.

9. Which among the following is referred as an array of pointers?
a) int *p;
b) int (*)p;
c) int p[4];
d) int*[4] p;
View Answer

Answer: d
Explanation: None.

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

Answer: d
Explanation: None.

11. How many values can be returned from a function simultaneously using pointers?
a) 1
b) 2
c) 3
d) as many as user wants
View Answer

Answer: d
Explanation: None.

12. Consider an integer pointer . *a.++*a will increment ___________ while *a++ will increment __________
a) value at a, address contained in a
b) value at a,value at a
c) address contained in a, address contained in a
d) address contained in a, value at a
View Answer

Answer: a
Explanation: None.

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int* a;
  6.         int a1 = 10;
  7.         int b1;
  8.         b1 = *&a1;
  9.         a = &b1;
  10.         {
  11.             Console.WriteLine(*a);
  12.             Console.ReadLine();
  13.         }
  14.     }
  15. }

a) program will print garbage value
b) program will print address of a
c) program will print value of a1
d) program will print address of a1
View Answer

Answer: c
Explanation: The address of variable a1 is stored in variable b1 by making a1 as a pointer to variable b1. Later, variable b1 address is stored in pointer a and hence using pointer operation value of a1 is displayed in a.
Output : 10

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

  1. class UnsafeCode
  2. {
  3.     unsafe static void Main()
  4.     {
  5.         int n = 10;
  6.         int* p = &n;
  7.         int** p1 = &p;
  8.         int*** p2 = &p1;
  9.         Console.WriteLine(*p * **p1 * ***p2);
  10.         Console.ReadLine();
  11.     }
  12. }

a) compile time error
b) garbage value is printed
c) program will print 1000
d) program will print 100
View Answer

Answer: c
Explanation: None.
Output :1000

15. 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.             p = (int*)(65535);
  7.             Console.WriteLine((uint)p);
  8.             Console.ReadLine();
  9.     }
  10. }

a) compile time error
b) garbage value
c) program prints value at address 65535
d) program prints 65535
View Answer

Answer: d
Explanation: None.
Output :

65535

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# Problems, 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.