You are on page 1of 6

BT8903 C# Programming

Question 1 - What is .Net Framework?


The .NET Framework is an integral Windows component that supports building and running the next generation of applications and XML Web services. The .NET Framework is designed to fulfill the following objectives: To provide a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely. To provide a code-execution environment that minimizes software deployment and versioning conflicts. To provide a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party. To provide a code-execution environment that eliminates the performance problems of scripted or interpreted environments. To make the developer experience consistency across widely varying types of applications, such as Windows-based applications and Web-based applications. To build all communication on industry standards to ensure that code based on the .NET Framework can integrate with any other code. The .NET Framework has two main components: the common language runtime and the .NET Framework class library. Common Language Runtime: The CLR is the foundation of the .NET Framework. You can think of the runtime as an agent that manages code at execution time, providing core services such as memory management, thread management, and remoting, while also enforcing strict type safety and other forms of code accuracy that promote security and robustness. The .NET Framework Class Library: The .NET Framework class library is a collection of reusable types that tightly integrate with the Common Language Runtime (CLR). The class library is object oriented, providing types from which your own managed code can derive functionality. This not only makes the .NET Framework types easy to use, but also reduces the time associated with learning new features of the .NET Framework. In addition, third-party components can be integrated seamlessly with classes in the .NET Framework. The major components of the .Net framework are shown in figure below:

Question 2 - What are literals? Explain different types of literals supported by C#.
Literals are value constants assigned to variables (or results of expressions) in a program. C# supports several types of literals as shown below:

There are six types of literals. Following below: Integer Literals: An integer literal refers to a sequence of digits. There are two types of integers, namely, decimal and hexadecimal. Example: Valid and Invalid integers: 123, -321, 0, 654564 Example: Valid Hex integers: 0X2, 0X9F, 0Xbcd, 0X Real Literals: These are represented by numbers containing fractional parts like 17.548. The real literal may also be expressed in exponential (or scientific) notation. The general form of representing Real Literals is like Mantissa e exponent, the mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer with an optional plus or minus sign. Boolean Literals: There are two Boolean literal values: True: This literal indicates that a statement or an expression is correct. False: This literal value indicates that a statement or an expression is wrong. Example: The value of 2+3 = 5 is True, and the value 2 * 3 = 9 is False. Single Character Literals: A single character literal (or simply character constant) contains a single character enclosed within a pair of single quote marks. Example: '5' 'X' ';' ' ' String Literals: A string literal is a sequence of characters enclosed between double quotes. The character may be letters, digits, special characters and blank spaces. Example: Hello C# 2001 Well done ?.......! 5+3 X Backslash Character Literals: C# supports some special backslash character constants that are used in output methods. Example: lists various constants used in C# language with the corresponding meaning given below: Constant Meaning Constant Meaning '\a' Alert '\r' Carriage return '\b' Back space '\t' Horizontal tab '\f' Form feed '\v' Vertical tab '\n' New-line '\'' Single quote '\' Double quote '\\' Backslash '\o' Null

Question 3 - Explain boxing and unboxing with examples.

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. Boxing means the conversion of a value type on the stack to an object type on the heap. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. Conversely, the conversion from an object type back to a value type is known as unboxing. The concept of boxing and unboxing underlies the C# unified view of the type system, in which a value of any type can be treated as an object. Boxing can formally be defined as the process of explicitly converting a value type into a corresponding reference type. Example: int i = 123; // The following line boxes i. object o = i; When you box a value, essentially all you are doing is allocating a new object on the heap and copying the internal value into that instance. What is returned to you is true reference to the newly allocated object. The opposite operation is also permitted through unboxing. Unboxing is the term given to the process of converting the value held in the object reference back into a corresponding value type on the stack. The unboxing operation begins by verifying that the receiving data type is equivalent to the boxed type, and if so, copying the value out of the box into a local stack based variable. Example: o = 123; i = (int)o; // unboxing .NET provides a unified type system. All types including value types derive from the type object. It is possible to call object methods on any value, even values of primitive types such as int.

Question 4 - What is a mixed mode arithmetic expression? Explain with an example.


If operands in an expression contain both INTEGER and REAL constants or variables, this is a mixed mode arithmetic expression. In mixed mode arithmetic expressions, INTEGER operands are always converted to REAL before carrying out any computations. As a result, the result of a mixed mode expression is of REAL type. The following is a table showing this fact. Operator INTEGER REAL INTEGER INTEGER REAL REAL REAL REAL

