Professional Documents
Culture Documents
INFO134 - SEPTEMBER - 2021 Question and Answer
INFO134 - SEPTEMBER - 2021 Question and Answer
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;}
}
(b). What is CLR? Explain the role of CLR in the .NET Framework? (5) marks.
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.
How is it evaluated?
Arithmetic expressions are evaluated in accordance with the following priority
rules:
The rules for evaluating mixed mode arithmetic expressions are simple:
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:
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.
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.
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
(b). “C# is modern, versionable and interoperable language.” Justify (12) marks.
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
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.
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;
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