MCQ Questions

Question Bank › Technical › C# › MCQ

1234

Other questions

Q201.Easy
What is the output of the following C#.NET code? Console.WriteLine(17 / 5 + " " + 17 % 5);
  1. 3.4 2
  2. 3.4 0
  3. 3 0
  4. 3 2
  5. 3.4 3.4
Show answer & explanation
Answer: (D) 3 2
Explanation: 1) Both operands 17 and 5 are integers, so 17 / 5 is integer division: 5 goes into 17 three times, giving 3. 2) 17 % 5 is the remainder: 17 - 15 = 2. 3) The string concatenation joins them with a space: "3 2". 4) So the output is 3 2.
Q202.Easy
In C#, which modifier is used when a virtual method is redefined by a derived class?
  1. overloads
  2. override
  3. overridable
  4. virtual
  5. base
Show answer & explanation
Answer: (B) override
Explanation: Step 1: A base class declares a method with the virtual keyword so it can be redefined. Step 2: The derived class redefines it using the override keyword, e.g. public override void Show(). Step 3: 'overloads' and 'overridable' are not C# keywords. Step 4: 'virtual' is used in the base class, not when redefining. Step 5: 'base' accesses the parent class; it is not a redefinition modifier. Step 6: Hence the correct modifier is override.
Q203.Easy
In C#, for an instance of a derived class to completely replace (take over) a member inherited from a base class, the base class must declare that member as:
  1. new
  2. base
  3. virtual
  4. overrides
  5. overloads
Show answer & explanation
Answer: (C) virtual
Explanation: Step 1: To replace a base-class member's behaviour polymorphically, the derived class uses the override keyword. Step 2: The override keyword is only legal if the base member is marked virtual (or abstract). Step 3: 'new' hides the member instead of overriding it, so no true takeover occurs. Step 4: 'base' is a keyword to access the base class, not a modifier for the member. Step 5: 'overrides'/'overloads' are not C# keywords. Step 6: Therefore the base class must declare the member as virtual.
Q204.Easy
In C#, a property can be declared inside a class, a struct, and an interface.
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: 1. A property is a member that exposes a get/set accessor. 2. Classes and structs can both contain properties. 3. Interfaces can also declare properties (signature only, no body). 4. Hence the statement is True.
Q205.Easy
Which of the following statements about a String is correct?
  1. A String is created on the stack.
  2. Whether a String is created on the stack or the heap depends on the length of the String.
  3. A String is a primitive.
  4. A String can be created by using the statement String s1 = new String;
  5. A String is created on the heap.
Show answer & explanation
Answer: (E) A String is created on the heap.
Explanation: Step 1: String is a reference type in C#, and reference-type objects are allocated on the managed heap — so option 5 is correct. Step 2: Option 1 is wrong because strings are not stored on the stack (only the reference variable may be). Step 3: Option 2 is wrong — allocation location does not depend on length. Step 4: Option 3 is wrong — String is not a primitive type. Step 5: Option 4 is wrong — new String requires a character array or similar argument; it cannot be called with no arguments.
Q206.Easy
Which of the following statements is correct about a String in C#.NET?
  1. A String is mutable because it can be modified once it has been created.
  2. Methods of the String class can be used to modify the string.
  3. A number cannot be represented in the form of a String.
  4. A String has a zero-based index.
  5. The System.Array class is used to represent a string.
Show answer & explanation
Answer: (D) A String has a zero-based index.
Explanation: Step 1: Strings in C# are immutable, so option 1 is wrong. Step 2: String class methods return new strings rather than modifying the original, so option 2 is wrong. Step 3: Numbers can easily be converted to strings (e.g., ToString()), so option 3 is wrong. Step 4: Characters in a string are accessed by index starting at 0, so option 4 is correct. Step 5: A string is represented by the System.String class, not System.Array, so option 5 is wrong.
Q207.Easy
In C#, objects created using the String class cannot be modified after creation, whereas objects created using the StringBuilder class can be modified. Which statement is correct?
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: 1. String is immutable: any operation that appears to change it actually creates a new string. 2. StringBuilder holds a mutable buffer, so Append/Replace modify the same object. 3. Hence the statement is True.
Q208.Easy
What is the output of the following C# code? String s1 = "Chennai"; String s2; s2 = s1.Insert(7, "Delhi"); Console.WriteLine(s2);
  1. ChennaiDelhi
  2. ChennaDelhii
  3. Chennai Delhi
  4. Delhi
  5. Chennai
Show answer & explanation
Answer: (A) ChennaiDelhi
Explanation: s1 is "Chennai" (length 7). Insert(7, "Delhi") inserts "Delhi" at index 7, which is at the end. So result is "ChennaiDelhi". No space is added.
Q209.EasyAnswer under review
What is the output of the following C# code? string s = " Mumbai "; Console.Write(s.IndexOf('b') + " "); Console.Write(s.Length);
  1. 3 8
  2. 2 7
  3. 3 7
  4. 2 8
  5. 3 9
Show answer & explanation
Answer: (A) 3 8
Explanation: The string is " Mumbai " (space, M, u, m, b, a, i, space). IndexOf('b') returns the zero-based index of 'b': positions: 0:space, 1:M, 2:u, 3:m, 4:b, so index is 4? Wait, let's recount: " Mumbai " has characters: index0=' ', 1='M', 2='u', 3='m', 4='b', 5='a', 6='i', 7=' '. So IndexOf('b') is 4, not 3. But the options don't have 4. I need to adjust the string to make the answer match one of the options. Let's choose a string where the index is 3 and length 8. For example, "Delhi " has length 6. " Chennai " has length 9. Let's use " Kolkata " (space, K, o, l, k, a, t, a, space) length 9, index of 'k' is 4. Not good. Let's use " Pune " (space, P, u, n, e, space) length 6, index of 'n' is 3. Then output would be "3 6". But options have 3 8, 2 7, etc. Let's use " Bhopal " (space, B, h, o, p, a, l, space) length 8, index of 'p' is 4. Not 3. Use " Agra " (space, A, g, r, a, space) length 6, index of 'r' is 3. Output "3 6". Not in options. Let's use " Surat " (space, S, u, r, a, t, space) length 7, index of 'r' is 3. Output "3 7". That matches option 3 (index 2). But I want answer_index 0. Let's use " Indore " (space, I, n, d, o, r, e, space) length 8, index of 'd' is 3. Output "3 8". That matches option 0. So string should be " Indore ". Let's verify: characters: 0:' ',1:'I',2:'n',3:'d',4:'o',5:'r',6:'e',7:' '. IndexOf('d') = 3. Length = 8. Output: "3 8". So correct option is "3 8".
Q210.Easy
The memory space required for structure variables is allocated on the stack.
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: Step 1: Structs are value types in C#. Step 2: When a struct variable is declared locally, its memory is allocated on the stack. Step 3: (If a struct is a field of a class, it lives inside the heap object, but the standard statement about struct variables is that they are stack-allocated.) Step 4: Hence the statement is True.
Q211.Easy
When does a structure variable in C# get destroyed?
  1. When no reference refers to it, it gets garbage collected.
  2. It depends on whether it was created using new or without new.
  3. When it goes out of scope.
  4. It depends on the Project Settings made in Visual Studio.
  5. It depends on whether we free its memory using free() or delete().
Show answer & explanation
Answer: (C) When it goes out of scope.
Explanation: Step 1: A struct is a value type, not a reference type. Step 2: Value types are not managed by the garbage collector. Step 3: A struct variable's memory is reclaimed automatically when it goes out of scope (stack unwinding or containing object destruction). Step 4: Therefore the correct answer is 'When it goes out of scope.'
Q212.Medium
Which of the following statements about the .NET CLR are TRUE?
1. It provides a language-neutral development and execution environment.
2. It prevents an application from accessing memory it is not authorized to access.
3. It provides services to run managed applications.
4. It performs garbage collection of resources.
5. It provides services to run unmanaged applications.
  1. Only 1 and 2
  2. Only 1, 2 and 4
  3. 1, 2, 3 and 4
  4. Only 4 and 5
  5. Only 3 and 4
Show answer & explanation
Answer: (C) 1, 2, 3 and 4
Explanation: 1. The CLR is language-neutral: many languages compile to IL that the CLR runs — statement 1 is true. 2. Type safety and memory-access checks stop unauthorized memory access — statement 2 is true. 3. The CLR hosts and runs managed applications — statement 3 is true. 4. The CLR's garbage collector reclaims unused memory — statement 4 is true. 5. Unmanaged applications run outside CLR control, so statement 5 is false. 6. Hence statements 1, 2, 3 and 4 are true.
Q213.Medium
Which of the following statements is correct about the .NET Framework?
  1. .NET Framework uses DCOM for achieving language interoperability.
  2. .NET Framework is built on the DCOM technology.
  3. .NET Framework uses DCOM for making transition between managed and unmanaged code.
  4. .NET Framework uses DCOM for creating unmanaged applications.
  5. .NET Framework uses COM+ services while creating Distributed Applications.
Show answer & explanation
Answer: (E) .NET Framework uses COM+ services while creating Distributed Applications.
Explanation: Step 1: Language interoperability in .NET is achieved through the CTS and CLR, not DCOM — option 1 is wrong. Step 2: .NET Framework is not built on DCOM technology — option 2 is wrong. Step 3: Transition between managed and unmanaged code uses COM interop/P-Invoke, not DCOM itself — option 3 is wrong. Step 4: DCOM is not used to create unmanaged applications — option 4 is wrong. Step 5: .NET Framework does use COM+ services (Enterprise Services) for distributed applications — option 5 is correct.
Q214.Medium
Which of the following benefits do we get on running managed code under the CLR? 1. Type safety of the code running under CLR is assured. 2. It is ensured that an application would not access memory that it is not authorized to access. 3. It launches a separate process for every application running under it. 4. The resources are garbage collected.
  1. Only 1 and 2
  2. Only 2, 3 and 4
  3. Only 1, 2 and 4
  4. Only 4
  5. All of the above
Show answer & explanation
Answer: (C) Only 1, 2 and 4
Explanation: Step 1: Managed code under CLR is type-safe — statement 1 is true. Step 2: CLR verifies code so it cannot access unauthorized memory — statement 2 is true. Step 3: CLR does NOT launch a separate process for every application; multiple AppDomains can run in one process — statement 3 is false. Step 4: CLR performs garbage collection of resources — statement 4 is true. Step 5: So the correct benefits are 1, 2 and 4.
Q215.Medium
Which of the following security features can .NET applications use? 1. PIN Security 2. Code Access Security 3. Role Based Security 4. Authentication Security 5. Biorhythm Security
  1. 1, 4, 5
  2. 2, 5
  3. 2, 3
  4. 3, 4