The rules for evaluating mixed mode arithmetic expressions are simple: Use the rules for evaluating single mode arithmetic expressions for scanning. After locating an operator for evaluation, do the following: If the operands of this operator are of the same type, compute the result of this operator. Otherwise, one of the operand is an integer while the other is a real number. In this case, convert the integer to a real (i.e., adding .0 at the end of the integer operand) and compute the result. Note that since both operands are real numbers, the result is a real number. There is an exception, though. In a**n, where a is a real and n is a positive integer, the result is computed by multiplying n copies of a. For example, 3.5**3 is computed as 3.5*3.5*3.5 Simple Examples: 1 + 2.5 is 3.5 1/2.0 is 0.5 2.0/8 is 0.25 -3**2.0 is -9.0 4.0**(1/2) is first converted to 4.0**0 since 1/2 is a single mode expression whose result is 0. Then, 4.0**0 is 1.0 An Important Note: In expression a**b where a is REAL, the result is undefined if the value of a is negative. For example, -4.0**2 is defined with -16.0 as its result, while (-4.0)**2 is undefined.

Question 5 - What are arrays? Explain one dimensional array with relevant examples.
In computer memory every byte is an array element. Abstractions translate these bytes into objects and give them meaning. Arrays are a foundational type. They are the basis of more usable collections. They use a special syntax form. An array is a fixed collection of same-type data that are stored contiguously and that are accessible by an index. Arrays are the simplest and most common type of structured data. Arrays give us a list of items of similar data types. For example, to store the names of students, you can use an array of type string; to store their ages, an integer type can be used. In C#, arrays are derived from the System.Array class. There are lot of properties and methods in this class; we can manipulate these for our programming tasks. Arrays in C# are declared in the same fashion as in Java or C++; they can be either single- or multidimensional. The following code declares a single dimensional array of type integer and with a dimension of 10. Dimension indicates the number of array elements. Arrays can be divided into the following four categories. Single-dimensional arrays Multidimensional arrays or rectangular arrays Jagged arrays Mixed arrays. Single Dimension Arrays: Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored contiguously starting from 0 to the size of the array -1. The following code declares an integer array that can store 3 items. As you can see from the code, first I declare the array using [] bracket and after that I instantiate the array by calling the new operator. int[] intArray; intArray = new int[3]; The following code declares and initializes an array of three items of integer type. int[] staticIntArray = new int[3] {1, 3, 5}; The following code declares and initializes an array of 5 string items. string[] strArray = new string[5] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" }; You can even directly assign these values without using the new operator. string[] strArray = { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" }; You can initialize a dynamic length array as follows: string[] strArray = new string[] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };

Question 6 - Explain different method parameters used in C# with examples.


A method invocation creates a copy, specific to that invocation, of the formal parameters and local variables of that method. The actual argument list of the invocation assigns values or variable references to the newly created formal parameters. Within the body of a method, formal parameters can be used like any other variables. The invocation involves not only passing the values into the method but also getting back values from the method. For managing the process of passing values and getting back the results, C# uses the following four kinds of parameters: Value parameters Output parameters Reference parameters Parameter arrays Value parameters are used for passing parameters into methods by value. By default, method parameters are passed by value. That is, a parameter declared with no modifier is passed by value and is called a value parameter. When a method is invoked, the values of actual parameters are assigned to the corresponding formal parameters. The values of the value parameters can be changed within the method. Reference parameters are used to pass into methods by reference. Unlike value parameters, a reference parameter does not create a new storage location. Instead, it represents the same storage location as the actual parameter used in the method invocation. A parameter declared with the ref modifier is a reference parameter. Example: void modify (ref int x) Output parameters are used to pass results back from the method. Output parameters are used to pass results back to the calling method. This is achieved by declaring the parameters with an out keyword. Similar to a reference parameter, an output parameter does not create a new storage location. Instead, it becomes an alias to the parameter in the calling method. Parameters arrays are used in a method to receive variable number of arguments when called. In C#, we can define methods that can handle variable number of arguments using what are known as parameter arrays. Parameter arrays are declared using the keyword params. Example: void function1 (params int [ ] x) { }

You might also like