Users Online

· Guests Online: 101

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Questions & Answers ? Unsafe code & Pointers Basics

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

Answer: b
Explanation: By definition.

 

 

 

 

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

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.

 

 

 

 

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

Answer: a
Explanation: None.
 

 

 

 

 

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

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.

 

 

 

 

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

Answer: d
Explanation: None.

 

 

 

 

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

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

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

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;

Answer: d
Explanation: None.

 

 

 

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

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

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

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

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

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

Answer: d
Explanation: None.
Output :

 

65535

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.95 seconds
10,838,105 unique visits