Classes Questions with Answers

8 practice questions on Classes (C#) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

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();
    }
}
  1. 10, 20, 30 base method.
  2. 10, 20, 0.
  3. compile time error.
  4. 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

  1. I and j are private fields of sample, because class members are private by default.
  2. Sample1 inherits the method, but it cannot access private fields of its base class.
  3. obj.i and obj.j in Main are not accessible, so a compile-time error occurs.
  4. 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(){.....}
  1. Yes
  2. 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

  1. Method overloading in C# requires different parameters.
  2. Both methods have same name and same parameter types.
  3. Only return type is different, which is not allowed.
  4. So DrawCircle() cannot be overloaded as shown.
  5. 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?

  1. Only one
  2. Only two
  3. Any number
  4. 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

  1. A C# class can have multiple constructors.
  2. Each constructor must have the same name as the class.
  3. Constructors can differ in parameters or types.
  4. C# allows any number of constructors.
  5. 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?

  1. :
  2. &
  3. ::
  4. 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

  1. In C#, a class inherits from another using a colon after the class name.
  2. Example: class Derived: Base.
  3. 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();
        }
    }
}
  1. 2, 10,12.
  2. 0, 10,10.
  3. 2, 0, 2.
  4. 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

  1. Obj has type A and holds a B object, so obj.i = 2 sets i to 2.
  2. obj1.j = 10 sets j on a different object, obj1, not on obj.
  3. obj.display() runs B's override: sum = i + j = 2 + 0 = 2, because obj.j is 0.
  4. 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();
    }
}
  1. I am a base class, I am a child class.
  2. I am a child class,I am a base class.
  3. compile time error.
  4. 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

  1. Creating a ChildClass object runs its constructor, which first calls the base constructor.
  2. BaseClass prints 'I am a base class' first.
  3. Then ChildClass prints 'I am a child class'.
  4. 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();

  1. I,III.
  2. II,IV.
  3. I,II,III.
  4. 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

  1. Statement I declares and creates the object in one line, which is valid.
  2. Statement III declares the variable and then assigns a new object, which is also valid.
  3. Statement II only declares a variable and creates no object.
  4. Statement IV uses a variable that was never declared, so it is not valid.
  5. 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.

  1. I,III,V.
  2. I,IV.
  3. II,IV,V.
  4. 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

  1. Class data members are private by default, so statement II is true and I is false.
  2. Member functions are private by default, so V is true and III is false.
  3. A private method can call a public method in the same class, so IV is true.
  4. 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 →

Related topics

See all C# topics · All topics