Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Questions & Answers ? Inheritance Implementation
C# Questions & Answers – Inheritance Implementation
This section of our 1000+ C# multiple choice questions focuses on implementation of inheritance in C# Programming Language.
1. What will be the output of the following C# 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
Explanation: 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.
-
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
Explanation: None.
3. The following C# code is run on single level of inheritance. What will be the Correct statement in the following C# 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
Explanation: ‘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 C# 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
Explanation: Explained in fundamentals of inheritance.
5. What will be the output of the following C# 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
Explanation: 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 C# 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);
Explanation: None.
7. What will be the Correct statement in the following C# code?
-
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)
b a
c)
a b
d) The program will work correctly if we replace base(a1) with base.baseclass(a1)
Explanation: None.
Output :
a b
8. Which C# 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();
Explanation: 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
Explanation: None.
10. What will be the output of the following C# 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
Explanation: 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. What will be the output of the following C# 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
Explanation: 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
Explanation: 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
Explanation:
class a { } class b : a { }
14. What will be the output of the following C# 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
Explanation: 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