Sei sulla pagina 1di 15
Exercises ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai
Exercises ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai
Exercises
Exercises
Exercises ICT Academy of Tamil Nadu ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai 600

ICT Academy of Tamil Nadu

ELCOT Complex, 2-7 Developed Plots, Industrial Estate, Perungudi, Chennai 600 096. Website : www.ictact.in , Email : contact@ictact.in , Phone : 044 4290 6800 , Fax : 044 4290 6820

Table of Contents

1. Introduction to C#

2. Identifiers, Variables, Data types Operators C# and Control

Identifiers, Variables, Data types Operators C# and Control Threading and Synchronization 3. Introducing Classes,

Threading and Synchronization

3. Introducing Classes, Objects, Methods and

4. Inheritance, Polymorphism and Interface

5. Operator Overloading

6. Arrays, Index and Collections

7. Delegates and Events

8. Strings and Regular Expressions

9. Generics

10. Reflection

11. Exception Handling

12.

1

2

4

5

6

7

8

9

10

11

12

13

C# Exercises

C# Exercises Topic - 1 Introduction to C# 1. Create a Console application named “ Demo1

Topic - 1

Introduction to C#

1. Create a Console application named “Demo1” that produces the following outputs

Hello C#.

Goodbye C#. Enter your name : <name of user> Enter your Age : <age> Your
Goodbye C#.
Enter your name : <name of user>
Enter your Age : <age>
Your name is <name of user> and age is <age>
*
* *
***

Note: Each star should be printed separately.

2. Create a console application named “Demo2” to get name and age as input from the user and display the same. The output of the program should be as below;

3. Create a console application “Demo3” that produces the following output.

4. Create a console application named “Demo4” that gets name from the user and displays only the first character of the name.

Sample Output

Enter Your Name: ICTACT

Your Name starts with - I.

1

C# Exercises

C# Exercises Topic - 2 Identifiers, Variables, Data types Operators C# and Control Statements. 1. Create

Topic - 2

Identifiers, Variables, Data types Operators C# and Control Statements.

1. Create a console application named “Demo5” to get 5 numbers from user and sort the number in ascending order. The output should be as below;

2.

3.

Enter No 1: 56 Enter No 2: 42 Enter No 3: 100 Enter No 4:
Enter No 1: 56
Enter No 2: 42
Enter No 3: 100
Enter No 4: 2
Enter No 5: 10
Ascending Sort: 2,10,42,56,100
1. Addition
2. Subtraction
3. Division

Create a console application named “Demo6” to get 5 numbers as input and find the smallest number among the 5 numbers.

Create a console application named “Demo7” to get two numbers as input from the user and allow the user to choose the arithmetic operation be performed on the input numbers. After entering the two numbers, the user should be asked to enter a number from 1 to 6 to choose the arithmetic operation to be performed.

4. Multiplication

5. Increment and

6. Decrement

2

C# Exercises

C# Exercises i.e. if the user enters 1 then addition should be performed , if the

i.e. if the user enters 1 then addition should be performed , if the user enters 5 then Increment should be done.

Note: Use switch-case control statement.

4. Create a console application named “Demo8” to get the name of the user as input and print each characters in his name as output. (Use ForEach Statement)

Enter your Name: ICTACT Output: I C T A C T
Enter your Name: ICTACT
Output:
I
C
T
A
C
T

3

C# Exercises

C# Exercises Topic - 3 Introducing Classes, Objects, Methods and Namespaces. 1. Create a console application

Topic - 3

Introducing Classes, Objects, Methods and Namespaces.

1. Create a console application named “Demo9” that contains a class “DateDemo” which has a method named “DiaplayDate” which when called displays the current date.

DiaplayDate ” which when called displays the current date. The grades Details: Grade = ‘A’ sal

The grades Details:

Grade = ‘A’ sal > 10000

Grade = ‘B’ sal > = 5000

2. Create a console application named “Demo10” having a class called EmpDetails.

Add functionality such as storing and retrieving employee details such as employee id, name, address, pin code, phone number, gross salary, and PF.

Display the net salary (that is gross less PF) and calculate the grade based on net salary.

3. Create a console application named “Demo11” to get a name as input from the user. If the length of the name is greater than 5 print the name 10 times else print the name 5 times.

4

C# Exercises

C# Exercises Topic - 4 Inheritance, Polymorphism and Interface 1. Create a console application named “

Topic - 4

Inheritance, Polymorphism and Interface

1. Create a console application named “Demo12” that contains a base class that accepts registration number and name of students. Write a program that will accept registration number, name, and Branch as input. Implement two branch called CSE and ECE using Inheritance. If the student enters CSE, then get the marks of computer Network, Data Structure and Testing. If the student enters ECE, get the marks for Electronic Circuits, Circuit Analysis and Analog Communication.

