Users Online

· Guests Online: 56

· 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 ? Constructor Overloading

C# Questions & Answers – Constructor Overloading

 

 

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

 

 

 

 

 

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

  1.  class maths
  2.  {
  3.      public int length;
  4.      public int breadth;
  5.      public maths(int x, int y)
  6.      {
  7.          length = x;
  8.          breadth = y;
  9.          Console.WriteLine(x + y);
  10.      }
  11.      public maths(double x, int y)
  12.      {
  13.          length = (int)x;
  14.          breadth = y;
  15.          Console.WriteLine(x * y);
  16.      }
  17.  }
  18. class Program
  19. {
  20.     static void Main(string[] args)
  21.     {
  22.         maths m = new maths(20, 40);
  23.         maths k = new maths(12.0, 12);
  24.         Console.ReadLine();
  25.     }
  26. }

a) 60, 24
b) 60, 0
c) 60, 144
d) 60, 144.0

Answer: c
Explanation: Matching the values passed as parameters. The respective constructors are overloaded according to the matching parameter type.
Output :

 

         60
        144

 

 

 

 

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

 
  1.  class maths
  2.  {
  3.      public int length;
  4.      public int breadth;
  5.      public  maths(int x)
  6.      {
  7.          length = x + 1;
  8.      }
  9.      public maths(int x, int y)
  10.      {
  11.          length = x + 2;
  12.      }
  13.  }
  14.  class Program
  15.  {
  16.      static void Main(string[] args)
  17.      {
  18.          maths m = new maths(6);
  19.          maths k = new maths(6, 2);
  20.          Console.WriteLine(m.length);
  21.          Console.WriteLine(k.length);
  22.          Console.ReadLine();
  23.      }
  24.  }

a) 8, 8
b) 0, 2
c) 8, 10
d) 7, 8

Answer: d
Explanation: None.

 

 

 

 

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

  1.  class maths
  2.  {
  3.      public maths()
  4.      {
  5.          Console.WriteLine("constructor 1 :");
  6.      }
  7.      public maths(int x)
  8.      {
  9.          int p = 2;
  10.          int u;
  11.          u = p + x;
  12.          Console.WriteLine("constructor 2: " +u);
  13.      }
  14.  }
  15.  class Program
  16.  {
  17.      static void Main(string[] args)
  18.      {
  19.          maths k = new maths(4);
  20.          maths t = new maths();
  21.          Console.ReadLine();
  22.      }
  23.  }

a)

constructor 1:
constructor 2: 6

b)

constructor 2: 6
constructor 2: 6

c)

constructor 2: 6
constructor 1:

d) none of the mentioned

Answer: c
Explanation: None.
Output:

 

        constructor 2: 6
        constructor 1:

 

 

 

 

 

 

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

  1. class maths
  2. {
  3.     int i;
  4.     public maths(int x)
  5.     {
  6.         i = x;
  7.         Console.WriteLine(" hello: ");
  8.     }
  9. }
  10. class maths1 : maths
  11. {
  12.     public  maths1(int x) :base(x)
  13.     {
  14.         Console.WriteLine("bye");
  15.     }
  16. }
  17. class Program
  18. {
  19.     static void Main(string[] args)
  20.     {
  21.         maths1 k = new maths1(12);
  22.         Console.ReadLine();
  23.     }
  24. }

a) hello bye
b) 12 hello
c) bye 12
d) Compile time error

Answer: a
Explanation: None.

 

 

 

 

 

 

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

  1.  class maths
  2.  {
  3.      int i;
  4.      public maths(int ii)
  5.      {
  6.          ii = 12;
  7.          int j = 12;
  8.          int r = ii * j;
  9.          Console.WriteLine(r);
  10.      }
  11.  }
  12.  class maths1 : maths
  13.  {
  14.      public maths1(int u) :base(u)
  15.      {
  16.          u = 13;
  17.          int h = 13;
  18.          Console.WriteLine(u + h);
  19.      }
  20.  }
  21.  class maths2 : maths1
  22.  {
  23.      public maths2(int k) :base(k)
  24.      {
  25.          k = 24;
  26.          int o = 6 ;
  27.          Console.WriteLine(k /o);
  28.      }
  29.  }
  30.  class Program
  31.  {
  32.      static void Main(string[] args)
  33.      {
  34.          maths2 t = new maths2(10);
  35.          Console.ReadLine();
  36.      }
  37.  }

a) 4, 26, 144
b) 26, 4, 144
c) 144, 26, 4
d) 0, 0, 0

Answer: c
Explanation: None.

 

 

 

6. Which keyword is used to refer baseclass constructor to subclass constructor?
a) This
b) Static
c) Base
d) Extend

Answer: c
Explanation: None.

 

 

 

7. When we call a constructor method among different given constructors. We match the suitable constructor by matching the name of constructor first, then the number and then the type of parameters to decide which constructor is to be overloaded. The process is also known as?
a) Method overriding
b) Inheritance
c) Polymorphism
d) Encapsulation

Answer: c
Explanation: None.

 

 

 

 

 

8. Correct statement about constructor overloading in C# is?
a) Overloaded constructors have the same name as the class
b) Overloaded constructors cannot use optional arguments
c) Overloaded constructors can have different type of number of arguments as well as differ in number of arguments
d) All of the mentioned

Answer: c
Explanation: By definition of overloaded constructors.

 

 

 

 

 

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

  1.   class maths
  2.   {
  3.       static maths()
  4.       {
  5.           int s = 8;
  6.           Console.WriteLine(s);
  7.       }
  8.       public  maths(int f)
  9.       {
  10.           int h = 10;
  11.           Console.WriteLine(h);
  12.       }
  13.   }
  14.   class Program
  15.   {
  16.       static void Main(string[] args)
  17.       {
  18.           maths p = new maths(0);
  19.           Console.ReadLine();
  20.       }
  21.   }

a) 10, 10
b) 0, 10
c) 8, 10
d) 8, 8

Answer: c
Explanation: None.
Output:

 

 8, 10

 

 

 

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

  1. class maths
  2. {
  3.     int i;
  4.     public  maths(int ii)
  5.     {
  6.         ii = -25;
  7.         int g;
  8.         g = ii > 0 ? ii : ii * -1;
  9.         Console.WriteLine(g);
  10.     }
  11. }
  12. class maths1 :maths
  13. {
  14.     public  maths1(int ll) :base(ll)
  15.     {
  16.         ll = -1000;
  17.         Console.WriteLine((ll > 0 ? ll : ll * -1));
  18.     }
  19. }
  20. class Program
  21. {
  22.     static void Main(string[] args)
  23.     {
  24.         maths1 p = new maths1(6);
  25.         Console.ReadLine();
  26.     }
  27. }

a)

-25
-1000

b)

-1000
-25

c)

25
1000

d) None of the mentioned

Answer: c
Explanation: None.
Output :

 

 25, 1000

 

 

 

 

 

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.69 seconds
10,843,820 unique visits