0% found this document useful (0 votes)
10 views15 pages

Dot Net Mid 2

mid 2 dot net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Dot Net Mid 2

mid 2 dot net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

STATE BOARD OF TECHNICAL EDUCATION AND TRAINING

TELANGANA
DIPLOMA EXAMINATION (C-21)
SEP-24
SEMESTER V , MID-II EXAM
CCB/CS
15041
CS-574
.NET Programming Through C#

Answer Key

Exam Date: 26-09-2024 Session: AN


Duration: 1 Hour [01:30 PM To 02:30 PM] [Total Marks: 20]

PART-A

Instructions: 1. Answer the following questions. 4X1=4


2. Each question carries ONE mark.

1. List various states of thread life cycle.


Ans.
A thread in C# at any point of time exists in any one of the following states.
A thread lies only in one of the shown states at any instant:

1. Unstarted state
2. Runnable state
3. Running state
4. Not Runnable state
5. Dead state

2. Define user-defined exception.


Ans.
User can define their own exceptions. These are called user defined
exceptions. User-defined exception classes are derived from the
ApplicationException or Exception class. Custom exceptions are what
we call user-defined exceptions.

3. What is used to call an anonymous method?


Ans.
Delegates are used to call an anonymous method.

4. Define an event in C#
Ans.
An event has a publisher, subscriber, notification and a handler. Generally,
UI controls use events extensively. For example, the button control in a
Windows form has multiple events such as click, mouseover

PART-B

Instructions: 1. Answer the following questions. 2X3=6


2. Each question carries THREE marks.

5(a). Write any three Thread class properties and methods.


Ans.
C# Thread class provides properties and methods to create and control
threads. It is found in System.Threading namespace.

C# Thread Properties

Following table lists some of the most commonly


used properties of Thread class to work with threads in c#.

Property Description
CurrentContext It will return the current context in which the thread is
executing.
CurrentThread It will return the currently running thread.
CurrentCulture It is useful to get or set the culture for current thread.
CurrentUICulture It is useful to get or set the Resource Manager current culture
to look up culture-specific resources at run time.
IsAlive Checks whether the current thread is alive or not. It is useful to
get the execution status of current thread.
IsBackground It is useful to get or set a value which indicating whether the
thread is background thread or not.
IsThreadPoolThread It will return a value which indicate whether the thread belongs
to the managed thread pool or not.
ManagedThreadId It will return unique identifier of the current managed thread.
Name It is useful to get or set the name of the thread.
Property Description
Priority We can get or set a value which indicate the schedule priority of
a thread.
ThreadState It is useful to get the state of current thread.
C# Thread Methods

Following table lists some of the most commonly used methods


of Thread class to work with threads in C#.

Method Description
Abort() This method is useful to terminate the thread. It raises
ThreadAbortException.
AllocateDataSlot() It will allocates an unnamed data slot on all the threads.
AllocateNamedDataSlot() It will allocates a named data slot on all threads.
BeginThreadAffinity() It will notify a host that the managed code is about to
execute the instructions that depend on the identity of
current physical operating system thread.
EndThreadAffinity() It will notify a host that the managed code has finished
executing the instructions that depend on the identity of
the current physical operating system thread.
Equals() It will determine whether the specified object is equal to
the current object or not.
Finalize() It will ensures that the resources are freed and other
cleanup operations are performed when the garbage
collector reclaims the Thread object.
GetData() It is useful to retrieve the value from the specified slot on
the current thread.
GetDomain() It will return the current domain in which the current
thread is running.
GetHashCode() It will return the hash code of current thread.
GetType() It will return the type of current instance.
Interrupt() It will interrupt a thread that is in the WaitSleepJoin thread
state.
Join() It will make the thread to finish its work or it will halt
other threads until it finishes work.
ResetAbort() It is used to cancel the Abort request for the current
thread.
Resume() It will resumes a thread that has been suspended.
Sleep() It will suspend the current thread for specified amount of
time in milliseconds.
Start() It will instruct the operating system to change the state of
current instance to Running.
Suspend() It will suspend the thread.
VolatileRead() It will read the value of a field and that value is the latest
written by any processor in a computer.
Method Description
VolatileWrite() It will write a value to the field immediately, so that the
value will be visible for all processors in the computer.
Yield() It will yield the execution to another thread that is ready to
run on the current processor.

----- OR ----
5(b). Describe try, catch, and finally blocks.
Ans.
Syntax Syntax:

Program statements that you want to monitor for exceptions are contained
within a try block. If an exception occurs within the try block, it is thrown.
Your code can catch this exception using catch and handle it in some
rational manner. System-generated exceptions are automatically thrown by
the runtime system. . Any code that absolutely must be executed upon
exiting from a try block is put in a finally block

try

// code that may raise exceptions

throw exception_object;

catch(Exception ex)

// handle exception

finally

// final cleanup code

6(a). Explain about indexers in C# .NET


Ans.
An indexer allows an object to be indexed such as an array. When you
define an indexer for a class, this class behaves similar to a virtual array.
You can then access the instance of this class using the array access
operator ([ ]).

Syntax

A one dimensional indexer has the following syntax −

element-type this[int index] {

// The get accessor.


get {
// return the value specified by index
}

// The set accessor.


set {
// set the value specified by index
}
}