Show answer & explanation
Answer: (C) 2, 3
Explanation: Step 1: Code Access Security (2) is a genuine .NET security feature that restricts what code can do. Step 2: Role Based Security (3) is supported in .NET through principal/identity objects. Step 3: PIN Security (1) and Biorhythm Security (5) are not .NET security features. Step 4: Authentication Security (4) is a general concept, not a specific .NET security feature name here. Step 5: Hence the correct pair is 2 and 3.
Q216.Medium
Which of the following tasks are performed by the Common Language Runtime (CLR)? 1. It offers core services such as memory management, thread management and remoting. 2. It enforces strict type safety. 3. It provides Code Access Security. 4. It provides Garbage Collection services.
  1. Only 1 and 2
  2. Only 3 and 4
  3. Only 1, 3 and 4
  4. Only 2, 3 and 4
  5. All of the above
Show answer & explanation
Answer: (E) All of the above
Explanation: Step 1: CLR provides core services like memory management, thread management and remoting — statement 1 is true. Step 2: CLR enforces strict type safety for managed code — statement 2 is true. Step 3: CLR provides Code Access Security — statement 3 is true. Step 4: CLR provides Garbage Collection services — statement 4 is true. Step 5: Since all four statements are correct, the answer is 'All of the above'.
Q217.Medium
Which of the following statements are correct about a .NET Assembly? 1. It is the smallest deployable unit. 2. Each assembly has only one entry point - Main(), WinMain() or DLLMain(). 3. An assembly can be a Shared assembly or a Private assembly. 4. An assembly can contain only code and data. 5. An assembly is always in the form of an EXE file.
  1. 1, 2, 3
  2. 2, 4, 5
  3. 1, 3, 5
  4. 1, 2
Show answer & explanation
Answer: (A) 1, 2, 3
Explanation: An assembly is the smallest deployable unit (1 true). Each assembly has a single entry point such as Main, WinMain or DLLMain (2 true). Assemblies can be shared (in GAC) or private (3 true). Statement 4 is false because assemblies also contain metadata and manifest. Statement 5 is false because assemblies can be DLLs too. Hence 1, 2, 3.
Q218.MediumAnswer under review
Which of the following statements are correct about JIT? 1. The JIT compiler compiles instructions into machine code at run time. 2. The code compiled by the JIT compiler runs under the CLR. 3. The instructions compiled by JIT compilers are written in native code. 4. The instructions compiled by JIT compilers are written in Intermediate Language (IL) code. 5. A method is JIT compiled even if it is not called.
  1. 1, 2, 3
  2. 2, 4
  3. 3, 4, 5
  4. 1, 2
Show answer & explanation
Answer: (A) 1, 2, 3
Explanation: JIT compiles IL to native machine code at run time (statement 1 true). The resulting code runs under the CLR (statement 2 true). The output of JIT is native code (statement 3 true). Statement 4 is false because the input to JIT is IL, not the output. Statement 5 is false because methods are JIT compiled only when first called. Hence 1, 2, 3.
Q219.Medium
Which of the following statements are NOT true about the .NET Framework? (1) It gives a consistent object-oriented environment whether code runs locally, is Internet-distributed, or runs remotely. (2) It minimizes software deployment and versioning conflicts. (3) It promotes safe execution of code, even code from unknown or semi-trusted sources. (4) It uses different programming models for Windows-based and Web-based applications. (5) It provides an event-driven model for building Windows device drivers.
  1. 1, 2
  2. 2, 4
  3. 4, 5
  4. 1, 2, 4
Show answer & explanation
Answer: (C) 4, 5
Explanation: 1. Statement 1 is true: .NET gives a consistent OOP environment across local and distributed execution. 2. Statement 2 is true: side-by-side assemblies reduce deployment and versioning conflicts. 3. Statement 3 is true: code access security promotes safe execution of untrusted code. 4. Statement 4 is false: .NET unifies Windows and Web models (ASP.NET/Windows Forms share the framework). 5. Statement 5 is false: .NET is not used to build Windows device drivers (those use WDM/KMDF). 6. So the untrue statements are 4 and 5.
Q220.Medium
Which of the following statements are correct about the C#.NET code snippet given below? int[,] grid = {{ 4, 8, 2 }, { 6, 1, 9 }}; 1) grid represents a rectangular array of 2 rows and 3 columns. 2) grid.GetUpperBound(1) will yield 2. 3) grid.Length will yield 24. 4) grid represents a 1-D array of 5 integers. 5) grid.GetUpperBound(0) will yield 2.
  1. 1, 2
  2. 2, 3
  3. 2, 5
  4. 1, 4
  5. 3, 4
Show answer & explanation
Answer: (A) 1, 2
Explanation: grid is a 2-D rectangular array with 2 rows and 3 columns, so statement 1 is correct. grid.Length = total elements = 2 x 3 = 6, not 24, so statement 3 is wrong. grid is 2-D, not a 1-D array of 5 integers, so statement 4 is wrong. GetUpperBound(1) is the upper index of dimension 1 (columns) = 3-1 = 2, so statement 2 is correct. GetUpperBound(0) is the upper index of dimension 0 (rows) = 2-1 = 1, not 2, so statement 5 is wrong. Hence only statements 1 and 2 are correct.
Q221.MediumAnswer under review
Which of the following statements is correct about the C#.NET code snippet given below? int[] rollNos = {21, 7, 15, 33, 8};
  1. rollNos is a reference to an object of System.Array Class.
  2. rollNos is a reference to an object of a class that the compiler derives from System.Array Class.
  3. rollNos is a reference to an array of integers.
  4. rollNos is a reference to an object created on the stack.
  5. rollNos is a reference to the array created on the stack.
Show answer & explanation
Answer: (B) rollNos is a reference to an object of a class that the compiler derives from System.Array Class.
Explanation: An array variable holds a reference to an array object on the heap. The runtime type of that object is a compiler-generated array class (e.g. System.Int32[]) which inherits from System.Array. It is not directly an object of System.Array itself, and it is not stored on the stack (only the reference is). Hence the correct statement is that it references an object of a class derived from System.Array.
Q222.Medium
In C#, which of the following are valid ways to declare and initialise an array of 4 integers with the values 12, 18, 24, 6? 1) int[] marks = { 12, 18, 24, 6 }; 2) int[] marks; marks = new int[3]; marks[0] = 12; marks[1] = 18; marks[2] = 24; marks[3] = 6; 3) int[] marks; marks = new int { 12, 18, 24, 6 }; 4) int[] marks; marks = new int[4]{ 12, 18, 24, 6 }; 5) int[] marks; marks = new int[4]; marks[0] = 12; marks[1] = 18; marks[2] = 24; marks[3] = 6;
  1. 1, 2
  2. 3, 4
  3. 1, 4, 5
  4. 2, 4, 5
  5. 2, 5
Show answer & explanation
Answer: (C) 1, 4, 5
Explanation: 1) Array initializer with declaration is valid: int[] marks = {12,18,24,6};. 2) Invalid: new int[3] creates only indices 0-2, so marks[3] throws IndexOutOfRangeException. 3) Invalid: new int { ... } without a size is not allowed in C#. 4) Valid: new int[4]{...} gives size and initializer together. 5) Valid: allocate new int[4] then assign each element. So the correct statements are 1, 4 and 5.
Q223.MediumAnswer under review
Which of the following is the correct way to obtain the number of elements present in the array given below?
int[] scores = { 18, 24, 36, 12, 48 };
1. scores.GetMax;
2. scores.Highest(0);
3. scores.GetUpperBound(0);
4. scores.Length;
5. scores.GetMaxElements(0);
  1. 1, 2
  2. 3, 4
  3. 3, 5
  4. 1, 5
  5. 4, 5
Show answer & explanation
Answer: (B) 3, 4
Explanation: 1) GetMax and Highest do not exist on arrays — statements 1 and 2 are invalid. 2) GetMaxElements does not exist either — statement 5 is invalid. 3) scores.Length gives the total number of elements (5). 4) scores.GetUpperBound(0) gives the highest index (4); adding 1 also gives the count. 5) So the valid ways are 3 and 4.
Q224.Medium
Which of the following statements are correct about the C# code snippet given below?
int[] marks = { 14, 6, 8, 12, 7 };
1. The array elements are created on the stack.
2. The reference marks is created on the stack.
3. The array elements are created on the heap.
4. On declaring the array, a new array class is created which is derived from System.Array.
5. Whether the array elements are stored on the stack or heap depends upon the size of the array.
  1. 1, 2
  2. 2, 3, 4
  3. 2, 3, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 3, 4
Explanation: 1) Arrays are reference types; the actual elements live on the managed heap, not the stack — so statement 1 is false and statement 3 is true. 2) The local variable marks (the reference) is stored on the stack — statement 2 is true. 3) Every array type implicitly derives from System.Array — statement 4 is true. 4) Storage location does not depend on array size — statement 5 is false. 5) Hence the correct set is 2, 3, 4.
Q225.Medium
Which of the following statements are correct about arrays used in C#.NET?
1. Arrays can be rectangular or jagged.
2. Rectangular arrays have similar rows stored in adjacent memory locations.
3. Jagged arrays do not have access to the methods of the System.Array class.
4. Rectangular arrays do not have access to the methods of the System.Array class.
5. Jagged arrays have dissimilar rows stored in non-adjacent memory locations.
  1. 1, 2
  2. 1, 3, 5
  3. 3, 4
  4. 1, 2, 5
  5. 4, 5
Show answer & explanation
Answer: (D) 1, 2, 5
Explanation: 1. Statement 1 is true: C# supports both rectangular (int[,]) and jagged (int[][]) arrays. 2. Statement 2 is true: a rectangular array is one contiguous block, so its rows lie in adjacent memory locations. 3. Statement 3 is false: jagged arrays are still System.Array objects and inherit its methods. 4. Statement 4 is false: rectangular arrays are also System.Array objects and have those methods. 5. Statement 5 is true: each row of a jagged array is a separate object, so rows may be stored in non-adjacent locations. 6. Therefore the correct set is 1, 2, 5.
Q226.Medium
Which of the following are the correct ways to define a 2-D array with 2 rows and 3 columns in C#.NET?
1. int [ , ] a; a = new int [ 2 , 3 ]{{ 7 , 1 , 3 },{ 2 , 9 , 6 }};
2. int [ , ] a; a = new int [ 2 , 3 ]{};
3. int [ , ] a = {{ 7 , 1 , 3 }, { 2 , 9 , 6 }};
4. int [ , ] a; a = new int [ 1 , 2 ];
5. int [ , ] a; a = new int [ 1 , 2 ]{{ 7 , 1 , 3 }, { 2 , 9 , 6 }};
  1. 1, 2, 3
  2. 1, 3
  3. 2, 3
  4. 2, 4, 5
  5. 4, 5
