Important facts Flashcards

1
Q
  1. With the aid of a concrete coded example, explain Polymorphism as used in C# programming.
A

Polymorphism in C# allows one interface to be used for a general class of actions, enabling a single method to perform different tasks based on the object that calls it. Polymorphism can be achieved through method overriding and interfaces. Example:

csharp public class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } public class Cat : Animal { public override void Speak() { Console.WriteLine("Cat meows"); } } class Program { static void Main() { Animal myAnimal = new Animal(); myAnimal.Speak(); Animal myDog = new Dog(); myDog.Speak(); Animal myCat = new Cat(); myCat.Speak(); } }
Output: Animal speaks, Dog barks, Cat meows.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Define Type Casting and differentiate between Explicit and Implicit casting as used in C#.
A

Type casting is the process of converting a value from one data type to another. Implicit Casting: Automatically performed by C#, e.g., converting smaller data types to larger data types (int to float). Explicit Casting: Requires the programmer to specify the cast, e.g., converting larger data types to smaller data types (float to int). Example:

csharp int num = 10; float implicitCast = num; // Implicit casting float value = 9.8f; int explicitCast = (int)value; // Explicit casting
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

20 a) What is an exception in a program?

A

An exception is an unexpected event or error that occurs during the execution of a program, disrupting the normal program flow.

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

20 b) Explain how exceptions are handled in C#.

A

Exceptions in C# are handled using try, catch, and finally blocks. The code that might throw an exception is placed in the try block. The catch block handles the exception, and the finally block contains code that always executes, regardless of whether an exception occurs.

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

20 c) Give brief example exception handling code in C#.

A
csharp using System; class Program { static void Main() { try { int[] numbers = {1, 2, 3}; Console.WriteLine(numbers[5]); } catch (IndexOutOfRangeException e) { Console.WriteLine($"Error: {e.Message}"); } finally { Console.WriteLine("Execution completed."); } } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

21 a) Explain what you understand by inheritance in an object-oriented programming language.

A

Inheritance is a feature of object-oriented programming where one class (child or derived class) can inherit the properties and methods of another class (parent or base class). It promotes code reuse and establishes a parent-child relationship.

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

21 b) Give one disadvantage of multiple inheritance.

A

Multiple inheritance can lead to ambiguity when two base classes have methods with the same name, making it difficult for the derived class to decide which method to inherit.

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

21 c) Using C#, show how a class A can inherit from a class B.

A
csharp public class B { public void Display() { Console.WriteLine("Hello from class B"); } } public class A : B { public void Show() { Console.WriteLine("Hello from class A"); } } class Program { static void Main() { A obj = new A(); obj.Display(); obj.Show(); } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

22 a) Differentiate between a file and a stream.

A

A file is a storage unit that stores data permanently on a disk. A stream is a sequence of bytes used to read from or write to a file or other data source in memory.

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

22 b) Write a short C# code to open a file.

A
csharp using System.IO; class Program { static void Main() { using (StreamReader sr = new StreamReader("example.txt")) { string content = sr.ReadToEnd(); Console.WriteLine(content); } } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly