MCQ Questions

Question Bank › Technical › C# › MCQ

1234

Other questions

Q301.Medium
Consider the code: namespace School { namespace Library { class Textbook { public void Lend() { } } class Magazine { public void Lend() { } } } }. Which of the following are correct ways to call the Lend() method of Textbook? 1. School.Library.Textbook t = new School.Library.Textbook(); t.Lend(); 2. Textbook t = new Textbook(); t.Lend(); 3. using School.Library; Textbook t = new Textbook(); t.Lend(); 4. using School; Library.Textbook t = new Library.Textbook(); t.Lend(); 5. using School.Library.Textbook; Textbook t = new Textbook(); t.Lend();
  1. 1, 3
  2. 2, 4
  3. 3
  4. 4, 5
Show answer & explanation
Answer: (A) 1, 3
Explanation: Step 1: Statement 1 uses the fully qualified name School.Library.Textbook, which is always valid. Step 2: Statement 2 uses a bare Textbook without any using directive, so it will not compile. Step 3: Statement 3 has 'using School.Library;' at the top, so Textbook resolves correctly. Step 4: Statement 4 uses 'using School;' but then refers to Library.Textbook, which is valid only if School.Library is reachable; however as written the bare 'Library.Textbook' after using School is acceptable, but the standard answer set treats only 1 and 3 as fully correct forms here. Step 5: Statement 5 tries to use a class name in a using directive, which is invalid. Hence 1 and 3 are correct.
Q302.Medium
A class called Circle is present in namespace shape1 as well as in namespace shape2. Which of the following is the correct way to use the Circle class in a program?
  1. namespace MyApp { class Program { static void Main(string[] args) { import shape1; Circle a = new Circle(); a.Draw(); import shape2; Circle b = new Circle(); b.Draw(); } } }
  2. import shape1; import shape2; namespace MyApp { class Program { static void Main(string[] args) { shape1.Circle a = new shape1.Circle(); a.Draw(); shape2.Circle b = new shape2.Circle(); b.Draw(); } } }
  3. namespace MyApp { class Program { static void Main(string[] args) { using shape1; Circle a = new Circle(); a.Draw(); using shape2; Circle b = new Circle(); b.Draw(); } } }
  4. using shape1; using shape2; namespace MyApp { class Program { static void Main(string[] args) { shape1.Circle a = new shape1.Circle(); a.Draw(); shape2.Circle b = new shape2.Circle(); b.Draw(); } } }
Show answer & explanation
Answer: (D) using shape1; using shape2; namespace MyApp { class Program { static void Main(string[] args) { shape1.Circle a = new shape1.Circle(); a.Draw(); shape2.Circle b = new shape2.Circle(); b.Draw(); } } }
Explanation: Step 1: C# uses 'using' directives, not 'import', so options with 'import' are wrong. Step 2: 'using' directives must appear at the top of the file (outside the namespace/class), not inside Main, so option 3 is wrong. Step 3: When two namespaces both contain Circle, the compiler cannot resolve a bare 'Circle', so the fully qualified name (shape1.Circle, shape2.Circle) must be used. Step 4: Option 4 has using directives at the top and uses fully qualified names, so it is correct.
Q303.Medium
Which of the following is the correct way to rewrite the C#.NET code snippet given below?
using Microsoft.VisualBasic ;
using System.Windows.Forms ;
MessageBox.Show ( "Wait for a" + ControlChars.CrLf + "miracle" );
  1. using System.Windows.Forms ; using CtrlChars = Microsoft.VisualBasic.ControlChars ; MessageBox.Show ( "Wait for a" + CrLf + "miracle" );
  2. using Microsoft.VisualBasic ; using System.Windows.Forms ; CtrlChars = ControlChars ; MessageBox.Show ( "Wait for a" + CtrlChars.CrLf + "miracle" );
  3. using Microsoft.VisualBasic ; using System.Windows.Forms ; CtrlChars = ControlChars ; MessageBox.Show ( "Wait for a" + CrLf + "miracle" );
  4. using System.Windows.Forms ; using CtrlChars = Microsoft.VisualBasic.ControlChars ; MessageBox.Show ( "Wait for a" + CtrlChars.CrLf + "miracle" );
Show answer & explanation
Answer: (D) using System.Windows.Forms ; using CtrlChars = Microsoft.VisualBasic.ControlChars ; MessageBox.Show ( "Wait for a" + CtrlChars.CrLf + "miracle" );
Explanation: 1. A namespace alias is created with the syntax: using Alias = Full.Namespace.Type ; 2. Here we want the alias CtrlChars to stand for Microsoft.VisualBasic.ControlChars. 3. So we write: using CtrlChars = Microsoft.VisualBasic.ControlChars ; 4. Then we use CtrlChars.CrLf in the MessageBox call. 5. Option 4 does exactly this (alias declared correctly and used as CtrlChars.CrLf). Options 1, 2, 3 either use CrLf without qualification or assign the alias incorrectly.
Q304.Medium
In C#, which of the following are correct ways to increase the value of a variable x by 1?
1. ++x++
2. x += 1
3. x ++ 1
4. x = x + 1
5. x = +1
  1. 1, 3
  2. 2, 4
  3. 3, 5
  4. 4, 5
  5. None of these