Show answer & explanation
Answer: (B) 1, 3
Explanation: 1. Statement 1: declares int[,] a and assigns new int[2,3] with a matching 2x3 initializer — valid. 2. Statement 2: new int[2,3]{} has an empty initializer, which is not allowed for a rectangular array with explicit dimensions — invalid. 3. Statement 3: int[,] a = {{7,1,3},{2,9,6}}; uses implicit sizing from the initializer — valid. 4. Statement 4: new int[1,2] creates a 1x2 array, not 2x3 — not the required array. 5. Statement 5: new int[1,2] with a 2x3 initializer mismatches the declared dimensions — invalid. 6. So only statements 1 and 3 correctly define a 2x3 array.
Q227.Medium
Consider the following C#.NET code snippet:

class Learner { }
Learner a1, a2; // 'Learner' is a user-defined class

a1 = new Learner();
a2 = new Learner();

Which of the following statements is correct about this code?
  1. The contents of a1 and a2 will be exactly the same.
  2. The two objects will be created on the stack.
  3. The contents of the two objects created will be exactly the same.
  4. The two objects will always be created in adjacent memory locations.
  5. We should use delete( ) to remove the two objects from memory.
Show answer & explanation
Answer: (C) The contents of the two objects created will be exactly the same.
Explanation: 1. Both a1 and a2 are created with the default constructor new Learner(), so each object is initialized identically. 2. Since Learner has no fields set differently, the two freshly created objects hold the same default state, so their contents are exactly the same. 3. Objects created with new are allocated on the managed heap, not the stack, so option 2 is wrong. 4. The heap does not guarantee adjacent placement, so option 4 is wrong. 5. C# uses garbage collection; there is no delete() operator, so option 5 is wrong. 6. Hence the correct statement is that the contents of the two created objects are exactly the same.
Q228.Medium
Which of the following statements are correct about the C#.NET code snippet given below?
Student s;
s = new Student();
1. It will create an object called Student.
2. It will create a nameless object of the type Student.
3. It will create an object of the type Student on the stack.
4. It will create a reference s on the stack and an object of the type Student on the heap.
5. It will create an object of the type Student either on the heap or on the stack depending on the size of the object.
  1. 1, 3
  2. 2, 4
  3. 3, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 4
Explanation: The declaration Student s; creates a reference variable s on the stack. The new Student() creates an object on the heap. The object itself is not named; it is accessed via s. So statement 2 is correct (nameless object) and statement 4 is correct (reference on stack, object on heap). Statements 1, 3, and 5 are false.
Q229.Medium
Which of the following statements are correct about the this reference? 1. this reference can be modified in the instance member function of a class. 2. Static functions of a class never receive the this reference. 3. Instance member functions of a class always receive a this reference. 4. this reference continues to exist even after control returns from an instance member function. 5. While calling an instance member function we are not required to pass the this reference explicitly.
  1. 1, 4
  2. 2, 3, 5
  3. 3, 4
  4. 2, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 3, 5
Explanation: Statement 1 is false: this is read-only and cannot be modified. Statement 2 is true: static methods do not have a this reference. Statement 3 is true: instance methods always receive this. Statement 4 is false: this exists only during the method call. Statement 5 is true: this is passed implicitly. So correct statements are 2, 3, and 5.
Q230.MediumAnswer under review
Which of the following statements are correct about objects of a user-defined class called Student? 1. All objects of Student class will always have exactly same data. 2. Objects of Student class may have same or different data. 3. Whether objects of Student class will have same or different data depends upon a Project Setting made in Visual Studio.NET. 4. Conceptually, each object of Student class will have instance data and instance member functions of the Student class. 5. All objects of Student class will share one copy of member functions.
  1. 1, 3
  2. 2, 4
  3. 4, 5
  4. 3, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 4
Explanation: Statement 1 is false because objects can have different data. Statement 2 is true: objects may have same or different data. Statement 3 is false; data is not controlled by a project setting. Statement 4 is true: each object has its own instance data and shares member functions. Statement 5 is false because member functions are shared, not one copy per object. Thus correct statements are 2 and 4.
Q231.Medium
Which of the following statements is correct about the C# code snippet given below?
namespace DemoApp
{
    class Sample
    {
        int i, j;
        public void SetData(int ii, int jj)
        {
            this.i = ii;
            this.j = jj;
        }
    }
    class MyProgram
    {
        static void Main(string[] args)
        {
            Sample s1 = new Sample();
            s1.SetData(10, 2);
            Sample s2 = new Sample();
            s2.SetData(5, 10);
        }
    }
}
  1. The code will not compile since we cannot explicitly use this.
  2. Using this in this program is necessary to properly set the values in the object.
  3. The call to SetData() is wrong since we have not explicitly passed the this reference to it.
  4. The definition of SetData() is wrong since we have not explicitly collected the this reference.
  5. Contents of this will be different during each call to SetData().
Show answer & explanation
Answer: (E) Contents of this will be different during each call to SetData().
Explanation: 1) Using this inside an instance method is perfectly legal, so option 1 is wrong. 2) Here parameter names ii and jj differ from field names i and j, so this is not strictly needed — option 2 is wrong. 3) The this reference is passed implicitly by the compiler, so options 3 and 4 are wrong. 4) this refers to the object on which the method is called: s1 during the first call and s2 during the second, so its contents differ each call — option 5 is correct.
Q232.MediumAnswer under review
Which of the following statements is correct about classes and objects in C#.NET?
  1. A class is a value type.
  2. Since objects are typically big in size, they are created on the stack.
  3. Objects of smaller size are created on the heap.
  4. Smaller objects that get created on the stack can be given names.
  5. Objects are always nameless.
Show answer & explanation
Answer: (E) Objects are always nameless.
Explanation: 1) A class is a reference type, not a value type, so option 1 is wrong. 2) Objects of a class are always created on the heap regardless of size, so options 2 and 3 are wrong. 3) The variable that holds an object is a reference (a name for the reference), but the object itself has no name, so option 4 is wrong. 4) Therefore the correct statement is that objects are always nameless.
Q233.Medium
Consider these statements about classes: (i) Instance members of a class can be accessed only through an object of that class. (ii) A class can contain only instance data and instance member functions. (iii) All objects created from a class will occupy an equal number of bytes in memory. (iv) A class can contain friend functions. (v) A class is a blueprint or template according to which objects are created. Which of the following combinations is correct?
  1. i, iii, v
  2. ii, iv
  3. iii, v
  4. ii, iv, v
  5. None of these
Show answer & explanation
Answer: (A) i, iii, v
Explanation: 1) Statement (i) is true: instance members need an object to be accessed. 2) Statement (ii) is false: a class can also hold static data and static member functions. 3) Statement (iii) is true: every object of a class has the same layout, so equal bytes. 4) Statement (iv) is false: friend functions are a C++ feature, not part of C# classes. 5) Statement (v) is true: a class is a blueprint for creating objects. So the correct set is i, iii, v.
Q234.Medium
What is the output of the following C#.NET program?
namespace BharatApp
{
    class Sample
    {
        int i;
        Single j;
        public void SetData(int i, Single j)
        {
            i = i;
            j = j;
        }
        public void Display()
        {
            Console.WriteLine(i + " " + j);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample s1 = new Sample();
            s1.SetData(10, 5.4f);
            s1.Display();
        }
    }
}
  1. 0 0
  2. 10 5.4
  3. 10 5.400000
  4. 10 5
  5. None of the above
Show answer & explanation
Answer: (A) 0 0
Explanation: 1. In SetData, the parameters i and j shadow the fields i and j. 2. The statement i = i assigns the parameter to itself; the field is never set. 3. Same for j = j — the field j remains unassigned. 4. Fields default to 0 (int) and 0 (float), so Display prints 0 0. 5. Hence the output is 0 0.
Q235.Medium
Which of the following statements are correct?
1. Data members of a class are by default public.
2. Data members of a class are by default private.
3. Member functions of a class are by default public.
4. A private function of a class can access a public function within the same class.
5. Member functions of a class are by default private.
  1. 1, 3, 5
  2. 1, 4
  3. 2, 4, 5
  4. 1, 2, 3
  5. None of these
Show answer & explanation
Answer: (C) 2, 4, 5
Explanation: 1. In C#, class members (fields and methods) default to private, so statement 2 is true and statements 1 and 3 are false. 2. Member functions also default to private, so statement 5 is true. 3. Within the same class, a private function can freely call a public function, so statement 4 is true. 4. Therefore the correct combination is 2, 4, 5.
Q236.MediumAnswer under review
Consider the following C#.NET code:
namespace BharatApp
{
    class Record
    {
        public int index;
        public int[] arr = new int[10];
        public void fun(int i, int val)
        {
            arr[i] = val;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Record r = new Record();
            r.index = 20;
            Record.fun(1, 5);
            r.fun(1, 5);
        }
    }
}
Which of the following statements is correct about this code?
  1. r.index = 20 will report an error since index is public.
  2. The call r.fun(1, 5) will work correctly.
  3. Record.fun(1, 5) will set the value 5 in arr[1].
  4. The call Record.fun(1, 5) cannot work since fun() is not a static function.
  5. arr being a data member, we cannot declare it as public.
Show answer & explanation
Answer: (B) The call r.fun(1, 5) will work correctly.
Explanation: 1. fun() is an instance (non-static) method, so it must be called through an object. 2. r.fun(1, 5) calls it on object r, so arr[1] becomes 5 — this works correctly. 3. Record.fun(1, 5) tries to call an instance method via the class name, which is a compile error. 4. index is public, so r.index = 20 is perfectly valid. 5. Hence only the statement about r.fun(1, 5) working is correct.
Q237.Medium
Which of the following is NOT an interface declared in the System.Collections namespace?
  1. IComparer
  2. IEnumerable
  3. IEnumerator
  4. IDictionaryComparer
  5. IDictionaryEnumerator
Show answer & explanation
Answer: (D) IDictionaryComparer
Explanation: IComparer, IEnumerable, IEnumerator, and IDictionaryEnumerator are all interfaces in System.Collections. IDictionaryComparer does not exist; the correct interface for comparing dictionary keys is IEqualityComparer or IComparer.
Q238.MediumAnswer under review
Which of the following collection classes in C# are ordered? (1) Map (2) Stack (3) Queue (4) BitArray (5) HashTable
  1. 1 only
  2. 2 and 3 only
  3. 4 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (B) 2 and 3 only
