C# Questions & Answers on Inheritance Implementation for Freshers

https://www.computersprofessor.com/2018/06/c-questions-answers-on-inheritance.html
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
b) 3
c) 2
d) Compile Time error
Answer: c
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.
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
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: d
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
base method
b) 10, 20, 0
c) compile time error
d) base method
Answer: c
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 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
b) 12 bytes
c) 16 bytes
d) 24 bytes
Answer: d
Explanation: 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
b) This is base class constructor
c) Compile time error
d) None of the mentioned
Answer: c
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 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);
b) Console.Writeline( mybase.a + ” ” + a);
c) console.writeline(a + ” ” + base.a);
d) console.writeline(base.a + ” ” + a);
Answer: c
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)
b) Output : b
a
c) Output : a
b
d) The program will work correctly if we replace base(a1) with base.baseclass(a1)
Answer: c
Output : a
b
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();
b) a();
c) base.a();
d) x::a();
Answer: c
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: d
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
b) 1 0
c) 0 2
d) 1 2
Answer: d
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. 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
b) 2, 3
c) 1, 2
d) compile time error
Answer: d
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
Answer: b
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
a) :
b) &
c) ::
d) extends
Answer: a
Explanation: class a
{
}
class b : 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
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: a
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