Tutorial
Tutorial
1. The employee list for a company contains employee code, name, designation and
basic pay. The employee is given a house rent allowance (HRA) of 10% of the
basic pay and dearness allowance (DA) of 45% of the basic pay. The total pay of
the employee is calculated as Basic Pay + HRA + DA. Write a class to define the
details of the employee. Write a constructor to assign the required initial values.
Add a method to calculate HRA, DA and total pay and print them. Write
another class with main method. Create objects for three different employees
and calculate HRA, DA and total pay.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p1
{
class Employee
{
// Employee details
private int employeeCode;
private string name;
private string designation;
private double basicPay;
// Method to calculate DA
private double CalculateDA()
{
return 0.45 * basicPay;
}
2. From the following code and given output, complete missing statements and find
out error code and correct it.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p2
{
class Shape
{
public double Width;
public double Height;
Console.WriteLine();
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p3
{
// Task 1:
class ParentClass
{
// Task 2:
private int privateData;
protected int protectedData;
public int publicData;
// Task 3:
public void SetPrivateData(int value)
{
privateData = value;
}
// Task 4:
class ChildClass : ParentClass
{
// Task 5:
private string privateInfo;
protected string protectedInfo;
public string publicInfo;
// Task 6:
public void SetPrivateInfo(string info)
{
privateInfo = info;
}
// Task 8:
ChildClass obj1 = new ChildClass();
ChildClass obj2 = new ChildClass();
Console.WriteLine("Data of obj1:");
obj1.PrintData();
obj1.PrintInfo();
Console.WriteLine();
Console.WriteLine("Data of obj2:");
obj2.PrintData();
obj2.PrintInfo();
Console.ReadLine();
}
}
}
4. From the following code and given output complete missing statements and
find out error code and correct it.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p4
{
class StaticVar
{
public static int num;
public void counting()
{
num++;
}
}
}
}
5. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p5
{
public class A
{
public A(int value)
{
public class B : A
{
public B(int value) : base(value)
{
Console.WriteLine("Derived constructor B()");
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");
A a = new A(0);
B b = new B(1);
Console.ReadLine();
}
}
}
6. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p6
{
abstract class Test
{
protected int a;
public abstract void A();
}
7. Refer given output and find out error code and correct it.
Code:-
namespace t4
{
internal class p7
{
class A
{
public int x;
public int y;
}
class B : A
{
public int z;
}
static void Main(string[] args)
{
Console.WriteLine("abhay");
A sc = new A();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
Console.ReadLine();
}
}
}
8. Find out error code and correct it. Print appropriate output as desired.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
class X
{
public virtual void F() { Console.WriteLine("X.F"); }
public virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
public override void F() { Console.WriteLine("Y.F"); }
public override void F2() { Console.WriteLine("Y.F2"); }
}
class Z : Y
{
}
class p8
{
static void Main()
{
Console.WriteLine("abhay");
Y Obj2 = new Y();
Obj2.F();
Obj2.F2();
Z Obj3 = new Z();
Obj3.F();
Obj3.F2();
Console.ReadLine();
}
}
}
9. This program will throw an exception. Add try, catch and finally blocks to
handle this exception.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t4
{
internal class p9
{
static void Main(string[] args)
{
Console.WriteLine("abhay");
int x = 0;
try
{
int div = 100 / x;
Console.WriteLine(div);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Finally block executed.");
}
Console.ReadLine();
}
}
}
namespace t4
{
internal class p10
{
class MyException : Exception
{
public MyException(string str, Exception e) : base(str, e)
{
}
}
static void Main(string[] args)
{
Console.WriteLine("abhay");
try
{
throw new MyException("my exception generated.", new Exception());
}
catch (Exception e)
{
Console.WriteLine("Exception caught here: " + e.Message);
}
Console.WriteLine("LAST STATEMENT");
Console.ReadLine();
}
}
}