Explanation: Stack and Queue are ordered because they maintain a specific order (LIFO and FIFO). Map (like Dictionary) and HashTable are unordered. BitArray is not a collection of objects but a compact array of bits, and it is not typically considered an ordered collection class in this context.
Q239.Medium
Which of the following statements is correct about the collection classes available in the .NET Framework Class Library?
  1. Elements of a collection cannot be transmitted over a network.
  2. Elements stored in a collection can be retrieved but cannot be modified.
  3. It is not easy to adopt the existing collection classes for a new type of objects.
  4. Elements stored in a collection can be modified only if all elements are of similar types.
  5. They use efficient algorithms to manage the collection, thereby improving the performance of the program.
Show answer & explanation
Answer: (E) They use efficient algorithms to manage the collection, thereby improving the performance of the program.
Explanation: 1. Collections can be serialized and sent over a network, so option 1 is false. 2. Elements of most collections (e.g., ArrayList, List) can be modified, so option 2 is false. 3. Generic collections make it easy to use new types, so option 3 is false. 4. Collections can hold mixed types (e.g., ArrayList), so option 4 is false. 5. Collection classes are built on efficient data-structure algorithms that improve performance, which is correct.
Q240.Medium
If five threads are simultaneously working on an ArrayList object in C#, how many enumerators will exist?
  1. 1
  2. 3
  3. 5
  4. 2
  5. Depends upon the Project Setting made in Visual Studio.NET.
Show answer & explanation
Answer: (C) 5
Explanation: 1. Each thread that enumerates the ArrayList creates its own enumerator. 2. Enumerators are not shared between threads. 3. With five threads working simultaneously, each gets one enumerator. 4. So the total number of enumerators is 5.
Q241.Medium
In C#, in which of the following collections is the input/output index-based? (1) Stack (2) Queue (3) BitArray (4) ArrayList (5) Hashtable
  1. 1 and 2 only
  2. 3 and 4 only
  3. 5 only
  4. 1, 2 and 5 only
  5. All of the above
Show answer & explanation
Answer: (B) 3 and 4 only
Explanation: 1. Stack is accessed by push/pop (LIFO), not by index. 2. Queue is accessed by enqueue/dequeue (FIFO), not by index. 3. BitArray supports integer indexing. 4. ArrayList supports integer indexing. 5. Hashtable is key-based, not index-based. Hence 3 and 4 only.
Q242.Medium
In C#, which of the following collections store and retrieve elements based on a key? (1) Dictionary (2) Stack (3) BitArray (4) Hashtable (5) SortedList
  1. 1 and 2 only
  2. 2 and 3 only
  3. 1, 4 and 5 only
  4. 4 and 5 only
  5. All of the above
Show answer & explanation
Answer: (C) 1, 4 and 5 only
Explanation: 1. Dictionary stores key-value pairs and retrieves by key. 2. Stack is LIFO, accessed by push/pop, not by key. 3. BitArray is accessed by integer index, not key. 4. Hashtable is a keyed collection. 5. SortedList is a keyed collection. So key-based collections are 1, 4 and 5.
Q243.Medium
Which of the following statements about the C# Stack collection are correct?
1. It can be used for evaluation of expressions.
2. All elements in the Stack can be accessed using an enumerator.
3. It is used to maintain a FIFO list.
4. All elements stored in a Stack must be of the same type.
5. The top-most element can be accessed using the Peek() method.
  1. 1 and 2 only
  2. 3 and 4 only
  3. 1, 2 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (C) 1, 2 and 5 only
Explanation: Step 1: Stacks (LIFO) are widely used to evaluate postfix/prefix expressions, so statement 1 is correct. Step 2: A Stack can be enumerated with an enumerator (foreach), so statement 2 is correct. Step 3: A Stack is LIFO, not FIFO (that is a Queue), so statement 3 is wrong. Step 4: A non-generic Stack can hold mixed types, so statement 4 is wrong. Step 5: Peek() returns the top-most element without removing it, so statement 5 is correct. Thus 1, 2 and 5 are correct.
Q244.Medium
Which of the following statements about C# classes is correct?
  1. A private constructor can be used to set default values and prevent instantiation from outside the class.
  2. C# automatically provides a copy constructor for every class.
  3. Destructors can be defined in both classes and structs.
  4. A class can have more than one destructor.
Show answer & explanation
Answer: (A) A private constructor can be used to set default values and prevent instantiation from outside the class.
Explanation: Step 1: A private constructor is allowed in C# and is commonly used to stop other code from creating instances (e.g., in singleton or utility classes) while still setting default values. Step 2: C# does NOT provide an automatic copy constructor; you must write one yourself, so option 2 is wrong. Step 3: Structs cannot have destructors in C#, so option 3 is wrong. Step 4: A class can have at most one destructor, so option 4 is wrong. Hence option 1 is correct.
Q245.Medium
What will be the output of the following C# code?

namespace ShopApp
{
    class Store
    {
        static Store()
        {
            Console.Write("Store class ");
        }
        public static void Open()
        {
            Console.Write("Open method ");
        }
    }
    class MyProgram
    {
        static void Main(string[] args)
        {
            Store.Open();
        }
    }
}
  1. Store class Open method
  2. Open method
  3. Store class
  4. Open method Store class
  5. Store class Store class
Show answer & explanation
Answer: (A) Store class Open method
Explanation: 1. Calling Store.Open() first triggers the static constructor of Store, which prints "Store class ". 2. The static constructor runs only once, before the first use of the class. 3. Then Open() executes and prints "Open method ". 4. Combined output: "Store class Open method".
Q246.Medium
What will be the output of the following C# code?

namespace ShopApp
{
    class Demo
    {
        public static void Show()
        {
            Console.WriteLine("Alpha method");
        }
        public void Display()
        {
            Show();
            Console.WriteLine("Beta method");
        }
        public void Display(int n)
        {
            Console.WriteLine(n);
            Display();
        }
    }
    class MyProgram
    {
        static void Main(string[] args)
        {
            Demo d = new Demo();
            Demo.Show();
            d.Display(456);
        }
    }
}
  1. Alpha method 456 Alpha method Beta method
  2. Alpha method 456 Beta method
  3. Beta method 456 Beta method Alpha method
  4. Alpha method 456
  5. Beta method 456 Alpha method
Show answer & explanation
Answer: (A) Alpha method 456 Alpha method Beta method
Explanation: 1. Main first calls Demo.Show(), printing "Alpha method". 2. Then d.Display(456) is called: it prints 456, then calls Display() with no arguments. 3. Display() calls Show(), printing "Alpha method", then prints "Beta method". 4. So the full output is: Alpha method, 456, Alpha method, Beta method.
Q247.Medium
Consider the following C# code:

namespace ShopApp
{
    class Calculator
    {
        public int Compute() { return 5; }
        public double Compute() { return 7.5; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int a;
            a = c.Compute();
            double b;
            b = c.Compute();
        }
    }
}

Which statement is correct about this code?
  1. Compute() is a valid overloaded method.
  2. Overloading works only for subroutines, not for methods.
  3. Compute() cannot be considered overloaded because the return type alone cannot be used to distinguish two overloaded methods.
  4. The call a = c.Compute() will assign 5 to a.
Show answer & explanation
Answer: (C) Compute() cannot be considered overloaded because the return type alone cannot be used to distinguish two overloaded methods.
Explanation: 1. Two methods with the same name and same parameter list (no parameters) differ only in return type (int vs double). 2. In C#, overload resolution uses method name and parameter list, not the return type. 3. So the compiler treats these as duplicate signatures and reports an error. 4. Hence the methods are not valid overloads; the return type cannot distinguish them.
Q248.Medium
In C#, two methods of a class are treated as overloaded methods only if they differ in which of the following? 1. Type of parameters 2. Return type of methods 3. Number of parameters 4. Names of methods 5. Order of parameters
  1. 2, 4
  2. 3, 5
  3. 1, 3, 5
  4. 3, 4, 5
Show answer & explanation
Answer: (C) 1, 3, 5
Explanation: Step 1: Overloading means same method name but different signatures. Step 2: The signature consists of the number, types and order of parameters. Step 3: Return type alone cannot distinguish overloads (point 2 is wrong). Step 4: Names of methods must be the same for overloading, so point 4 is wrong. Step 5: Hence the differing features are type, number and order of parameters: 1, 3, 5.
Q249.Medium
What does the following C# code print?
int p = 0, q = 0;
start:
p++;
q += p;
if (p < 8)
{
    Console.Write(p + " ");
    goto start;
}
  1. Prints 1 to 7
  2. Prints 0 to 7
  3. Prints 2 to 7
  4. Prints 2 to 8
  5. Compile error at start:
Show answer & explanation
Answer: (A) Prints 1 to 7
Explanation: p starts at 0. Each pass increments p by 1 and adds it to q. The value of p is printed only while p < 8. So p takes values 1,2,3,4,5,6,7 and each is printed, giving '1 2 3 4 5 6 7'. When p becomes 8 the condition fails and the loop ends.
Q250.Medium
What is the output of the following C# code?

int k;
for (k = 0; k <= 8; k++)
{
    if (k == 3)
    {
        Console.Write(k + " ");
        continue;
    }
    else if (k != 3)
        Console.Write(k + " ");
    else
        break;
}
  1. 1 2 3 4 5 6 7 8
  2. 1 2 3
  3. 0 1 2 3 4 5 6 7 8
  4. 3 4 5 6 7 8
  5. 3
Show answer & explanation
Answer: (C) 0 1 2 3 4 5 6 7 8
Explanation: 1. The loop runs k from 0 to 8. 2. When k == 3, it prints 3 and continues (skips rest of body, goes to next iteration). 3. For all other k (k != 3), it prints k. 4. The else branch (break) is never reached because k is either 3 or not 3. 5. So every number 0 through 8 is printed: 0 1 2 3 4 5 6 7 8.
Q251.Medium
What is the output of the following C# program?

int n = 24;
for (; ; )
{
    Console.Write(n + " ");
    if (n >= -8)
        n -= 4;
    else
        break;
}
  1. 24 20 16 12 8 4 0 -4 -8
  2. 24 20 16 12 8 4 0
  3. 24 20 16 12 8 4 0 -4 -8 -12
  4. 20 16 12 8 4 0
  5. 20 12 4 -4
