C#, Questions Flashcards

1
Q
  1. Explain the concept of polymorphism in C#. Provide an example to illustrate your answer.
A

Polymorphism in C# allows one interface to be used for different data types or classes, providing flexibility in code. It can be achieved via method overriding or method overloading. Example:
class Animal { public virtual void Speak() { Console.WriteLine(“Animal speaks”); } }
class Dog : Animal { public override void Speak() { Console.WriteLine(“Dog barks”); } }
Animal myDog = new Dog(); myDog.Speak(); // Output: Dog barks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What is a delegate in C#? Explain with an example of how to declare and use a delegate.
A

A delegate in C# is a type-safe function pointer that can reference a method. Example:
delegate void Greet(string name);
class Program { static void SayHello(string name) { Console.WriteLine(“Hello, “ + name); }
static void Main() { Greet greet = SayHello; greet(“Alice”); } } // Output: Hello, Alice

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Describe the use of the foreach loop in C#. Provide an example to demonstrate its usage.
A

The foreach loop in C# is used to iterate through elements in a collection or array. Example:
int[] numbers = { 1, 2, 3 }; foreach (int num in numbers) { Console.WriteLine(num); }
// Output: 1 2 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Explain the concept of garbage collection in C#. How does it help manage memory?
A

Garbage collection in C# is an automatic memory management feature that reclaims memory occupied by objects no longer in use, preventing memory leaks. The .NET runtime automatically identifies and frees unused objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What is an interface in C#? Explain with an example of how to implement and use an interface.
A

An interface is a class that is complete abstract and contain only abstact methods and properties

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Explain what LINQ is and provide a simple example of a LINQ query.
A

LINQ or Language Intergrated Query is powefull feature in C# that allows users to perform queries to different data sources using a consitent syntax directily integrated into C#.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Describe exception handling in C#. Include an example with a try-catch block.
A

Exception handling in C# uses try-catch blocks to handle runtime errors gracefully. Example:
try { int result = 10 / 0; }
catch (DivideByZeroException ex) { Console.WriteLine(“Cannot divide by zero!”); }
// Output: Cannot divide by zero!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Describe the difference between public, private, and protected access modifiers in C#. Provide an example for each.
A

Public: Accessible from anywhere. Private: Accessible only within the class. Protected: Accessible within the class and derived classes.
Example:
class Base { public int PublicValue; private int PrivateValue; protected int ProtectedValue; }
class Derived : Base { void Test() { Console.WriteLine(ProtectedValue); } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Explain the concept of encapsulation in C#. Provide an example to illustrate your answer.
A

Encapsulation is the bundling of data and methods in a class and restricting direct access to fields using access modifiers. Example:
class Person { private string name; public string Name { get { return name; } set { name = value; } } }
Person person = new Person(); person.Name = “Alice”; Console.WriteLine(person.Name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Describe how inheritance works in C#. Include an example to demonstrate inheritance between two classes.
A

Inheritance in C# allows a class to inherit members (fields, methods) from another class. Example:
class Animal { public void Eat() { Console.WriteLine(“Animal eats”); } }
class Dog : Animal { public void Bark() { Console.WriteLine(“Dog barks”); } }
Dog dog = new Dog(); dog.Eat(); dog.Bark(); // Output: Animal eats Dog barks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly