File

advertisement
.Net Question papers-2013 solution
Q1)
a) Explain the building block of .NET framework with a neat diagram.
Ans: See Q1a (VTU question 2012 solution)
b) What are the characteristics of C#? Give the difference between C++, Java
and C#.
Ans: Characteristics of C# is as follows:
1) C# is a simple , modern, object oriented language derived from C++ and Java.
2) Visual studio supports Vb, VC++, C++,Vbscript, Jscript. All of these languages
provide access to the Microsft .NET platform.
3) .NET includes a Common Execution engine and a rich class library.
4) Common language run time(CLR) is equivalent to JVM.
5) CLR accommodates more than one languages such as C#, VB.NET, Jscript,
ASP.NET,C ++.
6) The classes and data types are common to all of the .NET languages.
7) We may develop Console application,Windows application,Web application
using C#.
8) In C# microsoft has taken care of C++ problems such as Memory management,
pointers etc.
9) It support garbage collection, automatic memory management and a lot.
10) It supports exception handling, operator overloading and multithreading
programming.
1
dksingh9006@gmail.com
.Net Question papers-2013 solution
Q2)
a) What are namespaces? List and explain the purpose of at least four
namespaces.
Ans:
• A namespace is a grouping of related types contained in an assembly.
• The hierarchical representation of code into namespaces is a logical function
only.
• Namespaces can be used to organize code in any way the programmer
desires.
• Namespaces avoid naming conflicts between identifiers. Namespace
directives are C# language elements that allow a program to identify
namespaces use in a program.
• They allow the namespace members to be used without specifying fully
qualified name.
Syntax: To define a namespace
namespace namespace_name
{
// code declarations
}
Predefined namespace example:
o The System.IO namespace contains file I/O related types;
o The System.Data namespace defines basic database types
// program to demonstrate namespace.
namespace first_space{
class namespace_cl{
public void func(){
Console.WriteLine("Inside first_space");
}
}
2
dksingh9006@gmail.com
.Net Question papers-2013 solution
}
namespace second_space{
class namespace_cl{
public void func(){
Console.WriteLine("Inside second_space");
}
}
}
class TestClass{
static void Main(string[] args){
first_space.namespace_cl fc = new first_space.namespace_cl();
second_space.namespace_cl sc = new second_space.namespace_cl();
fc.func();
sc.func();
Console.ReadKey();
}
}
Output:
Inside first_space
Inside second_space
3
dksingh9006@gmail.com
.Net Question papers-2013 solution
4
dksingh9006@gmail.com
.Net Question papers-2013 solution
b) Write a C# program to illustrate the usage of System.Environment class.
Ans: The System.Environment Class provides information about the current
environment and platform. The System.Environment Class uses to retrieve
Environment variable settings, Version of the common language runtime, contents
of the call stack etc. This class cannot be inherited.
Some important System.Environment property and description.
Property
System.Environment.CurrentDirectory
Description
will return your current working
directory.
System.Environment.MachineName
will return your current machine name.
System.Environment.OSVersion.ToString will return your current operating
()
system version.
System.Environment.UserName
will return your current user name.
// program to demonstrate System.Environment class.
class TestClass{
static void Main(string[] args){
Console.WriteLine(System.Environment.CurrentDirectory);
Console.WriteLine(System.Environment.UserName);
Console.WriteLine(System.Environment.OSVersion.ToString());
Console.WriteLine(System.Environment.MachineName);
Console.ReadKey();
}
}
5
dksingh9006@gmail.com
.Net Question papers-2013 solution
c) Write a note on output options options of C# compiler.
Ans: See Q1c (VTU question 2012 solution)
Q3)
a) Difference between rectangular array and Jagged array Illustrate the same
with example program.
Ans:
Rectangular array: The rectangular array in C# is such type of array that contains
more than one row to store data on it. The rectangular array is also known as multi
dimensional array in c sharp, because it has same length of each row. It can be two
dimensional array or more. It contains more than one coma (,) within single
rectangular brackets (“[ , , ,]”). To storing and accessing the elements from
rectangular array
Different ways of declaring and initializing jagged array.
Way1:
int[,] numbers = new int[3, 2] {{1,2}, {3,4}, {5,6}};
Way2:
int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
Way3:
int[,] numbers = {{1,2}, {3,4}, {5,6}};
Way4:
int[,] numbers = new int[3, 2];
numbers[0][0]=1; numbers[0][1]=2;
numbers[1][0]=3; numbers[1][1]=4;
numbers[2][0]=5; numbers[2][1]=6;
// program to demonstrate rectangular array.
6
dksingh9006@gmail.com
.Net Question papers-2013 solution
class Program{
static void Main(string[] args){
int[,] arr = new int[3, 3]; //declaring a array with 3 rows and 3 columns
for (int i = 0; i < arr.GetLength(0); i++) //accepting values in the array
{
Console.WriteLine("Enter elements for row({0})", i + 1);
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = int.Parse(Console.ReadLine());
}
}
// displaying values of rectengular array
for (int i = 0; i < arr.GetLength(0); i++){
for (int j = 0; j < arr.GetLength(1); j++){
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
7
dksingh9006@gmail.com
.Net Question papers-2013 solution
Jagged array: A jagged array is an array whose elements are arrays. The elements
of a jagged array can be of different dimensions and sizes. A jagged array is also
called an "array of arrays".
A special type of array is introduced in C#. A Jagged Array is an array of an
array in which the length of each array index can be differ.
Different ways of declaring and initializing jagged array.
Way1
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[3];
jaggedArray[1] = new int[5];
jaggedArray[0] = new int[] { 3, 5, 7, };
jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 };
Way2
int[][] jaggedArray = new int[][]{
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 }
};
Way3
int[][] jaggedArray ={
new int[] { 3, 5, 7, },
new int[] { 1, 0, 2, 4, 6 }
};
8
dksingh9006@gmail.com
.Net Question papers-2013 solution
//program to demonstrate jagged array.
class Program
{
static void Main(string[] args)
{
// Declare the array of four elements:
int[][] arr = new int[4][];
// Initialize the elements:
arr[0] = new int[2] { 7, 9 };
arr[1] = new int[4] { 12, 42, 26, 38 };
arr[2] = new int[6] { 3, 5, 7, 9, 11, 13 };
arr[3] = new int[3] { 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Elements of row({0}): ", i + 1);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0} ", arr[i][j]);
}
System.Console.WriteLine();
}
9
dksingh9006@gmail.com
.Net Question papers-2013 solution
Console.ReadLine();
}
}
b) Explain the following with example:
ii) params keyword ii) ref keyword iii) out parameter iv) boxing & unboxing.
Ans: See Q2a & Q3c (VTU question 2012 solution)
Q4)
a) What is encapsulation? What are the two ways of enforcing Encapsulation?
Give examples of both methods.
Ans: Encapsulation is the mechanism in C# that hides the unwanted C# code
within a capsule and shows only essential features of an object. Encapsulation is
used to hide its members from outside class or interface.
In C# programming, Encapsulation uses five types of modifier to
encapsulate class members. These modifiers are public, private, internal, protected
and protected internal. These all includes different types of characteristics and
makes different types of boundary of code.
b) What is an indexer? Give an example. Difference between property and
indexers.
Ans: The indexers are usually known as smart arrays in C#. Defining a C# indexer
is much like defining properties. We can say that an indexer is a member that
enables an object to be indexed in the same way as an array.
Points regarding an indexer:
1) Indexer Concept is object act as an array.
2) Indexer an object to be indexed in the same way as an array.
3) Indexer modifier can be private, public, protected or internal.
4) The return type can be any valid C# types.
10
dksingh9006@gmail.com
.Net Question papers-2013 solution
5) Indexers in C# must have at least one parameter. Else the compiler will generate
a compilation error.
//program to demonstrate an indexer.
class ParentClass{
private string[] range = new string[5];
public string this[int indexrange]
{
get
{
return range[indexrange];
}
set
{
range[indexrange] = value;
}
}
}
class childclass{
public static void Main(){
ParentClass obj = new ParentClass();
obj[0] = "ONE";
obj[1] = "TWO";
11
dksingh9006@gmail.com
.Net Question papers-2013 solution
obj[2] = "THREE";
obj[3] = "FOUR ";
obj[4] = "FIVE";
for (int i = 0; i < 5;i++ )
Console.WriteLine(obj[i]);
Console.ReadLine();
}
}
Q5)
a) Write in detail about Garbage Collection optimizations.
Ans: See Q5b (VTU question 2012 solution)
b) Explain the following with respect to Exception class.
i) TargetSite ii) StackTrace
System.Exception Property
TargetSite
StackTrace
HelpLink
Message
Source
12
iii) HelpLink iv) Message
v) Source.
Description
This read-only property returns a MethodBase type,
which describes numerous details about the method
that threw the exception (invoking ToString() will
identify the method by name).
This read-only property contains a string that
identifies the sequence of calls that triggered the
exception. As you might guess, this property is very
useful during debugging if you wish to dump the
error to an external error log.
This property returns a URL to a help file or website
describing the error in full detail.
This read-only property returns the textual
description of a given error. The error message itself
is set as a constructor parameter.
This property returns the name of the assembly that
threw the Exception.
dksingh9006@gmail.com
.Net Question papers-2013 solution
InnerException
Data
This read-only property can be used to obtain
information about the previous exception(s) that
caused the current exception to occur.
This property retrieves a collection of key/value pairs
(represented by an object implementing IDictionary)
that provides additional, programmer-defined
information about the exception. By default, this
collection is empty (e.g., null).
Q6)
a) Difference between shallow copy and deep copy of ICloneable interface
with example programs.
Ans: See Q6c (VTU question 2012 solution)
b) Explain the usage of IComparable and IComparer interface with example
program.
Ans: “Both IComparable and IComparer interface allows us to do custom sorting
in our collection. The difference is IComparable allows sorting on only one
property while IComparer on multiple properties in our class.”
System.IComparable:
System.IComparable interface specifies a behavior that allows an object to
be sorted based on some specified key.
Syntax of System.IComparable interface:
public interface IComparable{
int CompareTo(object o);
}
CompareTo()
Any number less than zero
Zero
13
Return Value Meaning in Life
This instance comes before the specified
object in the sort order.
This instance is equal to the specified
object.
dksingh9006@gmail.com
.Net Question papers-2013 solution
Any number greater than zero
This instance comes after the specified
object in the sort order.
System.Collections.IComparer:
IComparer is to provide additional comparison mechanisms, This allow us
to create several classes that will compare the same two instances in different ways
thus creating a different sort order.
Syntax System.Collections.IComparer interface:
interface IComparer{
int Compare(object o1, object o2);
}
Points regarding IComparer interface :
 IComparer compares two objects that it's given.
 It provides Compare() method which takes two arguments as an object