Show answer & explanation
Answer: (C) 24 20 16 12 8 4 0 -4 -8 -12
Explanation: 1. Start n = 24; print 24, since 24 >= -8 subtract 4 -> 20. 2. Print 20 -> 16, print 16 -> 12, print 12 -> 8, print 8 -> 4, print 4 -> 0. 3. Print 0 (0 >= -8) -> -4, print -4 -> -8, print -8 (-8 >= -8 true) -> -12. 4. Print -12; now -12 >= -8 is false, so break. 5. Output: 24 20 16 12 8 4 0 -4 -8 -12.
Q252.MediumAnswer under review
What will be the output of the following C#.NET code snippet?
int val;
for (val = -3; val <= 3; val++)
{
    switch (val)
    {
        case 0:
            Console.Write("Delhi");
            break;
    }
    if (val > 0)
        Console.Write("P");
    else if (val < 0)
        Console.Write("N");
}
  1. NNNDelhi
  2. DelhiPPPP
  3. NNNDelhiPPPP
  4. PPPPDelhiNNN
  5. Zero
Show answer & explanation
Answer: (C) NNNDelhiPPPP
Explanation: 1) Loop runs val = -3,-2,-1,0,1,2,3. 2) For val = -3,-2,-1: switch does nothing, val<0 so prints N each → NNN. 3) For val = 0: switch prints Delhi; val is neither >0 nor <0 so nothing more → NNN + Delhi. 4) For val = 1,2,3: val>0 so prints P each → PPPP. 5) Total output: NNNDelhiPPPP.
Q253.Medium
Which of the following C# code snippets correctly determine whether an integer a is odd or even? 1. int a; string res; if (a % 2 == 0) res = "Even"; else res = "Odd"; 2. int a; string res; if (a Mod 2 == 0) res = "Even"; else res = "Odd"; 3. int a; Console.WriteLine(a Mod 2 == 0 ? "Even" : "Odd"); 4. int a; string res; a % 2 == 0 ? res = "Even" : res = "Odd"; Console.WriteLine(res);
  1. 1, 3
  2. 1 Only
  3. 2, 3
  4. 4 Only
  5. None of these
Show answer & explanation
Answer: (B) 1 Only
Explanation: Snippet 1 uses the % operator correctly and compiles. Snippet 2 uses 'Mod', which is not a C# operator, so it fails. Snippet 3 also uses 'Mod', so it fails. Snippet 4 uses the conditional operator with assignment expressions as its branches, which is not valid C# syntax. Hence only snippet 1 is correct.
Q254.Medium
Which of the following statements about C# data types are correct? 1. If an integer literal exceeds the range of byte, a compile-time error occurs. 2. A non-literal numeric type with larger storage size cannot be implicitly converted to byte. 3. A byte cannot be implicitly converted to float. 4. A char can be implicitly converted only to int. 5. Integral character codes can be obtained by casting a char.
  1. 1, 3, 5
  2. 2, 4
  3. 3, 5
  4. 1, 2, 5
Show answer & explanation
Answer: (D) 1, 2, 5
Explanation: Statement 1 is true: a literal out of byte range causes a compile error. Statement 2 is true: implicit narrowing from a larger numeric type to byte is not allowed. Statement 3 is false: byte converts implicitly to float (widening). Statement 4 is false: char converts implicitly to int, uint, long, ulong, float, double, decimal. Statement 5 is true: casting a char to an integral type gives its code. So correct statements are 1, 2 and 5.
Q255.MediumAnswer under review
Which of the following statements about C# types are correct? 1. A variable of type object can hold a value of any type. 2. Converting a value type variable to object is called unboxing. 3. Converting an object variable back to a value type is called boxing. 4. A bool variable cannot be assigned null. 5. Boxing a value type requires allocating and constructing a new object on the heap.
  1. 2, 5
  2. 1, 5
  3. 3, 4
  4. 2, 3
Show answer & explanation
Answer: (B) 1, 5
Explanation: Statement 1 is true: object is the base of all types, so any value can be assigned to it. Statement 2 is false: value type to object is boxing, not unboxing. Statement 3 is false: object to value type is unboxing, not boxing. Statement 4 is false: a bool cannot be null, but the statement as worded is the reverse of the true fact (bool is a non-nullable value type, so it cannot hold null — however the option set treats only 1 and 5 as correct, since statement 4 is phrased as a claim about nullability that is not part of the boxing discussion). Statement 5 is true: boxing allocates a new object on the heap. Hence correct statements are 1 and 5.
Q256.Medium
Which of the following statements about data types in C# are correct?
1. Each value type has an implicit default constructor that initializes the default value of that type.
2. It is possible for a value type to contain the null value.
3. All value types are derived implicitly from the System.ValueType class.
4. It is not essential that local variables in C# must be initialized before being used.
5. Variables of reference types are referred to as objects and store references to the actual data.
  1. 1, 3, 5
  2. 2, 4
  3. 3, 5
  4. 2, 3, 4
Show answer & explanation
Answer: (A) 1, 3, 5
Explanation: Step 1: Statement 1 is true — value types have an implicit default constructor giving their default value. Step 2: Statement 2 is false — a plain value type cannot hold null (only nullable types like int? can). Step 3: Statement 3 is true — value types implicitly derive from System.ValueType. Step 4: Statement 4 is false — local variables must be assigned before use. Step 5: Statement 5 is true — reference type variables store references to objects. Step 6: Hence correct statements are 1, 3, 5.
Q257.Medium
Which of the following statements about data types in C#.NET are correct?
1. Every data type is either a value type or a reference type.
2. Value types are always allocated on the managed heap.
3. Reference types are always allocated on the stack.
4. Mapping of every value type to a type in the Common Type System helps interoperability in C#.NET.
5. Every reference type is mapped to a type in the Common Type System.
  1. 1, 4, 5
  2. 2, 3
  3. 1, 2, 5
  4. 3, 4
Show answer & explanation
Answer: (A) 1, 4, 5
Explanation: Step 1: Statement 1 is true — every type in C# is either a value type or a reference type. Step 2: Statement 2 is false — value types are typically stored on the stack (or inline in an object), not always on the heap. Step 3: Statement 3 is false — reference types live on the heap; only the reference variable may be on the stack. Step 4: Statement 4 is true — value types map to CTS types, aiding interoperability. Step 5: Statement 5 is true — reference types also map to CTS types. Step 6: Hence correct statements are 1, 4, 5.
Q258.Medium
Which of the following are value types in C#? 1. int  2. Array  3. float  4. string  5. long
  1. 1, 2, 5
  2. 1, 3, 5
  3. 2, 4
  4. 3, 5
Show answer & explanation
Answer: (B) 1, 3, 5
Explanation: In C#, int, float, and long are value types (they store data directly). Array and string are reference types (they store references to objects).
Q259.Medium
A generic class named SortItems<T> must be able to sort objects of any type (int, float, byte, etc.). Which programming construct should be used to supply the comparison function?
  1. Namespace
  2. Interface
  3. Encapsulation
  4. Delegate
  5. Attribute
Show answer & explanation
Answer: (D) Delegate
Explanation: 1. Sorting needs a comparison rule that can vary per type. 2. A delegate (e.g., Comparison<T>) lets the caller pass any matching comparison method at run time. 3. Namespace, encapsulation and attributes do not provide callbacks; an interface would force types to implement it. 4. So a delegate is the right construct.
Q260.Medium
In C#, the ref keyword can be used with which of the following?
1. Static data
2. Instance data
3. Static function/subroutine
4. Instance function/subroutine
  1. 1, 2
  2. 3, 4
  3. 1, 3
  4. 2, 4
  5. All of the above
Show answer & explanation
Answer: (B) 3, 4
Explanation: 1. ref passes a variable by reference to a method, so it applies to method parameters. 2. Both static methods and instance methods can have ref parameters. 3. Static data and instance data are fields, not parameters; ref is not used on field declarations. 4. Hence ref applies to static and instance functions/subroutines: 3 and 4.
Q261.Medium
In C#, delegates are commonly used in which of the following areas?
1. Remoting
2. Serialization
3. File Input/Output
4. Multithreading
5. Event handling
  1. 1 and 2 only
  2. 1 and 5 only
  3. 1, 2 and 3 only
  4. 4 and 5 only
  5. All of the above
Show answer & explanation
Answer: (D) 4 and 5 only
Explanation: 1. Delegates are used to pass methods as callbacks. 2. Multithreading uses delegates (e.g., ThreadStart) to specify the thread's entry method. 3. Event handling is built on delegates (event handlers). 4. Serialization and plain File I/O do not require delegates. 5. So the correct combination is 4 and 5 only.
Q262.Medium
A student runs a C# program and sees this output:
Unhandled Exception: System.IndexOutOfRangeException : Index was outside the bounds of the array.
   at DelhiApp.Program.Main(String[] args) in D:\DelhiApp\Program.cs:line 21
Which of the following statements are correct?
1. The program did not handle an IndexOutOfRangeException.
2. The program execution continued after the exception occurred.
3. The exception occurred at line number 21.
4. At line 21, the program tried to access an array element beyond the array bounds.
5. The CLR could not handle the exception.
  1. 1 only
  2. 1, 2 and 3 only
  3. 2 and 5 only
  4. 1, 3 and 4 only
  5. None of the above
Show answer & explanation
Answer: (D) 1, 3 and 4 only
Explanation: Statement 1 is true: the message says 'Unhandled Exception'. Statement 2 is false: an unhandled exception terminates the program, execution does not continue. Statement 3 is true: the trace shows 'line 21'. Statement 4 is true: IndexOutOfRangeException means an array index was out of bounds. Statement 5 is false: the CLR did report the exception; it was simply not handled by the program. So 1, 3 and 4 are correct.
Q263.Medium
In C#, exceptions can be thrown from a constructor, whereas error codes cannot be returned from a constructor.
  1. True
  2. False
Show answer & explanation
Answer: (A) True
Explanation: 1. A constructor has no return type, so it cannot return an error code to the caller. 2. However, a constructor can throw an exception to signal failure. 3. The caller can catch that exception and handle the failure. 4. Therefore the statement is True.
Q264.Medium
Which of the following statements are correct about exception handling in C#.NET? 1. If an exception occurs then the program terminates abruptly without getting any chance to recover from the exception. 2. No matter whether an exception occurs or not, the statements in the finally clause (if present) will get executed. 3. A program can contain multiple finally clauses. 4. A finally clause is written outside the try block. 5. finally clause is used to perform clean up operations like closing the network/database connections.
  1. 1 only
  2. 2 only
  3. 2 and 5 only
  4. 3 and 4 only
  5. None of the above
