C# MCQ Set 1
1. Which procedure among the following should be used to implement a ‘Has a’ or a ‘Kind of’ relationship between two entities?
a) Polymorphism
b) Inheritance
c) Templates
d) All of the mentioned
Answer
Answer: b [Reason:] None.
2. The number of levels of inheritance are?
a) 5
b) 4
c) 3
d) 2
Answer
Answer: b [Reason:] None.
3. What does the following code signify?
-
class a
-
{
-
-
-
-
}
-
class b : a
-
{
-
variable declaration;
-
method declaration;
-
}
a) Declaration of a base class
b) Declaration of a sub class
c) Declaration of base class & sub class and how subclass inherits the base class
d) None of the mentioned
Answer
Answer: c [Reason:] None.
4. In an inheritance chain through which of following, the base class and its components are accessible to the derived class?
a) Scope resolution operator(:)
b) Class visibilty modifiers (public,private etc.)
c) Dot operator (.)
d) All of the mentioned
Answer
Answer: b [Reason:] None.
5. Select the class visibility modifiers among the following :
a) Private, protected, public, internal
b) Private, protected, public, internal, protected internal
c) Private, protected, public
d) All of the mentioned
Answer
Answer: b [Reason:] None.
6. In Inheritance concept, which of the following members of base class are accessible to derived class members?
a) static
b) protected
c) private
d) shared
Answer
Answer: b [Reason:] None.
7. Wrong statement about inheritance in C# .NET?
a) In inheritance chain, object construction begins from base class towards derived class
b) Inheritance cannot extend base class functionality
c) A derived class object contains all base class data
d) All of the mentioned
Answer
Answer: b [Reason:]None.
8. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
Answer
Answer: d [Reason:] A class member declared protected becomes private member of subclass.
9. Which of the following functionality is facilitated by inheritance mechanism?
a) Use the existing functionality of base class
b) Override the existing functionality of base class
c) Implements new functionality in derived class
d) All of the mentioned
Answer
Answer: d [Reason:]None.
10. Which statements among following are correct?
a) We can derive a class from a base class even if source code of base class not available
b) Multiple inheritance is different from multiple levels of inheritance
c) It is legal to make objects of one class as members of another class
d) All of the mentioned
Answer
Answer: d [Reason:] None.
11. If base class consist of two private integers,one static integer and derived class consist of two static integers and one private integer.What would be the size of derived class object?
a) size of object depends on sizes of its non static data members
b) size of a derived class object is sum of sizes of non static data members of base class and derived class
c) size of object is calculated using sizeof() method
d) none of the mentioned
Answer
Answer: a [Reason:] None.
12. Which is the correct way to create an object of the given class abc?
a) Declaring existing class as sealed
b) Declaring existing class as override
c) Declaring existing class as overloads
d) Declaring existing class as shadows
Answer
Answer: a [Reason:] None.
13. Given class sample is inherited by class sample 1. Which are the correct statements about construction of object of class sample?
a) While creating the object firstly the constructor of class sample will be called followed by constructor of class sample 1
b) The constructor of only sample class will be called
c) While creating the object firstly constructor of class sample 1 will be called followed by constructor of class sample
d) The order of calling constructors depend on whether constructors in class sample and sample 1 are private or public
Answer
Answer: c [Reason:] None.
14. Which form of inheritance is not supported directly by C# .NET?
a) Multiple inheritance
b) Multilevel inheritance
c) Single inheritance
d) Hierarchical inheritance
Answer
Answer: a [Reason:] Supported in form of interfaces in secondary form.
15. If no access modifier for a class is specified,then class accessibility is defined as?
a) public
b) protected
c) private
d) internal
Answer
Answer: c [Reason:] By default it is specified private in nature.
C# MCQ Set 2
1. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i = 30;
-
int j = 25 % 25;
-
if (Convert.ToBoolean(Convert.ToInt32(i = j)))
-
{
-
Console.WriteLine("In if");
-
}
-
else
-
{
-
Console.WriteLine("In else");
-
}
-
Console.WriteLine("In main");
-
Console.ReadLine();
-
}
a) In if
b) In else
c) In if
In main
d) In else
In main
Answer
Answer: d [Reason:] Usage of ‘=’ operator instead of ‘==’ operator .hence,the condition is not true.
Output: In else
In main
2. Select output for the following set of Code:
-
static void Main(string[] args)
-
{
-
int i;
-
int b = 8, a = 32;
-
for (i = 0; i <= 10; i++)
-
{
-
if ((a / b * 2)== 2)
-
{
-
Console.WriteLine( i + " ");
-
continue;
-
}
-
else if (i != 4)
-
Console.Write(i + " ");
-
else
-
break;
-
}
-
Console.ReadLine();
-
}
a) 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8
c) 0 1 2 3
d) 0 1 2 3 4
Answer
Answer: c [Reason:] The if condition will never be fulfilled as ((a / b) * 2 == 2) is never true.Hence,only else part of condition will be executed until i! = 4 i.e i = 0,1 ,2 ,3.
Output:0 1 2 3
3. Select the output for the following set of Code:
-
static void Main(string[] args)
-
{
-
Console.WriteLine("Enter a letter:");
-
char c = (char)Console.Read();
-
if (Char.IsDigit(c) == true)
-
Console.WriteLine("A number");
-
else if (char.IsLower(c) == true)
-
Console.WriteLine("A lower case letter");
-
else if (char.IsUpper(c) == true)
-
Console.WriteLine("An upper case letter");
-
Console.ReadLine();
-
}
-
1. Enter a letter :
-
a
-
An upper case letter
-
2. Enter a letter :
-
A
-
An upper case letter
-
3. Enter a letter :
-
2
-
A number
-
4. Enter a letter :
-
2
-
A lower case letter.
Which of the following conditions are true ?
a) a ,b ,c
b) b ,c ,d
c) a ,d ,b
d) b ,c
Answer
Answer: d
Output:Enter a letter :
A
An upper case letter
Enter a letter :
2
A number
4. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int i, j;
-
for (i = 2; i >= 0; i--)
-
{
-
for (j = 0; j <= 2; j++)
-
{
-
if (i == j)
-
{
-
Console.WriteLine("1");
-
}
-
else
-
{
-
Console.WriteLine("0");
-
}
-
}
-
Console.WriteLine("n");
-
Console.ReadLine();
-
}
-
}
a) 1 0 0
0 1 0
0 0 1
b) 0 1 0
1 0 0
0 0 1
c) 0 0 1
0 1 0
1 0 0
d) 1 0 0
0 0 1
0 1 0
Answer
Answer: c [Reason:] In first row for i = 2 : j = 0 == 0 as if condition fails for (i == j)
i = 2 : j = 1 == 0 as again if condition fails for ( i == j)
i = 2 : j = 2 == 1 as (i == j).
In Second row for i = 1 : j = 0 == 0 as if condition fails for (i == j)
i = 1 : j = 1 == 1 (as i==j)
i = 1 : j = 2 == 0 as (i==j) not true
In Third row for i = 0 : j = 0 == 1 as (i==j) true
i = 0 : j = 1 == 0 as (i==j) not true.
i = 0 : j = 2 == 0 .
So, 0 0 1
0 1 0
1 0 0
Output: 0 0 1
0 1 0
1 0 0
5. Select the correct ‘if statement’ to be filled in the given set of code :
-
static void Main(string[] args)
-
{
-
int []num = {50, 65, 56, 88, 43, 52};
-
int even = 0, odd = 0;
-
for (int i = 0 ;i < num.Length ;i++)
-
{
-
/*___________________________*/
-
}
-
Console.WriteLine("Even Numbers:" +even);
-
Console.WriteLine("Odd Numbers:" +odd);
-
Console.ReadLine();
-
}
a)
-
if ((num % 2) == 0)
-
{
-
even += 1;
-
}
-
else
-
{
-
odd += 1;
-
}
b)
-
if((num * i) == 0)
-
{
-
even += 1;
-
}
-
else
-
{
-
odd += 1;
-
}
c)
-
if(num[i] % 2 == 0)
-
{
-
even += 1;
-
}
-
else
-
{
-
odd += 1;
-
}
d)
-
if(num[i] % 2 = 0)
-
{
-
even += 1;
-
}
-
else
-
{
-
odd += 1;
-
}
Answer
Answer: c [Reason:] int []num = {50, 65, 56, 88, 43, 52};
int even = 0,odd = 0;
for (int i = 0 ;i < num.Length ;i++)
{
if (num[i] % 2 == 0)
{
even += 1;
}
else
{
odd += 1;
}
}
Console.WriteLine(“Even Numbers: ” +even);
Console.WriteLine(“Odd Numbers: ” +odd);
Console.ReadLine();
6. What is the output for the following code ?
-
static void Main(string[] args)
-
{
-
int a = 15, b = 10, c = 1;
-
if (Convert.ToBoolean(a) && (b > c))
-
{
-
Console.WriteLine("cquestionbank");
-
}
-
else
-
{
-
break;
-
}
-
}
a) cquestionbank
b) It will print nothing
c) Compile time error
d) Run time error
Answer
Answer: c [Reason:] Keyword “break” is not part of if-else statement.This keyword is used in case of loop or switch case statement.
7. What is the output for the following code ?
-
static void Main(string[] args)
-
{
-
int a = 5;
-
if (Convert.ToBoolean((.002f) -(0.1f)))
-
Console.WriteLine("Sachin Tendulkar");
-
else if (a == 5)
-
Console.WriteLine("Rahul Dravid");
-
else
-
Console.WriteLine("Ms Dhoni");
-
Console.ReadLine();
-
}
a) Rahul Dravid
b) Sachin Tendulkar
c) Ms Dhoni
d) Warning : Unreachable Code
Answer
Answer: b [Reason:] (0.002 – 0.1f) not equivalent to zero hence it is true. So,only first if clause will execute and print:Sachin Tendulkar on console.As,first condition is always true so no else if statement will be executed.
Output: Sachin Tendulkar
8. Select the output for the following set of Code :
-
static void Main(string[] args)
-
{
-
int a = -1;
-
int b = -1;
-
if (Convert.ToBoolean (++a = ++b))
-
Console.WriteLine("a");
-
else
-
Console.WriteLine("b");
-
Console.ReadLine();
-
}
a) a
b) b
c) Compile time error
d) Code execute successfully with no output
Answer
Answer: c [Reason:] Both a and b are constants.Illegal to assign a value to constant on left hand of ‘=’operator .Hence,it must be some variable.
Output: static void Main(string[] args)
{
int a = -1;
int b = -1;
if (Convert.ToBoolean(++a == ++b))
Console.WriteLine(“a”);
else
Console.WriteLine(“b”);
Console.ReadLine();
}
9. Select the output for the following set of Code :
-
static void Main(string[] args)
-
{
-
int a = 5, b = 10;
-
if (Convert.ToBoolean(Convert.ToInt32(0xB)))
-
if (Convert.ToBoolean(Convert.ToInt32(022)))
-
if (Convert.ToBoolean(Convert.ToInt32('xeb')))
-
Console.WriteLine("java");
-
else ;
-
else ;
-
else ;
-
}
a) Compile time error: Misplaced else
b) Compile time error: Undefined symbol
c) java
d) Warning: Condition is always true
Answer
Answer: c [Reason:] oxB: hexadecimal integer constant.
022: It octal integer constant.
‘xeb’: It is hexadecimal character constant.
As,zero is false and any non-zero number is true. All,constants return a non-zero value. So, all if conditions in the above program are true.
Output: java.
10. Select the output for the following set of Code :
-
static void Main(string[] args)
-
{
-
int a = 5, b = 10;
-
if (Convert.ToBoolean(Convert.ToInt32(++a)) || Convert.ToBoolean(Convert.ToInt32(++b)))
-
{
-
Console.WriteLine(a + "n" + b);
-
}
-
else
-
Console.WriteLine(" C# ");
-
}
a) 6 11
b) 6 16
c) 6 12
d) 6 10
Answer
Answer: d [Reason:] Consider the following expression:( ++a || ++b). In this expression || is ‘Logical OR operator’. Two important properties of this operator are:
Property 1:
(Expression1) || (Expression2)
|| operator returns 0 if and only if both expressions return a zero otherwise || operator returns 1.
initial value of a is 5. So ++a will be 6. Since ++a is returning a non-zero so ++b will not execute.
Output : 6 10.
C# MCQ Set 3
1. Select the correct output for the given set of code?
-
class sample
-
{
-
public int i;
-
void display()
-
{
-
Console.WriteLine(i);
-
}
-
}
-
class sample1 : sample
-
{
-
public int j;
-
public void display()
-
{
-
Console.WriteLine(j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample1 obj = new sample1();
-
obj.i = 1;
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
a) 1
b) 3
c) 2
d) Compile Time error
Answer
Answer: c [Reason:] class sample & class sample1 both contain display() method, class sample1 inherits class sample, when display() method is called by object of class sample 1, display() method of class sample 1 is executed rather than that of Class sample.
2. Correct statement about C#.NET code is?
-
class sample
-
{
-
protected int index;
-
public sample()
-
{
-
index = 0;
-
}
-
}
-
class sample 1: sample
-
{
-
public void add()
-
{
-
index += 1;
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample 1 z = new sample 1();
-
z . add();
-
}
-
}
a) Index should be declared as protected if it is to become available in inheritance chain
b) Constructor of sample class does not get inherited in sample 1 class
c) During constructing an object referred to by z, Firstly constructor of sample class will be called followed by constructor of sample 1 class
d) All of the mentioned
Answer
Answer: d [Reason:] None.
3. The following set of code is run on single level of inheritance. Find correct statement about the code?
-
class sample
-
{
-
int i = 10;
-
int j = 20;
-
public void display()
-
{
-
Console.WriteLine("base method ");
-
}
-
}
-
class sample1 : sample
-
{
-
public int s = 30;
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample1 obj = new sample1();
-
Console.WriteLine("{0}, {1}, {2}", obj.i, obj.j, obj.s);
-
obj.display();
-
Console.ReadLine();
-
}
-
}
a) 10, 20, 30
base method
b) 10, 20, 0
c) compile time error
d) base method
Answer
Answer: c [Reason:] ‘i’ and ‘ j’ are inaccessible due to protection level.Declare them as public variable and hence will be accessed in code.
4. What will be size of the object created depicted by csharp code snippet?
-
class baseclass
-
{
-
private int a;
-
protected int b;
-
public int c;
-
}
-
class derived : baseclass
-
{
-
private int x;
-
protected int y;
-
public int z;
-
}
-
class Program
-
{
-
static Void Main(string[] args)
-
{
-
derived a = new derived();
-
}
-
}
a) 20 bytes
b) 12 bytes
c) 16 bytes
d) 24 bytes
Answer
Answer: d [Reason:] Explained in fundamentals of inheritance.
5. What would be output of the following set of code?
-
class sample
-
{
-
public sample()
-
{
-
Console.WriteLine("THIS IS BASE CLASS constructor");
-
}
-
}
-
public class sample1 : sample
-
{
-
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
sample1 obj = new sample1();
-
Console.ReadLine();
-
}
-
}
a) Code executes successfully prints nothing
b) This is base class constructor
c) Compile time error
d) None of the mentioned
Answer
Answer: c [Reason:] Base class accessibility level is much less compared to derived class.Declare it public to get desired output.
6. Select the statement which should be added to the current set of code to get the output as 10 20 ?
-
class baseclass
-
{
-
protected int a = 20;
-
}
-
class derived : baseclass
-
{
-
int a = 10;
-
public void math()
-
{
-
/* add code here */
-
}
-
}
a) Console.writeline( a + ” ” + this.a);
b) Console.Writeline( mybase.a + ” ” + a);
c) console.writeline(a + ” ” + base.a);
d) console.writeline(base.a + ” ” + a);
Answer
Answer: c [Reason:] None.
7. Correct statement about the following C#.NET code is?
-
class baseclass
-
{
-
int a;
-
public baseclass(int a1)
-
{
-
a = a1;
-
console.writeline(" a ");
-
}
-
class derivedclass : baseclass
-
{
-
public derivedclass (int a1) : base(a1)
-
{
-
console.writeline(" b ");
-
}
-
}
-
class program
-
{
-
static void main(string[] args)
-
{
-
derivedclass d = new derivedclass(20);
-
}
-
}
-
}
a) Compile time error
b) Output : b
a
c) Output : a
b
d) The program will work correctly if we replace base(a1) with base.baseclass(a1)
Answer
Answer: c [Reason:] None.
Output : a
b
8. Which statement should be added in function a() of class y to get output “i love csharp”?
-
class x
-
{
-
public void a()
-
{
-
console.write("bye");
-
}
-
}
-
class y : x
-
{
-
public void a()
-
{
-
/* add statement here */
-
console.writeline(" i love csharp ");
-
}
-
}
-
class program
-
{
-
static void main(string[] args)
-
{
-
y obj = new obj();
-
obj.a();
-
}
-
}
a) x.a();
b) a();
c) base.a();
d) x::a();
Answer
Answer: c [Reason:] None.
9. Which statements are correct?
a) If a base class consists of a member function fun() and a derived class do not have any function with this name. An object of derived class can access fun()
b) A class D can be derived from class C, which is derived from class B which in turn is derived from class A
c) If a base class and a derived class each include a member function with same name,the member function of the derived class will be called by object of derived class
d) All of the mentioned
Answer
Answer: d [Reason:] None.
10. Select the output for the following set of code?
-
class A
-
{
-
public int i;
-
protected int j;
-
}
-
class B : A
-
{
-
public int j;
-
public void display()
-
{
-
base.j = 3;
-
Console.WriteLine(i + " " + j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.i = 1;
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
a) 2 1
b) 1 0
c) 0 2
d) 1 2
Answer
Answer: d [Reason:] Both class A & B have members with same name that is j, member of class B will be called by default if no specifier is used. i contains 1 & j contains 2, printing 1 2.
Output: 1, 2
11. Select the output of the following set of code?
-
class A
-
{
-
public int i;
-
private int j;
-
}
-
class B :A
-
{
-
void display()
-
{
-
base.j = base.i + 1;
-
Console.WriteLine(base.i + " " + base.j);
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
B obj = new B();
-
obj.i = 1;
-
obj.j = 2;
-
obj.display();
-
Console.ReadLine();
-
}
-
}
a) 1, 3
b) 2, 3
c) 1, 2
d) compile time error
Answer
Answer: d [Reason:] Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
12. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) base
c) this
d) None of the mentioned
Answer
Answer: b [Reason:] Whenever a subclass needs to refer to its immediate super class, it can do so by use of the keyword base.
13. Which of these operators must be used to inherit a class?
a) :
b) &
c) ::
d) extends
Answer
Answer: a [Reason:] class a
{
}
class b : a
{
}
14. What is the output of the following set of code ?
-
using System;
-
public class BaseClass
-
{
-
public BaseClass()
-
{
-
Console.WriteLine("I am a base class");
-
}
-
}
-
public class ChildClass : BaseClass
-
{
-
public ChildClass()
-
{
-
Console.WriteLine ("I am a child class");
-
}
-
static void Main()
-
{
-
ChildClass CC = new ChildClass();
-
}
-
}
a) I am a base class
I am a child class
b) I am a child class
I am a base class
c) compile time error
d) None of the mentioned
Answer
Answer: a [Reason:] This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructor is executed before the ChildClass constructor.
Output: I am a base class
I am a child class
C# MCQ Set 4
1. How many Bytes are stored by ‘Long’ Datatype in C# .net?
a) 8
b) 4
c) 2
d) 1
Answer
Answer: a [Reason:] ‘Long’ is the datatype keyword used for storing data of unlimited length so by definition its size is always maximum i.e 8.
2. Choose “.NET class” name from which datatype “UInt” is derived ?
a) System.Int16
b) System.UInt32
c) System.UInt64
d) System.UInt16
Answer
Answer: b [Reason:] By Defination class assigned to
a) System.Int16 = short.
b) System.UInt32 = UInt.
c) System.UInt64 = ULong.
d) System.UInt16 = UShort.
3. Correct Declaration of Values to variables ‘a’ and ‘b’?
a) int a = 32, b = 40.6;
b) int a = 42; b = 40;
c) int a = 32; int b = 40;
d) int a = b = 42;
Answer
Answer: c [Reason:] a) Although,declaration of ‘b’ and ‘a’ are correct but initialization of value to ‘b’ should be ‘int’ datatype not float.
b) Missing declaration type of ‘b’.
c) correctly declared datatypes ‘a’ and ‘b’.
d) ‘b’ can’t be assigned values before declaration.
4. Select error in the given program :
-
Static Void Main(String[] args)
-
{
-
const int m = 100;
-
int n = 10;
-
const int k = n / 5 * 100 * n ;
-
Console.WriteLine(m * k);
-
Console.ReadLine();
-
}
a) ‘k’ should not be declared constant
b) Expression assigned to ‘k’ should be constant in nature
c) Expression (m * k) is invalid
d) ‘m ‘ is declared in invalid format
Answer
Answer: b [Reason:]’k’ should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant.
Output :Error 1 – The expression being assigned to ‘k’ must be constant.
5. Arrange the following datatype in order of increasing magnitude sbyte, short, long, int.
a) long < short < int < sbyte
b) sbyte < short < int < long
c) short < sbyte < int < long
d) short < int < sbyte < long
Answer
Answer: b [Reason:] By definition.
6. Which datatype should be more preferred for storing a simple number like 35 to improve execution speed of a program?
a) sbyte
b) short
c) int
d) long
Answer
Answer: a [Reason:] Wider datatype like int,long takes more time for manipulation of a program.
7. Which Conversion function of ‘Convert.TOInt32()’ and ‘Int32.Parse()’ is efficient?
1) Int32.Parse() is only used for strings and throws argument exception for null string
2) Convert.Int32() used for datatypes and returns directly ‘0’ for null string
a) 2
b) Both 1,2
c) 1
d) None of the mentioned
Answer
Answer: a [Reason:] Convenient for every datatype so mostly preferred.
8. Correct way to assign values to variable ‘c’ when int a=12, float b=3.5,int c;
a) c = a + b;
b) c = a + int(float(b));
c) c = a + convert.ToInt32(b);
d) c = int(a + b);
Answer
Answer: c [Reason:] None.
9. Correct Set of Code for given data ‘a’ and ‘b’ to print output for ‘c’ as 74 ?
a)
-
int a = 12;
-
float b = 6.2f;
-
int c;
-
c = a / b + a * b;
-
Console.WriteLine(c);
b)
-
int a = 12;
-
float b = 6.2f;
-
int c;
-
c = a / convert.ToInt32(b) + a * b;
-
Console.WriteLine(c);
c)
-
int a = 12;
-
float b = 6.2f;
-
int c;
-
c = a / convert.ToInt32(b) + a * convert.ToInt32(b);
-
Console.WriteLine(c);
d)
-
int a = 12;
-
float b = 6.2f;
-
int c;
-
c = convert.ToInt32(a / b + a * b);
-
Console.WriteLine(c);
Answer
Answer: c [Reason:] Usage of typecasting operation. Seperately check each expression taking typecast operations in concern.
Output : 74.
10. Does the output remain same or different for both cases?
1)
-
char l ='k';
-
float b = 19.0f;
-
int c;
-
c = (l / convert.ToInt32(b));
-
Console.Writeline(c);
2)
-
char l ='k';
-
float b = 19.0f;
-
int c;
-
c = Convert.ToInt32(l / b);
-
console.writeline(c);
a) Yes
b) No
Answer
Answer: No [Reason:]
Output – 1) 5.
2) 6.
11. Default Type of number without decimal is ?
a) Long Int
b) Unsigned Long
c) Int
d) Unsigned Int
Answer
Answer: c [Reason:] By definition.
12. Correct output for code is?
-
static void Main(string[] args)
-
{
-
float a = 10.553f;
-
long b = 12L;
-
int c;
-
c = Convert.ToInt32(a + b);
-
Console.WriteLine(c);
-
}
a) 23.453
b) 22
c) 23
d) 22.453
Answer
Answer: c [Reason:] The two datatype ‘float’ and ‘long’ after arithmetic operation completely converted to nearest whole number 23.
Output : 23.
C# MCQ Set 5
1. Correct statement about c#.NET code snippet given below is?
-
interface a1
-
{
-
void f1();
-
int f2();
-
}
-
class a :a1
-
{
-
void f1()
-
{
-
}
-
int a1.f2()
-
{
-
}
-
}
a) class a is an abstract class
b) A method table would not be created for class a
c) The definition of f1() in class a should be void a1.f1()
d) None of the mentioned
Answer
Answer: c [Reason:] None.
2. Choose the wrong statement about ‘INTERFACE’ in C#.NET?
a) An explicitly implemented member could be accessed from an instance of the interface
b) Interfaces are declared public automatically
c) An interface could not contain signature of the indexer
d) None of the mentioned
Answer
Answer: c [Reason:] None.
3. Choose the correct statement about following code snippet given below:
-
interface a1
-
{
-
void f1();
-
void f2();
-
}
-
class a :a1
-
{
-
private int i;
-
void a1.f1()
-
{
-
}
-
}
a) Class a could not have an instance data
b) Class a is an abstract class
c) Class a fully implements the interface a1
d) None of the mentioned
Answer
Answer: b [Reason:] None.
4. Choose the correct statement about the following code snippet in C#.NET:
-
interface abc
-
{
-
String FirstName
-
{
-
get;
-
set;
-
}
-
String LastName
-
{
-
get;
-
set;
-
}
-
void print();
-
void stock();
-
int fun();
-
}
a) Functions should be declared inside an interface
b) It is workable code
c) Properties cannot be declared inside an interface
d) None of the mentioned
Answer
Answer: b [Reason:] None.
5. What will be the output of the given code snippet?
-
interface calc
-
{
-
void cal(int i);
-
}
-
public class maths :calc
-
{
-
public int x;
-
public void cal(int i)
-
{
-
x = i * i;
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
display arr = new display();
-
arr.x = 0;
-
arr.cal(2);
-
Console.WriteLine(arr.x);
-
Console.ReadLine();
-
}
-
}
a) 0
b) 2
c) 4
d) None of the mentioned
Answer
Answer: c [Reason:] None.
Output : 4
6. What will be the output of the given code snippet?
-
interface calc
-
{
-
void cal(int i);
-
}
-
class displayA :calc
-
{
-
public int x;
-
public void cal(int i)
-
{
-
x = i * i;
-
}
-
}
-
class displayB :calc
-
{
-
public int x;
-
public void cal(int i)
-
{
-
x = i / i;
-
}
-
}
-
class Program
-
{
-
public static void Main(string[] args)
-
{
-
displayA arr1 = new displayA();
-
displayB arr2 = new displayB();
-
arr1.x = 0;
-
arr2.x = 0;
-
arr1.cal(2);
-
arr2.cal(2);
-
Console.WriteLine(arr1.x + " " + arr2.x);
-
Console.ReadLine();
-
}
-
}
a) 0 0
b) 2 2
c) 4 1
d) 1 4
Answer
Answer: c [Reason:] class displayA executes the interface calculate by doubling the value of item . Similarly class displayB implements the interface by dividing item by item.So, variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output : 4, 1
7. Choose the correct output of the given code snippet?
-
interface i1
-
{
-
void fun();
-
}
-
interface i2
-
{
-
void fun();
-
}
-
public class maths :i1, i2
-
{
-
void i1.fun()
-
{
-
Console.WriteLine("i1.fun");
-
}
-
void i2.fun()
-
{
-
Console.WriteLine("i2.fun");
-
}
-
}
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
Sample obj = new Sample();
-
i1 i = (i1) obj;
-
i.fun();
-
i2 ii = (i2) obj;
-
ii.fun();
-
}
-
}
a) i1.fun
b) i2.fun
i1.fun
c) 0
d) i1.fun
i2.fun
Answer
Answer: d [Reason:] None.
8. Correct way to implement the interface given below?
-
interface abc
-
{
-
string name
-
{
-
get;
-
set;
-
}
-
}
a)
class emp :employee { private string str; public string firstname; { get { return str; } set { str = value; } } }
b)
class emp :implements person { private string str; public string firstname { get { return str; } set { str = value; } } }
c)
class emp: implements person { private string str; public string person.firstname { get { return str; } set { str = value; } } }
d) None of the mentioned
Answer
Answer: a [Reason:] None.
9. Choose the correct output of the following code snippet?
-
interface i1
-
{
-
void f1();
-
}
-
interface i2 :i1
-
{
-
void f2();
-
}
-
public class maths :i2
-
{
-
public void f2()
-
{
-
Console.WriteLine("fun2");
-
}
-
public void f1()
-
{
-
Console.WriteLine("fun1");
-
}
-
}
-
class Program
-
{
-
static Void Main()
-
{
-
maths m = new maths();
-
m.f1();
-
m.f2();
-
}
-
}
a) fun2
b) fun1
c) fun1
fun2
d) fun2
fun1
Answer
Answer: c [Reason:] None.
10. In order to avoid ambiguity among an interface derived from two base interfaces with same method name(and signature), the right code among the following given codes is?
a)
-
interface i1
-
{
-
void m1();
-
}
-
interface i2
-
{
-
void m1();
-
}
-
interface i3 :i1, i2
-
{
-
}
-
class c3 :i3
-
{
-
void i1.m1()
-
{
-
}
-
void i1.m1()
-
{
-
}
-
}
b)
interface i1 { void m1(); } interface i2 { void m1(); } interface i3 :i1, i2 { } class c3 :i3 { void i1.i2.m1() { } }
c)
interface i1 { void m1(); } interface i2 { void m1(); } interface i3 :i1, i2 { } class c3 :i3 { void i1.m1() { } void i2.m1() { } }
d) All of the mentioned
Answer
Answer: c [Reason:] None.