Question Bank › Technical › C# › MCQ
Namespaces
Q101.Medium
Which of the following statements is correct about a namespace used in C#.NET?
- Nested namespaces are not allowed.
- Importing an outer namespace imports the inner namespace.
- Nested namespaces are allowed.
- If nested, the namespaces cannot be split across files.
Show answer & explanation
Answer: (C) Nested namespaces are allowed.Explanation: 1) C# allows namespaces inside namespaces, e.g. namespace A { namespace B { ... } }, so nesting is allowed. 2) Importing the outer namespace does NOT automatically import inner namespaces. 3) Namespaces can be split across multiple files. 4) Therefore the only correct statement is that nested namespaces are allowed.
Q102.Medium
Which of the following is NOT a namespace in the .NET Framework Class Library?
- System.Process
- System.Security
- System.Threading
- System.Drawing
- System.Xml
Show answer & explanation
Answer: (A) System.ProcessExplanation: 1) The .NET Framework Class Library contains namespaces such as System.Security, System.Threading, System.Drawing and System.Xml. 2) There is no namespace called System.Process; process handling lives in System.Diagnostics (Process class). 3) Hence System.Process is the odd one out.
Q103.Medium
Which of the following are NOT .NET namespaces? 1. System.Net 2. System.Calculator 3. System.IO 4. System.SoundMixer 5. System.PrinterQueue
- 1, 3
- 2, 4, 5
- 3, 5
- 1, 2, 3
Show answer & explanation
Answer: (B) 2, 4, 5Explanation: Step 1: Recall the standard .NET namespaces. System.Net and System.IO are genuine .NET namespaces.
Step 2: System.Calculator is not a .NET namespace (no such namespace exists).
Step 3: System.SoundMixer is not a .NET namespace.
Step 4: System.PrinterQueue is not a .NET namespace.
Step 5: So the non-existent ones are 2, 4 and 5.
Operator Overloading
Q104.Easy
Which keyword is used in C# to overload user-defined types by defining static member functions?
- op
- opoverload
- operator
- operatoroverload
- udoperator
Show answer & explanation
Answer: (C) operatorExplanation: 1. C# overloads operators using the 'operator' keyword.
2. The method must be public static and named like 'operator +'.
3. Example: public static Money operator +(Money a, Money b).
4. Therefore the correct keyword is 'operator'.
Q105.Medium
In C#, which of the following unary operators can be overloaded? (1) true (2) false (3) + (4) new (5) is
- 1, 2, 3
- 3, 4, 5
- 3 only
- 5 only
Show answer & explanation
Answer: (A) 1, 2, 3Explanation: 1) C# allows overloading of the unary operators +, -, !, ~, ++, --, true and false.
2) So true (1), false (2) and unary + (3) can all be overloaded.
3) The 'new' operator (4) and the 'is' operator (5) cannot be overloaded.
4) Hence the correct set is 1, 2, 3.
Q106.Medium
In C#, which of the following operators cannot be overloaded?
1. true
2. false
3. new
4. ~
5. sizeof
- 1, 3
- 2, 4
- 3, 5
- All of the above
Show answer & explanation
Answer: (C) 3, 5Explanation: Step 1: C# allows overloading of true and false (they must be overloaded together), so 1 and 2 are overloadable.
Step 2: The ~ operator (bitwise complement) can be overloaded as a unary operator, so 4 is overloadable.
Step 3: The new operator cannot be overloaded — it is used for object creation.
Step 4: The sizeof operator cannot be overloaded either.
Step 5: So the non-overloadable ones are 3 (new) and 5 (sizeof).
Q107.Hard
Which of the following statements about C# operator overloading is correct?
- The conditional logical operators cannot be overloaded.
- When a binary operator is overloaded the corresponding assignment operator, if any, must be explicitly overloaded.
- We can use the default equality operator in an overloaded implementation of the equality operator.
- A public or nested public reference type does not overload the equality operator.
- The array indexing operator can be overloaded.
Show answer & explanation
Answer: (A) The conditional logical operators cannot be overloaded.Explanation: Step 1: In C#, the conditional logical operators && and || cannot be overloaded directly; they are evaluated using the overloaded & and | plus true/false operators. So option 1 is correct.
Step 2: Compound assignment operators (like +=) are never explicitly overloaded — they are derived automatically from the binary operator. So option 2 is false.
Step 3: Using the default equality operator inside an overloaded == would cause infinite recursion, so option 3 is false.
Step 4: Reference types do inherit a default equality operator; the statement in option 4 is false.
Step 5: The array indexing operator [] cannot be overloaded in C# (only indexers with a different syntax exist). So option 5 is false.
Properties
Q108.Easy
Which of the following statements is correct about properties used in C#.NET?
- A property can simultaneously be read only and write only.
- A property can be either read only or write only.
- A write only property will have only a get accessor.
- A write only property will always return a value.
Show answer & explanation
Answer: (B) A property can be either read only or write only.Explanation: 1. A property with only a get accessor is read-only; with only a set accessor it is write-only.
2. It cannot be both read-only and write-only at the same time, so option 1 is wrong.
3. A write-only property has only a set accessor, not a get accessor, so option 3 is wrong.
4. A write-only property does not return a value, so option 4 is wrong.
5. Therefore the correct statement is that a property can be either read only or write only.
Q109.Easy
A class named Employee has a property called Salary, and emp is a reference to an Employee object. We want the statement emp.Salary = 50000; to cause a compile-time error. Which of the following will ensure this?
- Declare Salary property with both get and set accessors.
- Declare Salary property with only a set accessor.
- Declare Salary property with get, set and normal accessors.
- Declare Salary property with only a get accessor.
- None of the above
Show answer & explanation
Answer: (D) Declare Salary property with only a get accessor.Explanation: 1. Assigning a value to a property requires a set accessor.
2. If the property has only a get accessor, it is read-only and any assignment such as emp.Salary = 50000; fails to compile.
3. Options with a set accessor (1 and 2) allow assignment; option 3 is invalid syntax.
4. Hence declaring Salary with only a get accessor achieves the requirement.
Q110.Easy
In C#, a property can be declared directly inside a namespace or inside a procedure.
- True
- False
Show answer & explanation
Answer: (B) FalseExplanation: 1. A property is a member of a class or struct.
2. Namespaces can only contain types (classes, structs, etc.), not properties.
3. Procedures (methods) can only contain local variables and statements, not property declarations.
Hence the statement is False.
Q111.Medium
A class named Student has an int property called Marks with both a get and a set accessor. Which of the following statements will compile and run correctly? (1) Student.Marks = 45; (2) Student s = new Student(); s.Marks = 30; (3) Console.WriteLine(Student.Marks); (4) Student s = new Student(); int m; m = s.Marks; (5) Student s = new Student(); s.Marks = s.Marks + 15;
- 1, 3
- 2, 4, 5
- 4 only
- 3, 5
Show answer & explanation
Answer: (B) 2, 4, 5Explanation: 1. A property is an instance member, so it cannot be accessed through the class name: statement 1 (Student.Marks = 45) and statement 3 (Student.Marks) are invalid.
2. Statement 2 creates an object s and sets s.Marks = 30 using the set accessor — valid.
3. Statement 4 reads s.Marks into an int using the get accessor — valid.
4. Statement 5 reads s.Marks and writes back the sum using both accessors — valid.
5. Hence the correct statements are 2, 4 and 5.
Properties and Accessors
Q112.Easy
A class named Account has a property called accountNo, and acc is a reference to an Account object. We want the following code to work: acc.accountNo = 10; Console.WriteLine(acc.accountNo); Which of the following options will ensure this functionality?
- Declare the accountNo property with both get and set accessors.
- Declare the accountNo property with only a get accessor.
- Declare the accountNo property with get, set and normal accessors.
- Declare the accountNo property with only a set accessor.
- None of the above
Show answer & explanation
Answer: (A) Declare the accountNo property with both get and set accessors.Explanation: 1) acc.accountNo = 10 writes the property, which needs a set accessor. 2) Console.WriteLine(acc.accountNo) reads the property, which needs a get accessor. 3) Both operations are used, so both accessors are required. 4) A get-only or set-only property would fail one of the two statements. 5) Hence the property must have both get and set.
Q113.Medium
If a class named Sample has a Length property with only a set accessor, then which of the following statements will work correctly?
- Sample m = new Sample(); int l; l = m.Length;
- Sample m = new Sample(); m.Length = m.Length + 20;
- Sample.Length = 20;
- Console.WriteLine(Sample.Length);
- Sample m = new Sample(); m.Length = 10;
Show answer & explanation
Answer: (E) Sample m = new Sample(); m.Length = 10;Explanation: 1) A set-only property can only be written to, never read. 2) Option A reads m.Length, which fails. 3) Option B reads m.Length on the right side, which fails. 4) Option C uses Sample.Length on the class name, which is invalid. 5) Option D reads Sample.Length, which fails. 6) Option E only assigns a value to m.Length, which is allowed.
Q114.Medium
A class named Employee has a property called age, and emp is a reference to an Employee object. We want the statement Console.WriteLine(emp.age) to cause a compile-time error. Which of the following options will ensure this functionality?
- Declare the age property with only a get accessor.
- Declare the age property with only a set accessor.
- Declare the age property with both get and set accessors.
- Declare the age property with get, set and normal accessors.
- None of the above
Show answer & explanation
Answer: (B) Declare the age property with only a set accessor.Explanation: 1) Reading emp.age requires a get accessor. 2) If the property has only a set accessor, reading it is a compile-time error. 3) A get-only property would allow reading (and block writing), not the reverse. 4) Both accessors allow reading. 5) Hence only a set-only property makes Console.WriteLine(emp.age) fail.
Static Members
Q115.Easy
Can static methods in C# access instance (non-static) data members directly?
- Yes
- No
Show answer & explanation
Answer: (B) NoExplanation: A static method belongs to the class itself, not to any particular object, so there is no instance context. It cannot directly access instance fields or methods; it can only access static members unless an object reference is explicitly provided. Hence the answer is No.
Q116.Medium
Which of the following statements is correct about the following C# code?
class Sample
{
static int i;
int j;
public void proc1()
{
i = 11;
j = 22;
}
public static void proc2()
{
i = 1;
j = 2;
}
static Sample()
{
i = 0;
j = 0;
}
}- i cannot be initialized in proc1().
- proc1() can initialize i as well as j.
- j can be initialized in proc2().
- The constructor can never be declared as static.
- proc2() can initialize i as well as j.
Show answer & explanation
Answer: (B) proc1() can initialize i as well as j.Explanation: In the code, i is a static field and j is an instance field. proc1() is an instance method, so it can access both static and instance members. Therefore, proc1() can initialize both i and j. proc2() is static, so it can only access static members; it cannot access j. The static constructor is allowed and can initialize static fields, but it cannot access instance field j. Thus, the correct statement is that proc1() can initialize i as well as j.
Q117.Medium
Which of the following statements are correct about static functions in C#? 1. Static functions can access only static data. 2. Static functions cannot call instance functions. 3. It is necessary to initialize static data. 4. Instance functions can call static functions and access static data. 5. this reference is passed to static functions.
- 1, 2, 4
- 2, 3, 5
- 3, 4
- 4, 5
- None of these
Show answer & explanation
Answer: (A) 1, 2, 4Explanation: Step 1: Static functions belong to the class, not an object, so they can access only static data — statement 1 is correct.
Step 2: Without an object reference, a static function cannot call instance functions — statement 2 is correct.
Step 3: Static data is automatically initialized to default values, so statement 3 is false.
Step 4: Instance functions have access to the whole class, so they can call static functions and use static data — statement 4 is correct.
Step 5: No 'this' reference exists inside a static function, so statement 5 is false.
Step 6: Hence the correct set is 1, 2, 4.
String Comparison
Q118.EasyAnswer under review
If s1 and s2 are references to two strings, then which of the following is the correct way to compare the two references?
- s1 is s2
- s1 = s2
- s1 == s2
- strcmp(s1, s2)
- s1.Equals(s2)
Show answer & explanation
Answer: (E) s1.Equals(s2)Explanation: s1.Equals(s2) compares the values of the strings (content). s1 == s2 also compares values in C# (operator overloaded). s1 is s2 checks reference identity. s1 = s2 is assignment. strcmp is not a C# method. The question asks for comparing references? Actually it says 'compare the two references' meaning compare the strings they refer to? Typically, to compare string contents, use Equals. So answer is s1.Equals(s2).
Q119.Medium
If s1 and s2 are references to two strings, which of the following are the correct ways to check whether the contents of the two strings are equal? (1) if (s1 = s2) (2) if (s1 == s2) (3) int c; c = s1.CompareTo(s2); (4) if ( strcmp(s1, s2) ) (5) if (s1 is s2)
- 1, 2
- 2, 3
- 4, 5
- 3, 5
Show answer & explanation
Answer: (B) 2, 3Explanation: Step 1: Option 1 uses a single = which is assignment, not comparison — wrong.
Step 2: Option 2 uses == which compares string contents in C# — correct.
Step 3: Option 3 uses CompareTo, which returns 0 when contents are equal — correct way to test equality.
Step 4: Option 4 uses strcmp, a C function not available for C# strings — wrong.
Step 5: Option 5 uses the is operator which checks type compatibility, not content equality — wrong.
Hence the correct ways are 2 and 3.
Q120.Medium
What will be the output of the following C# code? namespace TestApp { class Program { static void Main(string[] args) { string str = "India"; Console.WriteLine(String.Compare(str, "India!").GetType()); } } }- 0
- 1
- String
- India!
- System.Int32
Show answer & explanation
Answer: (E) System.Int32Explanation: String.Compare returns an int (negative, zero, or positive). Here "India" vs "India!": "India" is shorter, so it's less than "India!", returns a negative int. GetType() on an int returns System.Int32. So output is System.Int32.
Structs
Q121.Easy
In C#, is it allowed to declare a struct with no fields (an empty struct)?
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. In C#, a struct can be declared without any instance fields, e.g. struct Empty { }.
2. The compiler permits such an empty struct declaration.
3. Therefore the statement 'creating empty structures is allowed in C#.NET' is True.
Q122.Medium
What is the output of the following C# program?
namespace DemoApp
{
struct Point
{
public int v;
}
class Program
{
static void Main(string[] args)
{
Point a = new Point();
Point b;
a.v = 7;
b = a;
b.v = 3;
Console.WriteLine(a.v + " " + b.v);
}
}
}- 7 7
- 7 3
- 3 3
- 3 7
- None of the above
Show answer & explanation
Answer: (B) 7 3Explanation: 1. a is a struct (value type) with a.v set to 7.
2. b = a copies the whole struct, so b.v is 7 at that moment.
3. Then b.v = 3 changes only b's own copy.
4. a.v remains 7 because structs are copied by value.
5. Output is '7 3'.
Q123.MediumAnswer under review
Which of the following statements about structs in C# are correct?
1. A struct can be declared inside a method (procedure).
2. A struct can implement an interface but cannot inherit from another struct.
3. Struct members cannot be declared as protected.
4. A struct can be empty.
5. It is an error to initialize an instance field directly in a struct declaration.
- 1, 2, 4
- 2, 3, 5
- 2, 4
- 1, 3
Show answer & explanation
Answer: (B) 2, 3, 5Explanation: 1. Statement 1 is false: a struct cannot be declared inside a method; it must be a type member of a class or namespace.
2. Statement 2 is true: structs may implement interfaces but cannot inherit from another struct (all structs derive from System.ValueType).
3. Statement 3 is true: structs cannot have protected members because they do not support inheritance.
4. Statement 4 is false: in C# a struct must contain at least one member (empty structs are not allowed).
5. Statement 5 is true: instance fields of a struct cannot have initializers at declaration.
6. Hence the correct set is 2, 3, 5.
Switch Statement
Q124.Medium
Which of the following statements is correct about the C#.NET code snippet given below?
switch (id)
{
case 6 : grp = "Grp B" ; break ;
case 13 : grp = "Grp D" ; break ;
case 1 : grp = "Grp A" ; break ;
case marks > 20 : grp = "Grp E" ; break ;
case Else: grp = "Grp F" ; break ;
}- The compiler will report an error in 'case marks > 20' as well as in 'case Else'.
- There is no error in this switch-case statement.
- The compiler will report an error only in 'case Else'.
- The compiler will report an error because there is no default case.
- The first three cases must be written in ascending order: case 1, case 6, case 13.
Show answer & explanation
Answer: (A) The compiler will report an error in 'case marks > 20' as well as in 'case Else'.Explanation: In C#, each case label must be a compile-time constant.
'case marks > 20' is a relational expression, not a constant, so it is invalid.
The default label must be written as 'default:', not 'case Else:', so that is also invalid.
Case labels need not be in ascending order, and a default case is optional.
Hence errors occur in both 'case marks > 20' and 'case Else'.
Q125.Medium
Consider the following C# code snippet:
int a, b, choice = 0;
switch (choice)
{
case a:
Console.WriteLine("In case a");
break;
case b:
Console.WriteLine("In case b");
break;
}
Which statement is correct about this code?- The compiler reports errors on 'case a' and 'case b' because variables cannot be used as case labels.
- The compiler reports an error because there is no default case.
- The program prints "In case a".
- The program prints "In case b".
- The code compiles without any error.
Show answer & explanation
Answer: (A) The compiler reports errors on 'case a' and 'case b' because variables cannot be used as case labels.Explanation: 1. In C#, switch case labels must be compile-time constant expressions.
2. Here 'a' and 'b' are uninitialized variables, not constants.
3. Therefore the compiler reports errors for 'case a' and 'case b'.
4. A missing default case is not an error in C#.
5. So the correct statement is that the case labels are errors.
Q126.HardAnswer under review
What will be the output of the following C#.NET code snippet?
char ch = Convert.ToChar('x' | 'y' | 'z');
switch (ch)
{
case 'X':
case 'x':
Console.WriteLine("case X | case x");
break;
case 'Y':
case 'y':
Console.WriteLine("case Y | case y");
break;
case 'Z':
case 'z':
case 'W':
case 'w':
Console.WriteLine("case W | case w");
break;
}- case X | case x
- case Y | case y
- case W | case w
- Compile Error
- No output
Show answer & explanation
Answer: (C) case W | case wExplanation: 1) 'x' = 120, 'y' = 121, 'z' = 122.
2) Bitwise OR: 120 | 121 = 121; 121 | 122 = 123.
3) Convert.ToChar(123) = '{'.
4) '{' matches none of the case labels X,x,Y,y,Z,z,W,w.
5) Wait — check: 123 is '{', which is not 'w' (119). So no case matches and nothing is printed.
6) Rechecking: the intended result is that the OR value falls into the 'w' case only if it equals 119, which it does not. Hence no output.
Type Conversion
Q127.Medium
Which of the following statements about type conversion in C# is correct?
- Information is never lost during narrowing conversions.
- The CInteger() function can be used to convert a float to an int.
- Widening conversions take place automatically.
- Assigning an int to an object type is known as unboxing.
- 3.14 can be treated as decimal by using it in the form 3.14F.
Show answer & explanation
Answer: (C) Widening conversions take place automatically.Explanation: Widening conversions (e.g., int to long) are implicit and automatic. Narrowing conversions may lose data. C# uses Convert.ToInt32() or casts, not CInteger(). Assigning int to object is boxing, not unboxing. 3.14F is a float literal, not decimal (decimal uses M suffix).
Q128.Medium
In C#, which of the following are correct ways to convert the string s = "456" into an int variable i? (1) i = (int)s; (2) i = int.Parse(s); (3) i = Int32.Parse(s); (4) i = Convert.ToInt32(s); (5) i = CInt(s);
- 1, 3, 5
- 2, 4
- 3, 5
- 2, 3, 4
Show answer & explanation
Answer: (D) 2, 3, 4Explanation: 1. A direct cast (int)s is invalid because string and int are not related by an explicit conversion operator.
2. int.Parse(s) is valid — int is an alias of System.Int32.
3. Int32.Parse(s) is valid — same method via the full type name.
4. Convert.ToInt32(s) is valid — the Convert class provides this overload.
5. CInt is a VB.NET function, not available in C#.
So the correct ways are 2, 3 and 4.
Q129.Medium
Which of the following snippets are the correct way to convert a Single into a String in C#? 1. Single f = 9.8f; String s; s = (String)(f); 2. Single f = 9.8f; String s; s = Convert.ToString(f); 3. Single f = 9.8f; String s; s = f.ToString(); 4. Single f = 9.8f; String s; s = Clnt(f); 5. Single f = 9.8f; String s; s = CString(f);
- 1, 2
- 2, 3
- 1, 3, 5
- 2, 4
Show answer & explanation
Answer: (B) 2, 3Explanation: Snippet 1: (String)(f) is an explicit cast; Single cannot be directly cast to String, so it's invalid. Snippet 2: Convert.ToString(f) is valid. Snippet 3: f.ToString() is valid. Snippet 4: Clnt is not a method. Snippet 5: CString is not a method. So correct are 2 and 3.
Other questions
Q130.Easy
In .NET, which type of assembly is designed to be shared by multiple applications and can therefore be installed in the Global Assembly Cache (GAC)?
- Private assemblies
- Friend assemblies
- Shared assemblies
- Satellite assemblies
- Protected assemblies
Show answer & explanation
Answer: (C) Shared assembliesExplanation: 1. Private assemblies are used by a single application and kept in its own folder.
2. Shared assemblies are meant to be used by many applications on the machine.
3. The GAC is the machine-wide store for such shared assemblies (they need a strong name).
4. So the assemblies stored in the GAC are shared assemblies.
Q131.Easy
In .NET, code that runs under the control of the Common Language Runtime (CLR) is referred to as:
- Unmanaged code
- Distributed code
- Legacy code
- Managed code
- Native code
Show answer & explanation
Answer: (D) Managed codeExplanation: 1. The CLR provides services like garbage collection, type safety and JIT compilation.
2. Code that executes under these CLR services is called managed code.
3. Code that bypasses the CLR (e.g., raw C/C++ compiled to machine code) is unmanaged code.
4. Hence the correct term is 'Managed code'.
Q132.Easy
Which of the following is the root of the .NET type hierarchy?
- System.Object
- System.Type
- System.Base
- System.Parent
- System.Root
Show answer & explanation
Answer: (A) System.ObjectExplanation: Step 1: In .NET, every type directly or indirectly derives from a single base type.
Step 2: That base type is System.Object (also called object in C#).
Step 3: System.Type describes type metadata, not the root; System.Base, System.Parent and System.Root do not exist.
Step 4: Therefore the root of the .NET type hierarchy is System.Object.
Q133.Easy
In .NET, which statement best describes managed code?
- Code that is executed under the control of the Common Language Runtime (CLR), which provides services such as garbage collection and JIT compilation.
- Code that is compiled directly to native machine code by a C++ compiler before execution.
- Code that can only run on the Windows operating system.
- Code that is interpreted line by line by the operating system kernel.
Show answer & explanation
Answer: (A) Code that is executed under the control of the Common Language Runtime (CLR), which provides services such as garbage collection and JIT compilation.Explanation: Managed code is code that runs under the CLR. The CLR provides services like JIT compilation, garbage collection, and type safety. Options 2 and 4 describe unmanaged/native or interpreted execution, and option 3 is wrong because managed code can run on Linux via .NET Core.
Q134.Easy
Which .NET utility converts a managed assembly into processor-specific native code ahead of time, so it can start without JIT compilation?
- gacutil
- ngen
- sn
- dumpbin
- ildasm
Show answer & explanation
Answer: (B) ngenExplanation: 1. gacutil installs assemblies into the Global Assembly Cache.
2. ngen (Native Image Generator) pre-compiles a managed assembly into native code for the current processor.
3. sn handles strong-name signing, dumpbin inspects PE files, and ildasm disassembles IL.
4. Therefore the utility that produces processor-specific native code is ngen.
Q135.Easy
Which of the following statements correctly defines the .NET Framework?
- It is an environment for developing, building, deploying and executing desktop applications, web applications and web services.
- It is an environment for developing, building, deploying and executing only web applications.
- It is an environment for developing, building, deploying and executing distributed applications.
- It is an environment for developing, building, deploying and executing web services.
- It is an environment for development and execution of Windows applications only.
Show answer & explanation
Answer: (A) It is an environment for developing, building, deploying and executing desktop applications, web applications and web services.Explanation: The .NET Framework is a general-purpose platform, not limited to one application type.
It supports desktop (WinForms/WPF), web (ASP.NET) applications and web services.
Options that say 'only web applications' or 'only Windows applications' are too narrow.
The broad, complete definition is the one covering desktop, web applications and web services.
Q136.Easy
What will be the output of the C# code snippet given below?
namespace DelhiConsoleApp
{
class DemoProgram
{
static void Main(string[] args)
{
int i, j;
int[,] grid = new int[2, 2];
for (i = 0; i < 2; ++i)
{
for (j = 0; j < 2; ++j)
{
grid[i, j] = i * 13 + i * 13;
Console.Write(grid[i, j] + " ");
}
}
}
}
}- 0 0 26 26
- 0 0 13 13
- 0 0 0 0
- 13 13 0 0
- 26 26 0 0
Show answer & explanation
Answer: (A) 0 0 26 26Explanation: 1) i*13 + i*13 = 26*i. 2) When i = 0: value = 0 for both j = 0 and j = 1, printing '0 0 '. 3) When i = 1: value = 26 for both j = 0 and j = 1, printing '26 26 '. 4) Combined output: 0 0 26 26.
Q137.Easy
Which one of the following statements about C# arrays is correct?
- Array elements can only be of integer type.
- The rank of an array is the total number of elements it can hold.
- The length of an array is the number of dimensions in the array.
- The default value of numeric array elements is zero.
- Array elements are always guaranteed to be sorted.
Show answer & explanation
Answer: (D) The default value of numeric array elements is zero.Explanation: 1) Arrays can hold any type, not just int, so option 1 is wrong. 2) Rank = number of dimensions, not total elements, so option 2 is wrong. 3) Length = total number of elements, not dimensions, so option 3 is wrong. 4) When an int array is created with new, all elements are initialized to 0 by default — this is correct. 5) Arrays are not sorted automatically, so option 5 is wrong.
Q138.Easy
Which of the following is the correct output of the C#.NET code snippet given below?
int[ , , ] b = new int[ 3, 2, 3 ];
Console.WriteLine(b.Length);
- 20
- 4
- 18
- 10
- 5
Show answer & explanation
Answer: (C) 18Explanation: 1. b is a 3-dimensional rectangular array declared as new int[3,2,3].
2. The Length property of a multi-dimensional array returns the total number of elements, i.e. the product of all dimensions.
3. Total = 3 × 2 × 3 = 18.
4. So Console.WriteLine(b.Length) prints 18.
Q139.Easy
Which of the following is the correct way to create an object of the class Sample?
1. Sample s = new Sample();
2. Sample s;
3. Sample s; s = new Sample();
4. s = new Sample();
- 1, 3
- 2, 4
- 1, 2, 3
- 1, 4
- None of these
Show answer & explanation
Answer: (A) 1, 3Explanation: 1. Statement 1 declares and instantiates in one line — valid.
2. Statement 2 only declares a reference; no object is created.
3. Statement 3 declares then assigns a new object — valid.
4. Statement 4 uses s without declaring it — compile error.
5. So the correct ways are 1 and 3.
Q140.Easy
Which of the following statements is correct about the C#.NET code snippet given below?
int a;
int b = new int();
a = 15;
b = 25;
String s;
s = a.ToString();
s = b.ToString();
- This is a perfectly workable code snippet.
- Since int is a primitive, we cannot use new with it.
- Since an int is a primitive, we cannot call the method ToString() using it.
- a will get created on stack, whereas b will get created on heap.
- Both a and b will get created on heap.
Show answer & explanation
Answer: (A) This is a perfectly workable code snippet.Explanation: In C#, int is an alias for System.Int32, a value type. You can create it with new int(), which initializes to 0. Both a and b are value types and are stored on the stack (when local). You can call ToString() on them. So the code compiles and runs without error.
Q141.Easy
In C#, the this reference gets created when a non-static member function of a class is called.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. this refers to the current instance of the class.
2. It is only meaningful when an instance method is invoked on an object.
3. Static (shared) methods have no instance, so no this is created for them.
4. Hence the statement is True.
Q142.Easy
Which of the following will be the correct output for the C#.NET program given below?
namespace DelhiConsoleApp
{
class Employee
{
int id;
Single salary;
public void SetData(int id, Single salary)
{
this.id = id;
this.salary = salary;
}
public void Display()
{
Console.WriteLine(id + " " + salary);
}
}
class MyProgram
{
static void Main(string[] args)
{
Employee e1 = new Employee();
e1.SetData(45, 7.2f);
e1.Display();
}
}
}- 0 0.0
- 45 7.2
- 45 7.200000
- 45 7
- None of the above
Show answer & explanation
Answer: (B) 45 7.2Explanation: The SetData method assigns id=45 and salary=7.2f. Display prints id and salary separated by a space. The float value 7.2f is printed as 7.2 without trailing zeros. Therefore output is '45 7.2'.
Q143.Easy
In C#, it is illegal to declare an object of one class as a member (field) of another class.
- True
- False
Show answer & explanation
Answer: (B) FalseExplanation: 1) A class can contain fields whose type is another class; this is called composition/aggregation.
2) Example: class Engine {} and class Car { Engine e; } compiles fine.
3) Only restrictions apply to certain cases (e.g., a struct cannot contain a field of its own type directly), but a class member of another class type is perfectly legal.
4) Hence the statement is False.
Q144.Easy
Which of the following statements about programming paradigms is correct?
- Procedural programming and structured programming are completely unrelated paradigms.
- Object Oriented Programming stresses on dividing the logic into smaller parts and writing procedures for each part.
- Classes and objects are the corner stones of the structured programming paradigm.
- Object Oriented Programming gives equal importance to data and the procedures that operate on the data.
- Java is a purely structured programming language.
Show answer & explanation
Answer: (D) Object Oriented Programming gives equal importance to data and the procedures that operate on the data.Explanation: 1) Procedural and structured programming are closely related, so option 1 is wrong. 2) Dividing logic into procedures is the procedural approach, not OOP, so option 2 is wrong. 3) Classes and objects belong to OOP, not structured programming, so option 3 is wrong. 4) OOP bundles data and the methods that work on it together (encapsulation), giving both equal importance — this is correct. 5) Java is object oriented, not purely structured, so option 5 is wrong.
Q145.Easy
An ArrayList has its Capacity property set to 4. What will be the capacity after adding a fifth element?
- 4
- 8
- 16
- 32
Show answer & explanation
Answer: (B) 8Explanation: When the number of elements exceeds the capacity, ArrayList automatically doubles its capacity. Starting with capacity 4, adding the fifth element triggers resizing to 4 * 2 = 8.
Q146.Easy
In C#, which property of an ArrayList object named 'list' returns the number of elements currently stored in it?
- list.Count
- list.GrowSize
- list.MaxIndex
- list.Capacity
- list.UpperBound
Show answer & explanation
Answer: (A) list.CountExplanation: The ArrayList class has a Count property that gives the number of elements actually present. Capacity is the number of elements it can hold before resizing. GrowSize, MaxIndex, and UpperBound are not properties of ArrayList.
Q147.Easy
In a C# HashTable, a Key cannot be null, but a Value can be null.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: Step 1: A HashTable uses keys to compute hash codes for lookup; a null key cannot be hashed, so adding a null key throws an exception.
Step 2: Values are simply stored objects and are not hashed, so a null value is permitted.
Therefore the statement is True.
Q148.Easy
Consider the following C# code:
Stack st = new Stack();
st.Push("namaste");
st.Push(7.5);
st.Push(12);
st.Push('k');
st.Push(false);
Which of the following statements about this code is correct?- Dissimilar elements like "namaste", 7.5 and 12 cannot be stored in the same Stack.
- Boolean values can never be stored in a Stack collection.
- In the fourth call to Push(), we should write "k" instead of 'k'.
- To store dissimilar elements, a method PushAnyType() must be used instead of Push().
- This is a perfectly workable code.
Show answer & explanation
Answer: (E) This is a perfectly workable code.Explanation: Step 1: The non-generic Stack class stores items as object, so it accepts any type.
Step 2: Strings, doubles, ints, chars and bools are all valid objects, so mixing them is allowed — option 1 is wrong.
Step 3: Boolean values are objects and can be pushed, so option 2 is wrong.
Step 4: 'k' is a valid char literal, so option 3 is wrong.
Step 5: There is no PushAnyType() method; Push() already accepts any type, so option 4 is wrong.
Hence the code compiles and runs fine.
Q149.Easy
A HashTable named capitals stores Indian state names as keys and their capital cities as values. Which of the following correctly checks whether the key "Kerala" exists in this HashTable?
- capitals.ContainsKey("Kerala");
- capitals.HasValue("Kerala");
- capitals.HasKey("Kerala");
- capitals.ContainsState("Kerala");
- capitals.ContainsValue("Kerala");
Show answer & explanation
Answer: (A) capitals.ContainsKey("Kerala");Explanation: Step 1: In C#, the HashTable class provides the ContainsKey method to test whether a given key is present.
Step 2: HasValue, HasKey and ContainsState are not methods of HashTable, so those options are invalid.
Step 3: ContainsValue checks the values (capital cities), not the keys (state names), so it would not test for "Kerala" as a key.
Therefore capitals.ContainsKey("Kerala") is the correct call.
Q150.Easy
Which of the following statements is correct about static functions (static methods) in C#?
- Static functions are invoked using objects of a class.
- Static functions can access static data as well as instance data.
- Static functions are outside the class scope.
- Static functions are invoked using the class name.
Show answer & explanation
Answer: (D) Static functions are invoked using the class name.Explanation: 1. Static methods belong to the class, not to any object, so they are called using the class name (e.g., ClassName.Method()).
2. They cannot access instance data directly because no object is associated, so option 2 is false.
3. They are declared inside the class, so option 3 is false.
4. Hence the correct statement is that static functions are invoked using the class name.
Q151.Easy
In C#, which of the following can be used to terminate a while loop and transfer control outside the loop?
1. exit while
2. continue
3. exit statement
4. break
5. goto
- 1, 3
- 2, 4
- 3, 5
- 4, 5
- None of these
Show answer & explanation
Answer: (D) 4, 5Explanation: 'break' immediately exits the loop. 'goto' can jump to a label outside the loop, also terminating it. 'continue' only skips to the next iteration, and 'exit while'/'exit statement' are not valid C# constructs. Hence 4 and 5 are correct.
Q152.Easy
What kind of number series does the following C# code print?
int a = 1, b = 1, sum;
while (a < 30)
{
Console.Write(b + " ");
sum = a + b;
b = a;
a = sum;
}- Prime numbers
- Fibonacci numbers
- Palindrome numbers
- Odd numbers
Show answer & explanation
Answer: (B) Fibonacci numbersExplanation: Initially a=1, b=1. Each iteration prints b, then sets sum=a+b, b=a, a=sum. So the printed values are 1, 1, 2, 3, 5, 8, 13, 21, 34... which is the Fibonacci series. The loop stops when a reaches or exceeds 30.
Q153.Easy
Which of the following loops correctly prints every element of the array declared as:
char[] letters = new char[] { 'm', 'a', 'n', 'g', 'o' };
(Each character should be printed on its own line.)- do { Console.WriteLine((char)i); } while (int i = 0; i < letters; i++);
- foreach (char c in letters) { Console.WriteLine(c); }
- for (int i = 0; i < letters; i++) { Console.WriteLine((char)i); }
- while (int i = 0; i < letters; i++) { Console.WriteLine((char)i); }
- do { Console.WriteLine((char)i); } until (int i = 0; i < letters; i++);
Show answer & explanation
Answer: (B) foreach (char c in letters) { Console.WriteLine(c); }Explanation: 1. A foreach loop iterates over each element of the array directly.
2. 'char c' takes each character value 'm','a','n','g','o' in turn.
3. Console.WriteLine(c) prints each character on its own line.
4. The other options are invalid C# syntax (declarations inside while/do conditions, 'until' keyword, comparing int to array).
5. Hence the foreach loop is correct.
Q154.Easy
What is the output of the following C#.NET code snippet?
namespace ShopConsoleApplication
{
public enum shade { pink, violet, indigo };
class SampleProgram
{
static void Main(string[] args)
{
shade s = shade.indigo;
switch (s)
{
case shade.pink:
Console.WriteLine(shade.pink);
break;
case shade.violet:
Console.WriteLine(shade.violet);
break;
case shade.indigo:
Console.WriteLine(shade.indigo);
break;
}
}
}
}- pink
- indigo
- 0
- 1
- 2
Show answer & explanation
Answer: (B) indigoExplanation: 1) The enum shade has pink = 0, violet = 1, indigo = 2.
2) s is set to shade.indigo.
3) The switch matches case shade.indigo.
4) Console.WriteLine(shade.indigo) prints the enum name, i.e. indigo.
Q155.Easy
Which of the following is the correct way to rewrite the following C#.NET code snippet?
int i = 0;
do
{
Console.WriteLine(i);
i += 1;
} while (i <= 8);- int i = 0; do { Console.WriteLine(i); } until (i <= 8);
- int i; for (i = 0; i <= 8; i++) Console.WriteLine(i);
- int i = 0; while (i <= 9) { Console.WriteLine(i); i += 1; }
- int i = 0; do while (i <= 8) { Console.WriteLine(i); i += 1; }
- int i = 0; do until (i <= 8) { Console.WriteLine(i); i += 1; }
Show answer & explanation
Answer: (B) int i; for (i = 0; i <= 8; i++) Console.WriteLine(i);Explanation: 1) The do-while prints i = 0,1,2,...,8 (stops when i becomes 9).
2) A for loop with i = 0; i <= 8; i++ prints exactly 0 through 8.
3) Option 2 matches this exactly.
4) Option 3 prints 0..9 (one extra), options 1,4,5 use invalid C# syntax (until / do while).
Q156.Easy
Which of the following is NOT a valid form of decision control instruction in C#?
- if (Condition1) { // statement }
- if (Condition1) { // statement } else { // statement }
- if (Condition1) { // statement } else { // statement } else if (Condition2) { // statement }
- if (Condition1) { // statement } else if (Condition2) { // statement } else { // statement }
- if (Condition1) { // statement } else if (Condition2) { // statement } else if (Condition3) { // statement } else { // statement }
Show answer & explanation
Answer: (C) if (Condition1) { // statement } else { // statement } else if (Condition2) { // statement }Explanation: In C#, an if can be followed by at most one else, and any else if chain must come before the final else. Option 3 places an else if after an else, which is invalid syntax. Options 1, 2, 4 and 5 are all valid forms.
Q157.Easy
What is the size of the decimal data type in C#?
- 8 bytes
- 4 bytes
- 10 bytes
- 16 bytes
- None of the above
Show answer & explanation
Answer: (D) 16 bytesExplanation: The C# decimal type is a 128-bit (16-byte) floating-point type. It is not 8 bytes (that is double/long), not 4 bytes (that is int/float), and not 10 bytes. Therefore the correct size is 16 bytes.
Q158.Easy
What is the size of a Decimal data type in C#?
- 4 bytes
- 8 bytes
- 16 bytes
- 32 bytes
Show answer & explanation
Answer: (C) 16 bytesExplanation: 1. In C#, decimal is a 128-bit (16-byte) data type.
2. float is 4 bytes and double is 8 bytes.
3. Hence the size of decimal is 16 bytes.
Q159.Easy
In C#, which of the following correctly sets the value 3.14 in a variable pi such that it cannot be modified afterwards?
- float pi = 3.14F;
- #define pi 3.14F;
- const float pi = 3.14F;
- const float pi; pi = 3.14F;
Show answer & explanation
Answer: (C) const float pi = 3.14F;Explanation: Step 1: To make a value unmodifiable, use the const keyword.
Step 2: A const must be assigned a value at the point of declaration.
Step 3: 'const float pi = 3.14F;' satisfies both — declared const and initialized immediately.
Step 4: 'float pi = 3.14F;' is modifiable; '#define' is a preprocessor macro, not a variable; 'const float pi; pi = 3.14F;' is invalid because a const cannot be assigned later.
Q160.Easy
Which of the following are correct ways to initialize two integer variables i and j each to the value 10 in C#?
1. int i = 10; int j = 10;
2. int i, j; i = 10 : j = 10;
3. int i = 10, j = 10;
4. int i, j = 10;
5. int i = j = 10;
- 1, 3
- 2, 4
- 3, 5
- 4, 5
Show answer & explanation
Answer: (A) 1, 3Explanation: Step 1: Option 1 declares and initializes each variable separately — valid.
Step 2: Option 2 uses a colon ':' instead of a semicolon — invalid syntax.
Step 3: Option 3 declares both in one statement with initializers — valid.
Step 4: Option 4 initializes only j; i is left unassigned — not both set to 10.
Step 5: Option 5 uses j before it is declared — invalid.
Step 6: So the correct ways are 1 and 3.
Q161.Easy
In C#, what is the default value assigned to a bool variable that is declared as a field (class-level) and not explicitly initialized?
- true
- false
- 0
- null
Show answer & explanation
Answer: (B) falseExplanation: Step 1: In C#, fields that are not explicitly initialized get their type's default value.
Step 2: The default value of the bool type is false (not true, and not 0/null).
Step 3: So an uninitialized bool field holds false.
Q162.Easy
In C#.NET, structures are always value types.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: Step 1: In C#, a struct is defined as a value type.
Step 2: Value types hold their data directly and are copied by value.
Step 3: Unlike classes (reference types), structs are never reference types.
Step 4: Hence the statement is True.
Q163.Easy
In C#, the code inside a finally block always executes, whether or not an exception is thrown in the associated try block.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. A try block is followed by catch and/or finally.
2. The finally block is designed for cleanup and is guaranteed to run.
3. It runs whether the try block completes normally, throws an exception that is caught, or throws one that is not caught.
4. Hence the statement is True.
Q164.Easy
In C#, which of the following is the object-oriented way of handling run-time errors?
- OnError
- HRESULT
- Exceptions
- Error codes
- Setjump and Longjump
Show answer & explanation
Answer: (C) ExceptionsExplanation: 1) C# is object oriented and models run-time errors as objects of exception classes.
2) These are thrown and caught using try/catch/finally.
3) OnError, HRESULT, error codes and setjmp/longjmp are older or non-OO mechanisms.
4) Hence the OO way is Exceptions.
Q165.Easy
In C#, how many values can a single function return directly using the return statement?
- Exactly 1
- 0
- As many as the number of params arguments it uses
- Any number of values
- As many as the number of ref arguments it uses
Show answer & explanation
Answer: (A) Exactly 1Explanation: 1. A C# method's return type specifies a single value (or void for none).
2. The return statement can therefore send back only one value at a time.
3. Multiple results must be packaged (e.g., in an object, tuple, or via out/ref parameters), but the function itself returns one value.
4. Hence a function is capable of returning exactly 1 value.
Q166.Easy
How many values can a single method (subroutine) in C# return directly through its return statement?
- Depends on how many parameters it takes.
- Any number of values.
- Depends on how many ref arguments it uses.
- 0
- 1
Show answer & explanation
Answer: (E) 1Explanation: Step 1: A method's return type specifies exactly one value (or void for none). Step 2: Extra results can only be sent back through ref/out parameters, not through the return statement itself. Step 3: Hence a method returns at most one value directly.
Q167.Easy
Which of the following can appear only ONCE in a C# program?
- namespace
- Entry point (Main method)
- Class
- Function
- Subroutine
Show answer & explanation
Answer: (B) Entry point (Main method)Explanation: Step 1: A program may contain many namespaces, classes, functions and subroutines. Step 2: Execution must begin at exactly one place, so only one entry point (Main method) is allowed per executable. Step 3: Therefore the entry point cannot occur multiple times.
Q168.Easy
What will be the output of the following C#.NET code?
namespace DelhiConsoleApp
{
class DemoProgram
{
static void Main( string [ ] args )
{
int i ;
int res = compute( out i );
Console.WriteLine (res);
}
static int compute ( out int i )
{
int s = 1 ;
i = 6 ;
for ( int j = 1 ; j <= i ; j++ )
{
s = s * j;
}
return s;
}
}
}- 1
- 6
- 7
- 120
- 720
Show answer & explanation
Answer: (E) 720Explanation: Step 1: compute sets i = 6 and s = 1. Step 2: The loop runs j = 1 to 6, multiplying s by each j: s = 1*1*2*3*4*5*6. Step 3: That product is 720. Step 4: compute returns 720, which Main prints. So the output is 720.
Q169.Easy
In C#, a method that returns a value can be used inside an expression, whereas a method that returns void (a subroutine) cannot.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: A value-returning method produces a result that can be part of an expression, e.g. int t = 5 + GetValue();. A void method returns nothing, so it cannot appear in an expression — it can only be called as a statement. Hence the statement is True.
Q170.Easy
In C#.NET, a function returns a value, whereas a subroutine (void method) cannot return a value.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: In C#, a function is a method that returns a value using a return type other than void. A subroutine is a method declared with 'void' return type, which does not return a value. Therefore the statement is true.
Q171.Easy
What will be the output of the following C# code?
namespace ChennaiConsoleApp
{
class DemoProgram
{
static void Main(string[] args)
{
int[] arr = new int[]{ 2, 4, 6, 8 };
modify(ref arr);
}
static void modify(ref int[] a)
{
for (int i = 0; i < a.Length; i++)
{
a[i] = a[i] * 3;
Console.Write(a[i] + " ");
}
}
}
}- 2 4 6 8
- 5 7 9 11
- 6 12 18 24
- 6 36 216 1296
- 4 8 12 16
Show answer & explanation
Answer: (C) 6 12 18 24Explanation: The array is {2,4,6,8}. The loop multiplies each element by 3: 2*3=6, 4*3=12, 6*3=18, 8*3=24, printing each followed by a space. Output: 6 12 18 24.
Q172.Easy
What will be the output of the following C# code?
namespace DelhiConsoleApp
{
class DemoProgram
{
static void Main(string[] args)
{
int p = 4;
int q;
calc1(ref p);
calc2(out q);
Console.WriteLine(p + ", " + q);
}
static void calc1(ref int x)
{
x = x * x;
}
static void calc2(out int x)
{
x = 7;
x = x * x;
}
}
}- 4, 7
- 4, 49
- 16, 49
- 16, 0
- 4, 0
Show answer & explanation
Answer: (C) 16, 49Explanation: p starts at 4. calc1 takes p by ref and sets x = x*x = 4*4 = 16, so p becomes 16. calc2 takes q as out; inside, x = 7 then x = 7*7 = 49, so q becomes 49. The output is 16, 49.
Q173.Easy
What is the output of the following C# program?
namespace DemoApp
{
class Program
{
static void Main(string[] args)
{
int a = 4;
int sq = 0, cube = 0;
Compute(a, ref sq, ref cube);
Console.WriteLine(sq + " " + cube);
}
static void Compute(int x, ref int s, ref int c)
{
s = x * x;
c = x * x * x;
}
}
}- 0 0
- 16 16
- 64 64
- 16 64
- None of the above
Show answer & explanation
Answer: (D) 16 64Explanation: a=4 is passed by value; sq and cube are passed by reference. Inside Compute: s = 4*4 = 16, c = 4*4*4 = 64. Since ref is used, the changes reflect in sq and cube. Output: '16 64'.
Q174.Easy
A method named Compute() 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 method in C#?
- Compute( int a, float b, double c) decimal { ... }
- static decimal Compute( int a, float b, double c) { ... }
- Compute( int a, float b, double c) { ... return decimal; }
- static decimal Compute( int a, float b, double c) decimal { ... }
- A method can never return a value.
Show answer & explanation
Answer: (B) static decimal Compute( int a, float b, double c) { ... }Explanation: 1) A C# method signature is: access/static modifier, return type, name, parameter list, then the body in braces.
2) The return type (decimal) must come before the method name, not after the parameter list.
3) Option 1 and option 4 wrongly place 'decimal' after the parameter list.
4) Option 3 has no return type and returns the keyword 'decimal' instead of a value.
5) Option 5 is false because methods can return values.
6) So the correct definition is: static decimal Compute( int a, float b, double c) { ... }.
Q175.Easy
In C#, which of the following is the correct syntax to apply an attribute to an assembly?
- [ AssemblyDescription (" Bharat Component Library") ]
- [ assembly : AssemblyDescription (" Bharat Component Library") ]
- [ AssemblyInfo : AssemblyDescription (" Bharat Component Library") ]
- < Assembly: AssemblyDescription (" Bharat Component Library") >
- (Assembly: AssemblyDescription (" Bharat Component Library"))
Show answer & explanation
Answer: (B) [ assembly : AssemblyDescription (" Bharat Component Library") ]Explanation: 1. Assembly-level attributes use the target prefix 'assembly' followed by a colon.
2. The attribute is enclosed in square brackets: [assembly: ...].
3. So the correct form is [assembly : AssemblyDescription("Bharat Component Library")].
4. Options without the 'assembly:' target prefix, or using angle brackets/parentheses, are invalid syntax.
5. Hence option 2 is correct.
Q176.Easy
Which of the following is a correct way of applying an attribute in C#?
- [ WebService (Name = "BharatPay" , Description = "BharatPay WebService" )] class PaymentService : WebService { /* .... */ }
- < WebService ( Name : "BharatPay" , Description : "BharatPay WebService" )> class PaymentService : inherits WebService { /* .... */ }
- < WebService ( Name = "BharatPay" , Description = "BharatPay WebService" )> class PaymentService : extends WebService { /* .... */ }
- [ WebService ( Name := "BharatPay" , Description := "BharatPay WebService" )] class PaymentService : inherits WebService { /* .... */ }
Show answer & explanation
Answer: (A) [ WebService (Name = "BharatPay" , Description = "BharatPay WebService" )] class PaymentService : WebService { /* .... */ }Explanation: C# attributes use square brackets, named arguments use '=' (not ':' or ':='), and inheritance uses ':' followed by the base class name (not 'inherits' or 'extends'). Option 1 follows all these rules. Options 2 and 3 use angle brackets and wrong keywords; option 4 uses ':=' and 'inherits'. Hence option 1 is correct.
Q177.Easy
Which of the following forms of applying an attribute is correct in C#?
- < Serializable() > class sample { /* ... */ }
- (Serializable()) class sample { /* ... */ }
- [ Serializable() ] class sample { /* ... */ }
- Serializablef ) class sample { /* ... */ }
- None of the above
Show answer & explanation
Answer: (C) [ Serializable() ] class sample { /* ... */ }Explanation: In C#, attributes are applied using square brackets placed before the declaration they modify. So [Serializable()] class sample { } is valid. Angle brackets (< >) are used in other languages, parentheses alone are invalid, and 'Serializablef )' is malformed. Hence option 3 is correct.
Q178.Easy
Which of the following correctly describes the contents of the file AssemblyInfo.cs in a C# project?
- It contains method-level attributes.
- It contains class-level attributes.
- It contains assembly-level attributes.
- It contains structure-level attributes.
- It contains namespace-level attributes.
Show answer & explanation
Answer: (C) It contains assembly-level attributes.Explanation: 1) AssemblyInfo.cs is a source file added to a project by default.
2) It holds attributes applied at assembly level, such as AssemblyTitle, AssemblyVersion and AssemblyCompany.
3) These attributes describe the assembly as a whole, not individual methods, classes, structures or namespaces.
4) Therefore the correct description is assembly-level attributes.
Q179.Easy
Which of the following classes are present in the System.Collections.Generic namespace? 1. Stack 2. Tree 3. SortedDictionary 4. SortedArray
- 1 and 2 only
- 2 and 4 only
- 1 and 3 only
- All of the above
- None of the above
Show answer & explanation
Answer: (C) 1 and 3 onlyExplanation: 1) System.Collections.Generic contains Stack<T> and SortedDictionary<TKey,TValue>.
2) There is no class named Tree in that namespace.
3) There is no class named SortedArray in that namespace (SortedList and SortedSet exist).
4) Hence only items 1 and 3 are correct.
Q180.EasyAnswer under review
Consider the following code snippet:
public class TestIndiaBix
{
public void TestSub<M>(M arg)
{
Console.Write(arg);
}
}
class MyProgram
{
static void Main(string[] args)
{
TestIndiaBix bix = new TestIndiaBix();
bix.TestSub("IndiaBIX");
bix.TestSub(4.2f);
}
}
Which of the following statements is valid?- Program will compile and on execution will print: IndiaBIX 4.2
- A non-generic class Hello cannot have generic subroutine.
- Compiler will generate an error.
- Program will generate a run-time exception.
Show answer & explanation
Answer: (A) Program will compile and on execution will print: IndiaBIX 4.2Explanation: TestSub is a generic method that accepts any type M. The calls pass a string and a float, which are valid. The method writes the argument to the console without a newline, so the output is 'IndiaBIX4.2' (with no space). The option says 'IndiaBIX 4.2' but the actual output has no space; however, among the given options, option 1 is the closest and the only one that correctly states it compiles and prints the values. The other options are false.
Q181.Easy
Consider the following code snippet:
public class Generic<T>
{
public T Field;
}
class Program
{
static void Main(string[] args)
{
Generic<string> g = new Generic<string>();
g.Field = "Hello";
Console.WriteLine(g.Field);
}
}
Which of the following statements is valid?- It will print string "Hello" on the console.
- Name Generic cannot be used as a class name because it's a keyword.
- Compiler will give an error.
- Member Field of class Generic is not accessible directly.
Show answer & explanation
Answer: (A) It will print string "Hello" on the console.Explanation: The code defines a generic class Generic<T> with a public field Field of type T. In Main, it creates an instance with T=string, assigns "Hello" to Field, and prints it. This compiles and runs, printing Hello. 'Generic' is not a keyword, and the field is public so accessible.
Q182.Easy
In C#, multiple inheritance (deriving from more than one class) is different from multi-level inheritance (a chain of base classes). Is this statement correct?
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. Multiple inheritance means one class derives from two or more classes at once.
2. Multi-level inheritance means a class derives from a base class which itself derives from another base class (a chain).
3. These are different concepts.
4. Note: C# does not allow multiple class inheritance (only via interfaces), but the statement about the concepts being different is True.
Q183.Easy
In C#, a class D can be derived from class C, which is itself derived from class B, which is derived from class A. Is this multi-level inheritance chain allowed?
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. C# allows a class to inherit from a single base class, and that base class may itself inherit from another class.
2. So A -> B -> C -> D is a valid multi-level inheritance chain.
3. Each level adds or overrides members; D inherits members of C, B and A.
4. Therefore the statement is True.
Q184.Easy
In C#, just as a derived class member function can access public members of the base class, a base class member function can access public member functions of the derived class.
- True
- False
Show answer & explanation
Answer: (B) FalseExplanation: 1. A base class is written before any derived class exists, so it has no knowledge of derived classes.
2. Therefore base class code cannot directly access members declared in a derived class.
3. Access works the other way: derived classes can reach accessible base members.
4. So the statement is False.
Q185.Easy
In C#, is it true that there is no private or protected inheritance between classes?
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1) C# supports only public class inheritance (and interface implementation).
2) Unlike C++, C# has no syntax for private or protected class inheritance.
3) Access modifiers control member visibility, not the inheritance relationship itself.
4) Therefore the statement is true.
Q186.Easy
In C#, can you create a derived class from a base class whose source code you do not have (for example, a compiled library class)?
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1) C# inheritance requires only the compiled metadata of the base class, not its source code.
2) A referenced assembly exposes its public and protected types and members.
3) You can write 'class MyClass : LibraryClass' as long as LibraryClass is accessible and not sealed.
4) Hence deriving from a base class without its source code is possible.
Q187.Easy
In C#, class Vehicle has a public method Describe() that prints "Vehicle". Class Car inherits from Vehicle and also defines Describe(), which should first print the base class text "Vehicle" and then print " Car". Which statement should be placed inside Car.Describe() before its own Console.WriteLine?
- base.Describe();
- Vehicle::Describe();
- Describe();
- mybase.Describe();
- Vehicle.Describe();
Show answer & explanation
Answer: (A) base.Describe();Explanation: 1) Car.Describe() hides Vehicle.Describe() because both have the same signature.
2) To call the hidden base-class version from inside the derived method, C# provides the base keyword.
3) base.Describe() invokes Vehicle.Describe(), printing "Vehicle".
4) Then Car.Describe() continues and prints " Car".
5) The other options use invalid C# syntax (::, mybase, or calling the type name directly).
Q188.Easy
In C#, suppose a base class declares a method named Show(). A derived class does not declare any method with that name. Which statement is correct about accessing Show() through an object of the derived class?
- It is always impossible for a derived class object to call Show().
- It is possible only if Show() is declared public (or otherwise accessible) in the base class.
- It is possible only if Show() is declared private in the base class.
- The derived class must redeclare Show() before it can be called.
Show answer & explanation
Answer: (B) It is possible only if Show() is declared public (or otherwise accessible) in the base class.Explanation: 1) A derived class inherits accessible members of its base class.
2) If Show() is public in the base class, it remains accessible through a derived class object.
3) If Show() is private in the base class, it is not accessible outside the base class, so a derived object cannot call it.
4) Therefore the ability to call Show() depends on how it is declared in the base class, not on the derived class.
Q189.Easy
Which of the following is used to implement a 'Has-a' relationship between two entities in C#?
- Polymorphism
- Templates
- Containership
- Encapsulation
- Inheritance
Show answer & explanation
Answer: (C) ContainershipExplanation: 1. A 'Has-a' relationship means one class contains an object of another class as a member (composition/aggregation).
2. This is called containership.
3. Inheritance represents an 'Is-a' relationship, not 'Has-a'. So the answer is Containership.
Q190.Easy
If a base class and a derived class each define a member function with the same name and signature, then calling that function on an object of the derived class will invoke the derived class's version.
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. When a method is called on a derived class object, the compiler first searches the derived class for a matching method.
2. If found (same name and signature), the derived class's method is used and the base class version is hidden.
3. Only if not found does it look in the base class. So the statement is True.
Q191.EasyAnswer under review
In C#, which keyword is used to replace a member of a base class with a new derived-class member, thereby changing the data and behavior of the base class?
- new
- base
- overloads
- override
- overridable
Show answer & explanation
Answer: (A) newExplanation: 1. The 'new' modifier declares a member that hides the inherited base-class member of the same name.
2. 'base' accesses base-class members; it does not replace them.
3. 'overloads' and 'overridable' are not C# keywords.
4. 'override' extends a virtual member polymorphically rather than hiding it.
5. So the keyword that hides/replaces a base member is 'new'.
Q192.Easy
In C#, the size of an object of a derived class is equal to the sum of the sizes of the data members declared in the base class and the data members declared in the derived class (ignoring padding/alignment).
- True
- False
Show answer & explanation
Answer: (A) TrueExplanation: 1. A derived class object contains all the data members inherited from the base class plus its own data members.
2. So the total size is the sum of base class data member sizes and derived class data member sizes (plus any padding).
3. Hence the statement is True.
Q193.Easy
Which of the following statements is correct about the C#.NET code snippet given below?
interface IEmployee
{
String EmpName { get ; set ; }
String Dept { get ; set ; }
void Show();
void Update();
int GetId();
}- Properties cannot be declared inside an interface.
- This is a perfectly workable interface.
- The properties in the interface must have a body.
- Subroutines in the interface must have a body.
- Functions cannot be declared inside an interface.
Show answer & explanation
Answer: (B) This is a perfectly workable interface.Explanation: Step 1: An interface may declare properties (EmpName, Dept), methods (Show, Update) and functions returning values (GetId).
Step 2: Interface members are only declarations; they do not have bodies, so options 3 and 4 are wrong.
Step 3: Properties and functions are allowed in interfaces, so options 1 and 5 are wrong.
Step 4: Hence the snippet is a valid, perfectly workable interface.
Q194.Easy
Which of the following is the correct way to implement the interface shown below in C#?
interface IStudent
{
String RollNo { get ; set ; }
}- class Scholar : IStudent
{
private String str ;
public String RollNo
{
get { return str ; }
set { str = value ; }
}
}
- class Scholar
{
private String str ;
public String IStudent.RollNo
{
get { return str ; }
set { str = value ; }
}
}
- class Scholar : implements IStudent
{
private String str ;
public String RollNo
{
get { return str ; }
set { str = value ; }
}
}
- None of the above
Show answer & explanation
Answer: (A) class Scholar : IStudent
{
private String str ;
public String RollNo
{
get { return str ; }
set { str = value ; }
}
}Explanation: Step 1: In C# a class implements an interface using a colon (:), not the keyword 'implements' (which is Java syntax), so option 3 is wrong.
Step 2: The class must be declared as inheriting the interface, so option 2 (no ': IStudent') is wrong.
Step 3: A public property RollNo with get/set matching the interface signature correctly implements it, as in option 1.
Step 4: Therefore option 1 is the correct implementation.
Q195.Easy
If a namespace is present in a library then which of the following is the correct way to use the elements of the namespace?
- Add Reference of the namespace. Use the elements of the namespace.
- Add Reference of the namespace. Import the namespace. Use the elements of the namespace.
- Import the namespace. Use the elements of the namespace.
- Copy the library in the same directory as the project that is trying to use it. Use the elements of the namespace.
- Install the namespace in Global Assembly Cache. Use the elements of the namespace.
Show answer & explanation
Answer: (B) Add Reference of the namespace. Import the namespace. Use the elements of the namespace.Explanation: To use types from a library, you first add a reference to the library assembly, then import the namespace with a using directive, and finally use its elements. Just adding a reference (option 1) is not enough because the compiler needs the namespace imported or fully qualified. Importing without a reference (option 3) fails. Copying the library (option 4) or installing in the GAC (option 5) does not by itself make the namespace available. Hence option 2 is correct.
Q196.Easy
Which of the following CANNOT belong to a C#.NET namespace?
- class
- struct
- enum
- Data
- interface
Show answer & explanation
Answer: (D) DataExplanation: 1) A namespace can contain types such as classes, structs, enums, interfaces and delegates. 2) 'Data' is not a C# type keyword, so it cannot be a member of a namespace. 3) Hence Data is the correct answer.
Q197.Easy
Which of the following C#.NET code snippets will correctly print "Hello C#.NET"?
- import System; namespace SampleApp { class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello C#.NET"); } } }
- using System; namespace SampleApp { class MyProgram { static void Main(string[] args) { WriteLine("Hello C#.NET"); } } }
- using System.Console; namespace SampleApp { class MyProgram { static void Main(string[] args) { WriteLine("Hello C#.NET"); } } }
- using System; namespace SampleApp { class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello C#.NET"); } } }
Show answer & explanation
Answer: (D) using System; namespace SampleApp { class MyProgram { static void Main(string[] args) { Console.WriteLine("Hello C#.NET"); } } }Explanation: 1) C# uses 'using', not 'import', so option 1 fails. 2) Option 2 calls WriteLine without the Console class qualifier, so it fails. 3) Option 3 imports System.Console (a class, not a namespace) and calls WriteLine unqualified, so it fails. 4) Option 4 imports System and calls Console.WriteLine correctly, so it prints the message.
Q198.Easy
A class called Student is present in namespace School stored in an external library (a separate DLL). Which of the following is absolutely necessary before you can use the Student class?
- Use the fully qualified name of the Student class.
- Use a using statement before using the Student class.
- Add a reference to the library before using the Student class.
- Copy the library into the current project directory before using the Student class.
- Declare the Student class again in your own namespace.
Show answer & explanation
Answer: (C) Add a reference to the library before using the Student class.Explanation: Step 1: The class lives in an external library (DLL), so the compiler must know about that DLL.
Step 2: This is done by adding a reference to the library in the project.
Step 3: A using statement only saves typing; it does not bring the DLL in.
Step 4: Fully qualified names also fail without the reference.
Step 5: Copying the file into the directory does not register it with the compiler. Hence adding a reference is absolutely necessary.
Q199.Easy
Which of the following is NOT a compound assignment operator in C#?
- =
- /=
- *=
- +=
- %=
Show answer & explanation
Answer: (A) =Explanation: Compound assignment operators combine an operation with assignment: /=, *=, +=, %= are all compound assignment operators. The plain '=' is the simple assignment operator, not a compound assignment operator.
Q200.Easy
Which of the following are NOT relational operators in C#.NET? >= != Not <= <>=
- 1, 3
- 2, 4
- 3, 5
- 4, 5
- None of these
Show answer & explanation
Answer: (C) 3, 5Explanation: Step 1: Relational operators in C# are ==, !=, >, <, >=, <=.
Step 2: Item 1 (>=) is relational. Item 2 (!=) is relational. Item 4 (<=) is relational.
Step 3: Item 3 (Not) is a logical keyword, not a relational operator. Item 5 (<>=) is not a valid C# operator.
Step 4: So the non-relational items are 3 and 5, which is option 3.