Sample questions
Question 1Medium
The following set of code 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();
}
}
- 10, 20, 30 base method.
- 10, 20, 0.
- compile time error.
- base method
Answer: C. compile time error.
Explanation
The code gives a compile time error. The fields i and j are private by default, so Program cannot read obj.i or obj.j. Only obj.s is public and visible. Making i and j public would fix the error.
Step-by-step solution
- I and j are private fields of sample, because class members are private by default.
- Sample1 inherits the method, but it cannot access private fields of its base class.
- obj.i and obj.j in Main are not accessible, so a compile-time error occurs.
- So the correct answer, compile time error, is correct.
Why the other options are wrong
- A. obj.i and obj.j are private, so the code does not compile.
- B. The code does not compile, so no output is printed.
- D. The code does not compile, so the base method is never run.
Tip: Private members are hidden from derived classes and outside code; use protected or public when needed.
Question 2Medium
Can the method DrawCircle() be overloaded in the following ways in C#?
public int DrawCircle(){.....}
public float DrawCircle(){.....}
- Yes
- No
Answer: B. No
Explanation
C# provides feature of method overloading that means methods with same name but different types and arguments. Only return type does not decidethe method overloading.
Step-by-step solution
- Method overloading in C# requires different parameters.
- Both methods have same name and same parameter types.
- Only return type is different, which is not allowed.
- So DrawCircle() cannot be overloaded as shown.
- Final answer: B (No).
Why the other options are wrong
- A. Overloading allows different parameters but same name, but return type alone isn't enough.
Tip: Remember: Overloading in C# is based on parameters, not return type.
Question 3Medium
How many constructors a C# class can define?
- Only one
- Only two
- Any number
- None.
Answer: C. Any number
Explanation
A constructor looks like a method which has the same name as the class and used to create instance (object) of a class. C# class can define any number of constructors. Every class contains a default constructor initially which does not take any arguments but this default constructor is removed when any custom constructor (defined by programmer)is defined externally.
Step-by-step solution
- A C# class can have multiple constructors.
- Each constructor must have the same name as the class.
- Constructors can differ in parameters or types.
- C# allows any number of constructors.
- Final answer: Any number.
Why the other options are wrong
- A. Only one constructor is not allowed in C#.
- B. Two constructors are allowed but not the only option.
- D. A class must have at least one constructor.
Tip: Use parameterized constructors for different initialization scenarios.
Question 4Medium
Which of these operators must be used to inherit a class?
- :
- &
- ::
- Extends
Answer: A. :
Explanation
A class can inherit using a colon .you can extend the functionality of an existing class by creating a new class that derives from the existing class. The derived class inherits the properties of the base class.
For Example
class a
{
}
class b: a
{
}
Step-by-step solution
- In C#, a class inherits from another using a colon after the class name.
- Example: class Derived: Base.
- So option A is correct.
Why the other options are wrong
- B. & is a bitwise AND operator.
- C. The double colon is a scope or alias qualifier in C#.
- D. Extends is the Java keyword, not C#.
Tip: C# uses a colon for inheritance; Java uses extends.
Question 5Medium
What will be the output for given set of code?
namespace ConsoleApplication
{
abstract class A
{
public int i;
public abstract void display();
}
class B: A
{
public int j;
public int sum;
public override void display()
{
sum = i + j;
Console.WriteLine(+i + "\n" + +j);
Console.WriteLine("sum is:" +sum);
}
}
class Program
{
static void Main(string[] args)
{
A obj = new B();
obj.i = 2;
B obj1 = new B();
obj1.j = 10;
obj.display();
Console.ReadLine();
}
}
}
- 2, 10,12.
- 0, 10,10.
- 2, 0, 2.
- 0, 0,0.
Answer: C. 2, 0, 2.
Explanation
Abstract method implementation processed in subclass also the object obj of abstract class A initialize value of i as 2.
The object of class B also initialize value of j as 10.
Since, the method display()
is called using object of class A which is obj and hence i = 2
whereas j = 0
So, sum = 2.
Output: 2 0
sum is: 2.
Step-by-step solution
- Obj has type A and holds a B object, so obj.i = 2 sets i to 2.
- obj1.j = 10 sets j on a different object, obj1, not on obj.
- obj.display() runs B's override: sum = i + j = 2 + 0 = 2, because obj.j is 0.
- It prints 2 and 0 on separate lines, then sum is:2, so the answer is option C (2, 0, 2).
Why the other options are wrong
- A. J is 0 for obj, not 10, and sum is 2, not 12.
- B. I is 2, not 0, because obj.i was set to 2.
- D. I is 2 and sum is 2, so the output is not all zeros.
Tip: With a base-class variable, the object's own fields are used; a field set on another object does not change it.
Question 6Medium
What is output 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();
}
}
- I am a base class, I am a child class.
- I am a child class,I am a base class.
- compile time error.
- None of the mentioned
Answer: A. I am a base class, I am a child class.
Explanation
This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructorexecuted before the ChildClass constructor. Outpu t: I am a base class,I am a child class
Step-by-step solution
- Creating a ChildClass object runs its constructor, which first calls the base constructor.
- BaseClass prints 'I am a base class' first.
- Then ChildClass prints 'I am a child class'.
- So the marked output, base class then child class, is correct.
Why the other options are wrong
- B. The base constructor runs first, so the order given here is reversed.
- C. The code compiles; the constructors are valid.
- D. Option A is the correct output, so None of the mentioned is wrong.
Tip: Base class constructors always run before derived class constructors.
Question 7Medium
Which of the following is the correct way to create an object of the class Sample?
- I. Sample s = new Sample();
- II. Sample s;
- III. Sample s; s = new Sample();
- IV. s = new Sample();
- I,III.
- II,IV.
- I,II,III.
- IV,V.
Answer: A. I,III.
Explanation
We are having 2 creation of object.It describe 1 is default constructor and thanparameterised constructor. We can declare two methods to accept each object.
Step-by-step solution
- Statement I declares and creates the object in one line, which is valid.
- Statement III declares the variable and then assigns a new object, which is also valid.
- Statement II only declares a variable and creates no object.
- Statement IV uses a variable that was never declared, so it is not valid.
- So the correct answer, I and III, is correct.
Why the other options are wrong
- B. II only declares a variable and IV has no declaration, so II and IV is wrong.
- C. II creates no object, so I, II and III is wrong.
- D. IV is not valid without a declaration, and V is not in the list.
Tip: Creating an object needs new; a bare declaration creates nothing.
Question 8Medium
Which of the following statements are correct?
I. Data members of a class are by default public.
II. Data members of a class are by default private.
III. Member functions of a class are by default public.
IV. A private function of a class can access a public function within the same class.
V. Member functions of a class are by default private.
- I,III,V.
- I,IV.
- II,IV,V.
- I,II,III.
Answer: C. II,IV,V.
Explanation
Access specifiers specify the access rules for the members as well as the class itself, if not mentioned then the default access specifier for a class type is internal. Default access for the members is private
Step-by-step solution
- Class data members are private by default, so statement II is true and I is false.
- Member functions are private by default, so V is true and III is false.
- A private method can call a public method in the same class, so IV is true.
- So the correct answer, II, IV and V, is correct.
Why the other options are wrong
- A. I and III are false: members default to private, not public.
- B. I is false because class data members are private by default.
- D. I and III are false, so I, II and III is wrong.
Tip: Class members default to private unless an access modifier is written.
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 →