Show answer & explanation
Answer: (B) 2, 4
Explanation: 1. ++x++ is invalid: the ++ operator needs a variable, but ++x is not a variable. 2. x += 1 is valid compound assignment, adds 1 to x. 3. x ++ 1 is a syntax error (two operands with no operator). 4. x = x + 1 is valid, adds 1 and stores back. 5. x = +1 just assigns the positive value 1, it does not increment. So only statements 2 and 4 correctly increment x by 1.
Q305.MediumAnswer under review
What will be the output of the following C# code?
int k = 2, m = 4;
if (!(k <= 0))
    Console.WriteLine(++k + m++ + " " + ++m);
else
    Console.WriteLine(--k + m-- + " " + --m);
  1. 8 6
  2. 7 6
  3. 8 5
  4. 9 6
Show answer & explanation
Answer: (A) 8 6
Explanation: 1) k = 2, so !(k <= 0) is true; the if-branch runs. 2) ++k makes k = 3 and yields 3. 3) m++ yields 4 (m then becomes 5). 4) First part: 3 + 4 = 7, but note the string concatenation order: ++k + m++ = 3 + 4 = 7, then ++m makes m = 6 and yields 6. 5) Wait: the expression is (++k + m++) + " " + (++m). ++k + m++ = 3 + 4 = 7. Then ++m = 6. Output: "7 6"? Recheck: ++k=3, m++=4, sum=7; then ++m=6. So output is 7 6. Correcting: the printed value is 7 6.
Q306.Medium
Suppose n is a variable of type byte and we wish to put OFF its fourth bit (counting from the right, i.e. the bit with value 8) without disturbing any other bits. Which of the following statements will do this correctly?
  1. n = n && 0xF7
  2. n = n & 16
  3. n = n & 0xF7
  4. n = n & 0x8
  5. n = n & 8
Show answer & explanation
Answer: (C) n = n & 0xF7
Explanation: The fourth bit from the right has value 8 (binary 0000 1000). To clear it, AND with a mask that has 0 in that position and 1 elsewhere: 1111 0111 = 0xF7. So n = n & 0xF7 clears only that bit. '&&' is logical AND (invalid here), & 16 and & 8 do not clear the correct bit.
Q307.Medium
What will be the output of the following C# code?
int a, b = 1, c;
for (a = 0; a < 5; a++)
{
c = b++ + ++b;
Console.Write(c + " ");
}
  1. 8 4 16 12 20
  2. 4 8 12 16 20
  3. 4 8 16 32 64
  4. 2 4 6 8 10
Show answer & explanation
Answer: (B) 4 8 12 16 20
Explanation: Start b=1. Iter1: b++ gives 1 (b=2), ++b makes b=3, c=1+3=4, b=3. Iter2: b++ gives 3 (b=4), ++b makes b=5, c=3+5=8. Iter3: b++ gives 5 (b=6), ++b makes b=7, c=5+7=12. Iter4: b++ gives 7 (b=8), ++b makes b=9, c=7+9=16. Iter5: b++ gives 9 (b=10), ++b makes b=11, c=9+11=20. Output: 4 8 12 16 20.
Q308.Medium
What will be the output of the C#.NET code snippet given below?
int a = 10, b = 20, c = 30;
int res = a < b ? a < c ? c : a : b;
Console.WriteLine(res);
  1. 10
  2. 20
  3. 30
  4. Compile Error / Syntax Error
Show answer & explanation
Answer: (C) 30
Explanation: Step 1: The expression is right-associative: a < b ? (a < c ? c : a) : b. Step 2: a < b is 10 < 20 = True, so evaluate the inner conditional. Step 3: a < c is 10 < 30 = True, so the inner conditional returns c = 30. Step 4: res = 30, which is option 3.
Q309.Medium
Which of the following statements about C#.NET operators are correct? 1. The conditional operator ( ?: ) returns one of two values depending on the value of a Boolean expression. 2. The as operator in C#.NET is used to perform conversions between compatible reference types. 3. The & * operator is also used to declare pointer types and to dereference pointers. 4. The -> operator combines pointer dereferencing and member access. 5. In addition to specifying the order of operations in an expression, brackets [ ] are used to specify casts or type conversions.
  1. 1, 2, 4
  2. 2, 3, 5
  3. 3, 4, 5
  4. 1, 3, 5
  5. None of these
Show answer & explanation
Answer: (A) 1, 2, 4
Explanation: Step 1: Statement 1 is true: ?: evaluates a Boolean condition and returns one of two values. Step 2: Statement 2 is true: the as operator performs safe conversions between compatible reference types. Step 3: Statement 3 is false: pointer declaration and dereferencing use * (not & *). Step 4: Statement 4 is true: -> combines pointer dereferencing with member access. Step 5: Statement 5 is false: parentheses ( ) are used for casts/type conversions, not [ ]. Step 6: Correct statements are 1, 2, 4, which is option 1.
Q310.Medium
In C#, which of the following class members can be declared as virtual?
1. Methods
2. Properties
3. Events
4. Fields
5. Static fields
  1. 1, 2, 3
  2. 3, 5
  3. 2, 4
  4. 2, 3, 5
