Anda di halaman 1dari 30

Fun With

C#
Chandra Sekhar Vorugunti
This book is dedicated with Deep Love and Greatest respect to:

V.Radhika
(My Mother, The way she brought me up and providing whatever I want, sometimes greater than
her financial abilities)

Dr. Y.S Rajasekhara Reddy


(Most loved CM of Andhra Pradesh, Hero of Masses and a great warrior who doesn’t know the meaning
of Defeat. Whatever Mr. Reddy do, it’s a sensation even his death is a sensation, 600 plus people
committed suicide for not able to digest the death word of their real Hero.)

Birla Institute of Science and Technology-pilani (BITS-pilani)


(For its flexible, unique and student friendly education system, which gifted hundreds of
great Technocrats, Academicians to the world. for admitting students only on the basis of merit, BITS
is a unique institution in India in every aspect)

Dhirubhai H. Ambani (Founder Chairman, Reliance Industries Limited, India)

(For starting DA-IICT, where I did my post graduation and learnt from Asia’s best faculty with a
minimum cost which is far less than the fees collected by a normal affiliated college)

Prof .Dheeraj Sanghi (Director LNMIIT)


(For working towards improving the quality of technical education in India.Prof.Dheeraj Sanghi
Introduced several new ideas at LNMIIT in rewarding PhD students, encouraging research, industry-
Academic interaction, flexible undergraduate curriculum and admission process)
About The Author

Chandra sekhar vorugunti fondly called by his team mates as Chandra, did his
B.tech (CSE) from JNTU-HYD, Masters from DA-IICT Gandhi nagar,Gujarat.

The keen interest and the solid background in Programming languages gained
from best professors , made the author to come out with this book titled ‘Fun with
C#’.
Acknowledgement
/*some part of acknowledgement is removed for
security reasons*/
I wrote this book in conjunction with my job responsibilities. Morale boosting and flexibility
offered by my team has helped me greatly to work out a balance between job responsibility
and the responsibility to complete the book. My Sincere thanks to my entire OMATBLR team
who were very co-operative and friendly. My Special thanks to Ms.Pradnya Vaidya, my
previous Project Manager, She is always cool and patient. Pradnya is my first PM in
corporate world and I have learnt a lot from her. My Sincere thanks to Jyoti Agarwal, my
current PM. She is more than Just a PM. She encouraged and mentored me in all the
aspects of the book. Thanks to her ground to earth personality.

I must specially mention about my friends who were always supportive and encouraging,
Sai Sashikanth Indukuri, Raviteja Gokavarapu and Gattu Srinivas, with whom the
moments are always light and refreshing.

Date: 26 Jan 2010


Place: Mysore.
Introduction
Being a proud product from DA-IICT , having learnt programming from the
best professors of the world , going through the rigorous freshmen training in my
company (One among the toppers in the batch), made me think that there is a
need for a book which helps the students or professionals to gauge their skills on
some tough problems.

In my view there are two ways to learn a programming language, one is, by
reading a theoretical book and learn concepts of the language. The second one is
by applying the concepts learned. This book is written to cater the needs of
students/professionals, who prefer second way of learning. This book doesn’t teach
fundamentals of C# from the scratch, but it helps the reader to gauge his skills
and apply them meticulously which in turn provides an ample scope for learning.

We learn programming language from the mistakes and errors committed


during coding. In this book, every question is framed in such a way that the reader
needs to think twice before answering. My emphasis is always on to generate
compiler errors and allow the reader to think and learn about them. In my view,
this is the best way to learn a programming language.

Chandra can be reached for the solutions of this book at FREE OF COST
at sekhar.daiict@gmail.com.

I am requesting all the users please provide your valuable feedback at the email
mentioned above and please respect author’s Intellectual Property Rights
Topics Covered

Topic Id Topic Name Starting Page number

Chapter1 Introduction to C#
8
Chapter2 Switch and control 29

Chapter3 Classes and Objects 44

