Important facts Flashcards
- With the aid of a concrete coded example, explain Polymorphism as used in C# programming.
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.
- Define Type Casting and differentiate between Explicit and Implicit casting as used in C#.
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
20 a) What is an exception in a program?
An exception is an unexpected event or error that occurs during the execution of a program, disrupting the normal program flow.
20 b) Explain how exceptions are handled in C#.
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.
20 c) Give brief example exception handling code in C#.
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."); } } }
21 a) Explain what you understand by inheritance in an object-oriented programming language.
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.
21 b) Give one disadvantage of multiple inheritance.
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.
21 c) Using C#, show how a class A can inherit from a class B.
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(); } }
22 a) Differentiate between a file and a stream.
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.
22 b) Write a short C# code to open a file.
csharp using System.IO; class Program { static void Main() { using (StreamReader sr = new StreamReader("example.txt")) { string content = sr.ReadToEnd(); Console.WriteLine(content); } } }