Inheritance Questions
Practice Inheritance MCQs with answers and explanations. Page 1 of 2.
Category
C# Programming
Topic
Inheritance
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
Method hiding with new: what is printed?
namespace CuriousTabConsoleApplication
{
class Baseclass { public void fun() { Console.Write("Base class "); } }
class Derived1 : Baseclass { new void fun() { Console.Write("Derived1 class "); } }
class Derived2 : Derived1 { new void fun() { Console.Write("Derived2 class "); } }
class Program { static void Main(string[] args) { Derived2 d = new Derived2(); d.fun(); } }
}
Open
View answer
Reuse mechanisms in C#.NET: which of the following provide code reuse in typical C# design?
1) Inheritance 2) Encapsulation 3) Templates 4) Containership (composition/aggregation) 5) Polymorphism
Open
View answer
Preventing inheritance in C#.NET: which modifier should be applied to a class to disallow deriving from it?
Open
View answer
Accessing hidden and base fields: which line should be added so that fun() prints
9 13
for the following code?
class BaseClass { protected int i = 13; }
class Derived : BaseClass
{
int i = 9;
public void fun()
{
// [*** Add statement here ***]
}
}
Open
View answer
Combining base-class behavior with derived behavior: which statement should be added so that the output is exactly
Welcome to CuriousTab.com!
for the following code?
class A { public void fun() { Console.Write("Welcome"); } }
class B : A
{
public void fun()
{
// [*** Add statement here ***]
Console.WriteLine(" to CuriousTab.com!");
}
}
Open
View answer
C#.NET inheritance and accessibility — analyze the following program and choose the valid statements.
namespace CuriousTabConsoleApplication
{
class index
{
protected int count;
public index()
{
count = 0;
}
}
class index1 : index
{
public void increment()
{
count = count + 1;
}
}
class MyProgram
{
static void Main(string[] args)
{
index1 i = new index1();
i.increment();
}
}
}
Which statements are correct regarding accessibility and constructor behavior in this inheritance setup?
Open
View answer
C#.NET objects and memory model — estimate the size of an instance (ignoring object header).
namespace CuriousTabConsoleApplication
{
class Baseclass
{
private int i;
protected int j;
public int k;
}
class Derived : Baseclass
{
private int x;
protected int y;
public int z;
}
class MyProgram
{
static void Main(string[] args)
{
Derived d = new Derived();
}
}
}
What is the size contribution of the fields in one <code>Derived</code> object if each <code>int</code> is 4 bytes (ignore header/alignment)?
Open
View answer
Object relationships in software design — which construct implements a “Has a” relationship?
Open
View answer
C#.NET constructor chaining — predict the output.
namespace CuriousTabConsoleApplication
{
class Baseclass
{
int i;
public Baseclass(int ii)
{
i = ii;
Console.Write("Base ");
}
}
class Derived : Baseclass
{
public Derived(int ii) : base(ii)
{
Console.Write("Derived ");
}
}
class MyProgram
{
static void Main(string[] args)
{
Derived d = new Derived(10);
}
}
}
What will be printed?
Open
View answer
Object relationships in software design — which construct implements a “Like a / Kind of” relationship?
Open
View answer
C#.NET inheritance accessibility — which base-class members are accessible to derived classes?
Choose all that apply among: 1) static 2) protected 3) private 4) shared (VB equivalent of static) 5) public
Open
View answer
C#.NET inheritance facts — select the correct statements.
A derived class object contains all the base-class data.
Inheritance cannot suppress base-class functionality.
A derived class may not be able to access all the base-class data.
Inheritance cannot extend base-class functionality.
During object construction in an inheritance chain, construction happens from base towards derived.
Open
View answer
C#.NET constructor order — if class B inherits A, which constructor runs first when creating a B object?
Open
View answer
C#.NET — What can inheritance facilitate? Choose the best set.
Open
View answer
C#.NET method hiding vs overloading — predict the output.
namespace CuriousTabConsoleApplication
{
class Baseclass
{
public void fun()
{
Console.WriteLine("Hi" + " ");
}
public void fun(int i)
{
Console.Write("Hello" + " ");
}
}
class Derived : Baseclass
{
public void fun()
{
Console.Write("Bye" + " ");
}
}
class MyProgram
{
static void Main(string[] args)
{
Derived d = new Derived();
d.fun();
d.fun(77);
}
}
}
What will be printed?
Open
View answer
In C++ object-oriented programming, if both a base class and a derived class define a member function with the same name and compatible signature, then calls made on a derived-class object will resolve to the derived version. Is this statement accurate? Provide the most precise judgment.
Open
View answer
In C++ access control, private members of a base class cannot be accessed directly by member functions of a derived class, nor by objects of the derived class. Judge the statement.
Open
View answer
In C#.NET class inheritance, there is no multiple inheritance of classes (a class cannot derive from more than one base class). Assess the statement.
Open
View answer
In C++ object layout, is the size of a derived-class object always equal to the sum of the sizes of the base-class data members and the derived-class data members? Choose the most accurate evaluation.
Open
View answer
Concept check: Is multiple inheritance (deriving from more than one base class) the same as multilevel inheritance (a chain Base → Derived → MoreDerived)? Provide the best judgment.
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.