Chapter4 Arrays and Strings 55

Chapter5 More on Methods and 71


Classes
Chapter6 Operator Overloading 93

Chapter7 Indexers and Properties 102

Chapter8 Inheritance 108

Chapter9 Interfaces , Structures and 136


Enumerators
Chapter10 Exception Handling 146

Chapter11 Delegates 152

Chapter12 Lambda Expressions & 159


Partial Types
Chapter13 Anonymous Methods 168

Chapter14 Namespace & Assemblies 172

Chapter15 Preprocessor 181

Chapter16 Attributes 184

Chapter17 Generics 188

Chapter18 Enumerators 197

Chapter19 String 222

Chapter20 Object Initializers 229


Chapter1

Introduction to C#
True or false:

1. Is it a complier error when a program stored as xyz.cs, where xyz is not same as
the class which contains the main method.
2. Using System is mandatory to print a statement or variable.

3.C# doesn’t compile a class which doesn’t have a Main method.


4.C# doesn’t execute a class which doesn’t have a Main method .

5. A variable which contains a reference to another variable is called


a value type variable
6 C# supports pointer types.

7. In C# for switch statements the break statements are Optional.


8. More number of ‘{‘ , ‘}’ in program leads to increase in
execution time of a program.

Fill in the blanks:

1. ----------------Is the salient feature of C#, which distinguishes from Java?

2. The output of C# complier is --------------------


3. ------------------- plays a major role in .NET framework achieving portability
4. Constants in C# are called as ----
5. A variable of integer occupies ----- no of bits.
6. A variable whose type is determined by the type of initializing value are called-------
7. scope of a variable determines -------- and -------- of a variable
8. In an expression, promoting char, sbyte, byte, ushort, short to int
is Called -------------

Concept based:

1. What are the outputs when a C# program is compiled .


2. Identfier means:
i)Name given to a variable.
ii)Name given to a symbolic constant.
iii) Name given to a method. iV)Name
given to user-defined item. Answers:
a)only i&iii are correct.
b)only i,ii,iv are correct
c)only i,ii,iii are correct.
d) All are correct.
3.which of the following is not a valid
Identifier: i)_x
ii) __x
iii)@x
iv)12x
Answers:
a)ii,iii,iv.
b)iii,iv
c)i,iv.
d)iv.
4) What is the minimum type of result of any arithmetic operation?

Answers:
a)short
b)ushort
c)int
d)byte
5) Which of the following relational operators cannot be applied to Boolean type?

Answers:
a)==
b)!= c)>
d) All the above
6) Which of the following data types on which bitwise operators should not applied?

i)Int
ii)float
iii)double
iv)bool
Answers
a)only i
b)ii and iii
c)ii,iii and iv
d)ii and iii

Debug the following code snippets and find out the errors (if any):

1) class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("hi");
}
}

2)

class Program
{
static void Main(string[] args)
{
Console.WriteLine("hi");
}
}
3. using System;

class Program
{
static void main(string[] args)

{
Console.WriteLine("hi");
}
}

what will be the output of the following code snippets


: 1.

using System;

class Program
{
static void Main(string[] args)
{
int x;
int xQuo;
double d;

x = 100;
d = 100;
Console.WriteLine("x="+x+"d="+d);
}
}

Answers:
a)x=100 d = 100
b)x=100 d = 100.0
c)x=100.0 d=100.000000
d)x=100 d=100.00
2.
using System;

class Program
{
static void Main(string[] args)
{
int x;
double d;
x = 100; d
= 100.0;
Console.WriteLine("x="+x+"d="+d);
}
}

Answers:
a)x=100 d = 100
b)x=100 d = 100.0
c)x=100.0 d=100.000000
d)x=100 d=100.00
3.
using System;

class Program
{
static void Main(string[] args)
{
int x;
double d;
x = 100; d
= 100.0;
Console.WriteLine("x="+x/3+"d="+d/3);
}
}