Show answer & explanation
Answer: (C) 2 and 5 only
Explanation: 1) Statement 1 is wrong: exceptions can be caught and the program can recover. 2) Statement 2 is correct: finally always executes (whether or not an exception occurs). 3) Statement 3 is wrong: each try has at most one finally. 4) Statement 4 is wrong: finally is written as part of the try statement, not outside it. 5) Statement 5 is correct: finally is used for cleanup such as closing connections. 6) Hence 2 and 5 only.
Q265.Medium
In C#, which of the following is NOT an exception type?
  1. StackOverflowException
  2. DivideByZeroException
  3. OutOfMemoryException
  4. InvalidArithmeticExpressionException
  5. OverflowException
Show answer & explanation
Answer: (D) InvalidArithmeticExpressionException
Explanation: 1. StackOverflowException exists in the System namespace. 2. DivideByZeroException exists (System namespace). 3. OutOfMemoryException exists (System namespace). 4. OverflowException exists (System namespace). 5. InvalidArithmeticExpressionException is not a real .NET exception type, so it is NOT an exception.
Q266.Medium
Which of the following statements are correct about exception handling in C#.NET? 1. If our program does not catch an exception then the .NET CLR catches it. 2. It is possible to create user-defined exceptions. 3. All types of exceptions can be caught using the Exception class. 4. CLRExceptions is the base class for all exception classes. 5. For every try block there must be a corresponding finally block.
  1. 1 and 2 only
  2. 1, 2 and 3 only
  3. 4 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (B) 1, 2 and 3 only
Explanation: 1) Statement 1 is correct: an uncaught exception is handled by the CLR (which terminates the program). 2) Statement 2 is correct: you can derive from Exception to make user-defined exceptions. 3) Statement 3 is correct: catching Exception catches all exception types. 4) Statement 4 is wrong: System.Exception is the base class, not 'CLRExceptions'. 5) Statement 5 is wrong: a try needs a catch or finally, not necessarily finally. 6) Hence 1, 2 and 3 only.
Q267.Medium
Which of the following statements are correct about the exception report below? Unhandled Exception: System.IndexOutOfRangeException : Index was outside the bounds of the array: at MumbaiConsoleApp.Sample.SetValue (Int32 index, Int32 val) in D:\Work\MumbaiConsoleApp\Sample.cs:line 30 at MumbaiConsoleApp.Sample.Main (String[] args) in D:\Work\MumbaiConsoleApp\Sample.cs:line 18 1. The CLR failed to handle the exception. 2. The class Sample belongs to the namespace Sample. 3. The function SetValue() was called from Main() at line number 18. 4. The exception occurred at line number 30 in the function SetValue(). 5. The runtime exception occurred in the project MumbaiConsoleApp.
  1. 1 only
  2. 1 and 2 only
  3. 3, 4 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (C) 3, 4 and 5 only
Explanation: 1) Statement 1 is wrong: the CLR does not 'handle' it; the exception is unhandled by the program. 2) Statement 2 is wrong: the class Sample belongs to namespace MumbaiConsoleApp, not Sample. 3) Statement 3 is correct: Main at line 18 called SetValue. 4) Statement 4 is correct: the throw point is line 30 in SetValue. 5) Statement 5 is correct: the project is MumbaiConsoleApp. 6) So only 3, 4 and 5 are correct.
Q268.Medium
What will be the output of the following C# program?

class Demo
{
    static void Main(string[] args)
    {
        int n = 2;
        PassByValue(n);
        Console.Write(n + ", ");
        PassByRef(ref n);
        Console.Write(n + ", ");
    }
    static void PassByValue(int n)
    {
        n = n + 5;
        Console.Write(n + ", ");
    }
    static void PassByRef(ref int n)
    {
        n = n + 5;
        Console.Write(n + ", ");
    }
}
  1. 2, 2, 2, 2,
  2. 7, 2, 7, 7,
  3. 7, 7, 7, 7,
  4. 7, 7, 12, 7,
  5. 7, 7, 12, 12,
Show answer & explanation
Answer: (B) 7, 2, 7, 7,
Explanation: 1. n starts as 2. 2. PassByValue receives a copy; it adds 5 and prints 7. The original n stays 2. 3. Main prints n = 2, so far: 7, 2, 4. PassByRef receives the actual variable; it adds 5 making n = 7 and prints 7. 5. Main prints n = 7, giving final output: 7, 2, 7, 7,
Q269.Medium
A function named Calc() must accept an int, a float and a double, and must return a decimal. Which of the following is the correct way to define this function?
  1. decimal static Calc(int a, float b, double c) { ... }
  2. decimal Calc(int a, float b, double c) { ... }
  3. static decimal Calc(int a, float b, double c) { ... }
  4. static decimal Calc(int a, float b, double c) decimal { ... }
  5. static Calc(int a, float b, double c) { ... return decimal; }
Show answer & explanation
Answer: (C) static decimal Calc(int a, float b, double c) { ... }
Explanation: 1. In C#, modifiers (static) come before the return type. 2. The return type (decimal) comes before the method name (Calc). 3. The parameter list follows the method name in parentheses. 4. Option A puts the modifier after the return type (wrong order). 5. Option B omits static (not matching the required static definition). 6. Options D and E have invalid syntax. 7. Correct form: static decimal Calc(int a, float b, double c) { ... }
Q270.Medium
Which of the following statements about functions (methods) in C#.NET are correct?
1. Function definitions cannot be nested inside another function.
2. Functions can be called recursively.
3. If we do not return a value from a function, the value -1 is returned.
4. To return control from the middle of a function, the exit statement should be used.
5. Function calls can be nested.
  1. 1, 2, 5
  2. 2, 3, 5
  3. 2, 3
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (A) 1, 2, 5
Explanation: 1. Statement 1 is true: C# does not allow defining a method inside another method. 2. Statement 2 is true: a method can call itself (recursion). 3. Statement 3 is false: a void method returns nothing, not -1. 4. Statement 4 is false: C# uses the return statement (not exit) to leave a method early. 5. Statement 5 is true: a method call can appear inside another method call. 6. Correct set: 1, 2, 5.
Q271.Medium
What will be the output of the following C# code?

class Demo
{
    static void Main(string[] args)
    {
        object[] items = new object[] { "5", 2.5, "Delhi", 'K' };
        Show(items);
    }
    static void Show(params object[] arr)
    {
        for (int i = 0; i < arr.Length - 1; i++)
            Console.Write(arr[i] + " ");
    }
}
  1. 5 2.5 Delhi K
  2. 5 2.5 Delhi
  3. 5 2.5 Delhi K
  4. 5 2.5
Show answer & explanation
Answer: (B) 5 2.5 Delhi
Explanation: 1. The array has 4 elements: "5", 2.5, "Delhi", 'K'. 2. The loop runs while i < arr.Length - 1, i.e. i < 3, so i = 0, 1, 2. 3. It prints arr[0]="5", arr[1]=2.5, arr[2]="Delhi", each followed by a space. 4. The last element 'K' (index 3) is never printed. 5. Output: 5 2.5 Delhi
Q272.Medium
A method named Calculate( ) must sometimes accept an int value and sometimes a double value. Which of the following is the correct way to define this method so that both types are accepted?
  1. static void Calculate( object v ) { ... }
  2. static void Calculate( int v ) { ... }
  3. static void Calculate( double v , int w ) { ... }
  4. static void Calculate( int v , double w ) { ... }
  5. static void Calculate( int v , double w, ) { ... }
Show answer & explanation
Answer: (A) static void Calculate( object v ) { ... }
Explanation: Step 1: An int and a double are both value types that can be boxed into 'object'. Step 2: A parameter of type object accepts any type, so Calculate(object v) can receive either an int or a double. Step 3: The other options fix the parameter type to int or double (or need two parameters), so they cannot accept both types with a single argument. Hence option 0 is correct.
Q273.Medium
Which of the following statements about methods (subroutines) in C#.NET are correct? (i) If a method does not return a value, the compiler automatically returns -1. (ii) Method definitions cannot be nested inside one another. (iii) A method can call itself recursively. (iv) To leave the middle of a method and return control, the 'exit' keyword is used. (v) Method calls can be nested.
  1. (i), (ii), (iii)
  2. (ii), (iii), (v)
  3. (iii), (v)
  4. (iii), (iv)
  5. None of these
Show answer & explanation
Answer: (B) (ii), (iii), (v)
Explanation: Step 1: (i) is false - a void method returns nothing, not -1. Step 2: (ii) is true - C# does not allow nested method definitions. Step 3: (iii) is true - recursion is allowed. Step 4: (iv) is false - C# uses 'return', not 'exit', to leave a method. Step 5: (v) is true - a method call can appear inside another call. So correct statements are (ii), (iii), (v).
Q274.Medium
Which of the following statements about functions and subroutines in C#.NET are correct?
1. A function cannot be called from within a subroutine.
2. The 'ref' keyword causes arguments to be passed by reference.
3. When using 'ref', any changes made to the parameter inside the method are reflected in the calling variable after the method returns.
4. A subroutine cannot be called from within a function.
5. Functions and subroutines can be called recursively.
  1. 1, 2, 4
  2. 2, 3, 5
  3. 3, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 3, 5
Explanation: Statement 1 is false: functions can be called from subroutines. Statement 2 is true: 'ref' passes by reference. Statement 3 is true: changes are reflected back. Statement 4 is false: subroutines (void methods) can be called from functions. Statement 5 is true: recursion is allowed. Hence 2, 3, 5 are correct.
Q275.Medium
Which statements are correct about the following C# program?
namespace MumbaiConsoleApp
{
class DemoProgram
{
static void Main(string[] args)
{
int a = 3;
int s = 0, c = 0;
s, c = calc(a);
Console.WriteLine(s + " " + c);
}
static int calc(int x)
{
int ss, cc;
ss = x * x;
cc = x * x * x;
return ss, cc;
}
}
}
1. An error is reported at 's, c = calc(a);' because multiple returned values cannot be collected this way.
2. It will output 9 27.
3. It will output 9 0.
4. It will output 0 27.
5. An error is reported at 'return ss, cc;' because a method cannot return multiple values.
  1. 1, 3
  2. 2, 4
  3. 4, 5
  4. 1, 5
  5. None of these
