C# Questions & Answers on Unsafe code & Pointers Basics for Freshers

1. Fill up the blank :
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
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
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 ‘a’ and ‘b’
d) neither ‘a’ nor ‘b’
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 given code?
  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 given 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
10. Among the given pointer which of following cannot be incremented?

a) int
b) char
c) float
d) void
Answer: d
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
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
13. What will be the ouput of the given 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 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
Output :1000
15. What will be the output of the following 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
Output :65535

Related

CSS Multiple Choice Questions & Answers on CSS and (X)HTML Elements Fundamentals for Freshers

1. Which of the following elements are block and inline elements, respectively, that have no particular rendering? a) divb) spanc) box-modeld) both (a) and (b) Answer: d Explanation: The div elem...

C Programming Questions and Answers on Pointers and Arrays for Freshers

1. What is the output of this C code? #include void main() { int a[3] = {1, 2, 3}; int *p = a; printf("%p\t%p", p, a); } a) Same ad...

Java Multiple Choice Questions & Answers on Java.io package for Freshers

1. Which of these packages contain classes and interfaces used for input & output operations of a program? a) java.utilb) java.langc) java.iod) All of the mentioned Answer: c Explanation: jav...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item