Sunteți pe pagina 1din 15

Exercises

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# ............................................................................................................ 1 2. Identifiers, Variables, Data types Operators C# and Control Statements. .......................... 2 3. Introducing Classes, Objects, Methods and Namespaces. ................................................. 4 4. Inheritance, Polymorphism and Interface ......................................................................... 5 5. Operator Overloading ...................................................................................................... 6 6. Arrays, Index and Collections ........................................................................................... 7 7. Delegates and Events ....................................................................................................... 8 8. Strings and Regular Expressions ....................................................................................... 9 9. Generics ........................................................................................................................ 10 10. Reflection ...................................................................................................................... 11 11. Exception Handling ........................................................................................................ 12
12. Threading and Synchronization ...................................................................................... 13

C# Exercises

Topic - 1 Introduction to C#
1. Create a Console application named Demo1 that produces the following outputs Hello C#. Goodbye C#. 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; Enter your name : <name of user> Enter your Age : <age> Your name is <name of user> and age is <age> 3. Create a console application Demo3 that produces the following output. * ** *** Note: Each star should be printed separately. 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.

C# Exercises

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; 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 2. Create a console application named Demo6 to get 5 numbers as input and find the smallest number among the 5 numbers.

3. 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. 1. Addition 2. Subtraction 3. Division 4. Multiplication 5. Increment and 6. Decrement

C# Exercises

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

C# Exercises

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.

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. The grades Details: Grade = A sal > 10000 Grade = B sal > = 5000

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.

C# Exercises

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. 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.

C# Exercises

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

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.

C# Exercises

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.

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 1, 6524,5647,5823,4521}. 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.

C# Exercises

Topic - 7 Delegates and Events


1. Create a console application named Demo21 to implement delegate. Create a delegate called strMyDel that takes one string parameter and returns a string. Create a class named TestDelegate, that contains two Non static methods Space() and Reverse() having the , Non-static following signature: string Space (string str); string Reverse (string str); Create another class named DemoDell to Implement delegate Using Method group conversion. Note: Space() method will insert space between the input characters and Reverse() method will reverse the input string

2. Create a console application named Demo22 to get two integer numbers from user and perform addition and multiplication on the input numbers. Use the concept of MultiCast Event to achieve the above mentioned output.

3. Create a console application named onsole Demo23 that generates a windows form named MyForm as an output of the console application. Also MyForm should contain a Button control in the middle of the form. When the button is clicked a message box should be displayed. Use the concept of Delegates and Events to achieve the above output.

C# Exercises

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>. 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 @. Insert space between each character of the string

3. Create a console application named Demo26 to get Mobile Number as input from user. Validate the mobile number with the following cases 1. The first four number must be followed by then followed by next six numbers (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

C# Exercises

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.

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. Create two non generic class named GenInt and GenString. 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 T T T Add(T arg1, T arg2); Subtract(T arg1, T arg2); Multiply(T arg1, T arg2); 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

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 a, double b) { Console.Write("Inside set(double, double). "); } Invoke the above two methods from MyRefClass by using Reflection.

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 Type info Constructor info Method info and Parameter info

11

C# Exercises

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.

2. Create a console application named Demo33 the receives the following information from the user; Name (string), Age (integer), Salary (double) and Gender (Char). 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

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)

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 namedThreadDemo 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. Hint: use Lock ()

13

S-ar putea să vă placă și