Circuits, Circuit Analysis and Analog Communication. Then calculate the average of the three subjects. The output

Then calculate the average of the three subjects.

The output should display the registration number, name, and the details of subject wise marks and the average marks.

Hint: Student is the base class; ScienceStudent and CommerceStudent are inherited from it.

2. Create a console application named “Demo13” that has a base class called figure which contains x, y, and name as member variable. x and y are double data type. Then create 4 derived classes named rectangle, triangle, square and cube which inherit from base class figure. Then create a method named area in base class figure which should be overridden in all derived class to calculate the area for each derived classes. Use constructors for all classes to initialize data members. Data should be taken from user input.

3. Create a console application named “Demo14” that contains an Interface named “IShape” which contains method called area. Create two classes cube and circle that implements the interface. Calculate the area of cube and circle and print in the console window.

4. Create a console application named “Demo15” that contains two interface named “ICircle” and “ICube”. Both the interfaces contain a method named “area”. Create a class named “Shape” that implements both the interface. Calculate the area of cube and circle and print in the console window.

5

C# Exercises

C# Exercises Topic - 5 Operator Overloading 1. Create a console application named “ Demo16 ”

Topic - 5

Operator Overloading

1. Create a console application named “Demo16” that contains a class named “LoadOpt1” that overloads “/” operator (Division) to perform multiplication operation

operator (Division) to perform multiplication operation 2. Create a console application named “ Demo17 ” that

2. Create a console application named “Demo17” that contains a class named “LoadOpt2” that contains three integer variables x,y and z and a method that overloads “-” operator to calculate the difference between instance variable. Create two instance of LoadOpt2 and find the difference between the instance variable using the overloaded operator method and store the result in a instance of LoadOpt2. Print the result in console window.

6

C# Exercises

C# Exercises Topic - 6 Arrays, Index and Collections 1. Create a console application named “

Topic - 6

Arrays, Index and Collections

1. Create a console application named “Demo18” that contains a class named “ArraySort” that contains an integer array named MyArray of size 5. Get 5 numbers as inputs from user and initialize the MyArray array. Perform a sort operation on MyArray and Print the result in the console window.

• •

1, 6524,5647,5823,4521}.

2. Create a console application named “Demo19” that contains a class named “ArraySearch”. This class contains an integer array named RollNumber of size 20. The elements of RollNumber array are

