My Report

C# Overloading Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

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

 
 class maths
 {
     public static void fun1()
     {
         Console.WriteLine("method 1 :");
     }
     public void fun2()
     {
         fun1();
         Console.WriteLine("method 2 :");
     }
     public void fun2(int k)
     {
         Console.WriteLine(k);
         fun2();
     }
 }    
 class Program
 {
     static void Main(string[] args)
     {
         maths obj = new maths();
         maths.fun1();
         obj.fun2(20);
         Console.ReadLine();
     }
 }

2. What is the process of defining a method in terms of itself, that is a method that calls itself?

3. Which of these can be overloaded?

4. The process of defining two or more methods within the same class that have same name but different parameters list?

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

 
 class maths
 {
     public int x;
     public double y;
     public int add(int a, int b)
     {
         x = a + b;
         return x;
     }
     public int add(double c, double d)
     {
         y = c + d;
         return (int)y;
     }
     public maths()
     {
         this.x = 0;
         this.y = 0;
     }
 }    
class Program
{
    static void Main(string[] args)
    {
        maths obj = new maths();
        int a = 4;
        double b = 3.5;
        obj.add(a, a);
        obj.add(b, b);
        Console.WriteLine(obj.x + " " + obj.y);
        Console.ReadLine();
    }
}

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

 
  class overload
  {
      public int x;
      int y;
      public int add(int a)
      {
          x = a + 1;
          return x;
      }
      public int add(int a, int b)
      {
          x = a + 2;
          return x;
      }
  }    
  class Program
  {
      static void Main(string[] args)
      {
          overload obj = new overload();
          overload obj1 = new overload();
          int a = 0;
          obj.add(6);
          obj1.add(6, 2);
          Console.WriteLine(obj.x);
          Console.WriteLine(obj1.x);
          Console.ReadLine();
      }
  }

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

 class maths
 {
     public int fun(int ii)
     {
         return(ii > 0 ? ii :ii * -1);
     }
     public long fun(long ll)
     {
         return(ll > 0 ? ll :ll * -1);
     }
     public double fun( double dd)
     {
         return(dd > 0 ? dd :dd * -1);
     }
 }    
 class Program
 {
     static void Main(string[] args)
     {
         maths obj = new maths();
         int i = -25;
         int j ;
         long l = -100000l ;
         long m;
         double d = -12.34;
         double e;
         j = obj.fun(i);
         m = obj.fun(l);
         e = obj.fun(d);
         Console.WriteLine(j + "  " + m + "  " + e);
         Console.ReadLine();
     }
 }

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

 
 class maths
 {
     public int fun(int k, int y)
     {
         return k + y;
     }
     public int fun1(int t, float z)
     {
         return (t+(int)z);
     }
 }    
 class Program
 {
     static void Main(string[] args)
     {
         maths obj = new maths();
         int i;
         int b = 90;
         int c = 100;
         int d = 12;
         float l = 14.78f;
         i = obj.fun(b, c);
         Console.WriteLine(i);
         int j = (obj.fun1(d,  l));
         Console.WriteLine(j);
         Console.ReadLine();
     }
 }

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

 class maths
 {
     public int fun(int k, int y, int n)
     {
         Console.WriteLine(k + "  " + y + "  " + n);
         return (k);
     }
     public int fun1(int t,float z)
     {
         Console.WriteLine(t + "  " + z);
         return t;
     }
 }    
 class Program
 {
     static void Main(string[] args)
     {
         maths obj = new maths();
         int b = 90;
         int c = 100;
         int d ;
         float l;
         int i = obj.fun(b, c, 12);
         int j = (obj.fun1(12, 14.78f));
         Console.ReadLine();
     }
 }

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

 
 static void Main(string[] args)
 {
     int i = 5;
     int j  = 6;
     add(ref i);
     add(6);
     Console.WriteLine(i);
     Console.ReadLine();
 }
 static void add(ref int x)
 {
     x = x * x;
 }
 static void add(int x)
 {
     Console.WriteLine(x * x * x);
 } 

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

 
 class maths
 {
     public int fun1(int k)
     {
         k = 20;
         return k;
     }
     public Single fun1(float t)
     {
         t = 3.4f;
         return t;
     }
 }  
 class Program
 {
     static void Main(string[] args)
     {
         maths obj = new maths();
         int i;
         i = obj.fun1(30);
         Console.WriteLine(i);
         Single j;
         j = obj.fun1(2.5f);
         Console.WriteLine(j);
         Console.ReadLine();
     }
 }  

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

 
 class Program
 {
     static void Main(string[] args)
     {
         Console.WriteLine( vol(10));
         Console.WriteLine( vol(2.5f,  5));
         Console.WriteLine( vol( 5l,  4,  5));
         Console.ReadLine();
     }
     static int vol(int x)
     {
         return(x * x * x);
     }
     static float vol(float r,  int h)
     {
         return(3.14f * r * r * h);
     }
     static long vol(long l, int b, int h)
     {
         return(l * b * h);
     }
 }
 

 

Start practicing “1000 MCQs on C#”, and once you are ready, you can take tests on all topics by attempting our “C# Test Series”.

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.