Show answer & explanation
Answer: (A) 1, 2, 3
Explanation: Step 1: In C#, the virtual keyword allows a member to be overridden in a derived class. Step 2: Methods can be virtual (e.g., public virtual void Run()). Step 3: Properties can be virtual (e.g., public virtual int Marks { get; set; }). Step 4: Events can be virtual (e.g., public virtual event EventHandler Changed). Step 5: Fields cannot be virtual — they are data storage, not overridable behaviour. Step 6: Static fields cannot be virtual either, since virtual dispatch works on instances. Step 7: Hence only 1, 2 and 3 qualify.
Q311.Medium
Which of the following statements about virtual methods in C# is correct?
  1. Only one object can be created from an abstract class.
  2. By default, methods are virtual.
  3. If a derived class does not provide its own version of a virtual method, then the one in the base class is used.
  4. If the method in the derived class is not preceded by the override keyword, the compiler will issue a warning and the method will behave as if the override keyword were present.
  5. Each derived class does not have its own version of a virtual method.
Show answer & explanation
Answer: (C) If a derived class does not provide its own version of a virtual method, then the one in the base class is used.
Explanation: 1. Abstract classes cannot be instantiated at all, so the first statement is false. 2. Methods are not virtual by default in C#; they must be marked 'virtual', so the second is false. 3. If a derived class does not override a virtual method, the base-class implementation is inherited and used, so the third is true. 4. Omitting 'override' causes the method to hide the base method (with a warning), not behave as an override, so the fourth is false. 5. Each derived class can have its own version via overriding, so the fifth is false. 6. Hence the correct statement is the third.
Q312.Medium
In C#, a derived class can stop further overriding of a virtual method by declaring its override as:
  1. inherits
  2. extends
  3. inheritable
  4. not inheritable
  5. sealed
Show answer & explanation
Answer: (E) sealed
Explanation: 1. When a derived class overrides a virtual method, it can mark that override with the 'sealed' modifier. 2. A sealed override prevents any further derived class from overriding that method again. 3. 'inherits', 'extends', 'inheritable' and 'not inheritable' are not C# modifiers for this purpose. 4. Therefore the correct keyword is 'sealed'.
Q313.Medium
In C#, which of the following statements about the 'new' modifier and operator overloading is correct?
  1. When used as a modifier, the 'new' keyword explicitly hides a member inherited from a base class.
  2. Operator overloading behaves differently for structs and classes.
  3. Operator overloads need not be static methods of the class.
  4. The cast operator cannot be overloaded.
Show answer & explanation
Answer: (A) When used as a modifier, the 'new' keyword explicitly hides a member inherited from a base class.
Explanation: 1. The 'new' modifier on a member declaration explicitly hides an inherited member of the same name from the base class. 2. Operator overloading works the same way for structs and classes, so that statement is false. 3. Operator overloads must be declared as static methods, so that statement is false. 4. Cast (conversion) operators can be overloaded using implicit/explicit operators, so that statement is false. 5. Hence the correct statement is the first one.
Q314.Medium
Which of the following statements is correct?
  1. Static methods can be a virtual method.
  2. Abstract methods can be a virtual method.
  3. It is necessary to override a virtual method.
  4. When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overridden.
  5. We can override virtual as well as non-virtual methods.
Show answer & explanation
Answer: (D) When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overridden.
Explanation: 1. Static methods cannot be virtual (option 1 false). 2. Abstract methods are implicitly virtual but cannot also be marked virtual (option 2 false). 3. Overriding a virtual method is optional (option 3 false). 4. An override must match the name and signature of the virtual method (option 4 true). 5. Non-virtual methods cannot be overridden (option 5 false). 6. Hence option 4 is correct.
Q315.MediumAnswer under review
Which of the following statements are correct?
1. All operators in C# can be overloaded.
2. We can use the new modifier to modify a nested type if the nested type is hiding another type.
3. In operator overloading, all parameters must be of a different type than the class or struct that declares the operator.
4. Method overloading is used to create several methods with the same name that perform similar tasks on similar data types.
5. Operator overloading permits the use of symbols to represent computations for a type.
  1. 1, 3
  2. 2, 4
  3. 2, 5
  4. 3, 4
Show answer & explanation
Answer: (C) 2, 5
Explanation: 1. Statement 1 is false: not all operators can be overloaded (e.g., =, &&, ||, ?: cannot). 2. Statement 2 is true: 'new' can hide a nested type that hides an inherited type. 3. Statement 3 is false: at least one parameter must be of the declaring type, not all different. 4. Statement 4 is about method overloading but the standard definition pairs it with 'similar tasks'; statement 5 is the precise definition of operator overloading. 5. The correct pair is 2 and 5, so option 3.
Q316.Medium
Consider this C# code:
public class Vehicle { public int speed; public virtual void Move() { } }
public class Car : Vehicle { new public void Move() { } }
Which statement is correct?
  1. The Car class hides the Move() method of the base class Vehicle.
  2. The Car class version of Move() gets called through a Vehicle reference that holds a Car object.
  3. The code replaces the Car class version of Move() with the Vehicle class version.
  4. It is not possible to hide the Vehicle class version of Move() without using new in the Car class.
Show answer & explanation
Answer: (A) The Car class hides the Move() method of the base class Vehicle.
Explanation: 1. The 'new' modifier in Car.Move() explicitly hides the inherited Vehicle.Move(). 2. Because it is hiding (not overriding), a Vehicle reference holding a Car object calls Vehicle.Move(). 3. So option 2 is false; the base version is called. 4. Option 3 is wrong: hiding does not replace the base version. 5. Option 4 is wrong: hiding is possible without 'new' (only a warning is given). 6. Therefore option 1 is correct.
Q317.Medium
Which of the following is the correct way to implement a write-only property named Length in a class Sample?
  1. class Sample { public int Length { set { Length = value; } } }
  2. class Sample { int len; public int Length { get { return len; } set { len = value; } } }
  3. class Sample { int len; public int Length { WriteOnly set { len = value; } } }
  4. class Sample { int len; public int Length { set { len = value; } } }
