Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Section A

1. (a) C# is a type-safe language. Comment (15) marks.

C# fulfills the need for a language that is easy to write, read and maintain and also provides
the power and flexibility. C# is for the most part a type safe language because it prevents
values of one static type from being stored in variables of an incompatible type (and other
similar type errors). Type-safety promotes robust programs. C# incorporates a number of
type-safe measures. a. All dynamically allocated objects and arrays are initialized to zero
b. Use of any uninitialized variables produces an error message by the
compiler
c. Access to arrays are range-checked and warned if it goes out-of-bounds
d. C# does not permit unsafe casts
e. C# enforces overflow checking in arithmetic operations
f. Reference parameters that are passed are type-safe
g. C# supports automatic garbage collection

Example: Type safe in C# wouldn’t allow an object to sneak into other object’s
memory. Let us see an example to understand the concept of −

Example
public class One {
   public int Prop{ get; set;}
}

public class Two {


   public int Prop{get;set;}
   public int Prop1{get;set;}
}
Let’s say I have an object Class One −
One ob = new One();
Now you won’t be able to cast your object ob to the second class i.e. class Two. If
you will cast it then a compile time error will arise because of the Type Safe feature
in C#.

(b). What is CLR? Explain the role of CLR in the .NET Framework? (5) marks.

CLR is the heart of .NET Framework is a run-time environment.


Roles of CLR in the .NET Framework
The common language runtime manages memory, thread execution, code
execution, code safety verification, compilation, and other system services.
Garbage Collection :- CLR automatically manages memory thus eliminating memory
leakes. When objects are not referred GC automatically releases those memory thus
providing efficient memory management.
Code Access Security :- CAS grants rights to program depending on the security
configuration of the machine.Example the program has rights to edit or create a new
file but the security configuration of machine does not allow the program to delete a
file.CAS will take care that the code runs under the environment of machines
security configuration.

 Code Verification :- This ensures proper code execution and type safety while the
code runs.It prevents the source code to perform illegal operation such as accessing
invalid memory locations etc.

 IL( Intermediate language )-to-native translators and optimizer's :- CLR uses JIT
and compiles the IL code to machine code and then executes. CLR also determines
depending on platform what is optimized way of running the IL code.
Base Class Libraries: It provides class libraries supports to an application when
needed.
MSIL Code to Native Code: The Common Language Runtime is the engine that
compiles the source code in to an intermediate language. This intermediate
language is called the Microsoft Intermediate Language.

During the execution of the program this MSIL is converted to the native code or
the machine code. This conversion is possible through the Just-In-Time compiler.
During compilation the end result is a Portable Executable file (PE).

Thread Support: Threads are managed under the Common Language Runtime.
Threading means parallel code execution. Threads are basically light weight
processes responsible for multi-tasking within a single application.

COM Marshaler: It allows the communication between the application and COM
objects.

Code Manager: CLR manages code. When we compile a .NET application you
don't generate code that can actually execute on your machine. You actually
generate Microsoft Intermediate Language (MSIL or just IL). All .NET code is IL
code. IL code is also called Managed Code, because the .NET Common Language
Runtime manages it.

Debug Engine: CLR allows us to perform debugging an application during


runtime.

(c). What is mixed mode arithmetic expression? How is it evaluated? (5)


In a statement or expression if one the operand is real (float) and another one is integer
then expression.

How is it evaluated?
Arithmetic expressions are evaluated in accordance with the following priority
rules:

 All exponentiations are performed first; consecutive exponentiations are


performed from right to left.
 All multiplication and divisions are performed next, in the order in which
they appear from left to right.
 The additions and subtractions are performed last, in the order in which they
appear from left to right.

Typical precedence levels


– parentheses
– unary operators
– ** (if the language supports it)
– *, /
– +,-

The rules for evaluating mixed mode arithmetic expressions are simple:

 Use the rules for evaluating single mode arithmetic expressions for


scanning.
 After locating an operator for evaluation, do the following:
o if the operands of this operator are of the same type, compute the
result of this operator.
o otherwise, one of the operand is an integer while the other is a real
number. In this case, convert the integer to a real (i.e., adding .0 at the
end of the integer operand) and compute the result. Note that since
both operands are real numbers, the result is a real number.
Simple Examples:

 1 + 2.5 is 3.5
 1/2.0 is 0.5
 2.0/8 is 0.25
 -3**2.0 is -9.0
 4.0**(1/2) is first converted to 4.0**0 since 1/2 is a single mode expression