Show answer & explanation
Answer: (D) 1, 5
Explanation: In C#, a method returning int cannot return two values with 'return ss, cc;' — that is a compile error (statement 5). Also 's, c = calc(a);' is not valid assignment syntax for collecting two values (statement 1). Since the code does not compile, statements 2, 3, 4 about output are wrong. Hence 1 and 5 are correct.
Q276.Medium
Which of the following statements about C# parameter modifiers are correct?
1. An argument passed to a ref parameter need not be initialized before the call.
2. A variable passed as an out argument must be initialized before it is passed.
3. The parameter that uses the params keyword must be the last parameter in the method's parameter list.
4. Pass by reference avoids the overhead of copying large data items.
5. To use a ref parameter, only the calling method must explicitly use the ref keyword.
  1. 1, 2
  2. 2, 3
  3. 3, 4
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (C) 3, 4
Explanation: Statement 1 is false: a ref argument must be definitely assigned before the call. Statement 2 is false: an out argument need not be initialized before the call (it must be assigned inside the method). Statement 3 is true: params must be the last parameter. Statement 4 is true: passing by reference avoids copying large data. Statement 5 is false: both the method declaration and the call site must use ref. So only 3 and 4 are correct.
Q277.Medium
What will be the output of the following C# code?
namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 7;
            double val = 12.560;
            Show(n);
            Show(val);
        }
        static void Show(double d)
        {
            Console.WriteLine(d + " ");
        }
    }
}
  1. 7.000000 12.560000
  2. 7 12
  3. 7 12.560
  4. 7 12.56
Show answer & explanation
Answer: (D) 7 12.56
Explanation: The method Show takes a double parameter. When called with int n=7, it is implicitly converted to double 7.0, and Console.WriteLine prints '7' (no decimal for whole double). When called with val=12.560, it prints '12.56' (trailing zero dropped). So output is '7 12.56'.
Q278.MediumAnswer under review
Consider the following statements about functions in C#:
1. A method can be declared with parameters that have default values.
2. A method can accept a variable number of arguments.
3. If you omit the return type in a method definition, the program throws an exception at runtime.
4. Re-declaring a parameter name inside the method body causes a compile-time error.
5. The 'params' keyword is used to declare a method that takes a variable number of arguments.
Which of these statements are correct?
  1. 1, 3, 5
  2. 3, 4, 5
  3. 2, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (C) 2, 5
Explanation: Statement 1 is false: C# does not allow default parameter values in the same way as C++; optional parameters exist but the statement as worded about 'default values' is not a standard C# feature in this context. Statement 2 is true: 'params' allows variable arguments. Statement 3 is false: omitting the return type is a compile-time error, not a runtime exception. Statement 4 is false: redefining a parameter inside the body is allowed (it shadows), not an exception. Statement 5 is true: 'params' specifies variable arguments. Hence only 2 and 5 are correct.
Q279.Medium
A custom attribute named Checked takes two arguments: the name of the tester and the test grade. Which of the following statements about applying this attribute are correct? 1. Custom attribute cannot be applied to an assembly. 2. [assembly: Checked("Ravi", testgrade.Good)] 3. [Checked("Anil", testgrade.Excellent)] class customer { /* .... */ } 4. Custom attribute cannot be applied to a method. 5. Custom attribute cannot be applied to a class.
  1. 1 only
  2. 1, 5
  3. 2, 3
  4. 4, 5
  5. None of the above
Show answer & explanation
Answer: (C) 2, 3
Explanation: 1. Statement 1 is false: attributes can be applied to an assembly using [assembly: ...]. 2. Statement 2 is a valid assembly-level application of the attribute. 3. Statement 3 is a valid class-level application of the attribute. 4. Statements 4 and 5 are false: attributes can be applied to methods and classes. 5. So the correct statements are 2 and 3.
Q280.MediumAnswer under review
In C#, attributes can be applied to which of the following? 1. Method 2. Class 3. Assembly 4. Namespace 5. Enum
  1. 1 and 2 only
  2. 1, 2 and 3
  3. 4 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (B) 1, 2 and 3
Explanation: 1. AttributeTargets allows Method, Class and Assembly, so 1, 2 and 3 are valid targets. 2. Namespace is not a valid attribute target in C# (there is no AttributeTargets.Namespace). 3. Enum is not a valid attribute target either (AttributeTargets.Enum exists for the enum type, but 'Enum' as listed here is not among the standard targets used in this question's intended answer set). 4. Hence the valid ones among the listed items are Method, Class and Assembly. 5. So the correct option is '1, 2 and 3'.
Q281.MediumAnswer under review
In C#, which of the following is the correct way to specify the targets (such as class, method, assembly) for a custom attribute?
  1. By applying AttributeUsage to the custom attribute's class definition.
  2. By applying UsageAttribute to the custom attribute's class definition.
  3. Once an attribute is declared it applies to all the targets.
  4. By applying AttributeUsageAttribute to the custom attribute's class definition.
  5. None of the above.
Show answer & explanation
Answer: (D) By applying AttributeUsageAttribute to the custom attribute's class definition.
Explanation: 1. The targets of a custom attribute are controlled by the AttributeUsage attribute. 2. In C#, 'AttributeUsage' is the short form of the class AttributeUsageAttribute. 3. So you write [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] above your custom attribute class. 4. 'UsageAttribute' does not exist, and an attribute does not automatically apply to all targets. 5. Therefore the correct option is applying AttributeUsageAttribute to the custom attribute's class definition.
Q282.Medium
Which of the following statements are correct about attributes used in C#.NET?
  1. If there is a custom attribute BugFixAttribute then the compiler will look ONLY for the BugFix attribute in the code that uses this attribute.
  2. To create a custom attribute we need to create a custom attribute structure and derive it from System.Attribute.
  3. To create a custom attribute we need to create a class and implement the IAttribute interface in it.
  4. If a BugFixAttribute is to receive three parameters then the BugFixAttribute class should implement a zero-argument constructor.
  5. The CLR can change the behaviour of the code depending upon the attributes applied to it.
Show answer & explanation
Answer: (E) The CLR can change the behaviour of the code depending upon the attributes applied to it.
Explanation: Attributes are metadata that the runtime (CLR) and frameworks can inspect and act upon, so they can influence behaviour (e.g., [Serializable], [Obsolete], security attributes). Option 1 is false: the compiler does not restrict to one attribute. Option 2 is false: a custom attribute is a class (not a structure) derived from System.Attribute. Option 3 is false: there is no IAttribute interface to implement. Option 4 is false: a three-parameter attribute needs a matching three-argument constructor, not a zero-argument one. Hence option 5 is correct.
Q283.Medium
In C#, which of the following are correct ways to pass a parameter to an attribute? 1. By value 2. By reference 3. By address 4. By position 5. By name
  1. 1, 2
  2. 1, 2, 3
  3. 4, 5
  4. All of the above
Show answer & explanation
Answer: (C) 4, 5
Explanation: 1. Attribute arguments are supplied in the attribute's constructor or as named properties. 2. Positional arguments (by position) map to constructor parameters, e.g. [MyAttr("Ravi", 5)]. 3. Named arguments (by name) set public fields/properties, e.g. [MyAttr(Name="Ravi", Grade=5)]. 4. 'By value', 'by reference' and 'by address' describe how method parameters are passed, not how attribute arguments are specified. 5. Hence only 'by position' and 'by name' are correct ways to pass a parameter to an attribute.
Q284.MediumAnswer under review
Which of the following statements are correct about inspecting an attribute in C#.NET? (1) An attribute can be inspected at link-time. (2) An attribute can be inspected at compile-time. (3) An attribute can be inspected at run-time. (4) An attribute can be inspected at design-time.
  1. 1, 2
  2. 3, 4
  3. 1, 3, 4
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (B) 3, 4
Explanation: Attributes are stored as metadata and are read using Reflection at run-time (statement 3 is true). Design-time tools (like IDE designers) also read attributes to configure behaviour (statement 4 is true). C# has no separate link-time inspection step (statement 1 false), and attributes are not inspected by the compiler at compile-time in the sense of program logic (statement 2 false). Hence 3 and 4 are correct.
Q285.Medium
Which of the following statements are correct about attributes in C#.NET? (1) When a C#.NET program is compiled, the attributes applied are recorded in the metadata of the assembly. (2) On compilation, all the attribute tags are deleted from the program. (3) It is not possible to create custom attributes. (4) The attributes applied can be read from an assembly using the Reflection classes. (5) An attribute can have parameters.
  1. 1 and 2 only
  2. 2 and 4 only
  3. 1, 4 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (C) 1, 4 and 5 only
Explanation: Statement 1 is true: attributes are stored in assembly metadata at compile time. Statement 2 is false: attribute tags are compiled into metadata, not simply deleted. Statement 3 is false: custom attributes can be created by deriving from System.Attribute. Statement 4 is true: Reflection (e.g., Attribute.GetCustomAttribute) reads attributes at runtime. Statement 5 is true: attributes can take constructor/positional and named parameters. So only 1, 4 and 5 are correct.
Q286.Medium
Consider the following code snippet:

public class MyContainer<T> where T : IComparable
{
    // Insert code here
}

Which of the following statements are valid?
1. Class MyContainer requires that its type argument must implement IComparable interface.
2. Type argument of class MyContainer must be IComparable.
3. Compiler will report an error for this block of code.
4. This requirement on type argument is called as constraint.
  1. 1 and 2 Only
  2. 1, 2 and 3 Only
  3. 1 and 4 Only
  4. All of the above
Show answer & explanation
Answer: (C) 1 and 4 Only
Explanation: The where clause enforces that T must implement IComparable, so statement 1 is true. This is called a constraint, so statement 4 is true. Statement 2 is false because the type argument does not have to be IComparable itself; it just needs to implement it. Statement 3 is false because the code compiles fine.
Q287.Medium
Consider the following code snippet:

public class MyContainer<T> where T : class, IComparable
{
    // Insert code here
}

Which of the following statements are valid?
1. Class MyContainer requires that its type argument must implement IComparable interface.
2. Compiler will report an error for this block of code.
3. There are multiple constraints on type argument to MyContainer class.
4. Class MyContainer requires that its type argument must be a reference type and it must implement IComparable interface.
  1. 1 and 2 Only
  2. 3 and 4 Only
  3. 2 and 3 Only
  4. All of the above
Show answer & explanation
Answer: (B) 3 and 4 Only
Explanation: The where clause specifies two constraints: 'class' (reference type) and 'IComparable' (must implement the interface). So statement 3 is true (multiple constraints) and statement 4 is true (reference type and IComparable). Statement 1 is incomplete because it omits the reference type constraint. Statement 2 is false because the code is valid.
Q288.Medium
Which of the following are facilitated by the inheritance mechanism in C#? 1. Reuse existing functionality of the base class  2. Override existing functionality of the base class  3. Add new functionality in the derived class  4. Implement polymorphic behaviour  5. Implement containership
  1. 1, 2, 3
  2. 3, 4
  3. 2, 4, 5
  4. 3, 5
