Functions and Subroutines Questions
Practice Functions and Subroutines MCQs with answers and explanations. Page 1 of 2.
Category
C# Programming
Topic
Functions and Subroutines
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
C#.NET — Predict output using ref parameters to return square and cube.
Program:
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int a = 5;
int s = 0, c = 0;
Proc(a, ref s, ref c);
Console.WriteLine(s + " " + c);
}
static void Proc(int x, ref int ss, ref int cc)
{
ss = x * x;
cc = x * x * x;
}
}
}
Open
View answer
C#.NET — Choose the correct method signature: receives (int, Single, double), returns decimal.
Which definition is valid?
Open
View answer
C#.NET — Multiple return values via function: identify compile-time errors in call and return.
Program:
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int a = 5;
int s = 0, c = 0;
s, c = fun(a);
Console.WriteLine(s + " " + c);
}
static int fun(int x)
{
int ss, cc;
ss = x * x; cc = x * x * x;
return ss, cc;
}
}
}
Which statements are correct?
Open
View answer
C#.NET — Determine the printed output for the following program (note: the loop intentionally iterates to Length - 1, so the last element is skipped):
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
object[] o = new object[] {"1", 4.0, "India", 'B'};
fun(o);
}
static void fun(params object[] obj)
{
for (int i = 0; i < obj.Length - 1; i++)
Console.Write(obj[i] + " ");
}
}
}
Open
View answer
C#.NET — What does the following program print (factorial computed via out parameter)?
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i;
int res = fun(out i);
Console.WriteLine(res);
}
static int fun(out int i)
{
int s = 1;
i = 7;
for (int j = 1; j <= i; j++)
{
s = s * j;
}
return s;
}
}
}
Open
View answer
In C# terminology, how many values can a subroutine (a void method) effectively return?
Open
View answer
C# design: If a function sometimes receives an int and sometimes a double, which definition best supports both cases cleanly?
Open
View answer
True or False: A function returns a value, whereas a subroutine (void method) cannot return a value via the return keyword.
Open
View answer
True or False: A function can be used within an expression, whereas a subroutine (void method) cannot be used as an expression operand.
Open
View answer
C#.NET — What will the corrected program print? (Assume the intended method name is fun1, not funl.)
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i = 5;
int j;
fun1(ref i);
fun2(out j);
Console.WriteLine(i + ", " + j);
}
static void fun1(ref int x)
{
x = x * x;
}
static void fun2(out int x)
{
x = 6;
x = x * x;
}
}
}
Open
View answer
C#.NET — After fixing spacing in the array creation (new int[]), what does this program print?
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5 };
fun(ref arr);
}
static void fun(ref int[] a)
{
for (int i = 0; i < a.Length; i++)
{
a[i] = a[i] * 5;
Console.Write(a[i] + " ");
}
}
}
}
Open
View answer
C#.NET — Which of the following can occur only once per program (single entry point requirement)?
Open
View answer
In C#, how many values can a function return directly via its return value?
Open
View answer
C#.NET — Identify correct statements about functions (recursion, nesting, returns, and nested calls).
Which of the following statements are correct about functions used in C#.NET?
Function definitions cannot be nested.
Functions can be called recursively.
If we do not return a value from a function then a value -1 gets returned.
To return the control from middle of a function, the keyword "exit function" should be used.
Function calls can be nested.
Open
View answer
C#.NET — Choose the correct method declaration for: accepts (int, Single, double) and returns decimal.
If a procedure fun() is to receive an int, a Single, and a double, and it must return a decimal, which of the following is the correct way to define it?
Open
View answer
C#.NET — Functions vs “subroutines” (void methods): ref behavior, recursion, and call rules.
Which statements are correct about functions and subroutines used in C#.NET?
A function cannot be called from a subroutine.
The ref keyword causes arguments to be passed by reference.
With ref, changes in the method are reflected back in the caller.
A subroutine cannot be called from a function.
Functions and subroutines can be called recursively.
Open
View answer
C#.NET — Defaults, variable-argument methods, and error types in definitions.
Which statements are correct?
C# allows a function to have arguments with default values.
C# allows a function to have a variable number of arguments.
Omitting the return value type in a method definition results in an exception.
Redefining a method parameter inside the method body causes an exception.
The params keyword specifies a variable number of arguments.
Open
View answer
C#.NET — Predict output with overload resolution and double formatting.
Consider:
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i = 10;
double d = 34.340;
fun(i);
fun(d);
}
static void fun(double d)
{
Console.WriteLine(d + " ");
}
}
}
What will be printed?
Open
View answer
C#.NET — Identify correct statements about void methods (“subroutines”): nesting, recursion, exit, and nested calls.
Which statements are correct about subroutines used in C#.NET?
If we do not return a value from a subroutine then a value -1 gets returned.
Subroutine definitions cannot be nested.
Subroutine can be called recursively.
To return the control from the middle of a subroutine, use "exit subroutine".
Subroutine calls can be nested.
Open
View answer
C#.NET — Trace pass-by-value vs pass-by-reference (ref) and predict the print sequence.
Program:
namespace CuriousTabConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int num = 1;
funcv(num);
Console.Write(num + ", ");
funcr(ref num);
Console.Write(num + ", ");
}
static void funcv(int num)
{
num = num + 10; Console.Write(num + ", ");
}
static void funcr(ref int num)
{
num = num + 10; Console.Write(num + ", ");
}
}
}
What will be printed?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.