Govt. Exams
Entrance Exams
Advertisement
Topics in C# Programming
What is the difference between 'struct' and 'class' in C# regarding memory allocation?
Correct Answer:
C. struct is allocated on the stack, class on the heap
EXPLANATION
Value types (struct) are allocated on the stack, while reference types (class) are allocated on the heap.
Consider: int[] arr = {1, 2, 3}; What happens when you try to resize it using Array.Resize(ref arr, 5)?
Correct Answer:
B. Creates new array with 5 elements, preserving first 3 values
EXPLANATION
Array.Resize() creates a new array of specified size and copies elements from the original array using the ref parameter.
Which of the following is NOT a valid C# data type?
Correct Answer:
C. float32
EXPLANATION
C# has float (4 bytes) and double (8 bytes), but not float32. sbyte, uint, and decimal are valid types.
Which of the following statements about structs and classes in C# is TRUE?
Correct Answer:
B. Structs are value types, classes are reference types
EXPLANATION
In C#, structs are value types (stored on stack) while classes are reference types (stored on heap). This is a fundamental difference between the two.
Consider the following code. What will be the output?
int x = 10;
int y = 20;
int z = x++ + ++y;
Console.WriteLine(x + " " + y + " " + z);
int x = 10;
int y = 20;
int z = x++ + ++y;
Console.WriteLine(x + " " + y + " " + z);
Correct Answer:
B. 11 21 31
EXPLANATION
x++ returns 10 then increments x to 11. ++y increments y to 21 then returns 21. z = 10 + 21 = 31. Final output: 11 21 31