Dot Net Mid 2
Dot Net Mid 2
TELANGANA
DIPLOMA EXAMINATION (C-21)
SEP-24
SEMESTER V , MID-II EXAM
CCB/CS
15041
CS-574
.NET Programming Through C#
Answer Key
PART-A
1. Unstarted state
2. Runnable state
3. Running state
4. Not Runnable state
5. Dead state
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
C# Thread Properties
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
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
throw exception_object;
catch(Exception ex)
// handle exception
finally
Syntax
----- OR ----
6(b). List any three features of generics in C# .NET
Ans.
Features of Generics:
PART-C
using System;
using System.Threading;
namespace MultiThreadExample
class MThread
t1.Start();
t2.Start();
Console.ReadLine();
if(i==5)
Thread.Sleep(5000);
}
----- OR ----
7(b). Write a C# program to set and get Thread priorities.
Ans.
Example1:
using System;
using System.Threading;
class ThreadPrior
// Main Method
static public void Main()
T2.Priority = ThreadPriority.Highest;
T3.Priority = ThreadPriority.BelowNormal;
T1.Start();
T2.Start();
T3.Start();
Thread.Sleep(1000);
Output
i value: 1
i value: 1
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:
using System;
using System.Threading;
class ThreadPrior
// Main Method
T1.Priority = ThreadPriority.Lowest;
T2.Priority = ThreadPriority.Highest;
T1.Start();
T2.Start();
Console.ReadKey();
}
public static void method1()
Output
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..
using System;
namespace TutlaneExamples
class Program
// Create a delegate
int y = 10;
GetInfo(ops, 90);
Console.ReadLine();
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. }