types.
 It is defined within the System.Collections namespace.
 It is more flexible than System.IComparable interface.
// program to demonstrate usage of IComparable and IComparer interface.
class Car : IComparable{
private int carID;
private string petName;
public int ID
{
get { return carID; }
set { carID = value; }
}
14
dksingh9006@gmail.com
.Net Question papers-2013 solution
public string Name
{
get { return petName; }
set { petName = value; }
}
public Car(string name, int id){
petName = name;
carID = id;
}
// IComparable implementation.
int IComparable.CompareTo(object obj){
Car temp = (Car)obj;
if (this.carID > temp.carID)
return 1;
if (this.carID < temp.carID)
return -1;
else
return 0;
}
}
// IComparer implementation.
public class PetNameComparer : IComparer{
public PetNameComparer() { }
15
dksingh9006@gmail.com
.Net Question papers-2013 solution
// Test the pet name of each object.
int IComparer.Compare(object o1, object o2){
Car t1 = (Car)o1;
Car t2 = (Car)o2;
return String.Compare(t1.Name, t2.Name);
}
}
class Program{
static void Main(string[] args){
// Make an array of Car types.
Car[] myAutos = new Car[5];
myAutos[0] = new Car("Rusty", 1);
myAutos[1] = new Car("Mary", 234);
myAutos[2] = new Car("Viper", 34);
myAutos[3] = new Car("Mel", 4);
myAutos[4] = new Car("Chucky", 5);
Array.Sort(myAutos);
Console.WriteLine("Ordering by Car ID:");
foreach (Car c in myAutos)
Console.WriteLine("{0}\t{1}", c.ID, c.Name);
Array.Sort(myAutos, new PetNameComparer());
Console.WriteLine("\nOrdering by pet name:");
foreach (Car c in myAutos)
16
dksingh9006@gmail.com
.Net Question papers-2013 solution
Console.WriteLine("{0}\t{1}", c.ID, c.Name);
Console.ReadKey();
}
}
Output:
Q7)
a) Explain the use of the following keywords with relevant examples:
i) Checked ii) Unchecked iii) Unsafe iv) Sizeof v) Fixed.
Ans: See Q6a (VTU question 2012 solution)
b) Explain the concept of multicasting delegates along with the members of
the System.Multicast Delegate class.
Ans: See Q7a (VTU question 2012 solution)
17
dksingh9006@gmail.com
.Net Question papers-2013 solution
Q8)
a) Describes the core benefits of using assemblies in C#.
Ans: Benefits of assemblies are as follows:
• Assemblies are designed to simplify application deployment and to solve
versioning problems that can occur with component-based applications.
• End users and developers are familiar with versioning and deployment
issues that arise from today’s component-based systems. Some end users
have experienced the frustration of installing a new application on their
computer, only to find that an existing application has suddenly stopped
working. Many developers have spent countless hours trying to keep all
necessary registry entries consistent in order to activate a COM class.
• Many deployment problems have been solved by the use of assemblies in
the .NET Framework. Because they are self-describing components that
have no dependencies on registry entries, assemblies enable zero-impact
application installation. They also simplify uninstalling and replicating
applications.
b) Explain:
i) Single file assembly ii) Multifile assembly iii) Private assembly
iv) Shared assembly.
i) Single file assembly:
• Single-file assemblies contain all the necessary CIL, metadata, and
associated manifest in an autonomous, single, well-defined package.
• In a great number of cases, there is a simple one-to-one correspondence
between a .NET assembly and the binary file (*.dll or *.exe).
• Thus, if you are building a .NET *.dll, it is safe to consider that the binary
and the assembly are one and the same. Likewise, if you are building an
18
dksingh9006@gmail.com
.Net Question papers-2013 solution
executable desktop application, the *.exe can simply be referred to as the
assembly itself.
ii) Multifile assembly:
Ans: See Q7a (VTU question 2012 solution)
iii) Private assembly:
• Private assembly can be used by only one application.
• Private assembly will be stored in the specific application's directory or subdirectory.
• There is no other name for private assembly.
• Strong name is not required for private assembly.
• Private assembly doesn't have any version constraint.
iv) Shared assembly:
•
•
•
•
•
Public assembly can be used by multiple applications.
Public assembly is stored in GAC (Global Assembly Cache).
Public assembly is also termed as shared assembly.
Strong name has to be created for public assembly.
Public assembly should strictly enforce version constraint.
Thank You…
All the best!!!
19
dksingh9006@gmail.com
Download