Professional Documents
Culture Documents
What is boxing?
Converting a value type to a reference type object is called boxing. A value type is stored on stack
memory and requires conversion—boxing—to an object on the heap memory before it can be treated as
an object. The members of the new object can be invoked on the value, e.g., converting a double to a
string. Boxing may be performed implicitly at runtime by the CLR.
Ex:
int m = 10;
object om = m;
m = 20;
Console.WirteLine (m); // m = 20
Console.WriteLine (om); //om = 10
What is Unboxing?
Conversion of a reference typed object to the associated value type instance. Usually, unboxing is
performed explicitly by a cast operation.
Ex:
int m = 10;
object om = m;
int n = (int) om;
What is .NET?
The .NET Framework comprises:
• Common Language Runtime (CLR);
• .NET Framework Class Library;
• .NET languages including C#;
• Visual Studio.NET IDE.
.NET supports multiple languages making it suitable for cross-platform applications. All .NET
languages must comply with the Common Language Specification (CLS)—an agreement encompassing the
specifications for .NET languages. Microsoft offers four .NET languages—Visual Basic .NET, Visual C#,
Visual C++ with managed extensions, and Jscript.
The CLR comprises an execution engine, a garbage collector, a just-in-time (JIT) compiler, a security
system, and the .NET Framework fundamentals.
What are the tools provided by .NET framework for managing user and application interfaces?
• Windows forms
• Web forms
• Console Applications
• Web services
f) Mercury
g) Scheme
What are important features of C#, which are adopted from other languages?
C# borrows Java’s features such as grouping of classes, interfaces and implementation together in one file
so that programmers can edit the code more easily. C# also handles objects using references, the same
way as Java.
C# borrows VB’s approach to form design, namely, dragging controls from a tool box, dropping them onto
forms, and writing event handlers for them.
WHAT IS C#?
C# (pronounced as 'c’ sharp') is a new computer-programming language developed by Microsoft
Corporation, USA.
C# is a fully object-oriented language like Java and is the first Component-oriented language. It
has been designed to support the key features of .NET Framework, the new development
platform of Microsoft for building component-based software solutions.
It is a simple, efficient, productive and type-safe language derived from the popular C and C++
languages. Although it belongs to the family of C / C++, it is a purely objected-oriented, modem
language suitable for developing Web based applications.
System.Console.WriteLine
(
“Hello ECE”
)
;
What is NaN?
Floating-point data types support a special value known as Not‐a‐Number (NaN). NaN is used to
represent the result of operations such as dividing zero by zero, where an actual number is number is not
produced. Most operations that have NaN as an operand will produce NaN as a result.
b) Relational operators
c) Logical operators
d) Assignment operators
e) Increment and decrement operators
f) Conditional operators
g) Bitwise operators
h) Special operators
What is method modifier? List out all method modifiers available in C#?
The modifiers decide the nature of accessibility and the mode of application of the method.
Modifier Description
new The method hides an inherited method with the same signature.
public The method can be accessed from anywhere, including outside the class
protected The method can be accessed from within the class to which it belongs, or a type
derived from that class.
internal The method can be accessed from within the same program.
private The method can only be accessed from inside the class to which it belongs.
static The method does not operate on a specific instance of the class.
virtual The method can be overridden by a derived class.
abstract A virtual method which defines the signature of the method, but doesn’t provide an
implementation.
override The method overrides an inherited virtual or abstract method.
sealed The method overrides an inherited virtual method, but cannot be overridden by any
classes which inherit from this class. Must be used in conjunction with override. extern
The method is implemented externally, in a different language.
Ex:
using System;
class Output
{
static void addition (int a , int b, out int result)
{
result = a + b;
}
public static void Main()
{
int x = 5, y = 8, sum;
addition(x, y, out sum);
Console.WriteLine(“The sum of {0} and {1} is {2}”, x, y, sum);
}
}
Write a short note on parameter arrays (OR) params keyword (OR) variable argument list
In C#, the methods can be defined to handle variable number of arguments using what are known as
parameter arrays. Parameter arrays are declared using the keyword params. This can be combined with
the formal parameter list and in such cases, it must be the last parameter.
It is permitted to use parameter arrays along with the value parameters, but it is not allowed to combine
the params modifier with the ref and out modifiers.
Ex:
using System;
class ParamsTest
{
static int sum(params int[] val)
{
int tot=0;
foreach (int i in val)
tot = tot + i;
return tot;
}
public static void Main()
{
Console.WriteLine(“The sum of 40,50,60 is {0}”, sum(40,50,60));
Console.WriteLine(“The sum of 2,3,12,15,17 is {0}”, sum(2,3,12,15,17));
Console.WriteLine(“The sum of 12 is {0}”, sum(12));
}
}
Output:
The sum of 40,50,60 is 150
The sum of 2,3,12,15,17 is 49
The sum of 12 is 12
Can a method return more than one value in C#? Justify your answer.
Any method can return only one value if the return type is other than void. But in C#, it is possible to
return more than one value from the program using out parameter. For example,
using System;
class ReturnTest
{
static int test(int a, out int b)
{
b = a + a;
return ++a;
}
public static void Main()
{
int x = 10, y;
Console.WriteLine(“The value of x is {0}”, test(x,out y));
Console.WriteLine(“The value of y is {0}”, y);
}
}
Output:
The value of x is 11
The value of y is 20
UNIT II
What is a class?
A class is essentially a description of how to construct an object that contains fields and methods. It
provides a sort of template for an object and behaves like a basic data type such as int. Classes provide a
convenient approach for packing together a group of logically related data items and functions that work
on them.
What is inheritance?
Inheritance is the concept used to build new classes using the existing class definitions. Through
inheritance a class can be modified easily. The original class is known as base or parent class and the
modified one is known as derived class or subclass or child class.
The concept of inheritance facilitates the reusuability of existing code and thus improves the integrity of
programs and productivity of programmers.
What is polymorphism?
Polymorphism is the ability to take more than one form. The behavior of the method depends upon the
types of data used in the operation. This is extensively used while implementing inheritance.
Advantages of Inheritance
1. Reuse the existing code and extend the functionality.
2. Add new members to the derived class to specialize the class.
3. replace the implementation of existing methods by overriding a method that already exists in the
base class. use of virtual and override methods help to exhibit polymorphic behavior.
4. Organize software components into categories and subcategories resulting in classification of
software. Classification is the most widely accepted use of inheritance although other
mechanisms may also be used for classification.
passing an explicit object reference as a parameter to the static method, non-static members
may be accessed in it.
A static constructor is used to initialize static variables. It is invoked only once, no matter how
many objects were created.
What is a constructor?
A constructor is a special method declared in a class to initialize the fields at the time of the creation of an
instance (object) of a class. it is a special method because the name of the method is same as the class.
What is destructor?
A destructor is opposite to a constructor. It is a method called when an object is no more required. The
name of the destructor is the same as the class name and is preceded by a tilde (~). Like constructors, a
destructor has no return type.
What is enumeration?
An enumeration (enum) is a value type. It is a special integer data type. The declaration of this data type
defines a type name for a related group of symbolic constants. This data type associates an integral
datatype other than char.
Syntax:
enum identifier[: dataType]
{
enumerator1, enumerator2, … enumeratorN
}
Ex:
enum Vehicle : byte
{
car, bus, van, bike
}
class ThisTest
{
int x;
int y;
public ThisTest(int x, int y)
{
this.x = x;
this.y = y;
}
…..
}
What are accessor and mutator methods? Why we need these methods?
The accessor methods are used to access the data members. And the mutator methods are used to
modify the value of data members.
The object-oriented system will not permit to access the private members directly because of the
implication integrity. The accessor and mutator methods are used for this purpose. They are also called as
getset methods.
Ex:
class TestAccess
{ private int n;
public void SetNumber(int x) //mutator method
{
n = x;
}
public int GetNumber() // accessor method
{
return n;
}
}
Ex:
class TestProp
{
private int n;
public int number //property defines getter and setter methods
{
get
{
return n;
}
set
{
number = value;
}
}
}
It is an error for an indexer to declare a local variable with the same name as an indexer
parameter.
The indexer takes an index argument and looks like array.
The indexer is declared using the name this.
What are the constraints on the accessibility of members and classes in C#?
1. The direct base class of a derived class must be at least as accessible as the derived class itself.
2. Accessibility domain of a member is never larger that that of the class containing it.
3. The return type of method must be at least as accessible as the method itself.
Now, the compiler gets confused in selecting the method either from interface I1 or interface I2. But by
using explicit interface implementation this can be avoided.
Ex:
interface I1{ int sum(int x, int y); }
interface I2 {int sum (int a, int b); }
class C1 : I1, I2
{
//explicit interface implementation with no access modifier
int I1.sum (int m, int n)
{
return (m+n);
}
}
Access modifiers are prohibited on explicit interface implementations.
Can we convert an interface into an equivalent abstract class? Explain with example.
The interfaces are similar to abstract classes. And interfaces can be converted into an abstract class.
Ex:
interface Sample
{
void Print();
int sum(int x, int y);
}
This can be converted into an abstract class as follows:
abstract class Sample
{
abstract public void Print();
abstract public int sum(int x, int y);
}
By default all the members of an interface are public and abstract.
Define Delegate?
It is an Event handling mechanism of .NET. To raise events, a class must define one delegate per
event type. To handle events, types must implement one event handler per event type. Delegates can
reference both instance and static methods. C# uses the delegate keyword.
What is an event?
An event is a delegate type class member that is used by the object or class to provide a notification to
other objects that an event has occurred. The client object can act on an event by adding an event
handler to an event.
The type of an event declaration must be a delegate type and the delegate must be as accessible as the
event itself.
What is an error?
Error is a mistake that can make a program go wrong. An error may produce an incorrect output or may
terminate the execution of the program abruptly or even may cause the system to crash. There are two
types of error:
1. Compiler-time errors 2. Run-time errors
What is Exception?
When an unplanned or unexpected event occurs, an associated exception object is thrown. The
exception will be caught by an exception handler at some level and appropriate action taken. A fatal
exception—catastrophic error—is an event that cannot be properly handled to allow the application to
continue.
What is Override?
Supercede an instance field or virtual method in a base class with a new definition of that
member in the derived class (subclass).