You are on page 1of 8

C# DEMO 1 [1] Which of the following statements are correct? I.

Reference types are either stack-allocated or allocated inline in a struct ure. II. Value types are heap-allocated. Choice a Only I is correct. Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [ans] [because Value types are either stack-alloca ted or allocated inline in a structure. Reference types are heap-allocated. ] [2] Which of the following statements are correct? I. Assigning one value type variable to another copies the contained value. II. It is possible to derive a new type from a value type. Choice a Only I is correct. [ans][Assigning one value type variable to another copies th e contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the obj ect itself. All value types are derived implicitly from the System .ValueType.] [Unlike reference types, it is not possible to derive a new type from a value type. However, like reference types, structs can implement i nterfaces.] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [3] Which of the following statements are correct? I. It is possible for a value type to contain the null value. II. Each value type has an implicit default constructor that initializes the default value of that type. Choice a Only I is correct. Choice b Only II is correct.[ans] [Unlike reference types, a value type cannot contain t he null value. However, the nullable types feature does allow for values types t

o be assigned to null.] [Each value type has an implicit default constructor t hat initializes the default value of that type. For information about default va lues of value types, see Default Values Table.] Choice c Both I and II are correct. Choice d Both I and II are incorrect. [4] Which of the following statements are correct? I. Local variables in C# must be initialized before being used. II. Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. Choice a Only I is correct. Choice b Only II is correct. Choice c Both I and II are correct.[ans][Local variables in C# must be initialized befor e being used.] [Using the new operator calls the default constr uctor of the specific type and assigns the default value to the variable.] Choice d Both I and II are incorrect.

[5] Which of the following statements are correct? I. We can assign values of any type to variables of type object. II. When a variable of a value type is converted to object, it is said to be unboxed. Choice a Only I is correct.[ans] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [6] Which of the following statements are correct? I. Variables of the value types store references to the actual data while tho se of the reference types store data. II. It is possible to convert a value type to a reference type and back again to value types. Choice a Only I is correct. Choice b Only II is correct. Choice c

Both I and II are correct.[ans] [Variables of the value types store data, while those of the reference types store references to the actual data.] [Reference types are also referred to as objects. Poi nter types can be used only in unsafe mode.] [It is possible to convert a value type to a referenc e type, and back again to value types, by using boxing and unboxing. With the exception of a boxed value types, you cannot convert a reference type to a value type.] [This section also introduces void.Value types are al so nullable, which means they can store an addition non-value state.] Choice d Both I and II are incorrect. [10] Which of the following statements are correct? I. Nullable types represent value-type variables that can be assigned the val ue of null. II. We can create a nullable type based on a reference type. Choice a Only I is correct.[ans] [Nullable types represent value-type variables that can be assigned the value of null.] [You cannot create a nullable type based on a reference type. (Reference types already support the null value.)] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [12] Which of the following statements are correct? I. When a value type is boxed, an entirely new object must be allocated and c onstructed. II. The value type is stored on the garbage collected heap. III. Unboxing a value type packages it inside an instance of the Object refer ence type. Choice a Only I and II are correct.[ans] [When a value type is boxed, an entirely new ob ject must be allocated and constructed.] [Boxing a value type packages it inside an inst ance of the Object reference type.This allows the value ty

pe to be stored on the garbage collected heap.]

Choice b Only I and III are correct. Choice c Only II and III are correct. Choice d I, II and III are correct. [14] Which of the following statements are correct? I. A jump statement such as a break is required after each case block excludi ng the last block if it is a default statement. II. C# always supports an implicit fall through from one case label to anothe r. Choice a Only I is correct. [ans] [A jump statement such as a break is required after ea ch case block, including the last block whether it is a case stateme nt or a default statement.] [C# does not support an implicit fall through from one case label to another.]

Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect.

[15] Which of the following statements are correct? I. In nested if statements the else clause belongs to the last if that does n ot have a corresponding else. II. It is not possible to extend the if statement to handle multiple conditio ns using the else-if arrangement. Choice a Only I is correct.[ans] [ In nested if statements, the else clause belongs to t he last if that does not have a corresponding else.] [ It is also possible to extend the if statement to han

dle multiple conditions using the else-if arrangement] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [16] Which of the following statements are correct? I. The if statement selects a statement for execution based on the value of a Boolean expression. II. After executing the if statement, control is transferred to the next stat ement. Choice a Only I is correct. Choice b Only II is correct. Choice c Both I and II are correct.[ans] [The if statement selects a statement for execu tion based on the value of a Boolean expression.] [After executing the if statement, control is transferred to the next statement.] Choice d Both I and II are incorrect.

[18] Which of the following statements are correct? I. Branching is performed using jump statements which cause an immediate tran sfer of the program control. II. The goto statement passes control to the next iteration of the enclosing iteration statement in which it appears. Choice a Only I is correct. [ans] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [19] Which of the following statements are correct? I. Because the test of conditional expression takes place before the executio n of the loop, therefore, a for statement executes zero or more times.

II.

All of the expressions of the for statement are not optional.

Choice a Only I is correct.[ans][Because the test of conditional expression takes place before the execution of the loop, therefore, a for statement executes zero or more times.] [All of the expressions of the for statement are optiona l;] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [20] Which of the following statements are correct? I. The switch statement is a control statement that handles multiple selectio ns and enumerations by passing control to one of the case statements within its body. II. The switch statement can include any number of case instances with two ca se statements having the same value. Choice a Only I is correct.[ans][The switch statement is a control statement that handle s multiple selections and enumerations by passing control to one of the case statements within its body ] [The switch statement can include any number of case ins tances, but no two case statements can have the same value.] Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. [21] Which of the following statements are correct? I. If we wish to execute more than one statement, multiple statements can be conditionally executed by including them into blocks using { }. II. The statements to be executed upon testing the condition can be of any ki nd, including another if statement nested into the original if statement. Choice a Only I is correct. Choice b Only II is correct. Choice c Both I and II are correct.[ans][If you wish to execute more than one statement, multiple statements can be conditionally execut ed

by including them into blocks using {}] [The statement(s) to be executed upon testing th e condition can be of any kind, including another if statement nested into the original if statem ent.] Choice d Both I and II are incorrect. [22] Which of the following statements are correct? I. The foreach statement repeats a group of embedded statements for each elem ent in an array or an object collection. II. The foreach statement is used to iterate through the collection to get th e desired information and should be used to change the contents of the collectio n to avoid unpredictable side effects. Choice a Only I is correct.[ans] [The foreach statement repeats a group of embedded stat ements for each element in an array or an object collection.] [The the but the Choice b Only II is correct. Choice c Both I and II are correct. Choice d Both I and II are incorrect. foreach statement is used to iterate through collection to get the desired information, should not be used to change the contents of collection to avoid unpredictable side effects.]

[29] What will be the output of the code snippet given below? using System ; namespace ConsoleApplication1 { class Program { static void Main ( string[ ] args ) { int i = 123 ; object o = i ; i = 456 ; System.Console.WriteLine ( Convert.ToString ( i ) + " " + Convert.ToString ( o ) ) ; } } } Choice a 123 123 Choice b

456 456 Choice c 123 456 Choice d 456 123 [30] Which of the following statement is correct about the code snippet given b elow? using System ; namespace ConsoleApplication1 { class Program { static void Main ( string[ ] args ) { int i = 123 ; object o = i ; short j = ( short ) o ; Console.WriteLine ( j ) ; } } } Choice a The code reports an error. Choice b The code causes System.InvalidCastException. Choice c The code gets compiled successfully but does not give any output. Choice d The code gives an output as 123.

You might also like