whose result is 0. Then, 4.0**0 is 1.0

Section B
2. (a). Explain the for loop and while loop with proper syntax and examples. (8) marks

C# for loop
The for keyword is used to create for loop in C#. The syntax for for loop is:

for (initialization; condition; iterator)

// body of for loop

How for loop works?


1. C# for loop has three statements:  initialization ,  condition  and  iterator .
2. The  initialization  statement is executed at first and only once. Here, the
variable is usually declared and initialized.
3. Then, the  condition  is evaluated. The  condition  is a boolean expression, i.e.
it returns either  true  or  false .
4. If the  condition  is evaluated to  true :
a. The statements inside the for loop are executed.

b. Then, the  iterator  statement is executed which usually changes the value
of the initialized variable.
c. Again the  condition  is evaluated.
d. The process continues until the  condition  is evaluated to  false .
5. If the  condition  is evaluated to  false , the for loop terminates.

C# while and do...while loop


In this article, we will learn about while and do...while loop in C#, how to
use them and difference between them.

In programming, it is often desired to execute certain block of statements


for a specified number of times. A possible solution will be to type those
statements for the required number of times. However, the number of
repetition may not be known in advance (during compile time) or maybe
large enough (say 10000).

The best solution to such problem is loop. Loops are used in programming
to repeatedly execute a certain block of statements until some condition is
met.

In this article, we'll learn to use while loops in C#.

C# while loop
The while keyword is used to create while loop in C#. The syntax for while
loop is:

while (test-expression)

// body of while

How while loop works?


1. C# while loop consists of a  test-expression .
2. If the  test-expression  is evaluated to  true ,
a. statements inside the while loop are executed.

b. after execution, the  test-expression  is evaluated again.


3. If the  test-expression  is evaluated to  false , the while loop terminates.

(b). “C# is modern, versionable and interoperable language.” Justify (12) marks.

1. Modern - C# is called a modern language due to a number of features it supports; namely;


Automatic garbage collection; Rich intrinsic model for error handling; Decimal data type for
financial applications; and It supports Modern approach to debugging and Robust security
model

2. Versionable - Making new versions of software modules work with the existing applications
is known as versioning, C# provides support for versioning with the help of new and override
keywords. With this support, a programmer can guarantee that his new class library will
maintain binary compatibility with the existing client applications.

(c). When do we use the break and continue statements inside a while loop. (5) marks

Break Statement in while loop C#


Break forces a loop to exit immediately.

If we want to terminate while loop in between of iteration or before condition


reaches to false then use break statement.
In order to use break statement in while loop we have to use "break" keyword.

Example of Break Statement

int num = 10;


while (num > 0) {
Console.WriteLine("Current value of num is {0}", num);
if (num == 4){
Console.WriteLine("Breaking at {0}", num);
break;
}
num--;
}

Continue Statement in while loop C#


A continue statement within body of while loop will stops the execution of the
code and continues to re start it again.

If we want to re-evaluate the code in a loop we use continue statement in a loop.


In order to use continue statement in while loop we have to use "continue"
keyword.

3.
4. Explain simple if and nested if conditional statements with suitable examples.
(8) marks

simple if:
The if/else statement extends the if statement by specifying an action if the if (true/false expression) is
false.
if (condition)
{
// do this if condition is true
// if true statements
}
else
{
// do this is condition is false
// if false statements
}

With the if statement, a program will execute the true code block or do nothing. With the if/else statement,
the program will execute either the true code block or the false code block so something is always executed
with an if/else statement.

nested if conditional:
A nested if statement is an if statement placed inside another if statement. Nested if statements are often
used when you must test a combination of conditions before deciding on the proper action.

(b). Write short notes on exception handling in C#? (6) marks.

Exception Handling in C# is a process to handle runtime errors. The exception will cause the
abnormal termination of the program execution. So these errors (exceptions) are very
dangerous because whenever the exception occurs in the programs, the program gets
terminated abnormally on the same line where the error gets occurred without executing
the next line of code.

(C). Write a program to implement the use of checked and unchecked operators in try… catch block.
(11) marks.
using System;
namespace CheckedUncheckedDemo
{
class Program
{
static void Main(string[] args)
{
const int a = 2147483647;
const int b= 2147483647;

int c = unchecked(a + b);

Console.WriteLine(c);
Console.ReadLine();
}
}
}
Now, when you compile this code you will see that the compiler doesn’t throw any error
like below diagram.
https://dotnettutorials.net/wp-content/uploads/2020/05/word-image-54.png

You might also like