{1002,2001,3005,4002,5005,6005,7005,7009,8009,13006,4588,5125,57896,3698,4587,784

Perform a Binary Search on the RollNumber array to find the index of element 5125. Print the output in a console window.

3. Create a console application named “Demo20” that contains a class named “EmpArrayList” which contains the following members EmpID, Name and Age.

Create 5 instance of EmpArrayList class and store each instance in an array list.

Also allow the user to search the arraylist by entering EmpID and if the EmpID exist in the arrylist then display Name and Age of the employee.

7

C# Exercises

C# Exercises Topic - 7 Delegates and Events 1. Create a console application named “ Demo21

Topic - 7

Delegates and Events

1.

Create a console application named “Demo21” to implement delegate. Cre ate a delegate called strMyDel that takes o ne string parameter and returns a string. Creat e a class named TestDelegate, that contains two Non-static methods Space() and Reverse() having the following signature:

string Space (string str); string Rever se (string str); Create another class named conversion. “DemoDell”
string Space (string str);
string Rever se (string str);
Create another class named
conversion.
“DemoDell” to Implement delegate Using Met hod group
Note:
Space() method will ins ert space between the input characters and Re verse() method
will reverse the input s tring
2. Create a console application named “Demo22” to get two integer numbers from user and
perform addition and multip lication on the input numbers. Use the concept of MultiCast
Event to achieve the above
mentioned output.
3. Create a console application
named “Demo23” that generates a windows f orm named
“MyForm” as an output of t he console application. Also “MyForm” should c ontain a Button
control in the middle of the f orm. When the button is clicked a message bo x should be
displayed.

Use the concept of Delegate s and Events to achieve the above output.

8

C# Exercises

C# Exercises Topic - 8 Strings and Regular Expressions 1. Create a console application named “

Topic - 8

Strings and Regular Expressions

1. Create a console application named “Demo24” to get employee name and age as input from user. Employee name is of type String and Age is of type integer. Use the Concat() method of string class to generate the following output:

Your Name is <Emp Name > and Age is <Emp_age>. • • Insert space between
Your Name is <Emp Name > and Age is <Emp_age>.
Insert space between each character of the string
1.
(eg:9894-256874)
2.
Should contain only numbers.
3.
Should be of length 10.
4.
The first number should start only with 9 or 8

2. Create a console application named “Demo25” to get a string input from the user and perform the following operation

Check whether the input string contains any space and replace the space with “@”.

3. Create a console application named “Demo26” to get Mobile Number as input from user. Validate the mobile number with the following cases

The first four number must be followed by “–“ then followed by next six numbers

9

C# Exercises

C# Exercises Topic - 9 Generics 1. Create a console application named “ Demo27 ” that

Topic - 9

Generics

1. Create a console application named “Demo27” that contains a class named “GenDemo”. The “GenDemo” class should contain a non static generic method named “GenMethod” which should accept any type of argument and cast arguments to string type and return the same. Create a Class named “Demo” that calls the generic method (GenMethod) by passing different type of arguments at each call and display the generated output in a console window.

T Add(T arg1, T arg2); T Subtract(T arg1, T arg2);
T
Add(T arg1, T arg2);
T
Subtract(T arg1, T arg2);

Create two non generic class named “GenInt” and “GenString”.

2. Create a console application named “Demo28” that contains a two type parameter generic class named “GenCalss”. The “GenClass” also contains a generic virtual method “Add” which accepts two generic parameters and generic return type.

The “GenInt” class should inherit “GenClass” using two integer type parameter and override the Add method to perform arithmetic addition of the two integer parameters. The values of the two integer parameter should be given by the user.

The “GenString” class should inherit “GenClass” using two string type parameter and override the Add method to perform string concatenation of the two string parameters. The values of the two string parameter should be given by the user.

3. Create a console application named “Demo29” that contains a singletone generic interface named “GenInterface”. The “GenInterface” contains the following methods.

T

Multiply(T arg1, T arg2);

T

Divide(T arg1, T arg2);

Create a non generic class named “BasicMath” which implements the “GenInterface” using integer type parameter. Also provide the implementation of the above four methods in the “BasicMath” class and display the output of each method in the console window.

10

C# Exercises

C# Exercises Topic - 10 Reflection 1. Create a console application named “ Demo30 ” that

Topic - 10

Reflection

1. Create a console application named “Demo30” that contains two classes namely “MyClass” and “MyRefClass”. “MyClass” contains the following two methods.

public void set(int a, int b) { Console.Write("Inside set(int, int). "); } public void set(double
public void set(int a, int b)
{
Console.Write("Inside set(int, int). ");
}
public void set(double a, double b)
{
}
Invoke the above two methods from “MyRefClass” by using Reflection.
• Type info
• Constructor info
• Method info and
• Parameter info

Console.Write("Inside set(double, double). ");

2. Create a console application named “Demo31” which uses the assembly (Demo30.exe) generated in the previous Topic. Using reflection find the following details about the assembly

11

C# Exercises

C# Exercises Topic - 11 Exception Handling 1. Create a console application named “ Demo32 ”

Topic - 11

Exception Handling

1. Create a console application named “Demo32” that handles the “IndexoutofRange” exception when the user either tries to read or assign value to an array index greater than the size of array.

value to an array index greater than the size of array. Name (string), Age (integer), Salary

Name (string), Age (integer), Salary (double) and Gender (Char).

2. Create a console application named “Demo33” the receives the following information from the user;

If the user enters an age less than 18 and greater than 50, then throw a user defined exception and display the message as “Registration Failed. Enter age greater than 18 and less than 50”.

If the user enters an age between 18 and 50, then allow the user to enter other information (Salary and Gender) and display a message as “Registration Successful”.

12

C# Exercises

C# Exercises Topic - 12 Threading and Synchronization 1. Create a console application named “ Demo33

Topic - 12

Threading and Synchronization

1. Create a console application named “Demo33” that displays current date a time for 10 times with the time interval of 2 seconds. (Hint: Use Sleep() method)

the time interval of 2 seconds. (Hint: Use Sleep() method) Hint: use Lock () 2. Create

Hint: use Lock ()

2. Create a console application named “Demo34” that contains class named “MyClass”. The class contains a method called “Run()” which displays the name of the invoking thread for 5 times with a time gap of 2 seconds.

Create a class named”ThreadDemo” that invokes the Run() method using three child thread. All child thread should invoke the Run () method at the same time.

3. Create a console application named “Demo35” that contains class named “MyClass”. The class contains a method called “Run()” which displays the name of the invoking thread for 5 times with a time gap of 2 seconds.

Create a class named ”ThreadDemo” that invokes the Run() method using three child thread. Only one child thread should access the Run () method.

13