Answers:
a)x=33 d = 33
b)x=33.333333 d = 33.0
c)x=33.0 d=100.0
d)x=33 d=33.3333333333333
4. using System;
class Program
{
static void Main(string[] args)
{
int x;
double d;
x = 100/10.0;
d = 100.0/10;
Console.WriteLine("x="+x+"d="+d);
}
}
Answers
a)x=10 d=10.000000
b)Compilation error
c)x=10.0 d=10
d)x=10 d=10

5. using System;

class Program
{
static void Main(string[] args)
{
int x;
double d;
x = 100/10;
d = 100.0/10;
Console.WriteLine("x="+x+"d="+d);
}
}
Answers
a)x=10 d=10.000000
b)Compilation error
c)x=10.0 d=10
d)x=10 d=10
6.

using System;

class Program
{
static void Main(string[] args)
{
float f1=0.5;
int i=0;
while(f1>=0.0)
{
i++;
f1=f1-0.1;

}
Console.WriteLine(i);
}
}

Answers:
a)5
b)6
c)Compilation error
d)4
7. using System;

class Program
{
static void Main(string[] args)
{
float f1=0.5f;
int i=0;
while(f1>=0.0)
{
i++;
f1=f1-0.1f;

}
Console.WriteLine("i=" +i);
}
}

Answers:
a)5
b)6
c)Compilation error
d)4

8.
using System;

class Program
{
static void Main(string[] args)
{

float a=1.0f;
long i;
for(i=0; i<100;
i++){ a = a - 0.01f;
}
for(i=0; i<100;
i++){ a = a + 0.01f;
}
Console.WriteLine(a);
}
}

Answers: a)5
b)1.0000003

c)Compilation error
d)1

9.
using System;

class Program
{
static void Main(string[] args)
{

float a=0.0f;
int i;
for(i=0; i<100; i++){
a = a - 0.00000001f;
}
for(i=0; i<100; i++){ a
= a + 0.000000001f;
}
Console.WriteLine(a);
}
}
Answers:
a)Infinite Loop
b)A value close to 0.0 but not 0.0
c)Compilation error
d)0.0

10.
using System;

class Program
{
static void Main(string[] args)
{

float a=10.0f;
int i;
for (i = 0; i < 100; i++)
{
a = a - 0.1f;

}
Console.WriteLine(a);

}
}

Answers:
a)0
b) 8.99996
c) -1.862645E-06
d)None

11)
using System;

class Program
{
static void Main(string[] args)
{

float b = 1.0f;
long i = 0;
while (b > 0.0)
{
b -= 0.1f;
i++;
}

Console.WriteLine(i);
}
}
Answers:
a)11
b) 1.862645E-
06 c) 9
d)10
12)

using System;

class Program
{
static void Main(string[] args)
{

float b = 1.0f;
long i = 0;
while (b > 0.0)
{
b -= 0.01f;
i++;
}

Console.WriteLine(i);

}
}

Answers:
a)11
b) 99
c) 101
d)100

13) using System;

class Program
{
static void Main(string[] args)
{

float b = 1.0f;
long i = 0;
while (b > 0.0)
{
b -= 0.0001f;
i++;
}

Console.WriteLine(i);

}
}

Answers:
a) Compilation error
b) 9999
c) 10001
d)10000

14) using System;

class Program
{
static void Main(string[] args)
{

char ch;
ch = 67;
Console.WriteLine(ch);

}
}

Answers:
a) Compilation error
b) A
c) 67
d)Run time error

15)
using System;

class Program
{
static void Main(string[] args)
{

bool b;
int i = 5;
b = 1;
if (b)
{
i++;
}
Console.WriteLine(i);
}
}

Answers:
a) Run time error
b) 6
c) 5
d) Compilation error
Chapter2

Switch and control


True or false:

1) ‘break’ program control statement is mandatory in switch statements to successfully


compile C# code.

Fill in the blanks:

