· Question 1: Use virtual and abstact
public abstract class Base1
{
public int Add(int x, int y)
{
return x + y;
}
public abstract int Substract(int x, int y);
public virtual int Manipulation(int x, int y)
{
return 0;
}
}
public class Derived : Base1
{
public int Add(int x, int y, int z)
{
return x + y + z;
}
public override int Substract(int x, int y)
{
return x - y;
}
public override int Manipulation(int x, int y)
{
return x * y * 3;
}
}
class Program
{
public static void Main(string[] args)
{
Derived derived = new Derived();
Console.WriteLine(derived.Add(1, 2));
Console.WriteLine(derived.Add(1, 2, 3));
Console.WriteLine(derived.Substract(10, 2));
Console.WriteLine(derived.Manipulation(2, 2));
}
}
· Question 2: Polymorphic behaviour (Covariance)
using System;
public class Vehicle
{
public virtual void Start()
{
Console.WriteLine("Vehicle starting...");
}
}
public class Car : Vehicle
{
public override void Start()
{
Console.WriteLine("Car starting...");
}
public void Accelerate()
{
Console.WriteLine("Car accelerating...");
}
}
class Program
{
static void Main(string[] args)
{
// .............Upcasting (Implicit Casting)................
Vehicle vehicle = new Car();
vehicle.Start();
// vehicle.Accelerate(); // We can't directly call Accelerate() because vehicle is of type Vehicle, which doesn't have that method This would result in a compile-time error
// ............ Downcasting (Explicit Casting)...........
Car car = (Car)vehicle;
car.Start();
car.Accelerate();
}
}
· Question 3: In this task you have a class named “OrderHistory”. You have to change code in this class to use anonymous objects instead of tuples. No tuple should be present after your conversion. You also can’t create a custom class. You have to achieve the same result of the code using anonymous object.
// Quesion
(string name, int age)[] persons = new (string name, int age)[]
{
("jalaluddin", 41),
("tareq", 33),
("hasan", 52),
("rashed", 21),
("monir", 45)
};
(string name, double amount)[] orders = new (string name, double amount)[]
{
("monir", 300.5),
("rashed", 20.5),
("tareq", 29.9),
("hasan", 17.7),
("jalaluddin", 30.2)
};
var result = new List<(string, double)>();
foreach (var person in persons)
{
foreach (var order in orders)
{
if (person.name == order.name)
{
result.Add((person.name, order.amount));
}
}
}
foreach(var x in result)
Console.WriteLine(person.name + ", ", order.amount);
// Answer
var persons = new[]
{
new { name = "jalaluddin", age = 41 },
new { name = "tareq", age = 33 },
new { name = "hasan", age = 52 },
new { name = "rashed", age = 21 },
new { name = "monir", age = 45 }
};
var orders = new[]
{
new { name = "monir", amount = 300.5 },
new { name = "rashed", amount = 20.5 },
new { name = "tareq", amount = 29.9 },
new { name = "hasan", amount = 17.7 },
new { name = "jalaluddin", amount = 30.2 }
};
var result = new List<object>();
foreach (var person in persons)
{
foreach (var order in orders)
{
if (person.name == order.name)
{
result.Add(new { name = person.name, amount = order.amount });
}
}
}
// Printing the result
foreach (var x in result)
{
Console.WriteLine(
$"{x.GetType().GetProperty("name").GetValue(x)}: {x.GetType().GetProperty("amount").GetValue(x)}"
);
}
· Question 4: In this task, you have to convert the “Form” class into partial classes to separate the fields and properties.
// Question
public class Form
{
int _value1;
string _value2;
double _value3;
bool _value4;
DateTime _value5;
public int Value1
{
get { return _value1; }
set { _value1 = value; }
}
public string Value2
{
get { return _value2; }
set { _value2 = value; }
}
public double Value3
{
get { return _value3; }
set { _value3 = value; }
}
public bool Value4
{
get { return _value4; }
set { _value4 = value; }
}
public DateTime Value5
{
get { return _value5; }
set { _value5 = value; }
}
}
// Answer
public partial class Form
{
private int _value1;
private string _value2;
private double _value3;
private bool _value4;
private DateTime _value5;
}
public partial class Form
{
public int Value1
{
get { return _value1; }
set { _value1 = value; }
}
public string Value2
{
get { return _value2; }
set { _value2 = value; }
}
public double Value3
{
get { return _value3; }
set { _value3 = value; }
}
public bool Value4
{
get { return _value4; }
set { _value4 = value; }
}
public DateTime Value5
{
get { return _value5; }
set { _value5 = value; }
}
}
· Question 5: In this task, you have to replace 3 overloads in StringHelper class with one method only so that the code in program.cs works file and also can be used for more parameters if needed.
// Question
public class StringHelper
{
public string Append(string a, string b)
{
return a + b;
}
public string Append(string a, string b, string c)
{
return a + b + c;
}
public string Append(string a, string b, string c, string d)
{
return a + b + c + d;
}
}
class Program
{
public static void Main(string[] args)
{
var helper = new StringHelper();
Console.WriteLine(helper.Append("jalal ", "uddin"));
Console.WriteLine(helper.Append("Md. ", "jalal ", "uddin"));
Console.WriteLine(helper.Append("Mr", "Md. ", "jalal ", "uddin"));
}
}
// Answer
public class StringHelper
{
public string Append(params string[] parameters)
{
return string.Concat(parameters);
}
}
class Program
{
public static void Main(string[] args)
{
var helper = new StringHelper();
Console.WriteLine(helper.Append("jalal ", "uddin"));
Console.WriteLine(helper.Append("Md. ", "jalal ", "uddin"));
Console.WriteLine(helper.Append("Mr", "Md. ", "jalal ", "uddin"));
}
}
· Question 6: In this task, you have to change the Product abstract class into a normal class + an interface.
// Question
public abstract class Product
{
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public abstract string GenerateQRCode();
public abstract string GetDiscount(double discountPercent);
public string? GetShortName()
{
return Name?.Substring(0, 5);
}
}
// Answer
public interface ISellable
{
string GenerateQRCode();
string GetDiscount(double discountPercent);
}
public class Product
{
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string? GetShortName()
{
return Name?.Substring(0, 5);
}
}
· Question 7: In this task you have to convert the code in “Machine” class to replace the delegate with a Func or Action. You also need to use the Machine class to execute the event by writing example code in program.cs.
// Question
public class Machine<T> where T : class, new()
{
public delegate void Starter(params T[] args);
public event Starter OnStart;
public void Start(params T[] args)
{
OnStart.Invoke(args);
}
}
// Answer
public class Machine<T> where T : class, new()
{
public event Action<T[]> OnStart;
public void Start(params T[] args)
{
OnStart?.Invoke(args);
}
}
· Question 8: In this task, you have to create a 3 dimensional indexer for “Grid” class so that we can write Grid[2, 3, 4]
for example to get value from its private _grid array. Don’t make the _grid array public, only return value using indexer.
// Question
public class Grid<T> where T : struct
{
private T[,,] _grid;
}
// Answer
public class Grid<T> where T : struct
{
private T[,,] _grid;
}
public class Grid<T> where T : struct
{
private T[,,] _grid;
public Grid(int size1, int size2, int size3)
{
_grid = new T[size1, size2, size3];
}
public T this[int index1, int index2, int index3]
{
get { return _grid[index1, index2, index3]; }
set { _grid[index1, index2, index3] = value; }
}
}
class Program
{
static void Main(string[] args)
{
// Create a new Grid
var grid = new Grid<int>(3, 3, 3);
// Set values
grid[0, 0, 0] = 1;
grid[1, 1, 1] = 2;
grid[2, 2, 2] = 3;
// Get values
Console.WriteLine(grid[0, 0, 0]); // Should print 1
Console.WriteLine(grid[1, 1, 1]); // Should print 2
Console.WriteLine(grid[2, 2, 2]); // Should print 3
}
}
· Question 9: In this task, you have to write an extension method for Stack<int>
to calculate the sum of all items in it.
// Answer
public static class StackExtensions
{
public static int Sum(this Stack<int> stack)
{
var sum = 0;
while (stack.Count > 0)
{
sum += stack.Pop();
}
return sum;
}
}
class Program
{
public static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
stack.Push(30);
stack.Push(40);
stack.Push(50);
Console.WriteLine(stack.Sum());
}
}
· Question 10: In this task, you have to write code to read data from a text file 8 bytes at a time. You have to use filestream to achieve this.
// Answer
//file.txt
Hello, this is from Bangladesh.
This is a sample text file.
We have a nice course on C#.
using System.Buffers.Text;
using System.Collections;
using System.Text;
var home = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent.Parent;
var path = Path.Combine(home?.FullName, "file.txt");
using var stream = File.Open(path, FileMode.Open);
byte[] buffer = new byte[8];
while(true)
{
int read = stream.Read(buffer, 0, 8);
if(read == 0) break;
var text = Encoding.Default.GetString(buffer);
Console.WriteLine(text);
}