.NET - .NET Programming Concepts
17.
Determine the output of the code snippet.
int a = 29;
a--;
a -= ++a;
Console.WriteLine("The value of a is: {0}", a);
/* The output of the code is -1. */
18.
Differentiate between Boxing and Unboxing.
When a value type is converted to an object type, the process is known as boxing; whereas, when an object type is converted to a value type, the process is known as unboxing.
Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object obj.
Example:
Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object obj.
Example:
int i = 123;
object obj = i; /* Thi line boxes i. */
/* The object obj can then be unboxed and assigned to integer variable i: */
i = (int)obj; // unboxing
19.
Give the syntax of using the for loop in C# code?
The syntax of using the for loop in C# code is given as follows:
In the preceding syntax, initializer is the initial value of the variable, condition is the expression that is checked before the execution of the for loop, and loop expression either increments or decrements the loop counter.
The example of using the for loop in C# is shown in the following code snippet:
In the preceding code snippet, the word Hello will be displayed for five times in the output window.
for(initializer; condition; loop expression)
{
//statements
}
In the preceding syntax, initializer is the initial value of the variable, condition is the expression that is checked before the execution of the for loop, and loop expression either increments or decrements the loop counter.
The example of using the for loop in C# is shown in the following code snippet:
for(int i = 0; i < 5; i++)
Console.WriteLine("Hello");
In the preceding code snippet, the word Hello will be displayed for five times in the output window.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers