C# MCQ Set 1
1. Choose the correct statements about enum used in C#.NET?
a) An enum variable cannot have a private access modifier
b) An enum variable can be defined inside a class or a namespace
c) An enum variable cannot have a protected access modifier
d) An enum variable cannot have a public access modifier
Answer
Answer: c [Reason:] None.
2. Which among the following cannot be used as a datatype for an enum in C#.NET?
a) short
b) double
c) int
d) all of the mentioned
Answer
Answer: b [Reason:] None.
3. Choose the correct output for the C#.NET code given below?
-
enum days:int
-
{
-
sunday = -3,
-
monday,
-
tuesday
-
}
-
Console.WriteLine((int)days.sunday);
-
Console.WriteLine((int)days.monday);
-
Console.WriteLine((int)days.tuesday);
a) -3 0 1
b) 0 1 2
c) -3 -2 -1
d) sunday monday tuesday
Answer
Answer: c [Reason:] None.
4. Choose the correct statement about the C#.NET code given below?
-
enum color:byte
-
{
-
yellow = 500,
-
green = 1000,
-
pink = 1300
-
}
a) byte value cannot be assigned to enum elements
b) enum elements should always take successive values
c) enum must always be of int type
d) When the valid range of byte exceeds, the compiler will report an error
Answer
Answer: d [Reason:] None.
5. Wrong statement about enum used in C#.NET is?
a) An enum can be declared inside a class
b) An object cannot be assigned to an enum variable
c) An enum can be declared outside a class
d) An enum can have Single and Double values
Answer
Answer: d [Reason:] None.
6. Choose the correct output for given set of code?
-
enum per
-
{
-
a,
-
b,
-
c,
-
d,
-
}
-
per.a = 10;
-
Console.writeline(per.b);
a) 11
b) 1
c) 2
d) compile time error
Answer
Answer: d [Reason:] It will report an error since enum element cannot be assigned a value outside the enum declaration.
7. Choose the correct output for given set of code?
-
enum color:int
-
{
-
red,
-
green,
-
blue = 5,
-
cyan,
-
pink = 10,
-
brown
-
}
-
console.writeline((int)color.green);
-
console.writeline((int)color.brown);
a) 2 10
b) 2 11
c) 1 11
d) 1 5
Answer
Answer: c [Reason:] None.
Output: 1 11
8. Correct the output for the C#.NET code given below?
-
enum letters
-
{
-
a,
-
b,
-
c
-
}
-
letters l;
-
l = letters.a;
-
Console.writeline(l);
a) -1
b) 0
c) a
d) letters.a
Answer
Answer: c [Reason:] None.
Output: a
9. Correct output for the C#.NET code given below is?
-
enum colors
-
{
-
red,
-
black,
-
pink
-
}
-
colors s = colors.black;
-
type t;
-
t = c.GetType();
-
string[] str;
-
str = Enum.GetNames(t);
-
Console.WriteLine(str[0]);
a) 0
b) black
c) red
d) 1
Answer
Answer: c [Reason:] None.
Output: red
10. Choose the correct statement about enum used in C#.NET ?
a) By default the first enumerator has a value equal to the number of elements present in the list
b) Values of the enum elements cannot be populated from database
c) The value of each successive enumerator is decreased by 1
d) An enumerator has a white space in its name
Answer
Answer: b [Reason:] None.
11. Which among the following differentiates enum in C#.NET from enum in C language?
a) C is stricly a typed language, C#.NET also is a strictly typed language
b) In C, language variables of enum types can be used interchangeably with integers using type casts while enum variables cannot be used as a normal integers in C#.NET
c) None of the mentioned
d) All of the mentioned
Answer
Answer: b [Reason:] None.
C# MCQ Set 2
1. Which among the following is NOT an exception?
a) Stack Overflow
b) Arithmetic Overflow or underflow
c) Incorrect Arithmetic Expression
d) All of the mentioned
Answer
Answer: c [Reason:] None.
2. Which among the following is considered as .NET Exception class?
a) Exception
b) StackUnderflow Exception
c) File Found Exception
d) All of the mentioned
Answer
Answer: b, c [Reason:] None.
3. Which of the following is the object oriented way to handle run time errors?
a) Error codes
b) HERRESULT
c) OnError
d) Exceptions
Answer
Answer: d [Reason:] None.
4. Select the statements which describe the correct usage of exception handling over conventional error handling approaches?
a) As errors can be ignored but exceptions cannot be ignored
b) Exception handling allows separation of program’s logic from error handling logic making software more reliable and maintainable
c) try – catch – finally structure allows guaranteed cleanup in event of errors under all circumstances
d) All of the mentioned
Answer
Answer: d [Reason:] None.
5. Select the correct statement about an Exception?
a) It occurs during loading of program
b) It occurs during Just-In-Time compilation
c) It occurs at run time
d) All of the mentioned
Answer
Answer: c [Reason:]None.
6. Which of these keywords is not a part of exception handling?
a) try
b) finally
c) thrown
d) catch
Answer
Answer: c [Reason:]Exception handling is managed via 5 keywords – try, catch, throws, throw and finally.
7. Which of these keywords must be used to monitor exceptions?
a) try
b) finally
c) throw
d) catch
Answer
Answer: a [Reason:]None.
8. Which of these keywords is used to manually throw an exception?
a) try
b) finally
c) throw
d) catch
Answer
Answer: c [Reason:]None.
9. Choose the correct output for the given set of code:
-
class program
-
{
-
static void main(string[] args)
-
{
-
int i = 5;
-
int v = 40;
-
int[] p = new int[4];
-
try
-
{
-
p[i] = v;
-
}
-
catch(IndexOutOfRangeException e)
-
{
-
Console.WriteLine("Index out of bounds");
-
}
-
Console.WriteLine("Remaining program");
-
}
-
}
a) value 40 will be assigned to a[5];
b) The output will be :
Index out of bounds
Remaining program
c) The output will be :
Remaining program
d) None of the mentioned
Answer
Answer: b [Reason:]None.
10. Choose the correct output for the given set of code:
-
static void Main(string[] args)
-
{
-
try
-
{
-
Console.WriteLine("csharp" + " " + 1/Convert.ToInt32(0));
-
}
-
catch(ArithmeticException e)
-
{
-
Console.WriteLine("Java");
-
}
-
Console.ReadLine();
-
}
a) csharp
b) java
c) run time error
d) csharp 0
Answer
Answer: b [Reason:] 1 / 0, hence system out of flow exception error.
11. Which of the following is the wrong statement about exception handling in C#.NET?
a) finally clause is used to perform cleanup operations of closing network and database connections
b) a program can contain multiple finally clauses
c) the statement in final clause will get executed no matter whether an exception occurs or not
d) all of the mentioned
Answer
Answer: b [Reason:] None.
12. Choose the correct output for given set of code:
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
try
-
{
-
Console.WriteLine("csharp" + " " + 1/0);
-
}
-
finally
-
{
-
Console.WriteLine("Java");
-
}
-
Console.ReadLine();
-
}
-
}
a) csharp 0
b) Run time Exception generation
c) Compile time error
d) Java
Answer
Answer: b [Reason:] Run time Error of division by zero.
C# MCQ Set 3
1. Select a convenient declaration and initialization of a floating point number:
a) float somevariable = 12.502D
b) float somevariable = (Double) 12.502D
c) float somevariable = (float) 12.502D
d) float somevariable = (Decimal)12.502D
Answer
Answer: c [Reason:] We cannot implicitly convert a “double” number directly to any other datatype.Here, its float we have to add the required datatype to number as :
float somevariable = (float)12.502D;
or
Double somevariable = (Double)12.502D;
2. Number of digits upto which precision value of float datatype is valid ?
a) Upto 6 digit
b) Upto 8 digit
c) Upto 9 digit
d) Upto 7 digit
Answer
Answer: d [Reason:] By definition.
3. Valid Size of float datatype is ?
a) 10 Bytes
b) 6 Bytes
c) 4 Bytes
d) 8 Bytes
Answer
Answer: c [Reason:] By definition.
4. Correct way to define a value 6.28 in a variable ‘pi’ where value cannot be modified ?
a) #define pi 6.28F
b) pi = 6.28F
c) const float pi = 6.28F
d) const float pi
pi = 6.28F
Answer
Answer: c [Reason:] Const is a reserve keyword whenever they are declared with any variables.The value stored in that variable always remain fixed. Any modification done to change value of that variable results in error.Hence, options a, b and d are rejected because value is not declared fixed. Now, for value ‘c’ and ‘d’ only option ‘c’ is correct because for value ‘d’ while declaring a constant variable we need to provide a constant value too as provided in ‘c’. So, option ‘c’ is correct way of declaration of constant variable.
5. Select correct set of code to display the value of given variable ‘c’ as ‘25.302’.
-
a) float a = (double) 12.502f;
-
float b = 12.80f;
-
float c;
-
c = (float) a + b;
-
Console.Writeline(c);
-
Console.ReadLine();
-
b) float a = 12.502D;
-
float b = 12.80f;
-
float c;
-
c = a + b;
-
Console.WriteLine(c);
-
Console.ReadLine();
-
c) double a = 12.502;
-
float b = 12.802f;
-
float c;
-
c = (float)a + b;
-
Console.WriteLine(c);
-
Console.ReadLine();
-
d) double a = (float) 12.502f;
-
float b = 12.80f;
-
float c;
-
c = a + b;
-
Console.WriteLine(c);
-
Console.ReadLine();
Answer
Answer: c [Reason:] Expression c = (float) a + b.
6. Minimum and Maximum range of values supported by ‘float’ data type are ?
a) 1.5 * 10-40 to 3.4 * 1038
b) 1.5 * 10-45 to 3.4 * 1030
c) 1.5 * 10-45 to 3.4 * 1038
d) 1.5 * 10-45 to 3.4 * 1037
Answer
Answer: c [Reason:] By Definition.
7. Select appropriate difference between decimal, float and double data type in C# ?
1) Float and Double are floating binary point types while decimal is a floating decimal point type.
2) Precision difference for float is ‘7’ digit for double is ’15’ to ’16’ digit and for decimal is ’28’ to ’29’ digits.
3) Some values which cannot be exactly represented hence for those values float and double are more appropriate.
a) 1
b) 1, 3
c) 1, 2, 3
d) 2, 3
Answer
Answer: C [Reason:] By Definition.
8. Why does a float variable stop incrementing at number ‘16777216’ in given code in C#?
-
float a = 0 ;
-
while (true)
-
{
-
a++;
-
if (a > 16777216)
-
break;
-
}
a) Sign and Exponent for ‘16777217’ is same as for ‘16777216’
b) Mantissa is different for ‘16777216’ and ‘16777217’
c) Sign and Exponent for ‘16777217’ is different from ‘16777216’
d) None of the mentioned
Answer
Answer: b [Reason:] 16777216 is exactly 224, and would be represented as 32-bit float like so:
sign = 0 (positive number)
exponent = 24 (stored as 24 + 127 = 151 = 10010111)
mantissa = . 0
As 32 bits floating-point representation: 0 10010111 00000000000000000000000
Therefore: Value = (+ 1) * 2 ^ 24 * (1. 0 + . 0) = 2 ^ 24 = 16777216
Now let’s look at the number 16777217, or exactly 224 + 1:
sign and exponent are the same.
Mantissa should have to be exactly 2-24 so that (+ 1) * 2 ^ 24 * (1. 0 + 2 ^-24) = 2 ^ 24 + 1 = 16777217 and here lies the actual problem . The mantissa cannot have the value 2-24 because it only has 23 bits, so the number 16777217 just cannot be represented with the accuracy of 32-bit floating points numbers.
9. Select the output of the following set of code ?
-
static void Main(string[] args)
-
{
-
int x = 1;
-
float y = 2. 4f;
-
short z = 1;
-
Console. WriteLine((float) x + y * z - (x + = (short) y) );
-
Console. ReadLine();
-
}
a) 0.4000004
b) 0.4000023
c) 0.0400021
d) 0.4000001
Answer
Answer: d [Reason:] None.
Output : 0.4000001
10. A float occupies 4 bytes. If the hexadecimal equivalent of these 4 bytes are A, B, C and D, then when this float is stored in memory in which of the following order do these bytes gets stored ?
a) ABCD
b) DCBA
c) 0 * ABCD
d) Depends on big endian or little endian architecture
Answer
Answer: d [Reason:] “Little Endian” means that the lower-order byte of the number is stored in memory at the lowest address, and the high order byte at the highest address. For example, a 4 byte Integer
ABCD will be arranged in memory as follows:
Base Address + 0 Byte 0 .
Base Address + 1 Byte 1 .
Base Address + 2 Byte 2 .
Base Address + 3 Byte 3 .
Intel processors (those used in PC’s) use “Little Endian” byte order.
“Big Endian” means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. The same 4 byte integer would be stored as:
Base Address + 0 Byte 3 .
Base Address + 1 Byte 2 .
Base Address + 2 Byte 1 .
Base Address + 3 Byte 0 .
11. The Default value of Boolean DataType is ?
a) 0
b) True
c) False
d) 1
Answer
Answer: c [Reason:] By Definition.
12. Select the output choice for the following set of code:
-
public static void Main(string[] args)
-
{
-
double ZERO = 0;
-
Console.WriteLine("RESULT OF DIVISION BY ZERO IS :{0}", (0 / ZERO));
-
Console.ReadLine();
-
}
a) None of the mentioned
b) exception arguement is thrown
c) NaN
d) 0
Answer
Answer: c [Reason:] None.
13. Which of the following format specifiers is used to print hexadecimal values and return value of output as Octal equivalent in C# ?
a) %hx for small case letters and %HX for capital letters
b) %x for small case letters and %X for capital letters
c) No ease of doing it. C# don’t provides specifier like %x or %O to be used with ReadLine() OR WriteLine().We have to write our
own function
d) %Ox for smallcase letters and %OX for capital letters
Answer
Answer: c [Reason:] Explained in answer itself.
C# MCQ Set 4
1. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i;
-
for (i = 0; ; )
-
{
-
Console.WriteLine("hello");
-
}
-
Console.ReadLine();
-
}
a) No output
b) hello
c) hello printed infinite times
d) Code will give error as expression syntax
Answer
Answer: c [Reason:] Testing condition for the loop is absent.So,loop will continue executing.
Output : hello
hello
hello
.
.
.
2. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
float f;
-
for (f = 0.1f; f <= 0.5; f += 1)
-
Console.WriteLine( ++f );
-
Console.ReadLine();
-
}
a) 1.1
b) 0.1
c) 0.1 0.2 0.3 0.4 0.5
d) None of the mentioned
Answer
Answer: a [Reason:] f =0.1 and ++f = 0.1+1 = 1.1.So,1.1>0.5,Condition fails and hence loop terminates.
Output : 1.1
3. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
int I, X;
-
for (I = 1; I <= (9 % 2 + I); I++)
-
{
-
X = (I * 3 + I * 2) / I;
-
Console.WriteLine(X);
-
}
-
Console.ReadLine();
-
}
a) Output of code is 5 10
b) Output is 5 5 5 5
c) Print 5 infinite times
d) None of the mentioned
Answer
Answer: c [Reason:] Testing condition is always incremented i.e i never ‘>’ (9%2+I).So,loop will never terminate.
Output : 5 5 5…..
4. Select output for the following set of code:
-
static void Main(string[] args)
-
{
-
int I, J = 0;
-
for (I = 1; I < 10; ) ;
-
{
-
J = J + I;
-
I += 2;
-
}
-
Console.WriteLine("Sum of first 10 even numbers is:"+J);
-
Console.ReadLine();
-
}
a) 1 2 3 4 5 6 7 8 9
b) 25
c) 1
d) Run time error
Answer
Answer: d [Reason:] Due to presence of ‘;’ after for()loop condition do not work as according to the statement.Remove the ‘;’.
Output : 25.
5. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
int i = 5;
-
for (; Convert.ToBoolean(Convert.ToInt32(i)); Console.WriteLine(i--)) ;
-
Console.ReadLine();
-
}
a) 4 3 2 1
b) 3 2 1
c) 5 4 3 2 1
d) 2 1
Answer
Answer: c [Reason:] Since, i = 5 and test condition is executed until i!=0.So, i– decrements value of i till condition is satisfied.
Output: 5 4 3 2 1
6. Select the output for the following set of code:
-
static void Main(string[] args)
-
{
-
int i, s = 0;
-
for (i = 1; i <= 10; s = s + i, i++);
-
{
-
Console.WriteLine(s);
-
}
-
Console.ReadLine();
-
}
a) Code report error
b) Code runs in infinite loop condition
c) Code gives output as 0 1 3 6 10 15 21 28 36 45
d) Code give output as 55
Answer
Answer: d [Reason:] Since occurrence of termination symbol(;) at end of for loop.
Output: 55.
7. Which statement is correct among the mentioned statements?
1. The for loop works faster than a while loop
2. for( ; ; )implements an infinite loop
a) Only 1 is correct
b) Only 2 is correct
c) Both 1 and 2 are correct
d) Both 1 and 2 are incorrect
Answer
Answer: b [Reason:] By defination.
8. Select the output for the following set of code :
-
{
-
int i;
-
Console.WriteLine("Hi");
-
for (i = 1; i <= 10; i++)
-
Program.Main(args);
-
Console.ReadLine();
-
}
a) Prints ‘Hi’ for one time
b) Prints ‘Hi’ for infinite times
c) Stack overflow exception Condition generated
d) None of above mentioned
Answer
Answer: c [Reason:] Ocurrence of ‘main()’ condition after for loop.
Output: Hi
Hi
.
.
stack overflow exception.
9. Which of the code should be added to get the following output?
-
* * * * *
-
* * * *
-
* * *
-
* *
-
*
-
static void Main(string[] args)
-
{
-
int i,j;
-
/* Add code here */
-
-
}
a) for (i = 0;i <= 4; i++)
{
for(j = 0;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“n”);
}
b) for (i = 0;i <= 4; i++)
{
for(j = 4;j <= i; j–)
console.WriteLine(“*”);
console.WriteLine(“n”);
}
c) for (i = 0;i <= 4; i++)
{
for (j = i;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“n”);
}
d) for ( i = 0;i <= 4; i++)
{
for (j = 0;j <= i; j++)
console.WriteLine(“*”);
console.WriteLine(“n”);
}
Answer
Answer: c [Reason:] Input in Console and run the code.
10. Predict the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i;
-
for (i =-3; i <= 3; i++)
-
{
-
switch (i)
-
{
-
case 0:
-
Console.WriteLine("zero");
-
break;
-
}
-
if (i > 0)
-
Console.WriteLine("A");
-
else if (i < 0)
-
Console.WriteLine("B");
-
}
-
Console.ReadLine();
-
}
a) B B zero A A A
b) B zero A A A
c) B B B zero A A A
d) A A A zero B B B
Answer
Answer: c [Reason:] for i =-3,-2,-1 statement executed as B.for i = 0,it is zero and for i =1,2,3 again statement printed as A seperately for each value of i.
Output: B B B zero A A A.
11. Which of the following is not infinite loop?
a) for( ;’0′; )
b) for( ;’0′; )
c) for( ;’1′; )
d) for( ;’1′; )
Answer
Answer: b [Reason:] None.
12. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i, j;
-
for (i = 1, j = i; i <= 3 && j >= 0; i++, j--)
-
{
-
if (i == j)
-
continue;
-
else
-
Console.WriteLine(j);
-
}
-
Console.ReadLine();
-
}
a) i = 0, j = 1;
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
Answer
Answer: c [Reason:] Since for i = 1, j = 1 and 1 <= 3 also 1 >= 0 we had i == j.But after i++ and j–. The initial value of ‘j’ which is ‘0’ as j– preferred other than value of ‘j’ in i = j.
Output:j = 0.
13. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = -10;
-
for ( ;Convert.ToBoolean(Convert.ToInt32(i)) ;Console.WriteLine(i++)) ;
-
Console.ReadLine();
-
}
a) -9 -8 -7 -6 -5 -4 -3 -2 -1
b) -10 -9 -8 -7 -6 -5 -4 -3 -2
c) -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
d) -8 -7 -6 -5 -4 -3 -2 -1
Answer
Answer: c [Reason:] For first value of i = -10.Condition is executed until i!=0.
Output: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1.
C# MCQ Set 5
1. The ‘ref’ keyword can be used with which among the following?
a) Static function/subroutine
b) Static data
c) Instance function/subroutine
d) All of the mentioned
Answer
Answer: a [Reason:] None.
2. To implement delegates, the necessary condition is?
a) class declaration
b) inheritance
c) run time polymorphism
d) exceptions
Answer
Answer: a [Reason:] None.
3. Suppose a Generic class called as SortObjects is to be made capable of sorting objects of any type(integer, single, byte etc).Then, which of the following programming
constructs is able to implement the comparison function?
a) interface
b) encapsulation
c) delegate
d) attribute
Answer
Answer: c [Reason:] None.
4. To generate a simple notification for an object in runtime, the programming construct to be used for implementing this idea?
a) namespace
b) interface
c) delegate
d) attribute
Answer
Answer: c [Reason:] None.
5. Choose the incorrect statement among the following about the delegate?
a) delegates are of reference types
b) delegates are object oriented
c) delegates are type safe
d) none of the mentioned
Answer
Answer: d [Reason:] None.
6. Which among the following is the correct statement about delegate declaration ?
delegate void del(int i);
a) on declaring the delegate, a class called del is created
b) the del class is derived from the MulticastDelegate class
c) the del class will contain a one argument constructor and an invoke() method
d) all of the mentioned
Answer
Answer: d [Reason:] None.
7. Which of the following is an incorrect statement about delegate?
a) a single delegate can invoke more than one method
b) delegates could be shared
c) delegates are type safe wrappers for function pointers
d) delegate is a value type
Answer
Answer: c [Reason:] None.
8. Which among the following differentiates a delegate in C#.NET from a conventional function pointer in other languages?
a) delegates in C#.NET represent a new type in the Common Type System
b) delegates allows static as well as instance methods to be invoked
c) delegates are type safe and secure
d) none of the mentioned
Answer
Answer: d [Reason:] None.
9. Choose the incorrect statement about delegates?
a) delegates are not type safe
b) delegates can be used to implement callback notification
c) delegate is a user defined type
d) delegates permits execution of a method in an asynchronous manner
Answer
Answer: a [Reason:] None.
10. Which of the following statements is correct about a delegate?
a) inheritance is a prerequisite for using delegates
b) delegates are not type safe
c) delegates provides wrappers for function pointers
d) none of the mentioned
Answer
Answer: c [Reason:] None.