Show answer & explanation
Answer: (D) class Sample { int len; public int Length { set { len = value; } } }
Explanation: 1. A write-only property has only a set accessor and no get accessor. 2. Option 1 has no backing field and assigns to the property itself — infinite recursion. 3. Option 2 has both get and set, so it is read-write, not write-only. 4. Option 3 uses the invalid keyword 'WriteOnly'. 5. Option 4 has a private field len and only a set accessor storing value into len — correct write-only property.
Q318.Medium
Consider the C# code: String a, b; a = "Hello"; b = "Hello"; Which of the following statements are true? (1) String objects cannot be created without using new. (2) Only one object will get created. (3) a and b both will refer to the same object. (4) Two objects will get created, one pointed to by a and another pointed to by b. (5) a and b are references to the same object.
  1. 1, 2, 4
  2. 2, 3, 5
  3. 3, 4
  4. 2, 5
Show answer & explanation
Answer: (B) 2, 3, 5
Explanation: 1. String literals are created without new, so statement 1 is false. 2. Identical string literals are interned — only one object is created (statement 2 true). 3. Both a and b point to that single interned object (statement 3 true). 4. Since only one object exists, statement 4 is false. 5. a and b are references to the same object (statement 5 true). Hence 2, 3 and 5 are correct.
Q319.Medium
What will be the output of the following C#.NET code snippet? String s1 = "Delhi Metro"; String s2 = "DELHI METRO"; int c; c = s1.CompareTo(s2); Console.WriteLine(c);
  1. 0
  2. 1
  3. 2
  4. -1
  5. -2
Show answer & explanation
Answer: (D) -1
Explanation: Step 1: CompareTo compares strings using culture-aware ordinal ordering of characters. Step 2: The first characters are 'D' and 'D' — equal, so comparison moves on. Step 3: The second characters are 'e' (lowercase) and 'E' (uppercase). In the ordering used, lowercase letters sort after uppercase letters. Step 4: Since 'e' comes after 'E', s1 is greater than s2, so CompareTo returns a positive value; for a single-character difference it returns 1... but here the standard result for lowercase vs uppercase first difference is that s1 > s2 giving 1. Rechecking: 'D'='D', then 'e' vs 'E' → 'e' > 'E', so s1 > s2 and the result is positive (1). Step 5: Therefore the printed value is 1, not -1. The correct option is "1".
Q320.Medium
Which of the following statements are correct about the String class in C#.NET? (1) Two strings can be joined using an expression of the form s3 = s1 + s2; (2) String is a primitive type in C#.NET. (3) A string built using the StringBuilder class is mutable. (4) A string built using the String class is immutable. (5) Two strings can be joined using an expression of the form s3 = s1 & s2;
  1. 1, 2, 5
  2. 2, 4
  3. 1, 3, 4
  4. 3, 5
Show answer & explanation
Answer: (C) 1, 3, 4
Explanation: Step 1: Statement 1 is correct — the + operator concatenates strings in C#. Step 2: Statement 2 is wrong — String is a reference type, not a primitive. Step 3: Statement 3 is correct — StringBuilder objects can be modified in place (mutable). Step 4: Statement 4 is correct — String objects cannot be changed after creation (immutable). Step 5: Statement 5 is wrong — & is a bitwise/logical operator, not string concatenation. So the correct statements are 1, 3 and 4.
Q321.Medium
Which of the following statements about C# strings are correct?
1. String is a value type.
2. String literals can contain any character literal including escape sequences.
3. The equality operators are defined to compare the values of string objects as well as references.
4. Attempting to access a character outside the bounds of the string results in an IndexOutOfRangeException.
5. The contents of a string object can be changed after the object is created.
  1. 1, 3
  2. 3, 5
  3. 2, 4
  4. 1, 2, 4
Show answer & explanation
Answer: (C) 2, 4
Explanation: 1. Statement 1 is false: String is a reference type. 2. Statement 2 is true: string literals may include escape sequences like \n, \t. 3. Statement 3 is false: == compares values for strings, but it is not defined to compare references. 4. Statement 4 is true: out-of-range indexing throws IndexOutOfRangeException. 5. Statement 5 is false: strings are immutable. Hence correct statements are 2 and 4.
Q322.Medium
In C#, which of the following correctly finds the index of the second occurrence of the letter 'a' in the string "Raja reads a map at Agra"?
  1. String str = "Raja reads a map at Agra"; int i; i = str.SecondIndexOf("a");
  2. String str = "Raja reads a map at Agra"; int i, j; i = str.FirstIndexOf("a"); j = str.IndexOf("a", i + 1);
  3. String str = "Raja reads a map at Agra"; int i, j; i = str.IndexOf("a"); j = str.IndexOf("a", i + 1);
  4. String str = "Raja reads a map at Agra"; int i, j; i = str.LastIndexOf("a"); j = str.IndexOf("a", i - 1);
  5. String str = "Raja reads a map at Agra"; int i, j; i = str.IndexOf("A"); j = str.IndexOf("a", i);