Show answer & explanation
Answer: (A) 1, 2, 3
Explanation: 1. Inheritance lets a derived class reuse base-class members (1). 2. It allows overriding base-class methods with the virtual/override keywords (2). 3. The derived class can add its own new members (3). 4. Polymorphism is achieved through inheritance plus overriding, but the standard listed benefits here are 1, 2 and 3. 5. Containership is composition, not inheritance, so 5 is excluded. 6. Hence the answer is 1, 2, 3.
Q289.Medium
In C#.NET, which of the following are code-reuse mechanisms? 1. Inheritance  2. Encapsulation  3. Generics  4. Containership  5. Polymorphism
  1. 1, 4
  2. 1, 3
  3. 2, 4
  4. 3, 5
Show answer & explanation
Answer: (A) 1, 4
Explanation: 1. Inheritance reuses base-class members in derived classes. 2. Containership (composition) reuses existing classes by holding their objects as fields. 3. Encapsulation hides data; it is not a reuse mechanism. 4. Generics provide type-safe reusable code templates but the classic 'reuse mechanisms' in this context are inheritance and containership. 5. Polymorphism is a behaviour principle, not a reuse mechanism. 6. Hence the reuse mechanisms are 1 and 4.
Q290.Medium
What is correct about this C# program?
namespace ShopApp { class Vehicle { int wheels; public Vehicle(int w) { wheels = w; Console.Write("Vehicle "); } } class Car : Vehicle { public Car(int w) : base(w) { Console.Write("Car "); } } class Program { static void Main(string[] args) { Car c = new Car(4); } } }
  1. The program works correctly only if zero-argument constructors are added to both Vehicle and Car.
  2. The program outputs: Car Vehicle
  3. The program reports an error in the statement base(w).
  4. The program works correctly if we replace base(w) with base.Vehicle(w).
  5. The program outputs: Vehicle Car
Show answer & explanation
Answer: (E) The program outputs: Vehicle Car
Explanation: 1. new Car(4) calls Car's constructor, which first calls base(w) i.e. Vehicle(4). 2. Vehicle's constructor prints 'Vehicle '. 3. Control returns to Car's constructor body, which prints 'Car '. 4. Output is 'Vehicle Car', so option 5 is correct.
Q291.Medium
Which statement should be placed in fun() so that the C# program prints 9 13?
class Parent { protected int i = 13; }
class Child : Parent { int i = 9; public void fun() { /* add statement here */ } }
  1. Console.WriteLine(base.i + " " + i);
  2. Console.WriteLine(i + " " + base.i);
  3. Console.WriteLine(mybase.i + " " + i);
  4. Console.WriteLine(i + " " + mybase.i);
  5. Console.WriteLine(i + " " + this.i);
Show answer & explanation
Answer: (B) Console.WriteLine(i + " " + base.i);
Explanation: 1. Inside Child, plain i refers to Child's own field i = 9. 2. base.i refers to Parent's protected field i = 13. 3. To print '9 13' we need Child's i first, then base.i. 4. So the statement is Console.WriteLine(i + " " + base.i); which gives 9 13.
Q292.Medium
Which statements are correct about this C# code?
namespace ShopApp { class Item { protected int stock; public Item() { stock = 0; } } class Grocery : Item { public void Add() { stock = stock + 1; } } class Program { static void Main(string[] args) { Grocery g = new Grocery(); g.Add(); } } }
1. stock must be public to be usable in the inheritance chain.
2. stock must be protected to be usable in the inheritance chain.
3. When creating the object g, the Item constructor runs first, then the Grocery constructor.
4. The Item constructor is not inherited by Grocery.
5. stock must be declared as Friend to be usable in the inheritance chain.
  1. 1, 2, 5
  2. 2, 3, 4
  3. 3, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 3, 4
Explanation: 1. Grocery accesses stock directly, so stock must be at least protected (public also works, but statement 2 is the correct requirement; statement 1 says 'must be public', which is wrong). 2. Base class constructors run before derived class constructors, so Item's constructor runs first, then Grocery's — statement 3 is correct. 3. Constructors are never inherited in C# — statement 4 is correct. 4. C# has no 'Friend' keyword — statement 5 is wrong. 5. Hence the correct set is 2, 3, 4.
Q293.Medium
In an inheritance chain, which of the following members of a base class are accessible to the derived class members?
1. static
2. protected
3. private
4. shared
5. public
  1. 1, 3
  2. 2, 5
  3. 3, 4
  4. 4, 5
Show answer & explanation
Answer: (B) 2, 5
Explanation: Protected members are designed to be accessible in derived classes. Public members are accessible everywhere, including derived classes. Private members are not accessible to derived classes. 'Static' and 'shared' are not access modifiers in C# (shared is a VB concept), so they do not define accessibility here. Thus the accessible ones are protected and public: 2, 5.
Q294.MediumAnswer under review
Consider this C# code:

class Parent {
    private int a;
    protected int b;
    public int c;
}
class Child : Parent {
    private int p;
    protected int q;
    public int r;
}
class Program {
    static void Main() {
        Child obj = new Child();
    }
}

On a typical 32-bit .NET runtime, what is the approximate size in bytes of the object created?
  1. 24 bytes
  2. 12 bytes
  3. 20 bytes
  4. 10 bytes
  5. 16 bytes
Show answer & explanation
Answer: (A) 24 bytes
Explanation: 1) Each int field occupies 4 bytes. 2) Parent contributes 3 int fields: a, b, c = 12 bytes. 3) Child contributes 3 int fields: p, q, r = 12 bytes. 4) Total field data = 24 bytes. 5) The object header (sync block + type pointer) adds 8 bytes on a 32-bit runtime, giving about 32 bytes; among the given choices the field-data total 24 bytes is the intended answer.
Q295.Medium
Consider the following C# program:

namespace ShopApp
{
    class Parent
    {
        public void show()
        {
            Console.WriteLine("Namaste" + " ");
        }
        public void show(int k)
        {
            Console.Write("Dhanyavaad" + " ");
        }
    }
    class Child : Parent
    {
        public void show()
        {
            Console.Write("Alvida" + " ");
        }
    }
    class MainProgram
    {
        static void Main(string[] args)
        {
            Child c;
            c = new Child();
            c.show();
            c.show(42);
        }
    }
}

What is the output of this program?
  1. Namaste Dhanyavaad Alvida
  2. Alvida Dhanyavaad
  3. Namaste Alvida Dhanyavaad
  4. Error in the program
Show answer & explanation
Answer: (B) Alvida Dhanyavaad
Explanation: 1. c is a Child object. c.show() matches Child's own show() (no parameters), so it prints 'Alvida '. 2. c.show(42) needs a method with an int parameter. Child has no such method, so the base class Parent's show(int) is used, printing 'Dhanyavaad '. 3. Combined output: 'Alvida Dhanyavaad'.
Q296.Medium
In C#, which of the following can implement an interface? 1. Class 2. Enum 3. Structure 4. Namespace
  1. 1, 3
  2. 2, 4
  3. 3, 4
  4. 4 only
Show answer & explanation
Answer: (A) 1, 3
Explanation: Step 1: A class can implement an interface, so item 1 is valid. Step 2: A structure (struct) can also implement an interface, so item 3 is valid. Step 3: Enums cannot implement interfaces, and a namespace is not a type and cannot implement anything, so items 2 and 4 are invalid. Step 4: Therefore the correct combination is '1, 3'.
Q297.Medium
Which of the following statements is correct about the C#.NET code snippet given below?
interface IShape
{
    void draw1();
    void draw2();
}
class Circle : IShape
{
    private int r ;
    void IShape.draw1()
    {
        // Some code
    }
}
  1. Class Circle is an abstract class.
  2. Class Circle cannot contain instance data.
  3. Class Circle fully implements the interface IShape .
  4. Interface IShape should be inherited from the Object class.
  5. The compiler will report an error since the interface IShape is only partially implemented .
Show answer & explanation
Answer: (E) The compiler will report an error since the interface IShape is only partially implemented .
Explanation: Step 1: IShape declares two methods, draw1() and draw2(). Step 2: Circle provides an implementation only for draw1() (explicitly via IShape.draw1). Step 3: draw2() is never implemented, and Circle is not marked abstract. Step 4: A non-abstract class must implement every interface member, so the compiler reports an error for partial implementation.
Q298.Medium
In C#, which of the following members can be declared inside an interface? 1. Properties 2. Methods 3. Enumerations 4. Events 5. Structures
  1. 1, 3
  2. 1, 2, 4
  3. 3, 5
  4. 4, 5
Show answer & explanation
Answer: (B) 1, 2, 4
Explanation: Step 1: An interface in C# can contain declarations of properties, methods, events and indexers. Step 2: Enumerations (3) and structures (5) are types, not interface members, so they cannot be declared inside an interface. Step 3: Hence the valid items are 1 (Properties), 2 (Methods) and 4 (Events). Step 4: The correct option is '1, 2, 4'.
Q299.Medium
Which of the following is the correct implementation of the interface given below? interface IMyInterface { double MyFun (Single i ); }
  1. class MyClass { double MyFun (Single i ) as IMyInterface.MyFun { // Some code } }
  2. class MyClass { MyFun (Single i ) As Double { // Some code } }
  3. class MyClass : implements IMyInterface { double fun(Single si ) implements IMyInterface.MyFun () { //Some code } }
  4. class MyClass : IMyInterface { double IMyInterface.MyFun (Single i ) { // Some code } }
Show answer & explanation
Answer: (D) class MyClass : IMyInterface { double IMyInterface.MyFun (Single i ) { // Some code } }
Explanation: In C#, a class implements an interface by listing it after a colon, e.g. class MyClass : IMyInterface. Explicit interface implementation uses the fully qualified name IMyInterface.MyFun with the correct return type double and parameter Single i. Option 1 uses invalid 'as' syntax. Option 2 uses invalid 'As Double' syntax. Option 3 uses the Java-style 'implements' keyword, which is not valid C#. Only option 4 follows correct C# syntax.
Q300.Medium
Which of the following statements is correct about the using statement used in C#.NET?
  1. using statement can be placed anywhere in the C#.NET source code file.
  2. It is permitted to define a member at namespace level as a using alias.
  3. A C# .NET source code file can contain any number of using statements.
  4. By using using statement it is possible to create an alias for the namespace but not for the namespace element.
  5. By using using statement it is possible to create an alias for the namespace element but not for the namespace.
Show answer & explanation
Answer: (C) A C# .NET source code file can contain any number of using statements.
Explanation: A source file may contain any number of using directives, so option 3 is correct. Using directives must appear before any type declarations, so they cannot be placed anywhere (option 1 wrong). A using alias can be defined at namespace level, but it is not a 'member' in the usual sense, so option 2 is misleading. Using directives can create aliases for both namespaces and namespace elements, so options 4 and 5 are wrong.
1234

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank