Search Placementyogi.com
 

C# Inheritance - Advanced Objective Questions

1

Which of the following options shows the output of the C# program given below in correct order?

using System;
namespace PlacementYogi
{
    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("I am zero-parameterized base class constructor.");
        }
        public BaseClass(int a)
        {
            Console.WriteLine("I am parameterized base class constructor.");
        }
    }
    class DerivedClass : BaseClass
    {
        public DerivedClass()
        {
            Console.WriteLine("I am zero-parameterized derived class constructor.");
        }
        public DerivedClass(int b)
        {
            Console.WriteLine("I am parameterized derived class constructor.");
        }
    }
    class Testing
    {
        static void Main(string[] args)
        {
            DerivedClass obj1 = new DerivedClass();
            DerivedClass obj2 = new DerivedClass(1);
        }
    }
}

P: I am zero-parameterized base class constructor.

Q: I am parameterized base class constructor.

R: I am zero-parameterized derived class constructor.

S: I am parameterized derived class constructor.

A.

PQRS

B.

PRPS

C.

PRQS

D.

RSPQ

E.

RPSQ

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Whenever a derived class object is created, base class 0-parameterized constructor is called first and then derived class constructor.

When obj1 is created, base class 0-parameterized constructor is class first then 0-parameterized derived class constructor.

When obj2 is created, base class 0-parameterized constructor is class first then parameterized derived class constructor.

Hence option B is correct.

 
2

What is the output of the following C# program?

using System;
namespace PlacementYogi
{
    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();
        }
    }
}
A.

1 2

B.

2 2

C.

Compilation error

D.

None of these

See Answer & Explanation Lets Discuss
Correct answer is : A
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

 
3

What is the size of the object obj created in class Testing in below C# program?

using System;
namespace PlacementYogi
{
    class BaseClass
    {
        private int a = 1;
        protected int b = 1;
        public int c = 1;
    }
    class DerivedClass : BaseClass
    {
        private int d = 2;
        protected int e = 2;
        public int f = 2;
    }
    class Testing
    {
        public static void Main(string[] args)
        {
            DerivedClass obj = new DerivedClass();           
        }
    } 
}
A.

8 bytes

B.

12 bytes

C.

16 bytes

D.

20 bytes

E.

24 bytes

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Object obj is created in class Testing so it can access only the public members of BaseClass and DerivedClass. Here obj can access only c and f so its size will be 8 bytes.

 
 

Banner to LogicGuns.com

Hide Page Information

C# Inheritance - Advanced Objective Questions and Answers with Explanation

Here you can find advanced level C# inheritance interview questions with answers and explanation.

Why Inheritance in C#?

In this section you can learn and practice different types of advanced level questions with answers on inheritance to improve your skills in order to face the interviews, competitive examinations and various entrance tests with full confidence.

Where can I get C# Inheritance Advanced Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved advanced level inheritance questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Operators quiz questions with answers as PDF files and eBooks.

Where can I get Advanced Level C# Inheritance Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type advanced inheritance questions and answers for interview and entrance examination. Multiple choice and TRUE or FALSE type and YES or NO type questions are also provided.