1)------data type is having no restriction on its size and dynamic.

Concept based:

1)which of the following program control statement is to be used only with


loops. a)goto
b)break
c)Continue
d)return

2) which among the following are in the corect order among character Datatypes.
a)string<char<bool
b)char>bool<string
c)bool<char<string
d)none

3)which among the following are in the corect order among integer
datatypes. a)long>int<short>byte
b)int>short>byte>long
c)long>int>byte>short
d)byte<short<int<long

4) which among the following are in the corect order among


decimal datatypes.

a)decimal<double<float
b)float>decimal<double
c)decimal>double>float
d)none

Debug the following code


snippets: 1)using System;

class Program
{
static void Main(string[] args)
{
int count = 0;

for (int i = -5; i <=5;i++ ) ;


{
count++;
i = i * 2;

}
Console.Write("count=" + count);
Console.Write("i="+i);

}
}

2) If we consider following code snippet:

Line 1: int i = 10;


Line 2 :int j = 20;
Line 3 :j = j & i;
Line 4 :j = j && i;
Line 5 :i = i << 2;
Which line is errorneous:
Answers:
a) Line 1
b) Line 2
c) Line 3
d) Line 4
3) If we consider following code snippet:

using System;

class Program
{
static void Main(string[] args)
{
Line 1: float i = 10;
Line 2: int j = 20;
Line 3 : j = j & i;
Line 4 : j = j && i;
Line 5 : i = i << 2;
Line 6 : i = i >> 2;
Console.Write("i= "+i);
Console.Write("j= "+j);

}
}
Which line is errorneous and specify the reason:

Answer:
a)line 1,2,3
b)line 2,3,4,5
c)line 1,2,3,5
d)line 3,4,5,6
4)specify which of the following cast operations wont work.

using System;

class Program
{
static void Main(string[] args)
{
long l = 40;//64
int i = 30; //32
short s = 20;//16
byte b = 10;//8

decimal de = 30.0M;//128
double d = 20.0;//64 float
f = 10.0f;//32

bool bol = true;//8


char c = 'A';//16
string str;//infinite

Console.WriteLine(bol = (bool)c);
Console.WriteLine(de = (decimal)c);
Console.WriteLine(str = (string)c);
Console.WriteLine(str = (string)bol);
Console.WriteLine(b = (byte)bol);
Console.WriteLine(f = (float)bol);
Console.WriteLine(str = (string)c);
Console.WriteLine(str = (string)bol);
Console.WriteLine(str = (string)f);
Console.WriteLine(str = (string)d);
Console.WriteLine(str = (string)de);
Console.WriteLine(str = (string)b);
Console.WriteLine(str = (string)s);
Console.WriteLine(l = (long)f);
Console.WriteLine(b = (byte)f);
Console.WriteLine(f = (float)l);

}
}

5)
using System;

class Program
{
static void Main(string[] args)
{

int i = 0;
for (i = 0; i <= 5; i++)
{
switch (i)
{

case 0: Console.Write("0");
break;
case 1:
if (i == 1)
{
Console.Write("1");
}
break;
case 2: if (i == 2)
{
Console.Write("2");
}
break;
default: Console.Write("default");

}
}

}
}
What will be the output of the following Code Snippets:

1) using System;

class Program
{
static void Main(string[] args)
{

int i=0;

switch (i)
{

case 0: Console.Write("0");
case 1: Console.Write("1");
case 2: Console.Write("2");
default: Console.Write("default");
}

}}

Answer
a)0 1 2 default
b)0
c)compilation error
d)0 1 2

2)
using System;
class Program
{
static void Main(string[] args)
{

int i=5;

switch (i)
{

case 0: Console.WriteLine("0");
break;
default: Console.WriteLine("default");
break;
case 1: Console.WriteLine("1");
break;

Answer
a)default
b)0
c)compilation error
d)0 default 1

3)
using System;

