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)
(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)
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.
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.
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
Chapter1 Introduction to C#
8
Chapter2 Switch and control 29
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.
Concept based:
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");
}
}
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
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
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
Concept based:
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
a)decimal<double<float
b)float>decimal<double
c)decimal>double>float
d)none
class Program
{
static void Main(string[] args)
{
int count = 0;
}
Console.Write("count=" + count);
Console.Write("i="+i);
}
}
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
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)
{
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.