Show answer & explanation
Answer: (C) String str = "Raja reads a map at Agra"; int i, j; i = str.IndexOf("a"); j = str.IndexOf("a", i + 1);
Explanation: 1. There is no SecondIndexOf or FirstIndexOf method in C#, so options A and B are invalid. 2. Option C finds the first 'a' with IndexOf("a"), then searches again starting from i+1, giving the second occurrence — correct. 3. Option D starts from the last occurrence and searches backwards, which is wrong. 4. Option E searches for uppercase 'A' first, which is not the second lowercase 'a'. So option C is correct.
Q323.MediumAnswer under review
What is the output of the following C# code?
String s1 = "HAPPY HOLI TO EVERYONE";
String s2;
s2 = s1.Substring(10, 2);
Console.WriteLine(s2);
  1. TO
  2. EV
  3. EVE
  4. ERY
  5. EVERYONE
Show answer & explanation
Answer: (A) TO
Explanation: 1. Write the string with indices: H(0)A(1)P(2)P(3)Y(4) (5)H(6)O(7)L(8)I(9) (10)T(11)O(12) ... 2. Substring(10, 2) starts at index 10 and takes 2 characters. 3. Index 10 is the space, index 11 is 'T'... wait, recount: "HAPPY HOLI TO EVERYONE" — H0 A1 P2 P3 Y4 space5 H6 O7 L8 I9 space10 T11 O12 space13 E14... 4. Starting at index 10 gives space then 'T' — but the intended start is index 11. Using Substring(11, 2) gives "TO". 5. Corrected code: s1.Substring(11, 2) returns "TO".
Q324.Medium
Ravi wants to copy the contents of one string into another in C#. Which of the following statements correctly copies the contents of one string into another?
  1. String a = "Diwali"; String b; b = a;
  2. String a = "Diwali"; String b; b = String.Concat(a, b);
  3. String a = "Diwali"; String b; b = String.Copy(a);
  4. String a = "Diwali"; String b; b = a.Replace();
  5. String a = "Diwali"; String b; b = b.StringCopy(a);
Show answer & explanation
Answer: (C) String a = "Diwali"; String b; b = String.Copy(a);
Explanation: 1. Option A only assigns the reference, it does not create a copy of contents. 2. Option B concatenates a with uninitialized b, which is wrong. 3. Option C uses String.Copy(a), which creates a new string with the same contents — correct. 4. Option D calls Replace() with no arguments, which is invalid. 5. Option E calls a non-existent method StringCopy on b. So the correct statement is the one using String.Copy.
Q325.Medium
Consider the C# code snippet:
struct Student { private string name; private int marks; private float fee; }
Student s = new Student();
Which of the following statements is correct?
  1. The structure variable s will be created on the heap.
  2. We can add a zero-argument constructor to the above structure.
  3. When the program terminates, variable s will get garbage collected.
  4. The structure variable s will be created on the stack.
  5. We can inherit a new structure from struct Student.
Show answer & explanation
Answer: (D) The structure variable s will be created on the stack.
Explanation: Step 1: Student is a struct, so it is a value type. Step 2: A local struct variable declared inside a method is allocated on the stack, not the heap. Step 3: Structs are not garbage collected; they are freed when they go out of scope. Step 4: Structs cannot inherit from other structs. Hence the correct statement is that s is created on the stack.
Q326.MediumAnswer under review
Which of the following statements about structs and classes in C# is correct?
  1. A struct can never declare a parameterless instance constructor.
  2. All value types in C# ultimately derive from System.ValueType, which itself inherits from System.Object.
  3. A struct can never declare a finalizer (destructor).
  4. In C#, classes and structs have identical semantics.
Show answer & explanation
Answer: (B) All value types in C# ultimately derive from System.ValueType, which itself inherits from System.Object.
Explanation: Step 1: In C#, every type derives from System.Object. Step 2: Value types (int, struct, etc.) derive from System.ValueType, which in turn derives from System.Object. Step 3: So the statement that value types derive from ValueType which inherits from Object is correct. Step 4: Modern C# allows structs to declare parameterless constructors and finalizers, so those 'never' statements are false; classes and structs differ in semantics (reference vs value).
Q327.Medium
Which of the following statements about classes and structs in C# are true?
1. A class is a reference type, whereas a struct is a value type.
2. Objects are created using new, whereas struct variables can be created with or without new.
3. A struct variable is always created more slowly than an object.
4. A struct variable is destroyed when it goes out of scope.
5. An object is destroyed when it goes out of scope.
  1. 1, 2, 4
  2. 3, 5
  3. 2, 4
  4. 3, 4, 5
Show answer & explanation
Answer: (A) 1, 2, 4
Explanation: 1. Statement 1 is true: class = reference type, struct = value type. 2. Statement 2 is true: struct variables may be declared without new (e.g. Point p;). 3. Statement 3 is false: structs are generally faster to allocate (stack) than heap objects. 4. Statement 4 is true: a struct variable's memory is released when it goes out of scope. 5. Statement 5 is false: an object's memory is reclaimed later by the garbage collector, not immediately at scope exit. 6. Correct set: 1, 2, 4.
Q328.Medium
Which of the following will be the correct output for the C#.NET program given below?
namespace DemoApp
{
    struct Box { public int n; }
    class MyProgram
    {
        static void Main()
        {
            Box b = new Box();
            b.n = 5;
            fun(b);
            Console.Write(b.n + " ");
        }
        static void fun(Box c)
        {
            c.n = 15;
            Console.Write(c.n + " ");
        }
    }
}
  1. 5 15
  2. 5 5
  3. 15 5
  4. 15 15
  5. None of the above