----- OR ----
6(b). List any three features of generics in C# .NET
Ans.
Features of Generics:

● It helps you to maximize code reuse, type safety, and performance.


● You can create generic collection classes. The .NET Framework class
library contains several new generic collection classes in the
System.Collections.Generic namespace. You may use these generic
collection classes instead of the collection classes in the
System.Collections namespace.
● You can create your own generic interfaces, classes, methods, events, and
delegates.
● You may create generic classes constrained to enable access to methods
on particular data types.
● You may get information on the types used in a generic data type at run-
time by means of reflection.

PART-C

Instructions: 1. Answer the following questions. 2 X 5 = 10


2. Each question carries FIVE marks.
7(a). Write a C# program to create Multiple Threads.
Ans.
Following is the example of creating multiple threads to execute the
multiple tasks simultaneously in c#.

using System;

using System.Threading;

namespace MultiThreadExample

class MThread

static void Main(string[] args)

// Create child threads

Thread t1 = new Thread(new ThreadStart(method1));

Thread t2 = new Thread(new ThreadStart(method2));

t1.Start();

t2.Start();

Console.ReadLine();

static void method1()

for (int i = 1; i <= 10; i++)

Console.WriteLine("i value: {0}", i);

if(i==5)

Thread.Sleep(5000);
}

Console.WriteLine("First method completed");

static void method2()

for (int j = 1; j <= 10; j++)

Console.WriteLine("j value: {0}", j);

Console.WriteLine("Second method completed");

----- OR ----
7(b). Write a C# program to set and get Thread priorities.
Ans.
Example1:

// C# program to illustrate how to set and get the priority of threads

using System;

using System.Threading;

class ThreadPrior

// Main Method
static public void Main()

// Creating and initializing threads

Thread T1 = new Thread(method);

Thread T2 = new Thread(method);

Thread T3 = new Thread(method);

// Set the priority of threads

T2.Priority = ThreadPriority.Highest;

T3.Priority = ThreadPriority.BelowNormal;

T1.Start();

T2.Start();

T3.Start();

// Display the priority of threads

Console.WriteLine("The priority of T1 is: {0}", T1.Priority);

Console.WriteLine("The priority of T2 is: {0}", T2.Priority);

Console.WriteLine("The priority of T3 is: {0}", T3.Priority);

public static void method()

for (int i = 1; i <= 5; i++)


{

Console.WriteLine("i value: {0}", i);

Thread.Sleep(1000);

Output

i value: 1

i value: 1

The priority of T1 is: Normal

The priority of T2 is: Highest

The priority of T3 is: BelowNormal

i value: 1

i value: 2

i value: 2

i value: 2

i value: 3

i value: 3

i value: 3

i value: 4

i value: 4

i value: 4

i value: 5

i value: 5

i value: 5
Example 2:

// C# program to illustrate the Priority property of Thread class

using System;

using System.Threading;

class ThreadPrior

// Main Method

static public void Main()

// Creating and initializing threads

Thread T1 = new Thread(method1);

Thread T2 = new Thread(method2);

// Set the priority of threads

// Here T2 thread executes first because the Priority of T2 is highest as


compare to T1 thread

T1.Priority = ThreadPriority.Lowest;

T2.Priority = ThreadPriority.Highest;

T1.Start();

T2.Start();

Console.ReadKey();

}
public static void method1()

for (int i = 1; i <= 5; i++)

Console.WriteLine("T1 thread is working..");

public static void method2()

for (int i = 1; i <= 5; i++)

Console.WriteLine("T2 thread is working..");

Output

The output is unpredictable because threads are highly system dependent.


It may follow any algorithm preemptive or non-preemptive.

T2 thread is working..

T2 thread is working..

T2 thread is working..

T2 thread is working..

T2 thread is working..

T1 thread is working..

T1 thread is working..
T1 thread is working..

T1 thread is working..

T1 thread is working..

8(a). Write a C# program on passing parameters to anonymous methods in C#


.NET.
Ans.
In c#, we can pass an anonymous method as a parameter to
the method that accepts delegate as a parameter.

Following is the example of sending an anonymous method as a parameter


to another method in c#.

using System;

namespace TutlaneExamples

class Program

// Create a delegate

public delegate void MathOps(int a);

static void Main(string[] args)

int y = 10;

// Instantiate the delegate using an anonymous method

MathOps ops = delegate(int x)

Console.WriteLine("Add Result: {0}", x + y);

Console.WriteLine("Subtract Result: {0}", x - y);


};

GetInfo(ops, 90);

Console.ReadLine();

static void GetInfo(MathOps ops, int k)

ops(k);

----- OR ----
8(b). Write a C# .NET program on type of operator
Ans.
1. using System;
2.
3. namespace typeofOperatorSample
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. Type tp = typeof(int);
10. Console.WriteLine($"typeof {tp}");
11. Console.WriteLine(typeof(String));
12. Console.WriteLine(typeof(Double));
13.
14. // Get type of a variable
15. string name = "Mahesh Chand";
16. Type namenameType = name.GetType();
17. Console.WriteLine(nameType);
18.
19. // Get a typeof a class
20. Console.WriteLine(typeof(Author));
21.
22. Console.ReadKey();
23. }
24. }
25.
26. public class Author
27. {
28. public Author() { }
29. }
30. }

You might also like