This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What is inheritance? Does C# support multiple inheritance? |
|
Answer» Inheritance means acquiring some of the properties from a master CLASS. Multiple Inheritance in C#Here, class C can inherit properties from Class A and Class B. Here is an example of inheritance: // C# program to illustrate// multiple class inheritanceusing System;using System.Collections;// Parent class 1class Scaler { // Providing the implementation // of features() METHOD public void features() { // Creating ArrayList ArrayList My_features= new ArrayList(); // Adding elements in the // My_features ArrayList My_features.Add("Abstraction"); My_features.Add("ENCAPSULATION"); My_features.Add("Inheritance"); Console.WriteLine("Features provided by OOPS:"); FOREACH(var elements in My_features) { Console.WriteLine(elements); } }}// Parent class 2class Scaler2 :Scaler{ // Providing the implementation // of courses() method public void languages() { // Creating ArrayList ArrayList My_features = new ArrayList(); // Adding elements in the // My_features ArrayList My_features.Add("C++"); My_features.Add("C#"); My_features.Add("JScript"); Console.WriteLine("\nLanguages that use OOPS CONCEPTS:"); foreach(var elements in My_features) { Console.WriteLine(elements); } }}// Child classclass ScalertoScaler : Scaler2 {}public class Scaler1 { // Main method static public void Main() { // Creating object of ScalertoScaler class ScalertoScaler obj = new ScalertoScaler(); obj.features(); obj.languages(); }}Also, C# doesn’t support multiple inheritances. |
|
| 2. |
What is the difference between an Array and ArrayList in C#? |
|
Answer» An array is a collection of similar variables clubbed together under one common name. While ARRAYLIST is a collection of OBJECTS that can be indexed individually. With ArrayList you can access a number of features like dynamic MEMORY allocation, adding, searching, and sorting items in the ArrayList.
For ex.: // C# program to illustrate the ArrayListusing System;using System.Collections; class IB { // Main Method public static void Main(string[] args) { // Create a list of strings ArrayList al = new ArrayList(); al.Add("Bruno"); al.Add("Husky"); al.Add(10); al.Add(10.10); // Iterate list element using foreach loop foreach(var names in al) { Console.WriteLine(names); } }} |
|
| 3. |
What are Generics in C#? |
|
Answer» In C# collections, defining any kind of object is termed okay which compromises C#’s basic rule of type-safety. Therefore, generics were INCLUDED to type-safe the code by allowing re-use of the data processing ALGORITHMS. Generics in C# mean not LINKED to any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting OBJECTS. Generics are always defined INSIDE angular brackets <>. To create a generic class, this syntax is used: GenericList<float> list1 = new GenericList<float>();GenericList<Features> list2 = new GenericList<Features>();GenericList<Struct> list3 = new GenericList<Struct>();Here, GenericList<float> is a generic class. In each of these instances of GenericList<T>, every occurrence of T in the class is substituted at run time with the type argument. By substituting the T, we have created three different type-safe using the same class. |
|
| 4. |
What are extension methods in C#? |
|
Answer» Extension methods help to add new methods to the existing ones. The methods that are added are STATIC. At times, when you want to add methods to an existing class but don’t PERCEIVE the right to modify that class or don’t hold the rights, you can create a new static class containing the new methods. Once the extended methods are declared, bind this class with the existing one and see the methods will be added to the existing one. // C# program to illustrate the concept// of the extension methodsusing System; namespace ExtensionMethod {static class NewMethodClass { // Method 4 public static void M4(this Scaler s) { Console.WriteLine("Method NAME: M4"); } // Method 5 public static void M5(this Scaler s, string str) { Console.WriteLine(str); }} // Now we create a new class in which// Scaler class access all the five methodspublic class IB { // MAIN Method public static void Main(string[] ARGS) { Scaler s = new Scaler(); s.M1(); s.M2(); s.M3(); s.M4(); s.M5("Method Name: M5"); }}}Output: Method Name: M1 Method Name: M2 Method Name: M3 Method Name: M4 Method Name: M5 |
|
| 5. |
What are the differences between ref and out keywords? |
|
Answer» C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword, you need to explicitly mention ‘ref’. void Method(ref int refArgument){ refArgument = refArgument + 10;}int number = 1;Method(ref number);Console.WriteLine(number);// Output: 11
|
|
| 6. |
What is the difference between an abstract class and an interface? |
|
Answer» LET’s dig into the differences between an abstract class and an interface:
An abstract class has constructors while an interface ENCOMPASSES none. Ex. Abstract class: PUBLIC abstract class SHAPE{public abstract void draw();}Interface: public interface Paintable{void paint();} |
|
| 7. |
What is a managed and unmanaged code? |
|
Answer» Managed code lets you run the code on a managed CLR runtime ENVIRONMENT in the .NET framework. |
|
| 8. |
What are the types of classes in C#? |
|
Answer» Class is an entity that ENCAPSULATES all the PROPERTIES of its objects and instances as a single unit. C# has FOUR types of such classes:
Sample code: static class classname { //static data members //static methods }
|
|
| 9. |
What is garbage collection in C#? |
|
Answer» Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the HEAP memory. Now, after you perform all the ACTIONS on the object, the memory space occupied by the object becomes waste. It is necessary to FREE up memory. Garbage collection happens in THREE cases:
|
|
| 10. |
What is Common Language Runtime (CLR)? |
|
Answer» CLR handles PROGRAM execution for VARIOUS languages INCLUDING C#. The architecture of CLR handles MEMORY management, garbage collection, security handling, and LOOKS like: Architecture of CLR |
|
| 11. |
How is C# different from C? |
|
Answer» You would ALWAYS know C being the procedural language while C# is a more object-oriented language. The biggest difference is that C# SUPPORTS automatic garbage collection by Common Language RUNTIME (CLR) while C does not. C# primarily needs a .NET FRAMEWORK to execute while C is a platform-agnostic language. |
|