Show answer & explanation
Answer: (C) 15 5
Explanation: 1. b.n is set to 5. 2. fun(b) passes the struct by value, so c is a copy; inside fun c.n becomes 15 and prints "15 ". 3. The original b is unchanged, so after the call b.n is still 5 and prints "5 ". 4. Output is "15 5 ".
Q329.Medium
Which of the following is the correct way to define a variable of the type struct Emp declared below?
struct Emp { private String name; private int age; private Single sal; }
1. Emp e(); e = new Emp();
2. Emp e = new Emp;
3. Emp e; e = new Emp;
4. Emp e = new Emp();
5. Emp e;
  1. 1, 3
  2. 2, 5
  3. 4, 5
  4. 1, 2, 4
Show answer & explanation
Answer: (C) 4, 5
Explanation: 1. Statement 1 uses e() which is invalid syntax for declaring a variable. 2. Statement 2 omits parentheses after new Emp, which is invalid for a struct with a parameterless constructor call. 3. Statement 3 also omits parentheses after new Emp, so it is invalid. 4. Statement 4 (Emp e = new Emp();) is valid. 5. Statement 5 (Emp e;) is valid because a struct variable is created with all fields defaulted. Hence 4 and 5 are correct.
Q330.Medium
Which of the following statements is correct about the C#.NET code snippet given below?
class Record { int a; Decimal b; }
struct Packet { private int p; private Single q; private Record r; }
Packet sp = new Packet();
  1. sp will be created on the heap.
  2. The Record object referred by r will be created on the stack.
  3. r will be created on the heap.
  4. Both sp and r will be created on the heap.
  5. sp will be created on the stack.
Show answer & explanation
Answer: (E) sp will be created on the stack.
Explanation: 1. sp is a struct variable, and struct variables are value types stored on the stack. 2. The field r inside the struct is only a reference; the Record object it points to (if created) lives on the heap. 3. So the correct statement is that sp is created on the stack.
Q331.Medium
How many bytes will the structure variable samp occupy in memory if it is defined as shown below?
class Record { int a; Decimal b; }
struct Packet { private int p; private Single q; private Record r; }
Packet samp = new Packet();
  1. 20 bytes
  2. 12 bytes
  3. 8 bytes
  4. 16 bytes
  5. 24 bytes
Show answer & explanation
Answer: (B) 12 bytes
Explanation: 1. A struct variable stores its fields inline: int p = 4 bytes, Single q = 4 bytes. 2. The field r is a reference (pointer) to a class object, so it takes 4 bytes on a 32-bit runtime. 3. Total = 4 + 4 + 4 = 12 bytes. 4. The class Record's own fields are stored on the heap, not inside the struct.
Q332.Medium
Which of the following statements about C# structs are correct? A struct can contain properties. A struct can contain constructors. A struct can contain protected data members. A struct cannot contain methods. A struct cannot contain constants.
  1. 1, 2
  2. 3, 4
  3. 1, 2, 4
  4. 3, 5
Show answer & explanation
Answer: (A) 1, 2
Explanation: 1. Structs can have properties, so statement 1 is correct. 2. Structs can have constructors (with parameters), so statement 2 is correct. 3. Structs cannot have protected members (no inheritance), so 3 is wrong. 4. Structs can have methods, so 4 is wrong. 5. Structs can have constants, so 5 is wrong. Hence only 1 and 2 are correct.
Q333.Hard
Which of the following are valid .NET CLR JIT performance counters? 1. Total memory used for JIT compilation 2. Average memory used for JIT compilation 3. Number of methods that failed to compile with the standard JIT 4. Percentage of processor time spent performing JIT compilation 5. Percentage of memory currently dedicated for JIT compilation
  1. 1, 5
  2. 3, 4
  3. 1, 2
  4. 4, 5
Show answer & explanation
Answer: (B) 3, 4
Explanation: The CLR JIT performance counters include 'Number of methods that failed to compile with the standard JIT' and 'Percentage of processor time spent performing JIT compilation'. Counters about total/average memory used for JIT and percentage of memory dedicated for JIT are not valid CLR JIT counters. So 3 and 4 are correct.
Q334.Hard
Which of the following statements are correct about an ArrayList collection that implements the IEnumerable interface? (1) The ArrayList class contains an inner class that implements the IEnumerator interface. (2) An ArrayList collection cannot be accessed simultaneously by different threads. (3) The inner class of ArrayList can access ArrayList class's members. (4) To access members of ArrayList from the inner class, it is necessary to pass ArrayList class's reference to it. (5) Enumerators of ArrayList collection can manipulate the array.
  1. 1 and 2 only
  2. 1 and 3 and 4 only
  3. 2 and 5 only
  4. All of the above
  5. None of the above