class Program
{
static void Main(string[] args)
{

char ch = 'A';

switch (ch)
{

case 65 : Console.WriteLine("65");
break;
default: Console.WriteLine("default");
break;
case 'A': Console.WriteLine("A");
break;
}

Answer
a)65
b)’A’
c) 65 ‘A’ default
d) compilation error

4)
using System;

class Program
{
static void Main(string[] args)
{
int ch = 66;
switch (ch)
{

case 'C' : Console.WriteLine("C");


break;
default: Console.WriteLine("default");
break;
case 'A'+1 : Console.WriteLine("A");
break;
}
}

Answer
a)C
b)A
c)compilation error
d)default

5)
using System;

class Program
{
static void Main(string[] args)
{
int ch = 66;
switch (ch)
{
case 'C' :
default:
case 'A' + 1: Console.Write("B");
break;
}
}
}
Answer
a)C
b)compilation error
c) B
d)default

6) using System;

class Program
{
static void Main(string[] args)
{
int i = 10;
int j = 0;
switch (i)
{
case 10 :
i++;
Console.Write("i="+i);
break;

case 11:
i--;
Console.Write("i="+i);
break;

default:
break;
}
}
}
Answer
a)11
b)10
c)Infinite switch
d)default

7) For the question 77, does the compiler compiles without warnings or with warnings?
Provide the reason.
8) using System;

class Program
{
static void Main(string[] args)
{
int i = 10;
int j = 0;
switch (i)
{
case 10 :
i++;
Console.Write("i="+i);
break;

case 11:
i--;
Console.Write("i="+i);
break;

default:

}
}
}
Answer
a)11
b)10
c)Infinite switch
d)default
e) Compilation error.

9)
using System;

class Program
{
static void Main(string[] args)
{
int i = 10;
int count = 0;
for (i = 1; i <= 100; i = i % 2)
{
count++;
}
Console.Write(count);

}
}
Answer
a)10
b)1
c)Infinite
loop d)0
10)
using System;

class Program
{
static void Main(string[] args)
{
int i;
int count;
for (i = 10,count=0; i >= 100 && count>=0; i = i % 2)
{
count++;
}
Console.Write(count);

}
}
Answer
a)10
b)1
c)Infinite
loop d)0

11)
using System;

class Program
{
static void Main(string[] args)
{
int i;
int count;
for (i = 10,count=0; i <= 100 && count<=10;)
{
count++;
i = i * 2;

}
Console.Write(count);

}
}
Answer
a)0
b)4
c)Infinite
loop d)1

12)
using System;

class Program
{
static void Main(string[] args)
{
int i;
int count;
for (i = 10,count=0; i <= 100 | count<=10;)
{
count++;
i = i * 2;

}
Console.Write(i);

}
}
Answer
a)0
b)4
c)204080
d)Infinite
loop 13)
using System;

class Program
{
static void Main(string[] args)
{
int i;
int count;
for (i = -5; i <=5;i++ ) ;
{
count++;
i = i * 2;

}
Console.Write("i="+i+"count="+count);

}
}
Answer
a)i=-5 count = 1
b)Infinite loop
c)Compilation error
d)i=12 count =1

14)
using System;

class Program
{
static void Main(string[] args)
{
int i;
int count;
for (i = -5,count=0; i <=5;i++ ) ;
{
count++;
i = i * 2;

}
Console.Write("i="+i+"count="+count);
}
}
Answer
a)i=-5 count = 1
b)Infinite loop
c)Compilation error
d)i=12 count =1
15)

using System;

class Program
{
static void Main(string[] args)
{

int i= -5;
int count = 5;
for (;i<=10;i++)
{
if(i>0)
break;
--i=count--;

}
Console.Write("i=" +i);
}
}

Answer
a)i=-5 count = 1
b)Infinite loop
c)Compilation error
d)i=12 count =1

Thanks for patience reading, please provide your valuable feedback to author.

Please mailto: sekhar.daiict@gmail.com

Anda mungkin juga menyukai