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.
| 151. |
Do Structs Support Inheritance? |
|
Answer» No, STRUCTS do not SUPPORT INHERITANCE, but they can IMPLEMENT INTERFACES. No, structs do not support inheritance, but they can implement interfaces. |
|
| 152. |
What Are The 4 Pillars Of Any Object Oriented Programming Language? |
|
Answer» 1. ABSTRACTION 1. Abstraction |
|
| 153. |
Can A Sealed Class Be Used As A Base Class? |
|
Answer» No, SEALED CLASS cannot be USED as a BASE class. A compile time ERROR will be generated. No, sealed class cannot be used as a base class. A compile time error will be generated. |
|
| 154. |
How Can You Force Derived Classes To Provide New Method Implementations For Virtual Methods? |
|
Answer» Abstract classes can be used to FORCE derived classes to provide NEW method IMPLEMENTATIONS for virtual methods. An example is SHOWN below. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method. In the above example, Method() on class NonAbstractChildClass cannot call Method() on class BaseClass. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods. Abstract classes can be used to force derived classes to provide new method implementations for virtual methods. An example is shown below. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method. In the above example, Method() on class NonAbstractChildClass cannot call Method() on class BaseClass. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods. |
|
| 155. |
What Are Abstract Methods? |
|
Answer» ABSTRACT METHODS are methods that only the DECLARATION of the METHOD and no IMPLEMENTATION. Abstract methods are methods that only the declaration of the method and no implementation. |
|
| 156. |
What Is A Sealed Class? |
|
Answer» A sealed class is a class that cannot be inherited from. This means, If you have a class CALLED Customer that is marked as sealed. No other class can inherit from Customer class. For EXAMPLE, the below code generates a compile time error "MainClass cannot derive from sealed type Customer. USING System; public sealed class Customer { } public class MainClass : Customer { public static VOID Main() { } }A sealed class is a class that cannot be inherited from. This means, If you have a class called Customer that is marked as sealed. No other class can inherit from Customer class. For example, the below code generates a compile time error "MainClass cannot derive from sealed type Customer. using System; |
|
| 157. |
Can You Create An Instance Of An Abstract Class? |
|
Answer» No, ABSTRACT classes are INCOMPLETE and you cannot create an INSTANCE of an abstract class. No, abstract classes are incomplete and you cannot create an instance of an abstract class. |
|
| 158. |
Can You Access A Hidden Base Class Method In The Derived Class? |
|
Answer» YES, Hidden BASE class methods can be accessed from the derived class by CASTING the instance of the derived class to an instance of the base class as shown in the example below. Yes, Hidden base class methods can be accessed from the derived class by casting the instance of the derived class to an instance of the base class as shown in the example below. |
|
| 159. |
Give An Example To Show For Hiding Base Class Methods? |
|
Answer» Use the new KEYWORD to hide a base class METHOD in the DERIVED class as shown in the EXAMPLE below. Use the new keyword to hide a base class method in the derived class as shown in the example below. |
|
| 160. |
Can Fields Inside A Class Be Virtual? |
|
Answer» No, Fields INSIDE a CLASS cannot be virtua. Only METHODS, properties, events and indexers can be VIRTUAL. No, Fields inside a class cannot be virtua. Only methods, properties, events and indexers can be virtual. |
|
| 161. |
What Is The Difference Between A Virtual Method And An Abstract Method? |
|
Answer» A VIRTUAL METHOD MUST have a BODY where as an ABSTRACT method should not have a body. A virtual method must have a body where as an abstract method should not have a body. |
|
| 162. |
When Can A Derived Class Override A Base Class Member? |
|
Answer» A DERIVED class can OVERRIDE a BASE class member only if the base class member is declared as virtual or ABSTRACT. A derived class can override a base class member only if the base class member is declared as virtual or abstract. |
|
| 163. |
Explain Polymorphism In C# With A Simple Example? |
|
Answer» Polymorphism allows you to invoke derived class METHODS through a base class reference during run-time. An example is shown below. Polymorphism allows you to invoke derived class methods through a base class reference during run-time. An example is shown below. |
|
| 164. |
Why Should You Override The Tostring() Method? |
|
Answer» All TYPES in .Net inherit from SYSTEM.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below. using System; In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method. If you have a Customer class as SHOWN in the below example and when you call the ToString() method the OUTPUT doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class. using System; The code sample below shows how to override the ToString() method in a class, that would give the output you want. using System; Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method. All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below. using System; In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method. If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class. using System; The code sample below shows how to override the ToString() method in a class, that would give the output you want. using System; Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method. |
|
| 165. |
What Is The Difference Between Int.parse And Int.tryparse Methods? |
|
Answer» Parse method throws an EXCEPTION if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if PARSING FAILS. Hence TryParse is more EFFICIENT than Parse. Parse method throws an exception if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse. |
|
| 166. |
How Do You Determine Whether A String Represents A Numeric Value? |
|
Answer» To determine whether a STRING represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have SPECIFIED, TryParse returns false and sets the out parameter to zero. OTHERWISE, it returns true and sets the out parameter to the numeric value of the string. string str = "One"; To determine whether a String represents a numeric value use TryParse method as shown in the example below. If the string contains nonnumeric characters or the numeric value is too large or too small for the particular type you have specified, TryParse returns false and sets the out parameter to zero. Otherwise, it returns true and sets the out parameter to the numeric value of the string. string str = "One"; |
|
| 167. |
What Is The Difference Between System.text.stringbuilder And System.string? |
|
Answer» 1. OBJECTS of type StringBuilder are MUTABLE where as objects of type SYSTEM.STRING are immutable. 1. Objects of type StringBuilder are mutable where as objects of type System.String are immutable. |
|
| 168. |
How Do You Create Empty Strings In C#? |
|
Answer» USING STRING.empty as SHOWN in the EXAMPLE below. Using string.empty as shown in the example below. |
|
| 169. |
Will The Following Code Compile And Run? |
|
Answer» STRING str = null; string str = null; |
|
| 170. |
What Is A Verbatim String Literal And Why Do We Use It? |
|
Answer» The "@" symbol is the verbatim string literal. Use verbatim strings for convenience and better READABILITY when the string text contains backslash characters, for example in file PATHS. Because verbatim strings PRESERVE new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to EMBED a quotation mark INSIDE a verbatim string. The following example shows some common uses for verbatim strings: string ImagePath = @"C:\Images\Buttons\SaveButton.jpg"; string MultiLineText = @"This is multiline Text written to be in three lines."; string DoubleQuotesString = @"My Name is ""Vankat."""; The "@" symbol is the verbatim string literal. Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings: string ImagePath = @"C:\Images\Buttons\SaveButton.jpg"; string MultiLineText = @"This is multiline Text written to be in three lines."; string DoubleQuotesString = @"My Name is ""Vankat."""; |
|
| 171. |
What Will Be The Output Of The Following Code? |
|
Answer» string str1 = "Hello "; The output of the above code is "Hello" and not "Hello C#". This is BCOS, if you create a REFERENCE to a string, and then "modify" the original string, the reference will continue to point to the original OBJECT INSTEAD of the new object that was created when the string was modified. string str1 = "Hello "; The output of the above code is "Hello" and not "Hello C#". This is bcos, if you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. |
|
| 172. |
What Do You Mean By String Objects Are Immutable? |
|
Answer» String objects are immutable MEANS, they cannot be CHANGED after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are CONCATENATED to form a single string, the two original strings are unmodified. The += operator creates a new string that CONTAINS the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a REFERENCE to it. string s1 = "First String "; // Concatenate s1 and s2. This actually creates a new System.Console.WriteLine(s1); String objects are immutable means, they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it. string s1 = "First String "; // Concatenate s1 and s2. This actually creates a new System.Console.WriteLine(s1); |
|
| 173. |
Are String Objects Mutable Or Immutable? |
|
Answer» String objects are immutable. |
|
| 174. |
What Is The Difference Between String Keyword And System.string Class? |
|
Answer» STRING keyword is an alias for Syste.String class. THEREFORE, System.String and string keyword are the same, and you can use WHICHEVER naming convention you prefer. The String class provides many methods for safely creating, manipulating, and COMPARING strings. string keyword is an alias for Syste.String class. Therefore, System.String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. |
|
| 175. |
Can You Use Foreach Iteration On Arrays In C#? |
|
Answer» Yes,Since ARRAY TYPE implements IEnumerable, you can USE foreach iteration on all arrays in C#. Yes,Since array type implements IEnumerable, you can use foreach iteration on all arrays in C#. |
|
| 176. |
What Is The Base Class For Array Types? |
|
Answer» System.Array. System.Array. |
|
| 177. |
Are Arrays Value Types Or Reference Types? |
|
Answer» Arrays are reference types. |
|
| 178. |
What Is Jagged Array? |
|
Answer» A JAGGED ARRAY is an array of ARRAYS. A jagged array is an array of arrays. |
|
| 179. |
What Are The 3 Different Types Of Arrays? |
|
Answer» 1. Single-Dimensional 1. Single-Dimensional |
|
| 180. |
What Is An Array? |
|
Answer» An array is a DATA STRUCTURE that contains several VARIABLES of the same TYPE. An array is a data structure that contains several variables of the same type. |
|
| 181. |
What Happens During The Process Of Boxing? |
|
Answer» Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value TYPE to the type object or to any interface type IMPLEMENTED by this value type. Boxing a value type allocates an object INSTANCE on the heap and copies the value into the new object. Due to this boxing and UNBOXING can have performance impact. Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact. |
|
| 182. |
Is Unboxing An Implicit Conversion? |
|
Answer» No, UNBOXING is an EXPLICIT CONVERSION. No, unboxing is an explicit conversion. |
|
| 183. |
Is Boxing An Implicit Conversion? |
|
Answer» YES, BOXING happens IMPLICITLY. Yes, boxing happens implicitly. |
|
| 184. |
If Casting Fails What Type Of Exception Is Thrown? |
|
Answer» InvalidCastException. InvalidCastException. |
|
| 185. |
What Operators Can Be Used To Cast From One Reference Type To Another Without The Risk Of Throwing An Exception? |
|
Answer» The is and as operators can be used to cast from ONE reference type to ANOTHER WITHOUT the risk of throwing an EXCEPTION. The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception. |
|
| 186. |
If You Want To Convert A Base Type To A Derived Type, What Type Of Conversion Do You Use? |
|
Answer» Explicit CONVERSION as shown below. // Explicit conversion is REQUIRED to cast BACK to derived type. The CODE below will compile but throw an exception at run time if the right-side object is not a Car object. Explicit conversion as shown below. // Explicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right-side object is not a Car object. |
|
| 187. |
What Type Of Data Type Conversion Happens When The Compiler Encounters The Following Code? |
|
Answer» ChildClass CC = new ChildClass(); Implicit Conversion. For REFERENCE types, an implicit conversion always exists from a class to any one of its DIRECT or indirect base classes or interfaces. No special SYNTAX is necessary because a derived class always CONTAINS all the MEMBERS of a base class. ChildClass CC = new ChildClass(); Implicit Conversion. For reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. No special syntax is necessary because a derived class always contains all the members of a base class. |
|
| 188. |
What Is The Difference Between An Implicit Conversion And An Explicit Conversion? |
|
Answer» 1. Explicit conversions require a CAST operator where as an implicit converstion is done AUTOMATICALLY. 1. Explicit conversions require a cast operator where as an implicit converstion is done automatically. |
|
| 189. |
What Are The 2 Kinds Of Data Type Conversions In C#? |
|
Answer» Implicit conversions: No special SYNTAX is required because the conversion is TYPE safe and no data will be lost. Examples include conversions from smaller to larger INTEGRAL types, and conversions from derived classes to base classes. Explicit conversions: Explicit conversions require a cast operator. The source and destination variables are COMPATIBLE, but there is a risk of data LOSS because the type of the destination variable is a smaller size than (or is a base class of) the source variable. Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes. Explicit conversions: Explicit conversions require a cast operator. The source and destination variables are compatible, but there is a risk of data loss because the type of the destination variable is a smaller size than (or is a base class of) the source variable. |
|
| 190. |
What Do You Mean By Casting A Data Type? |
|
Answer» Converting a variable of one data TYPE to another data type is CALLED casting. This is also called as data type CONVERSION. Converting a variable of one data type to another data type is called casting. This is also called as data type conversion. |
|
| 191. |
What Are The Differences Between Value Types And Reference Types? |
|
Answer» 1. Value types are stored on the stack where as reference types are stored on the MANAGED heap. 1. Value types are stored on the stack where as reference types are stored on the managed heap. |
|
| 192. |
Give Examples For Reference Types? |
|
Answer» CLASS Class |
|
| 194. |
What Is The Base Class From Which All Value Types Are Derived? |
|
Answer» System.ValueType. System.ValueType. |
|
| 196. |
If You Define A User Defined Data Type By Using The Class Keyword, Is It A Value Type Or Reference Type? |
|
Answer» Reference type |
|
| 197. |
If You Define A User Defined Data Type By Using The Struct Keyword, Is It A Value Type Or Reference Type? |
|
Answer» Value Type. |
|
| 198. |
What Are The 2 Types Of Data Types Available In C#? |
|
Answer» 1. VALUE Types 1. Value Types |
|
| 199. |
How Do You Create User Defined Data Types In C#? |
|
Answer» You use the STRUCT, CLASS, interface, and enum constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types PROVIDED by Microsoft that you can use in your own APPLICATIONS. You use the struct, class, interface, and enum constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. |
|
| 200. |
Give Some Examples For Built In Datatypes In C#? |
|
Answer» 1. int |
|