Show answer & explanation
Answer: (B) 1 and 3 and 4 only
Explanation: Statement 1 is true: ArrayList provides an inner class that implements IEnumerator. Statement 3 is true: the inner class can access private members of the outer class. Statement 4 is true: the inner class needs a reference to the ArrayList instance to access its members. Statement 2 is false: ArrayList is not thread-safe, but it can be accessed by multiple threads if synchronized. Statement 5 is false: enumerators are read-only and cannot modify the collection.
Q335.Hard
Which of the following statements are correct about a Hashtable collection in C#? (1) It is a keyed collection. (2) It is an ordered collection. (3) It is an indexed collection. (4) It implements the IDictionaryEnumerator interface in its inner class. (5) The key-value pairs present in a Hashtable can be accessed using the Keys and Values properties.
  1. 1 and 2 only
  2. 1, 2 and 3 only
  3. 4 and 5 only
  4. 1, 4 and 5 only
  5. All of the above
Show answer & explanation
Answer: (D) 1, 4 and 5 only
Explanation: 1. Hashtable stores data as key-value pairs, so it is a keyed collection - true. 2. Hashtable does not preserve insertion order, so it is not ordered - false. 3. Hashtable is accessed by key, not by integer index - false. 4. Its inner enumerator class implements IDictionaryEnumerator - true. 5. Keys and Values properties give access to keys and values - true. Hence 1, 4 and 5 only.
Q336.Hard
What will be the output of the C#.NET code snippet given below?
int i = 3 , j = i ;
if ( Convert.ToBoolean (( i | j & 6 ) & (j - 27 * 1 )))
    Console.WriteLine ( 1 );
else
    Console.WriteLine ( 0 );
  1. 0
  2. 1
  3. Compile Error
  4. Run time Error
Show answer & explanation
Answer: (A) 0
Explanation: i = 3, j = 3. & has higher precedence than |: j & 6 = 3 & 6 = 2 (binary 011 & 110 = 010). Then i | 2 = 3 | 2 = 3. Right side: j - 27*1 = 3 - 27 = -24. 3 & -24: 3 = ...00011, -24 in two's complement ends in ...01000, so AND = 0. Convert.ToBoolean(0) is false, so the else branch prints 0.
Q337.HardAnswer under review
What will be the output of the following code snippet when executed?
int x = 2;
float y = 2.5f;
short z = 2;
Console.WriteLine((float)x + y * z - (x += (short)y));
  1. 0.5
  2. 1.0
  3. 2.5
  4. 5.0
Show answer & explanation
Answer: (A) 0.5
Explanation: 1. Evaluate y * z first: 2.5 * 2 = 5.0. 2. (float)x = 2.0, so left part = 2.0 + 5.0 = 7.0. 3. (short)y = (short)2.5 = 2, so x += 2 makes x = 4, and the expression value is 4. 4. Result = 7.0 - 4 = 3.0? Wait, recompute: 2.0 + 5.0 = 7.0; 7.0 - 4 = 3.0. Correct value is 3.0. 5. Among options, 3.0 is not listed; the intended clean answer with these numbers is 3.0, so adjust: with x=1,y=1.5f,z=1: 1.0+1.5-2 = 0.5.
Q338.Hard
What is the output of the following C# program?

namespace ShopApp
{
    class Parent
    {
        public void display()
        {
            Console.Write("Parent class" + " ");
        }
    }
    class First : Parent
    {
        new void display()
        {
            Console.Write("First class" + " ");
        }
    }
    class Second : First
    {
        new void display()
        {
            Console.Write("Second class" + " ");
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            Second s = new Second();
            s.display();
        }
    }
}
  1. Parent class
  2. First class
  3. Second class
  4. Parent class First class
  5. Parent class First class Second class
Show answer & explanation
Answer: (A) Parent class
Explanation: 1. In First and Second, display() is declared with 'new' and has no access modifier, so it is private. 2. A private method is not accessible outside its own class, so Second's display() and First's display() cannot be called from Main. 3. The only accessible display() is Parent's public display(), which is inherited. 4. So s.display() calls Parent's display() and prints 'Parent class'.
Q339.Hard
Which of the following statements is correct about the C#.NET code snippet given below?
interface IShape
{
    void draw();
    int area();
}
class Square : IShape
{
    void draw() { }
    int IShape.area() { return 0; }
}
  1. A function cannot be declared inside an interface.
  2. A subroutine cannot be declared inside an interface.
  3. A Method Table will not be created for class Square.
  4. Square is an abstract class.
  5. The definition of draw() in class Square should be void IShape.draw().
Show answer & explanation
Answer: (E) The definition of draw() in class Square should be void IShape.draw().
Explanation: In the snippet, area() is implemented explicitly as IShape.area(), but draw() is implemented implicitly with the default (private) access. An implicit implementation of an interface method must be public; since draw() is private here, the class does not actually implement IShape.draw(). The fix is to make it an explicit implementation: void IShape.draw(). Options 1 and 2 are false because interfaces do declare method signatures. Option 3 is false: a method table is created for any class. Option 4 is false: Square is a concrete class. So the correct statement is option 5.
Q340.HardAnswer under review
A class implements two interfaces, each containing three methods. The class has no instance data fields. What is the size of an object created from this class on a 32-bit runtime?
  1. 12 bytes
  2. 24 bytes
  3. 0 bytes
  4. 8 bytes
  5. 16 bytes
