C# Questions & Answers – Delegates in Detail

This set of Basic C# Questions and Answers focuses on “Delegates in Detail”.

1. Choose the correct way to call subroutine fun() of the sample class?

  1. class a
  2. {
  3.     public void x(int p, double k)
  4.     {
  5.         Console.WriteLine("k : csharp!");
  6.     }
  7. }

a)

advertisement
advertisement
   delegate void del(int i);
   x s = new x();
   del d = new del(ref s.x);
   d(8, 2.2f);

b)

   delegate void del(int p,  double k);
   del d;
   x s = new x();
   d = new del(ref s.x);
   d(8, 2.2f);

c)

advertisement
   x s = new x();
   delegate void d = new del(ref x);
   d(8, 2.2f);

d) all of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

2. Which of the following is the correct way to call the function abc() of the given class in the following C# code?

  1. class csharp
  2. {
  3.     public int abc(int a)
  4.     {
  5.         Console.WriteLine("A:Just do it!");
  6.         return 0;
  7.     }
  8. }

a)

   delegate void del(int a);
   csharp s = new csharp();
   del d = new del(ref s.abc);
   d(10);

b)

   csharp s = new csharp();
   delegate void d = new del(ref abc);
   d(10);

c)

   delegate int del(int a);
   del d;
   csharp s = new csharp();
   d = new del(ref s.fun);
   d(10);

d) none of the mentioned
View Answer

Answer: c
Explanation: None.

3. Which of the following is the correct way to call the subroutine function abc() of the given class in the following C# code?

  1. class csharp
  2. {
  3.     void abc()
  4.     {
  5.         console.writeline("A:Just do it!");
  6.     }
  7. }

a)

   csharp c = new csharp();
   delegate void d = new del(ref abc);
   d();

b)

   delegate void del();
   del d;
   csharp s = new csharp();
   d = new del(ref s.abc);
   d();

c)

   csharp s = new csharp();
   delegate void del = new delegate(ref abc);
   del();

d) None of the mentioned
View Answer

Answer: b
Explanation: None.

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

  1. {
  2.     delegate void A(ref string str);
  3.     class sample
  4.     {
  5.         public static void fun( ref string a)
  6.         {
  7.             a = a.Substring( 7, a.Length - 7);
  8.         }
  9.     }
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             A str1;
  15.             string str = "Test Your C#.net skills";
  16.             str1 = sample.fun;
  17.             str1(ref str);
  18.             Console.WriteLine(str);
  19.         }
  20.     }
  21. }

a) Test Your
b) ur C#.NET
c) ur C#.NET Skills
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

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

  1. {
  2.     delegate string F(string str);
  3.     class sample
  4.     {
  5.         public static string fun(string a)
  6.         {
  7.             return a.Replace(',''-');
  8.         }
  9.     }
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             F str1 = new F(sample.fun);
  15.             string str = str1("Test Your c#.NET skills");
  16.             Console.WriteLine(str);
  17.         }
  18.     }
  19. }

a) Test Your
b) Test-Your-C#.NET-Skills
c) ur C#.NET Skills
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
Output:

Test-Your-C#.NET-Skills

6. Choose the statements which makes delegate in C#.NET different from a normal class?
a) Delegates in C#.NET is a base class for all delegates type
b) Delegates created in C#.NET are further not allowed to derive from the delegate types that are created
c) Only system and compilers can derive explicitly from the Delegate or MulticasteDelegate class
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

7. Which of the following are the correct statements about delegates?
a) Delegates can be used to implement callback notification
b) Delegates permit execution of a method on a secondary thread in an asynchronous manner
c) Delegate is a user defined type
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

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

  1. {
  2.  delegate string f(string str);
  3.  class sample
  4.  {
  5.      public static string fun(string a)
  6.      {
  7.          return a.Replace('k', 'o');
  8.      }
  9.  }
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {
  14.          f str1 = new f(sample.fun);
  15.          string str = str1("Test Ykur C#.NET Skills");
  16.          Console.WriteLine(str);
  17.          Console.ReadLine();
  18.      }
  19.  }
  20. }

a) Test Ykur C#.NET Skills
b) Test Ykour C#.NET Skills
c) Test Your C#.NET Skills
d) Test ur C#.NET Skills
View Answer

Answer: c
Explanation: None.
Output:

Test Your C#.NET Skills

9. Incorrect statements about delegates are?
a) Delegates are reference types
b) Delegates are object oriented
c) Delegates are type safe
d) Only one method can be called using a delegate
View Answer

Answer: d
Explanation: None.

10. Select the modifiers which control the accessibility of the delegate?
a) new
b) protected
c) public
d) all of the mentioned
View Answer

Answer: d
Explanation: By definition

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

Here’s the list of Best Books in C# Programming Language.

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.