C# Questions & Answers – Method Overloading

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

1. The process of defining two or more methods within the same class that have same name but different parameters list?
a) Method overloading
b) Method overriding
c) Encapsulation
d) None of the mentioned
View Answer

Answer: a
Explanation: Two or more methods can have same name as long as their parameters declaration and definitions are different, the methods are said to be overloaded and the process is called method overloading. Method overloading is used when methods are required to perform similar tasks using different input parameters.

2. Which of these can be overloaded?
a) Constructors
b) Methods
c) Both Constructors & Methods
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          Console.WriteLine( vol(10));
  6.          Console.WriteLine( vol(2.5f,  5));
  7.          Console.WriteLine( vol( 5l,  4,  5));
  8.          Console.ReadLine();
  9.      }
  10.      static int vol(int x)
  11.      {
  12.          return(x * x * x);
  13.      }
  14.      static float vol(float r,  int h)
  15.      {
  16.          return(3.14f * r * r * h);
  17.      }
  18.      static long vol(long l, int b, int h)
  19.      {
  20.          return(l * b * h);
  21.      }
  22.  }

a)

   1000
   0
   100

b)

advertisement
advertisement
   0
   0
   100

c) compile time error
d)

   1000
   98.125
   100
View Answer
Answer: d
Explanation: The concept of method overloading is implemented in method “vol” with same name but different definitions and parameter list which is overloaded three times and each time the return type is different for each method and hence matches the method using types of parameters.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
        1000
        98.125
        100
 
 

advertisement

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

  1.   class overload
  2.   {
  3.       public int x;
  4.       int y;
  5.       public int add(int a)
  6.       {
  7.           x = a + 1;
  8.           return x;
  9.       }
  10.       public int add(int a, int b)
  11.       {
  12.           x = a + 2;
  13.           return x;
  14.       }
  15.   }    
  16.   class Program
  17.   {
  18.       static void Main(string[] args)
  19.       {
  20.           overload obj = new overload();
  21.           overload obj1 = new overload();
  22.           int a = 0;
  23.           obj.add(6);
  24.           obj1.add(6, 2);
  25.           Console.WriteLine(obj.x);
  26.           Console.WriteLine(obj1.x);
  27.           Console.ReadLine();
  28.       }
  29.   }

a)

advertisement
   8
   8

b)

   0
   2

c)

   8
   10

d)

   7
   8
View Answer
Answer: d
Explanation: None.
Output:

 7, 8
 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 5;
  4.      int j  = 6;
  5.      add(ref i);
  6.      add(6);
  7.      Console.WriteLine(i);
  8.      Console.ReadLine();
  9.  }
  10.  static void add(ref int x)
  11.  {
  12.      x = x * x;
  13.  }
  14.  static void add(int x)
  15.  {
  16.      Console.WriteLine(x * x * x);
  17.  }

a) Compile time error
b)

   25
   0

c)

   216
   0

d)

   216
   25
View Answer
Answer: d
Explanation: None.
Output:

         216
         25
 
 

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

  1.  class maths
  2.  {
  3.      public int x;
  4.      public double y;
  5.      public int add(int a, int b)
  6.      {
  7.          x = a + b;
  8.          return x;
  9.      }
  10.      public int add(double c, double d)
  11.      {
  12.          y = c + d;
  13.          return (int)y;
  14.      }
  15.      public maths()
  16.      {
  17.          this.x = 0;
  18.          this.y = 0;
  19.      }
  20.  }    
  21. class Program
  22. {
  23.     static void Main(string[] args)
  24.     {
  25.         maths obj = new maths();
  26.         int a = 4;
  27.         double b = 3.5;
  28.         obj.add(a, a);
  29.         obj.add(b, b);
  30.         Console.WriteLine(obj.x + " " + obj.y);
  31.         Console.ReadLine();
  32.     }
  33. }

a) 4, 3.5
b) 8, 0
c) 7.5, 8
d) 8, 7
View Answer

Answer: d
Explanation: None
Output:

8, 7

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

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

a)

   method 1:
   method 2:
   20
   method 1:

b)

   method 2:
   20
   method 1:
   method 1:

c)

   method 1:
   0
   method 2:
   method 2:

d)

   method 1:
   20
   method 1:
   method 2:
View Answer
Answer: d
Explanation: None.
Output :

         method 1:
         20
         method 1:
         method 2:

8. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
View Answer

Answer: d
Explanation: None.

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

  1.  class maths
  2.  {
  3.      public int fun1(int k)
  4.      {
  5.          k = 20;
  6.          return k;
  7.      }
  8.      public Single fun1(float t)
  9.      {
  10.          t = 3.4f;
  11.          return t;
  12.      }
  13.  }  
  14.  class Program
  15.  {
  16.      static void Main(string[] args)
  17.      {
  18.          maths obj = new maths();
  19.          int i;
  20.          i = obj.fun1(30);
  21.          Console.WriteLine(i);
  22.          Single j;
  23.          j = obj.fun1(2.5f);
  24.          Console.WriteLine(j);
  25.          Console.ReadLine();
  26.      }
  27.  }

a)

   30
   2.5f

b)

   2.5f
   30

c)

   20
   2.5f

d)

   20
   3.4f
View Answer
Answer: d
Explanation: None.
Output:

        20
        3.4f
 
 

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

  1.  class maths
  2.  {
  3.      public int fun(int k, int y)
  4.      {
  5.          return k + y;
  6.      }
  7.      public int fun1(int t, float z)
  8.      {
  9.          return (t+(int)z);
  10.      }
  11.  }    
  12.  class Program
  13.  {
  14.      static void Main(string[] args)
  15.      {
  16.          maths obj = new maths();
  17.          int i;
  18.          int b = 90;
  19.          int c = 100;
  20.          int d = 12;
  21.          float l = 14.78f;
  22.          i = obj.fun(b, c);
  23.          Console.WriteLine(i);
  24.          int j = (obj.fun1(d,  l));
  25.          Console.WriteLine(j);
  26.          Console.ReadLine();
  27.      }
  28.  }

a) 190, 26.78f
b) 0, 26.78f
c) 190, 26
d) 190, 0
View Answer

Answer: c
Explanation: None.
Output:

         190
         26

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

  1.  class maths
  2.  {
  3.      public int fun(int k, int y, int n)
  4.      {
  5.          Console.WriteLine(k + "  " + y + "  " + n);
  6.          return (k);
  7.      }
  8.      public int fun1(int t,float z)
  9.      {
  10.          Console.WriteLine(t + "  " + z);
  11.          return t;
  12.      }
  13.  }    
  14.  class Program
  15.  {
  16.      static void Main(string[] args)
  17.      {
  18.          maths obj = new maths();
  19.          int b = 90;
  20.          int c = 100;
  21.          int d ;
  22.          float l;
  23.          int i = obj.fun(b, c, 12);
  24.          int j = (obj.fun1(12, 14.78f));
  25.          Console.ReadLine();
  26.      }
  27.  }

a)

   0, 0, 0
   12, 14.78

b)

   0, 0, 0
   0, 0

c)

   90, 100, 12
   12, 14

d)

   90, 100, 12
   12, 14.78
View Answer
Answer: d
Explanation: None.
Output:

        90,  100,  12
        12,  14.78

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

  1.  class maths
  2.  {
  3.      public int fun(int ii)
  4.      {
  5.          return(ii > 0 ? ii :ii * -1);
  6.      }
  7.      public long fun(long ll)
  8.      {
  9.          return(ll > 0 ? ll :ll * -1);
  10.      }
  11.      public double fun( double dd)
  12.      {
  13.          return(dd > 0 ? dd :dd * -1);
  14.      }
  15.  }    
  16.  class Program
  17.  {
  18.      static void Main(string[] args)
  19.      {
  20.          maths obj = new maths();
  21.          int i = -25;
  22.          int j ;
  23.          long l = -100000l ;
  24.          long m;
  25.          double d = -12.34;
  26.          double e;
  27.          j = obj.fun(i);
  28.          m = obj.fun(l);
  29.          e = obj.fun(d);
  30.          Console.WriteLine(j + "  " + m + "  " + e);
  31.          Console.ReadLine();
  32.      }
  33.  }

a) 1 1 1
b) 0 0 0
c) 25 100000 12.34
d) -25 -100000 -12.34
View Answer

Answer: c
Explanation: None.
Output:

 25 100000 12.34

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.