InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 201. |
What Are The 2 Broad Classifications Of Data Types Available In C#? |
|
Answer» 1. Built in data TYPES. 1. Built in data types. |
|
| 202. |
Is C# A Strongly-typed Language? |
|
Answer» Yes. Yes. |
|
| 203. |
What Are The 3 Types Of Comments In C#? |
|
Answer» 1. Single Line Comments. You DEFINE single line comments with // as shown below. 1. Single Line Comments. You define single line comments with // as shown below. |
|
| 204. |
Can You Specify An Access Modifier For An Enumeration? |
|
Answer» Enumeration MEMBERS are always public, and no ACCESS modifiers can be SPECIFIED. Enumeration members are always public, and no access modifiers can be specified. |
|
| 205. |
What Is The Default Access Modifier For A Class,struct And An Interface Declared Directly With A Namespace? |
|
Answer» internal. internal. |
|
| 206. |
What Does Protected Internal Access Modifier Mean? |
|
Answer» The protected internal ACCESS MEANS protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, INCLUDING DERIVED classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its MEMBERS as protected. The protected internal access means protected OR internal, not protected AND internal. In simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected. |
|
| 207. |
Can Destructors Have Access Modifiers? |
|
Answer» No, DESTRUCTORS cannot have ACCESS MODIFIERS. No, destructors cannot have access modifiers. |
|
| 208. |
Can The Accessibility Of A Type Member Be Greater Than The Accessibility Of Its Containing Type? |
|
Answer» No, the accessibility of a type member can never be greater than the accessibility of its containing type. For EXAMPLE, a PUBLIC METHOD declared in an INTERNAL class has only internal accessibility. No, the accessibility of a type member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal class has only internal accessibility. |
|
| 209. |
Can You Declare Struct Members As Protected? |
|
Answer» No, STRUCT members cannot be DECLARED protected. This is because structs do not SUPPORT INHERITANCE. No, struct members cannot be declared protected. This is because structs do not support inheritance. |
|
| 210. |
Can Derived Classes Have Greater Accessibility Than Their Base Types? |
|
Answer» No, Derived classes cannot have greater accessibility than their base types. For example the FOLLOWING code is illegal. using System; When you compile the above code an error will be generated stating "INCONSISTENT accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this SIMPLE, you cannot have a public class B that DERIVES from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class. No, Derived classes cannot have greater accessibility than their base types. For example the following code is illegal. using System; When you compile the above code an error will be generated stating "Inconsistent accessibility: base class InternalBaseClass is less accessible than class PublicDerivedClass".To make this simple, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class. |
|
| 211. |
Can You Use All Access Modifiers For All Types? |
|
Answer» No, Not all access modifiers can be used by all TYPES or MEMBERS in all CONTEXTS, and in some cases the ACCESSIBILITY of a type member is constrained by the accessibility of its containing type. No, Not all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type. |
|
| 212. |
What Are Access Modifiers Used For? |
|
Answer» ACCESS MODIFIERS are USED to control the accessibilty of types and MEMBERS with in the types. Access Modifiers are used to control the accessibilty of types and members with in the types. |
|
| 213. |
What Are Access Modifiers In C#? |
|
Answer» In C# there are 5 different types of Access Modifiers. PRIVATE PROTECTED INTERNAL Protected Internal In C# there are 5 different types of Access Modifiers. Private Protected Internal Protected Internal |
|
| 214. |
What Is The Difference Between A Constant And A Static Readonly Field? |
|
Answer» A static READONLY FIELD is very SIMILAR to a constant, except that the C# compiler does not have ACCESS to the value of a static read-only field at compile TIME, only at run time. A static readonly field is very similar to a constant, except that the C# compiler does not have access to the value of a static read-only field at compile time, only at run time. |
|
| 215. |
What Is Wrong With The Sample Program Below? |
|
Answer» USING System; using System; |
|
| 216. |
Can You Declare A Field Readonly? |
|
Answer» Yes, a field can be declared readonly. A read-only field can only be ASSIGNED a value during initialization or in a constructor. An example is shown below. using SYSTEM; Yes, a field can be declared readonly. A read-only field can only be assigned a value during initialization or in a constructor. An example is shown below. using System; |
|
| 217. |
What Is A Static Field? |
|
Answer» A STATIC FIELD belongs to the class itself, and is shared among all INSTANCES of that class. Changes made from instance A will be VISIBLE immediately to instances B and C if they access the field. A static field belongs to the class itself, and is shared among all instances of that class. Changes made from instance A will be visible immediately to instances B and C if they access the field. |
|
| 218. |
What Are Instance Fields In C#? |
|
Answer» Instance FIELDS are specific to an instance of a type. If you have a class T, with an instance FIELD F, you can CREATE TWO objects of type T, and modify the VALUE of F in each object without affecting the value in the other object. Instance fields are specific to an instance of a type. If you have a class T, with an instance field F, you can create two objects of type T, and modify the value of F in each object without affecting the value in the other object. |
|
| 219. |
What Are The 2 Broad Classifications Of Fields In C#? |
|
Answer» 1. INSTANCE FIELDS 1. Instance fields |
|
| 220. |
How Do You Access A Constant Field Declared In A Class? |
|
Answer» Constants are accessed as if they were static fields because the value of the CONSTANT is the same for all instances of the TYPE. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a PERIOD, and the name of the constant to access the constant. In the example below constant FIELD PI can be accessed in the Main method using the class name and not the instance of the class. Trying to access a constant field using a class instance will generate a compile time error. using System; Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. You do not use the static keyword to declare them. Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. In the example below constant field PI can be accessed in the Main method using the class name and not the instance of the class. Trying to access a constant field using a class instance will generate a compile time error. using System; |
|
| 221. |
Can You Change The Value Of A Constant Filed After Its Declaration? |
|
Answer» No, you cannot CHANGE the value of a constant filed after its DECLARATION. In the example below, the constant field PI is always 3.14, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in C# source code (for example, PI), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference. USING System; No, you cannot change the value of a constant filed after its declaration. In the example below, the constant field PI is always 3.14, and it cannot be changed even by the class itself. In fact, when the compiler encounters a constant identifier in C# source code (for example, PI), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference. using System; |
|
| 222. |
Does C# Support Const Methods, Properties, Or Events? |
|
Answer» No, C# does not support const METHODS, PROPERTIES, or EVENTS. No, C# does not support const methods, properties, or events. |
|
| 223. |
Can You Declare A Class Or A Struct As Constant? |
|
Answer» No, User-defined TYPES including classes, structs, and arrays, cannot be const. Only the C# built-in types excluding System.Object may be declared as const. Use the READONLY modifier to CREATE a CLASS, struct, or array that is initialized one time at runtime (for example in a CONSTRUCTOR) and thereafter cannot be changed. No, User-defined types including classes, structs, and arrays, cannot be const. Only the C# built-in types excluding System.Object may be declared as const. Use the readonly modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed. |
|
| 224. |
What Are Constants In C#? |
|
Answer» Constants in C# are immutable values which are known at compile time and do not CHANGE for the life of the program. Constants are declared using the CONST keyword. Constants must be initialized as they are declared. You cannot assign a value to a CONSTANT after it isdeclared. An example is shown below. using System; Constants in C# are immutable values which are known at compile time and do not change for the life of the program. Constants are declared using the const keyword. Constants must be initialized as they are declared. You cannot assign a value to a constant after it isdeclared. An example is shown below. using System; |
|
| 225. |
Can You Use Virtual, Override Or Abstract Keywords On An Accessor Of A Static Property? |
|
Answer» No, it is a COMPILE time error to use a VIRTUAL, ABSTRACT or override KEYWORDS on an accessor of a static PROPERTY. No, it is a compile time error to use a virtual, abstract or override keywords on an accessor of a static property. |
|
| 226. |
What Is An Abstract Property. Give An Example? |
|
Answer» A property that is MARKED with abstract KEYWORD is considered abstract property. An abstract property should not have any IMPLEMENTATION in the class. The derived classes must write their own implementation. In the example below FullName property is abstract in the Customer class. BankCustomer class overrides the INHERITED abstract FullName property with its own implementation. using System; A property that is marked with abstract keyword is considered abstract property. An abstract property should not have any implementation in the class. The derived classes must write their own implementation. In the example below FullName property is abstract in the Customer class. BankCustomer class overrides the inherited abstract FullName property with its own implementation. using System; |
|
| 227. |
What Is A Virtual Property. Give An Example? |
|
Answer» A property that is marked with virtual keyword is considered virtual property. Virtual properties enable derived classes to override the property behavior by using the override keyword. In the example below FullName is virtual property in the Customer class. BankCustomer class inherits from Customer class and overrides the FullName virtual property. In the output you can see the over riden IMPLEMENTATION. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual. using System; A property that is marked with virtual keyword is considered virtual property. Virtual properties enable derived classes to override the property behavior by using the override keyword. In the example below FullName is virtual property in the Customer class. BankCustomer class inherits from Customer class and overrides the FullName virtual property. In the output you can see the over riden implementation. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual. using System; |
|
| 228. |
What Is A Static Property. Give An Example? |
|
Answer» A PROPERTY that is marked with a static keyword is considered as static property. This makes the property available to CALLERS at any time, even if no instance of the class exists. In the example below PI is a static property. using System; A property that is marked with a static keyword is considered as static property. This makes the property available to callers at any time, even if no instance of the class exists. In the example below PI is a static property. using System; |
|
| 229. |
What Are The Advantages Of Properties In C#? |
|
Answer» 1. Properties can VALIDATE data before allowing a change. 1. Properties can validate data before allowing a change. |
|
| 230. |
Explain The 3 Types Of Properties In C# With An Example? |
|
Answer» 1. Read Only PROPERTIES: Properties WITHOUT a set accessor are considered read-only. In the above example FullName is read only property. 1. Read Only Properties: Properties without a set accessor are considered read-only. In the above example FullName is read only property. |
|
| 231. |
What Are Properties In C#. Explain With An Example? |
|
Answer» Properties in C# are class members that provide a FLEXIBLE mechanism to read, write, or COMPUTE the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called ACCESSORS. This enables data to be accessed easily and still helps promote the safety and flexibility of methods. In the example below _firstName and _lastName are private string variables which are accessible only inside the Customer class. _firstName and _lastName are exposed using FirstName and LastName public properties respectively. The get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. The value keyword is used to define the value being assigned by the set accessor. The FullName property computes the full name of the customer. Full Name property is readonly, because it has only the get accessor. Properties that do not implement a set accessor are read only. The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value. using System; Properties in C# are class members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods. In the example below _firstName and _lastName are private string variables which are accessible only inside the Customer class. _firstName and _lastName are exposed using FirstName and LastName public properties respectively. The get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. The value keyword is used to define the value being assigned by the set accessor. The FullName property computes the full name of the customer. Full Name property is readonly, because it has only the get accessor. Properties that do not implement a set accessor are read only. The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value. using System; |
|
| 232. |
If A Method's Return Type Is Void, Can You Use A Return Keyword In The Method? |
|
Answer» Yes, Even though a method's return type is void, you can use the return keyword to stop the execution of the method as shown in the example below. using SYSTEM; Yes, Even though a method's return type is void, you can use the return keyword to stop the execution of the method as shown in the example below. using System; |
|
| 233. |
Can You Pass Value Types By Reference To A Method? |
|
Answer» Yes, we can pass VALUE types by by REFERENCE to a method. An example is shown below. USING System; Yes, we can pass value types by by reference to a method. An example is shown below. using System; |
|
| 234. |
Explain The Difference Between Passing Parameters By Value And Passing Parameters By Reference With An Example? |
|
Answer» We can pass parameters to a method by value or by reference. By DEFAULT all value types are PASSED by value where as all reference types are passed by reference. By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method.An example is shown below. using System; By default, reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy of the object. Changes made through this reference will therefore be reflected in the calling method. Reference types are created by using the class keyword as shown in the example below. using System; We can pass parameters to a method by value or by reference. By default all value types are passed by value where as all reference types are passed by reference. By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method.An example is shown below. using System; By default, reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy of the object. Changes made through this reference will therefore be reflected in the calling method. Reference types are created by using the class keyword as shown in the example below. using System; |
|
| 235. |
What Is The Difference Between Method Parameters And Method Arguments. Give An Example? |
|
Answer» In the example below FirstNumber and SecondNumber are method parameters where as FN and LN are method arguments. The method DEFINITION specifies the names and types of any parameters that are required. When calling code CALLS the method, it provides concrete values CALLED arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method. using System; In the example below FirstNumber and SecondNumber are method parameters where as FN and LN are method arguments. The method definition specifies the names and types of any parameters that are required. When calling code calls the method, it provides concrete values called arguments for each parameter. The arguments must be compatible with the parameter type but the argument name (if any) used in the calling code does not have to be the same as the parameter named defined in the method. using System; |
|
| 236. |
Is The Following Code Legal? |
|
Answer» using System; No, The above code does not compile. You cannot overload a METHOD based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the SIGNATURE of the method for the purposes of method overloading. However, it is part of the signature of the method when DETERMINING the compatibility between a DELEGATE and the method that it points to. using System; No, The above code does not compile. You cannot overload a method based on the return type. To overload a method in C# either the number or type of parameters should be different. In general the return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to. |
|
| 237. |
Does C# Provide Copy Constructor? |
|
Answer» No, C# does not PROVIDE COPY CONSTRUCTOR. No, C# does not provide copy constructor. |
|
| 238. |
Give 2 Scenarios Where Static Constructors Can Be Used? |
|
Answer» 1. A typical USE of static CONSTRUCTORS is when the class is USING a LOG file and the constructor is used to write entries to this file. 1. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file. |
|
| 239. |
What Happens If A Static Constructor Throws An Exception? |
|
Answer» If a STATIC constructor throws an exception, the runtime will not INVOKE it a second time, and the type will remain uninitialized for the LIFETIME of the application DOMAIN in which your program is running. If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running. |
|
| 240. |
Can You Have Parameters For Static Constructors? |
|
Answer» No, STATIC CONSTRUCTORS cannot have PARAMETERS. No, static constructors cannot have parameters. |
|
| 241. |
Can You Mark Static Constructor With Access Modifiers? |
|
Answer» No, we cannot USE ACCESS MODIFIERS on STATIC CONSTRUCTOR. No, we cannot use access modifiers on static constructor. |
|
| 242. |
Can A Class Have Static Constructor? |
|
Answer» Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are ACCESSED, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are REFERENCED. Static constructors are called before instance constructors. An example is shown below. using System; Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are referenced. Static constructors are called before instance constructors. An example is shown below. using System; |
|
| 243. |
If A Child Class Instance Is Created, Which Class Constructor Is Called First - Base Class Or Child Class? |
|
Answer» When an instance of a child class is created, the base class constructor is called before the child class constructor. An example is SHOWN below. using System; When an instance of a child class is created, the base class constructor is called before the child class constructor. An example is shown below. using System; |
|
| 244. |
Can A Child Class Call The Constructor Of A Base Class? |
|
Answer» YES, a child class can call the constructor of a BASE class by using the base keyword as shown in the example below. using SYSTEM; Yes, a child class can call the constructor of a base class by using the base keyword as shown in the example below. using System; |
|
| 245. |
Can A Class Or A Struct Have Multiple Constructors? |
|
Answer» Yes, a CLASS or a struct can have multiple CONSTRUCTORS. Constructors in csharp can be OVERLOADED. Yes, a class or a struct can have multiple constructors. Constructors in csharp can be overloaded. |
|
| 246. |
Can You Prevent A Class From Being Instantiated? |
|
Answer» Yes, a class can be prevented from being instantiated by using a private constructor as SHOWN in the example below. using SYSTEM; Yes, a class can be prevented from being instantiated by using a private constructor as shown in the example below. using System; |
|
| 247. |
We Cannot Create Instances Of Static Classes. Can We Have Constructors For Static Classes? |
|
Answer» YES, STATIC CLASSES can ALSO have CONSTRUCTORS. Yes, static classes can also have constructors. |
|
| 248. |
Structs Are Not Reference Types. Can Structs Have Constructors? |
|
Answer» YES, EVEN THOUGH Structs are not REFERENCE types, structs can have constructors. Yes, even though Structs are not reference types, structs can have constructors. |
|
| 249. |
In C#, What Will Happen If You Do Not Explicitly Provide A Constructor For A Class? |
|
Answer» If you do not provide a constructor EXPLICITLY for your CLASS, C# will create one by default that instantiates the OBJECT and sets all the member variables to their default VALUES. If you do not provide a constructor explicitly for your class, C# will create one by default that instantiates the object and sets all the member variables to their default values. |
|
| 250. |
What Is A Constructor In C#? |
|
Answer» Constructor is a class METHOD that is executed when an object of a class is CREATED. Constructor has the same name as the class, and usually used to INITIALIZE the data MEMBERS of the new object. Constructor is a class method that is executed when an object of a class is created. Constructor has the same name as the class, and usually used to initialize the data members of the new object. |
|