C# Questions & Answers – Constructors in Class

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

1. Number of constructors a class can define is?
a) 1
b) 2
c) Any number
d) None of the mentioned
View Answer

Answer: c
Explanation: A constructor is a simple method which has the same name as the class and hence used to create object of a class. C# class can define any number of constructors. Every class contains a default constructor.

2. The correct way of defining constructor of the given class as and when objects of classes are created is:

   maths s1 = new maths();
   maths s2 = new maths(5, 5.4f);

a)

advertisement
advertisement
   public maths(int pp, single tt)
   {
       p = pp;
       t = tt;
   }

b) sample s;
c)

   public sample()
   {
      p = 0;
      t = 0.0f;
   }
  public sample(int pp, single tt)
  {
       p = pp;
       t = tt;
  }

d) s = new sample();
View Answer

Answer: a
Explanation: None.
advertisement

3. Correct way to define object of sample class in which C# code will work correctly is:

advertisement
  1.  class abc
  2.  {
  3.      int i;
  4.      float k;
  5.      public abc(int ii, float kk)
  6.      {  
  7.          i = ii;
  8.          k = kk;
  9.      }
  10.  }

a) abc s1 = new abc(1);
b) abc s1 = new abc();
c) abc s2 = new abc(1.4f);
d) abc s2 = new abc(1, 1.4f);
View Answer

Answer: d
Explanation: Return types of parameters of object of class matches with defined constructor arguments types.

4. Correct statement about constructors in C#.NET is?
a) Constructors can be overloaded
b) Constructors are never called explicitly
c) Constructors have same name as name of the class
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

5. Which among the following is the correct statement: Constructors are used to?
a) initialize the objects
b) construct the data members
c) initialize the objects & construct the data members
d) none of the mentioned
View Answer

Answer: a
Explanation: Once the object is declared means, the constructor is also declared by default.

6. Can the method add() be overloaded in the following ways in C#?

public int add()  {    }
public float add(){    }

a) True
b) False
View Answer

Answer: b
Explanation: C# provides the feature of method overloading which means methods with same name but different types and arguments.

7. Which of the following statements is correct about constructors in C#.NET?
a) A constructor cannot be declared as private
b) A constructor cannot be overloaded
c) A constructor can be a static constructor
d) None of the mentioned
View Answer

Answer: c
Explanation: Static constructor is a constructor which can be called before any object of class is created or any static method is invoked. A static constructor is implicitly called by .net CLR.

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

  1.  class abc
  2.  {
  3.      public static void a()
  4.      {
  5.          console.writeline("first method");
  6.      }
  7.      public void b()
  8.      {
  9.          a();
  10.          console.writeline("second method");
  11.      }
  12.      public void b(int i)
  13.      {
  14.          console.writeline(i);
  15.          b();
  16.      }
  17.  }
  18.  class program
  19.  {
  20.      static void main()
  21.      {
  22.          abc k = new abc();
  23.          abc.a();
  24.          k.b(20);
  25.      }
  26.  }

a)

   second method
   20
   second method
   first method

b)

   first method
   20
   first method
   second method

c)

   first method
   20

d)

   second method
   20
   first method
View Answer
Answer: b
Explanation: The class ‘abc’ first calls method()’a’ then object of class ‘k’ when calls method ‘b’. First of all, the method ‘a’ will be executed and then executes the second statement.
Output :

        first method
        20
        first method
        second method
 
 

9. What is the return type of constructors?
a) int
b) float
c) void
d) none of the mentioned
View Answer

Answer: d
Explanation: Constructors do not have any return type not even void included in it.

10. Which method has the same name as that of its class?
a) delete
b) class
c) constructor
d) none of the mentioned
View Answer

Answer: c
Explanation: By definition.

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.