Explore topic-wise InterviewSolutions in Current Affairs.

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. 
Instead, you can use interfaces to inherit the properties using the class name in the signature.

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. 

  • When declaring an array the size of the items is FIXED therefore, the memory allocation is fixed. But with ArrayList, it can be increased or DECREASED dynamically.
  • Array belongs to system.array namespace while ArrayList belongs to the system.collection namespace.
  • All items in an array are of the same datatype while all the items in an ArrayList can be of the same or different data types.
  • While arrays cannot accept null, ArrayList can accept null values.

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


C# out keywords pass arguments within methods and functions. 
‘out’ keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialised before it is passed. Here, The out and ref keywords are useful when we WANT to return a value in the same variables that are passed as an ARGUMENT

PUBLIC static STRING GetNextFeature(ref int id) { string returnText = "Next-" + id.ToString(); id += 1; return returnText; } public static string GetNextFeature(out int id) { id = 1; string returnText = "Next-" + id.ToString(); return returnText; }
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:

  • Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the METHODS inside the interface are abstract methods.
  • Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.
  • Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.

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. 
Managed code runs on the managed runtime environment than the operating system itself. 
Benefits: Provides various services like a garbage collector, exception handling, etc. 

UNMANAGED code is when the code doesn’t run on CLR, it is an unmanaged code that works outside the .NET framework. 
They don’t provide services of the high-level languages and therefore, run WITHOUT them. Such an EXAMPLE is C++. 

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:

  • Static class: Static class, defined by the keyword ‘static’ does not allow inheritance. Therefore, you cannot create an object for a static class.

Sample code:

static class classname  {  //static data members  //static methods  }
  • Partial class: Partial class, defined by the keyword ‘partial’ allows its members to partially divide or share source (.cs) files.
  • Abstract class: Abstract classes are classes that cannot be instantiated where you cannot create objects. Abstract classes work on the OOPS concept of abstraction. Abstraction helps to extract essential DETAILS and hide the unessential ones.
  • Sealed class: Sealed classes are classes that cannot be inherited. USE the keyword sealed to restrict access to users to inherit that class. 
     
sealed class InterviewBit{ // data members // 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:

  • If the occupied memory by the objects exceeds the pre-set threshold value.
  • If the garbage collection method is called
  • If your SYSTEM has low physical memory
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. 

Previous Next