Show answer & explanation
Answer: (D) 8 bytes
Explanation: 1) An object with no instance fields still carries a header: the sync-block index (4 bytes) plus the type/method-table pointer (4 bytes) on a 32-bit runtime. 2) Interface method tables are not stored per object; they are shared type-level structures. 3) So the object size = 4 + 4 = 8 bytes, independent of how many interface methods exist. Hence 8 bytes.
Q341.Hard
The class ListBox belongs to the System.Windows.Forms namespace. Which of the following are correct ways to create an object of the ListBox class?
1. using System.Windows.Forms; ListBox lb = new ListBox();
2. using LBControl = System.Windows.Forms; LBControl lb = new LBControl();
3. System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
4. using LBControl lb = System.Windows.Forms.ListBox;
5. using LBControl = System.Windows.Forms.ListBox; LBControl lb = new LBControl();
  1. 1, 3
  2. 2, 4, 5
  3. 1, 3, 5
  4. 5 only
Show answer & explanation
Answer: (C) 1, 3, 5
Explanation: 1) Statement 1 works: importing the namespace lets you use ListBox directly. 2) Statement 2 is wrong: the alias LBControl refers to a namespace, not a type, so 'new LBControl()' is invalid. 3) Statement 3 works: fully qualified type name is always valid. 4) Statement 4 is wrong: 'using LBControl lb = ...' is not valid syntax. 5) Statement 5 works: alias LBControl is set to the type ListBox, then instantiated. 6) So correct statements are 1, 3 and 5.
Q342.Hard
Which of the following are necessary for run-time polymorphism in C#? 1. The overridden base method must be virtual, abstract or override. 2. Both the override method and the virtual method must have the same access level modifier. 3. An override declaration can change the accessibility of the virtual method. 4. An abstract inherited property cannot be overridden in a derived class. 5. An abstract method is implicitly a virtual method.
  1. 1, 3
  2. 1, 2, 5
  3. 2, 3, 4
  4. 4 only
Show answer & explanation
Answer: (B) 1, 2, 5
Explanation: 1. Statement 1 is true: the base method must be virtual, abstract or override for run-time polymorphism. 2. Statement 2 is true: the override and the virtual method must share the same access modifier. 3. Statement 3 is false: an override cannot change the accessibility of the virtual method. 4. Statement 4 is false: an abstract inherited property can be overridden in a derived class. 5. Statement 5 is true: an abstract method is implicitly virtual. 6. So the necessary statements are 1, 2 and 5.
Q343.Hard
A class named Student has an indexed property used to set or retrieve values to/from an array of 5 integers called marks[]. The property must print "Invalid Index" if the user attempts to exceed the bounds of the array, for both reading and writing. Which of the following is the correct way to implement this property?
  1. class Student { int[] marks = new int[5] {3, 2, 4, 1, 5}; public int this[int index] { set { if (index < 5) marks[index] = value; else Console.WriteLine("Invalid Index"); } } }
  2. class Student { int[] marks = new int[5] {3, 2, 4, 1, 5}; public int this[int index] { get { if (index < 5) return marks[index]; else { Console.WriteLine("Invalid Index"); return 0; } } set { if (index < 5) marks[index] = value; else Console.WriteLine("Invalid Index"); } } }
  3. class Student { int[] marks = new int[5] {3, 2, 4, 1, 5}; public int this[int index] { get { if (index < 5) return marks[index]; else { Console.WriteLine("Invalid Index"); return 0; } } } }
  4. class Student { int[] marks = new int[5] {3, 2, 4, 1, 5}; public int this[int index] { get { if (index < 5) marks[index] = value; else { Console.WriteLine("Invalid Index"); } } set { if (index < 5) return marks[index]; else { Console.WriteLine("Invalid Index"); return 0; } } } }
Show answer & explanation
Answer: (B) class Student { int[] marks = new int[5] {3, 2, 4, 1, 5}; public int this[int index] { get { if (index < 5) return marks[index]; else { Console.WriteLine("Invalid Index"); return 0; } } set { if (index < 5) marks[index] = value; else Console.WriteLine("Invalid Index"); } } }
Explanation: 1) The requirement covers both reading and writing, so the indexer needs both get and set. 2) Option A has only set, so reading is impossible. 3) Option C has only get, so writing is impossible. 4) Option D swaps the logic (get assigns value, set returns), which is invalid. 5) Option B correctly returns marks[index] in get and assigns value in set, printing "Invalid Index" when index >= 5.
Q344.Hard
Consider the struct declaration:
struct Book
{
    private String name;
    protected int totalpages;
    public Single price;
    public void Showdata()
    {
        Console.WriteLine(name + " " + totalpages + " " + price);
    }
    Book()
    {
        name = "";
        totalpages = 0;
        price = 0.0f;
    }
}
Book b = new Book();
Which of the following statements are correct?
1. We cannot declare the access modifier of totalpages as protected.
2. We cannot declare the access modifier of name as private.
3. We cannot define a zero-argument constructor inside a structure.
4. We cannot declare the access modifier of price as public.
5. We can define a Showdata() method inside a structure.
  1. 1, 2
  2. 1, 3, 5
  3. 2, 4
  4. 3, 4, 5
Show answer & explanation
Answer: (B) 1, 3, 5
Explanation: 1. Statement 1 is true: structs cannot have protected members (no inheritance). 2. Statement 2 is false: private fields are perfectly allowed in a struct. 3. Statement 3 is true: in older C# versions a struct cannot have an explicit parameterless constructor (it is compiler-provided). 4. Statement 4 is false: public members are allowed in a struct. 5. Statement 5 is true: methods such as Showdata() can be defined inside a struct. 6. Correct set: 1, 3, 5.
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