Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Question & Answers ? Pointers Operation ? 2
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?
-
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();
-
}
-
}
a) Compile time error
b) Run time error
c) 200
d) 30
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
2. 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();
-
}
-
}
a) Compile time error
b) 120
c) Run time error
d) 4
Explanation: None.
Output :
120
3. 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();
-
}
-
}
-
}
a) Run time error
b) Compile time error
c) p and p2 point to the same address
d) Only p2
Explanation: None.
Output:
p and p2 point to same address
4. 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();
-
}
-
}
a) Run time error
b) 0
c) Result has no value
d) Compile time error
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?
-
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();
-
}
-
}
a) Run time error
b) 110
c) Result has no value
d) Compile time error
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
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
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
Explanation: None.
9. 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();
-
}
-
}
a) Run time error
b) 80
c) 96
d) Compile time error
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?
-
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();
-
}
-
}
a) 3 2 1
b) 1 2 3
c) None of the mentioned
d) Run time error
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