University Questions With Answer: B.SC - It/Cs Department of Rizvi College of Arts, Science and Commerce
University Questions With Answer: B.SC - It/Cs Department of Rizvi College of Arts, Science and Commerce
A namespace is designed for providing a way to keep one set of names separate
from another.
Namespace are used to organize your programs.
The class names declared in one namespace does not conflict with the same class
names in another.
Namespace don‟t correspond to file, directory or assembly names.
They could be written in separate files and / or separate assemblies and still belong
to the same namespace.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespacefirst_space
{
classnamespace_cl
{
publicvoidfunc()
{
Console.WriteLine("Inside first_space");
}
}
}
namespacesecond_space
{
classnamespace_cl
{
publicvoidfunc()
{
Console.WriteLine("Inside second_space");
Declaration
In Jagged arrays, user has to provide the number of rows only. If the user is also going to
provide the number of columns, then this array will be no more Jagged Array.
Syntax:
Intialization : The elements of Jagged Array must be initialized before its use. You can
separately initialize each array element. There are many ways to initialize the Jagged
array‟s element.
Example
using System;
namespace Jagged_Array_ex
{
class Program
{
static void Main()
{
int[][] jagged = new int[3][];//dec
jagged[0] = new int[4];// 1st row contain 4 column
jagged[1] = new int[3];
jagged[2] = new int[5];
int i;
// Store values in first array and display the values.
for (i = 0; i < 4; i++)
{
jagged[0][i] = i;
Console.Write(jagged[0][i] + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
The CLR is the core of the .NET framework and is responsible for loading and running
C# programs. Base classes provide basic data types, collection classes and other general
classes for use by C# and other .NET languages. The top layer contains a set of classes
for developing web services and to deal with the user interface.
The Common Language Runtime
The Common Language Runtime is a runtime environment in which programs written in
C# and other .NET languages are executed. It also supports cross-language
interoperability.
The CLR provides a number of services that include:
Loading and execution of programs
Memory isolation for applications
Verification of type safety
Compilation of IL into native executable code
Providing metadata
Security R
Garbage collection
Class loader
Memory layout
Common type System (CTS) - The .NET framework provides multiple languages
support using the feature known as Common Type System that is built in to the CLR. The
CTS support variety of types and operations found in most programming languages and
therefore calling one language from another does not require type conversions.CTS
supports two categories of types witch derived from the base type called System. Object.
1. Value types directly contain the data. Actual data is always stored in stack.
2. Reference types store a reference to the memory address on the stack but the
actual value is stored o heap.
Common Language Specification (CLS) - The Common Language Specification
defines a set of rules that enables interoperability on the .NET platform. The CLS is a
C# does not support multiple inheritance. However, you can use interfaces to
implement multiple inheritance.
An interface is a collection of public instance (that is, non static) methods and
properties that are grouped together to encapsulate specific functionality. After an
interface has been defined, you can implement it in a class.
This means that the class will then support all of the properties and members
specified by the interface.
Interfaces cannot exist on their own. You can‟t „„instantiate an interface‟‟ as you
can a class. In addition, interfaces cannot contain any code that implements its
members; it just defines the members themselves.
The implementation must come from classes that implement the interface.
Interfaces are declared by using the interface keyword. Here is a simplified form of an
interface declaration:
interface name
{
ret-type method-name1(param-list);
ret-type method-name2(param-list);
// ...
using System;
interface abc
{
void xyz();
}
interface def
{
void xyz();
}
class Demo : abc, def
{
public static void Main()
{
onsole.WriteLine("Hello Interfaces");
Demo ObjDemo = new Demo();
abc objabc = ObjDemo;
objabc.xyz();
def objdef = ObjDemo;
objdef.xyz();
Console.Read();
}
void abc.xyz()
{
Console.WriteLine("In abc.xyz");
}
void def.xyz()
{
Console.WriteLine("In def.xyz");
}
Types of Constructors in C#
There are 5 types of constructor in C# as listed below
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
5. Private Constructor
Default Constructor :-
A constructor without any parameters is called as default constructor means a constructor
which does not have any input parameter is known as default constructor.
Syntax :
class User
{
// Default Constructor
User()
{
// Your Custom Code
}
}
namespace ConstructorEX
{
class C1
{
public int a, b;
class Demo
{
static void Main()
{
C1 obj = new C1(); // constuctor to initilize object
obj.display();
Console.Read();
}
}
}
class b
{
public static void Main(string[] args)
{
c1 obj1 = new c1(45, 98);
obj1.displayed();
Console.WriteLine("\n \n ");
Console.Read();
}
}
Copy Constructor :-
A constructor that contains a parameter of same class type is called as copy constructor.
C# does not provide a copy constructor. A copy constructor enables you to copy the data
stored in the member variables of an object of the class into another new object means it
helps to copy data stored in one object into another new object of the same instance.
PROGRAM :-
using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
public Sample(string x, string y)
{
param1 = x;
param2 = y;
}
public Sample(Sample obj) // Copy Constructor
{
param1 = obj.param1;
param2 = obj.param2;
}
}
class Program
{
static void Main(string[] args)
{
Sample obj = new Sample("Welcome", "abc"); // Create instance to class Sample
Static Constructor :-
A static constructor should be parameter less means it should not contain any input
parameter. Program will not execute if static constructor is having any input parameter.
Static constructor can be invoked once for any number instances are created and it is
invoked only during the first initialization of instance. It is used to initialize static fields
of the class Static constructor is created using a static keyword as shown below.
using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
static Sample()
{
Console.WriteLine("Static Constructor");
}
public Sample()
{
param1 = "Sample";
param2 = "Instance Constructor";
}
}
class Program
{
static void Main(string[] args)
{
// Here Both Static and instance constructors are invoked for first instance
Sample obj=new Sample();
Console.WriteLine(obj.param1 + " " + obj.param2);
Private Constructor :-
In c#, Private Constructor is a special instance constructor and it is used in a classes that
contains only static members. If a class contains one or more private constructors and no
public constructors, then the other classes are not allowed to create an instance for that
particular class except nested classes.
using System;
namespace ConsoleApplication3
{
public class Sample
{
public string param1, param2;
public Sample(string a,string b)
{
param1 = a;
param2 = b;
}
private Sample() // Private Constructor Declaration
{
Console.WriteLine("Private Constructor with no prameters");
}
}
class Program
{
static void Main(string[] args)
{
// Here we don't have chance to create instace for private constructor
Sample obj = new Sample("Welcome","toxyz");
Console.WriteLine(obj.param1 +" " + obj.param2);
Console.ReadLine();
Delegate:
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference
type variable that holds the reference to a method. ... Delegates are especially used for
implementing events and the call-back methods. All delegates are implicitly derived
from the System.Delegate class.
Declaring a Delegate using delegate keyword
public delegate typeofdelegate delegatename();
Example:
Multicast Delegate:
A delegate can invoke only one method,(whose reference is encapsulated into the
delegate).
It is possible for certain delegate to hold & invoke multiple methods.
Such delegates are called as Multicast delegate.
They are also known as combinable delegates.
Must satisfy the following conditions:
1. Return type of delegate must be void.
}
}
OUTPUT:
CODE: //Using Foreach Loop
using System;
classMyArray
{
staticvoid Main(string[] args)
{
int[] number = newint[10]; // number is an array of 10 integers.
inti; //integer variables are declare.
Console.WriteLine("Number{["+j+"]} = "+i);
}
}
}
OUTPUT:
Thus, every element in the array a is identified by an element name of the form
a[ i , j ], where a is the name of the array, and i and j are the subscripts that
uniquely identify each element in array a.
using System;
classMyArray
{
staticvoid Main(string[] args)
{
//an array with 5 rows and 2 columns.
Prof. Haider_Zaidi Page 21
B.Sc - IT/CS DEPARTMENT OF RIZVI COLLEGE OF
ARTS, SCIENCE AND COMMERCE
int[,] a = newint[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6},
{ 4, 8 } };
inti, j;
OUTPUT:
8.Explain any five properties / methods of Math Class. [May 2019 ATKT]
The System.Math class offers many constant fields and static methods that you can use to
do trigonometric, logarithmic, and other mathematical calculations.
Methods
METHOD DESCRIPTION
Abs() Returns the absolute value of a specified number.
Acos() Returns the angle whose cosine is the specified number.
Acosh() Returns the Inverse hyperbolic cosine of the specified number.
Asin() Returns the angle whose sine is the specified number.
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
// To find E constant values
double e = Math.E;
// Print result
Console.WriteLine("Math.E = " + e);
// To find PI constant values
}
}
}
namespace casting_example
{
class Program
{
static void Main(string[] args)
{
int a = 10;
long b = 959898989898898718;
Console.WriteLine("Before casting \n");
Console.WriteLine("Value of a = " + a);
Console.WriteLine("Value of b = " + b);
Console.WriteLine("\n");
//b = a;// Implicit Type Conversion
a = (int)b; // Explicit type conversion
Console.WriteLine("After casting \n");
Prof. Haider_Zaidi Page 25
B.Sc - IT/CS DEPARTMENT OF RIZVI COLLEGE OF
ARTS, SCIENCE AND COMMERCE
Console.WriteLine("Value of a = " + a);
Console.WriteLine("Value of b = " + b);
Console.Read();
}
}
}
10.Write short notes on Value and Reference types. [May 2019 ATKT]
Value Type:
A data type is a value type if it holds a data value within its own memory space. It means
variables of these data types directly contain their values. For example, consider integer
variable:
int i=100;
The system stores 100 in the memory space allocated for the variable 'i'.
The following image illustrates how 100 is stored at some hypothetical location in the
memory (0x239110) for 'i':
We use this process when we want to use but don't want to change the values of the
parameters passed.
using System;
namespace PassByValue
{
public class XX
{
public int sum(int a, int b)
{
Prof. Haider_Zaidi Page 27
B.Sc - IT/CS DEPARTMENT OF RIZVI COLLEGE OF
ARTS, SCIENCE AND COMMERCE
a = a + 100;
b = b + 200;
return (a + b);
}
}
class Program
{
static void Main(string[] args)
{
int a = 100, b = 200;
XX obj = new XX();
Reference Type
Unlike value types, a reference type doesn't store its value directly. Instead, it stores the
address where the value is being stored. In other words, a reference type contains a
pointer to another memory location that holds the data.
For example, consider following string variable:
String s=”Hello World!!”;
The following image shows how the system allocates the memory for the above string
variable.
As you can see in the above image, the system selects a random location in
memory (0x803200) for the variable 's'. The value of a variable is 0x600000which is the
memory address of the actual data value. Thus, reference type stores the address of the
location where the actual value is stored instead of value itself.
The following data types are of reference type:
string
All arrays, even if their
elements are value types
Class
Delegates
Passing parameters by ref uses the address of the actual parameters to the formal
parameters. It requires ref keyword in front of variables to identify in both actual and
formal parameters.
A parameter declared with a ref modifier is a reference parameter. Contrastingly to value
parameter, a reference parameter does not make a new storage location. Instead, a
reference parameter represents the same storage location as the variable given as the
argument in the method invocation. Within a method, a reference parameter is always
considered definitely assigned.
The process of ref is bidirectional i.e. we have to supply value to the formal parameters
and we get back processed value.
We use this process when we want to use or change the values of the parameters passed.
The ref keyword passes arguments by reference. It means any changes made to this
argument in the method will be reflected in that variable when control returns to the
calling method.
using System;
Prof. Haider_Zaidi Page 29
B.Sc - IT/CS DEPARTMENT OF RIZVI COLLEGE OF
ARTS, SCIENCE AND COMMERCE
namespace PassByRef
{
class XX
{
b = b + 200;
return (a + b);
}
}
class Program
{
static void Main(string[] args)
{
int a = 100, b = 200;
Output:
Sum of a and b is : 600
Value of a is : 200
Value of b is : 400
Static Members : -
A class can be static, and it can have static members, both functions and fields.
A static class can't be instantiated, so in other words, it will work more as a
grouping of related members than an actual class.
You may choose to create a non-static class instead, but let it have certain static
members.
A non-static class can still be instantiated and used like a regular class, but you
can't use a static member on an object of the class. A static class may only contain
static members.
First, here is an example of a static class:
PROGRAM :-
public static class MyStaticClass
{
public static int myStaticMyStaticClass
public static void MyStaticMethod()
{
Console.WriteLine("This is a static method.");
}
public static int MyStaticProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“MyStaticClass.myStaticVariable”);
MyStaticClass.MyStaticMethod();
MyStaticClass.MyStaticProperty = 100;
Console.WriteLine(“MyStaticClass.MyStaticProperty”);
}
}
OUTPUT :-
0
This is a static method.
100
Partial class :-
There are several situations when splitting a class definition is desirable:
When working on large projects, spreading a class over separate files enables
multiple programmers to work on it at the same time. When working with
automatically generated source, code can be added to the class without having to
recreate the source file.
Syntax :
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
The partial keyword indicates that other parts of the class, struct, or interface can be
defined in the namespace. All the parts must use the partial keyword. All the parts must
be available at compile time to form the final type. All the parts must have the same
accessibility, such as public, private, and so on.