Sample questions
Question 1Medium
Inheritance is one of the pillar of Object-Oriented-Programming (OOP).
- True
- False
- Piler concept does not supported
- None of the Above
Answer: A. True
Explanation
Inheritance is one of the four major pillars of OOP, along with encapsulation, abstraction and polymorphism. So the answer is True.
Step-by-step solution
- Inheritance is one of the main pillars of object-oriented programming.
- The four commonly listed pillars are encapsulation, abstraction, inheritance and polymorphism.
- So the statement is True, which is option A.
Why the other options are wrong
- B. False is wrong, because inheritance is a core OOP pillar.
- C. C# does support inheritance, so the claim that the concept is not supported is wrong.
- D. None of the Above is wrong, because option A is correct.
Tip: Remember the four pillars: encapsulation, abstraction, inheritance and polymorphism.
Question 2Medium
How can you prevent inheritance from a class in C#.NET?
- Declare the class as shadows.
- Declare the class as overloads
- Declare the class as sealed.
- Declare the class as suppress.
Answer: C. Declare the class as sealed.
Explanation
C#.NET allows sealed attribute to be used as a part of class statement. Classes declared with sealed keyword cannot be used as based class for other classes. Most important reason to do this world be to prevent behavior of a class to be changed in any way.
Step-by-step solution
- In C#, a class marked sealed cannot be used as a base class.
- So declaring the class as sealed prevents inheritance from it.
Why the other options are wrong
- A. 'shadows' is a VB.NET member modifier, not a C# class modifier for stopping inheritance.
- B. 'overloads' is a VB.NET keyword for overloading methods, not a class modifier.
- D. 'suppress' is not a C# keyword.
Tip: In C#, sealed stops inheritance. Use abstract to force inheritance instead.
Question 3Medium
What is the output of the following C# program?
using System;
namespace comp
{
class BaseClass
{
private int p;
protected int x;
public BaseClass()
{
p = 1;
}
public BaseClass(int a)
{
x = a;
}
}
class DerivedClass : BaseClass
{
int y;
public DerivedClass(int b)
{
y = b;
}
public void Calculate()
{
x = x + 1;
y = y + 1;
Console.WriteLine(x + " " + y);
}
}
class Testing
{
static void Main(string[] args)
{
DerivedClass obj = new DerivedClass(1);
obj.Calculate();
}
}
}
- 1 2
- 2 2
- Compilation error
- None of these
Answer: A. 1 2
Explanation
When object obj is created, 0-parametrized base class constructor sets p = 1 then derived class constructor sets value of y to 1. Remember parameterized base class won't be called by default so value of x is not set.
Hence x = x + 1
= 0 + 1
= 1
y = y + 1
= 1 + 1
= 2
Step-by-step solution
- Creating DerivedClass(1) first runs the base class's default constructor, which sets p = 1; p is private and is not used here.
- The derived constructor sets y = b = 1; the parameterised base constructor is not called, so x stays 0.
- Calculate: x = 0 + 1 = 1 and y = 1 + 1 = 2.
- It prints 1 2, so the answer is option A (1 2).
Why the other options are wrong
- B. X starts at 0, not 1, because the base constructor with the int argument is not called.
- C. The code compiles; x is protected, so the derived class can use it.
- D. Option A is correct, so 'none of these' is wrong.
Tip: A base class's default constructor runs automatically; a parameterised one runs only if the derived constructor asks for it.
Question 4Medium
Which form of inheritance is not supported directly by C# .NET?
- Multiple inheritance.
- Multilevel inheritance.
- Single inheritance.
- Hierarchical inheritance
Answer: A. Multiple inheritance.
Explanation
C# does not support multiple inheritance directly. Instead, it uses interfaces to achieve similar functionality.
Step-by-step solution
- C# only allows a class to inherit from one base class.
- This means single inheritance is supported directly.
- Multiple inheritance is when a class inherits from more than one base class.
- C# does not allow this directly, so it's not supported.
- The correct answer is Multiple inheritance.
Why the other options are wrong
- B. Multilevel inheritance is allowed in C# through chaining base classes.
- C. Single inheritance is the default and is fully supported in C#.
- D. Hierarchical inheritance involves multiple derived classes from one base class, which is allowed in C#.
Tip: Remember: C# uses interfaces to simulate multiple inheritance.
Question 5Medium
What will be the total size of the instance fields of the object created by the following C#.NET code snippet (ignoring the object header)?
namespace compConsoleApplication
{
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();
}
}
}
- 24 bytes
- 12 bytes
- 20 bytes
- 10 bytes
Answer: A. 24 bytes
Explanation
In case of inheritance all the members of base class are inherited to derived class including "private" members.
But the fact is that derived class can't access the private members although it have the private members.Hence the size of derived class=base class+derived class itself.
=12bytes (base class)
+12 bytes (derived class)
=24 bytes.
Step-by-step solution
- Baseclass has three int fields: i (private), j (protected) and k (public).
- Each int takes 4 bytes, so Baseclass takes 3 x 4 = 12 bytes.
- Derived inherits all base fields, including the private i. Derived cannot access i, but i still takes memory in the object.
- Derived adds three more int fields: x (private), y (protected) and z (public), which take 3 x 4 = 12 bytes.
- Total size = 12 + 12 = 24 bytes, which is option A.
Why the other options are wrong
- B. 12 bytes counts only one class's fields. Derived also holds the base fields plus its own three ints.
- C. 20 bytes does not match the field total. Six ints at 4 bytes each give 24 bytes.
- D. 10 bytes is not a multiple of 4 and ignores that each int takes 4 bytes.
Tip: Count every instance field along the inheritance chain, including private base fields, since they still take memory. This question counts field data only; real .NET also adds runtime header overhead, which it ignores.
Question 6Medium
In an inheritance chain which of the following members of base class are accessible to the derived class members?
I. Static.
II. Protected.
III. Private.
IV. Shared.
V. public
- I, III.
- II, V.
- III, IV.
- IV, V.
Answer: B. II, V.
Explanation
All the members of class (Except Static) gets inherited in Derived Class. But only Public and Protected are Accessible.
Step-by-step solution
- A derived class can use public and protected members of its base class.
- Private members are not accessible to it.
- So the accessible ones are II and V.
- Option B is correct.
Why the other options are wrong
- A. It includes private (III).
- C. It includes private (III).
- D. It leaves out protected (II).
Tip: Derived classes can see protected and public members, never private ones.
Question 7Medium
In an inheritance chain through which ofgiven following the base class and it s components accessible to derived class?
- Scope resolution operator(:).
- Class visibilty modifiers (public,private etc.).
- Dot operator (.).
- All of the mentioned
Answer: B. Class visibilty modifiers (public,private etc.).
Explanation
Access modifiers such as public, protected and private decide which base class members a derived class can use. The other options are not about inheritance access.
Step-by-step solution
- In inheritance, a derived class can use only the base members that the access modifiers allow.
- Class visibility and access modifiers (public, private and so on) decide what a derived class can see.
- The scope resolution operator and the dot operator do not decide inheritance access.
- So the answer is option B, class visibility modifiers.
Why the other options are wrong
- A. The scope resolution operator does not make base members accessible to a derived class.
- C. The dot operator reaches members of an object; it does not decide inheritance access.
- D. Options A and C are wrong, so 'all of the mentioned' is wrong.
Tip: Access modifiers decide what a derived class can use; private members are not usable from a derived class.
Question 8Medium
A class member declared protected becomes member of subclass of which type?
- public member.
- private member.
- protected member.
- static member
Answer: C. protected member.
Explanation
A protected member of a class remains protected when inherited by a subclass. This means the subclass can access it, but it is not accessible outside the class hierarchy.
Step-by-step solution
- A protected member stays protected when inherited.
- So the subclass can use it, but code outside the class hierarchy cannot.
- Option C, protected member, is the correct answer.
Why the other options are wrong
- A. Public would be open to everyone; protected is not public.
- B. Private members are not accessible to the subclass.
- D. Static is a different keyword, not an access level.
Tip: Inheritance keeps protected as protected; it does not make members public.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →