Users Online

· Guests Online: 54

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Questions & Answers ? Inheritance Implementation

C# Questions & Answers – Inheritance Implementation

 

 

This section of our 1000+ C# multiple choice questions focuses on implementation of inheritance in C# Programming Language.

 

 

 

 

 

1. What will be the output of the following C# code?

  1.  class sample
  2.  {
  3.      public  int i;
  4.      void display() 
  5.      {
  6.          Console.WriteLine(i);
  7.      }
  8. }    
  9. class sample1 : sample 
  10. {
  11.     public  int j;
  12.     public void display() 
  13.     {
  14.         Console.WriteLine(j);
  15.     }
  16. }    
  17. class Program
  18. {
  19.     static void Main(string[] args)
  20.     {
  21.         sample1 obj = new sample1();
  22.         obj.i = 1;
  23.         obj.j = 2;
  24.         obj.display();
  25.         Console.ReadLine();
  26.     }
  27. }

a) 1
b) 3
c) 2
d) Compile Time error

Answer: c
Explanation: class sample & class sample1 both contain display() method, class sample1 inherits class sample, when display() method is called by object of class sample 1, display() method of class sample 1 is executed rather than that of Class sample.
 
2. What will be the Correct statement in the following C# code?
  1. class sample
  2. {
  3.     protected int index;
  4.     public sample()
  5.     {
  6.         index = 0;
  7.     }
  8. }
  9. class sample 1: sample
  10. {
  11.     public void add()
  12.     {
  13.         index += 1;
  14.     }
  15. }
  16. class Program
  17. {
  18.     static void Main(string[] args)
  19.     {
  20.         sample 1 z = new sample 1();
  21.         z . add();
  22.     }
  23. }

a) Index should be declared as protected if it is to become available in inheritance chain
b) Constructor of sample class does not get inherited in sample 1 class
c) During constructing an object referred to by z, Firstly constructor of sample class will be called followed by constructor of sample 1 class
d) All of the mentioned

Answer: d
Explanation: None.
 

 

 

3. The following C# code is run on single level of inheritance. What will be the Correct statement in the following C# code?

  1.  class sample
  2.  {
  3.      int i = 10;
  4.      int j = 20;
  5.      public void display() 
  6.      {
  7.          Console.WriteLine("base method ");
  8.      }
  9.  }    
  10.  class sample1 : sample 
  11.  {
  12.      public  int s = 30;
  13.  }    
  14.  class Program
  15.  {
  16.      static void Main(string[] args)
  17.      {
  18.          sample1 obj = new sample1();
  19.          Console.WriteLine("{0}, {1}, {2}", obj.i,  obj.j,  obj.s);
  20.          obj.display();
  21.          Console.ReadLine();
  22.      }
  23.  }

a)

10, 20, 30
base method

b) 10, 20, 0
c) compile time error
d) base method

Answer: c
Explanation: ‘i’ and ‘ j’ are inaccessible due to protection level. Declare them as public variable and hence will be accessed in code.

 

 

 

 

 

4. What will be size of the object created depicted by C# code snippet?

  1.  class baseclass
  2.  {
  3.      private int a;
  4.      protected int b;
  5.      public int c;
  6.  }
  7.  class derived : baseclass
  8.  {
  9.      private int x;
  10.      protected int y;
  11.      public int z;
  12.  }
  13.  class Program
  14.  {
  15.      static Void Main(string[] args)
  16.      {
  17.          derived a = new derived();
  18.      }
  19.  }

a) 20 bytes
b) 12 bytes
c) 16 bytes
d) 24 bytes

Answer: d
Explanation: Explained in fundamentals of inheritance.

 

 

 

 

5. What will be the output of the following C# code?

  1. class sample
  2. {
  3.     public sample()
  4.     {
  5.         Console.WriteLine("THIS IS BASE CLASS constructor");
  6.     }
  7.  }    
  8. public class sample1 : sample 
  9. {
  10.  
  11. }    
  12. class Program
  13. {
  14.     static void Main(string[] args)
  15.     {
  16.         sample1 obj = new sample1();
  17.         Console.ReadLine();
  18.     }
  19. }

a) Code executes successfully prints nothing
b) This is base class constructor
c) Compile time error
d) None of the mentioned

Answer: c
Explanation: Base class accessibility level is much less compared to derived class. Declare it public to get desired output.

 

 

 

 

 

6. Select the statement which should be added to the current C# code to get the output as 10 20?

  1. class baseclass
  2. {
  3.     protected int a = 20;
  4. }
  5. class derived : baseclass
  6. {
  7.     int a = 10;
  8.     public void math()
  9.     {
  10.          /* add code here */
  11.     }   
  12. }

a) Console.writeline( a + ” ” + this.a);
b) Console.Writeline( mybase.a + ” ” + a);
c) console.writeline(a + ” ” + base.a);
d) console.writeline(base.a + ” ” + a);

Answer: c
Explanation: None.

 

 

 

 

 

 

7. What will be the Correct statement in the following C# code?

  1.  class baseclass
  2.  {
  3.      int a;
  4.      public baseclass(int a1) 
  5.      {
  6.          a = a1;
  7.          console.writeline(" a ");
  8.      }
  9.      class derivedclass : baseclass
  10.      {
  11.          public derivedclass (int a1) : base(a1)
  12.          {
  13.              console.writeline(" b ");
  14.          }
  15.      }
  16.      class program
  17.      {
  18.          static void main(string[] args)
  19.          {
  20.              derivedclass d =  new derivedclass(20);
  21.          }
  22.      }
  23.  }

a) Compile time error
b)

b
a

c)

a
b

d) The program will work correctly if we replace base(a1) with base.baseclass(a1)

Answer: c
Explanation: None.
Output :

 

         a
         b

 

 

8. Which C# statement should be added in function a() of class y to get output “i love csharp”?

  1.  class x
  2.  {
  3.      public void a()
  4.      {
  5.          console.write("bye");
  6.      }
  7.  }
  8.  class y : x
  9.  {
  10.      public void a()
  11.      {
  12.      /* add statement here */
  13.          console.writeline("  i love csharp ");
  14.      }
  15.  }
  16.  class program
  17.  {
  18.      static void main(string[] args)
  19.      {
  20.          y obj =  new obj();
  21.          obj.a();
  22.      }
  23.  }

a) x.a();
b) a();
c) base.a();
d) x::a();

Answer: c
Explanation: None.

 

 

 

 

 

9. Which statements are correct?
a) If a base class consists of a member function fun() and a derived class do not have any function with this name. An object of derived class can access fun()
b) A class D can be derived from class C, which is derived from class B which in turn is derived from class A
c) If a base class and a derived class each include a member function with same name, the member function of the derived class will be called by object of derived class
d) All of the mentioned

Answer: d
Explanation: None.

 

 

 

 

10. What will be the output of the following C# code?

  1. class A 
  2. {
  3.     public int i;
  4.     protected int j;
  5. }    
  6. class B : A 
  7. {
  8.     public int j;
  9.     public void display() 
  10.     {
  11.         base.j = 3;
  12.         Console.WriteLine(i + " " + j);
  13.     }
  14. }    
  15. class Program
  16. {
  17.     static void Main(string[] args)
  18.     {
  19.         B obj = new B();
  20.         obj.i = 1;
  21.         obj.j = 2;
  22.         obj.display();     
  23.         Console.ReadLine();
  24.     }
  25. }

a) 2 1
b) 1 0
c) 0 2
d) 1 2

Answer: d
Explanation: Both class A & B have members with same name that is j, member of class B will be called by default if no specifier is used. i contains 1 & j contains 2, printing 1 2.
Output:

 

1, 2

 

 

 

 

11. What will be the output of the following C# code?

  1.  class A
  2.  {
  3.      public int i;
  4.      private int j;
  5.  }     
  6.  class B :A 
  7.  {
  8.      void display() 
  9.      {
  10.          base.j = base.i + 1;
  11.          Console.WriteLine(base.i + "  " + base.j);
  12.      }
  13. }    
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         B obj = new B();
  19.         obj.i = 1;
  20.         obj.j = 2;
  21.         obj.display();     
  22.         Console.ReadLine();
  23.     }
  24. }

a) 1, 3
b) 2, 3
c) 1, 2
d) compile time error

Answer: d
Explanation: Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.

 

 

 

12. Which of these keywords is used to refer to member of base class from a sub class?
a) upper
b) base
c) this
d) none of the mentioned

Answer: b
Explanation: Whenever a subclass needs to refer to its immediate super class, it can do so by use of the keyword base.

 

 

 

13. Which of these operators must be used to inherit a class?
a) :
b) &
c) ::
d) extends

Answer: a
Explanation:

 

             class a
             {

             }
             class b : a
             {
              
             }

 

 

 

 

 

 

14. What will be the output of the following C# code?

  1.  using System;
  2.  public class BaseClass
  3.  {
  4.      public BaseClass()
  5.      {
  6.          Console.WriteLine("I am a base class");
  7.      }
  8.  }
  9.  public class ChildClass : BaseClass
  10.  {
  11.      public ChildClass()
  12.      {
  13.          Console.WriteLine ("I am a child class");
  14.      }
  15.  static void Main()
  16.  {
  17.      ChildClass CC = new ChildClass();
  18.  }
  19.  }

a)

I am a base class
I am a child class

b)

I am a child class
I am a base class

c) Compile time error
d) None of the mentioned

Answer: a
Explanation: This is because base classes are automatically instantiated before derived classes. Notice the output, The BaseClass constructor is executed before the ChildClass constructor.
Output: I am a base class
I am a child class

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.68 seconds
10,843,015 unique visits