Professional Documents
Culture Documents
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
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.
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" };