C# Questions & Answers – Methods in Class

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

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

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int s = 0, c = 0;
  5.      Mul (a, ref s, ref c);
  6.      Console.WriteLine(s + "t " +c);
  7.      Console.ReadLine();
  8.  }
  9.  static void Mul (int x, ref int ss, ref int cc)
  10.  {
  11.      ss = x * x;
  12.      cc = x * x * x;
  13.  }

a) 125 25
b) 25 125
c) Compile time error
d) 0 0
View Answer

Answer: b
Explanation: The value of variable a is passed by value while value of variable s and c is passed by reference.
Output:

advertisement
advertisement
 25 125

2. Which of the following statements are correct about functions?
a) C# allows a function to have arguments with default values
b) Redefining a method parameter in the method’s body causes an exception
c) C# allows function to have arguments with default values
d) Omitting the return type in method definition results into exception
View Answer

Answer: a
Explanation: None.

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

  1. static void Main(string[] args)
  2. {
  3.     Mul();
  4.     m();
  5.     Console.ReadLine();
  6. }
  7. static void Mul()
  8. {
  9.     Console.WriteLine("4");
  10. }
  11. static void m()
  12. {
  13.     Console.WriteLine("3");
  14.     Mul();
  15. }

a) 4 3 3
b) 4 4 3
c) 4 3 4
d) 3 4 4
View Answer

Answer: c
Explanation: First Mul() will be executed to print the number ‘4’ after that function m() will be executed to print the number ‘3’ and at last mentioned function Mul() will be executed to print the statement 4 to return the output as 4 3 4.
Output:

advertisement
 4 3 4

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

advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      m();
  4.      Console.ReadLine();
  5.  }
  6.  static void m()
  7.  {
  8.      Console.WriteLine("HI");
  9.      m();
  10.  }

a) HI HI HI
b) HI
c) Stack overflow exception
d) Compile time error
View Answer

Answer: c
Explanation: Control of statement when enters for once in m() does not go out, then it executes again and again inside the block until stack overflow exception occurs.

5. When a function fun() is to receive an int, a single & a double and it is to return a decimal, then the correct way of defining this C# function is?
a)

   static fun(int i, single j, double k)
   {
       return decimal;
   }

b)

   static decimal fun(int i, single, double k)
   {
 
   }

c)

   decimal fun(int i, single j, double k)
   {
 
   }

d)

   decimal static fun(int i, single j, double k)
   {
 
   }
View Answer
Answer: b
Explanation: Correct way of declaration of function is defined as return_type name of function(return type).
 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 10;
  4.      double d = 35.78;
  5.      fun(i);
  6.      fun(d);
  7.      Console.ReadLine();
  8.  }
  9.  static void fun(double d)
  10.  {
  11.      Console.WriteLine(d);
  12.  }

a)

35.78
   10

b)

10
   35.00

c)

10
   35.78

d) None of the mentioned
View Answer

Answer: c
Explanation: ‘int’ datatype is sub datatype of ‘double’. Hence, when first part of func() is executed it is integer part and hence when the second part is executed it is double.
Output:

       10
       35.78

7. How many values does a function return?
a) 0
b) 2
c) 1
d) any number of values
View Answer

Answer: c
Explanation: A method can return only either single value or no value if no then it’s declared as void method();

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

  1.  static void Main(string[] args)
  2.  {
  3.      int y = 3;
  4.      y++;
  5.      if (y <= 5)
  6.      { 
  7.          Console.WriteLine("hi");
  8.          Main(args);
  9.      }
  10.      Console.ReadLine();
  11.  }

a) hi hi
b) hi
c) Stack overflow exception
d) None of the mentioned
View Answer

Answer: c
Explanation: If loop never gets over, it will execute continuously. The control never goes out of ‘if’ statement.

Output: hi
        hi
         .
         .
         .
        stack overflow exception

9. Which return statement correctly returns the output?
a)

   public int cube(int x)
   {
       return (x + x);
   }

b)

   public int cube(int x)
   return (x + x);

c)

   public int cube(int x)
   {
       return x + x;
   }

d) None of the mentioned
View Answer

Answer: a
Explanation: The correct syntax of return statement is defined within block of statements as { return(statement);}.

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

  1.  public static void Main(string[] args)
  2.  {
  3.      p();
  4.      void p()
  5.      {
  6.          Console.WriteLine("hi");
  7.      }
  8.  }

a) Compile time error
b) hi
c) hi infinite times
d) None of the mentioned
View Answer

Answer: a
Explanation: Invalid definition of function p() inside main().

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.

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.