C# Methods Aptitude Questions and Answers

C# Methods Aptitude Questions: This section contains aptitude questions and answers on C# methods.
Submitted by Nidhi, on March 24, 2020

1) There are following statements are given below, which of them are correct about methods in C#?
  1. Methods are used for the reusability of code.
  2. The ref keyword is used to implement pass by reference.
  3. Methods cannot be declared in the class.
  4. In C #, methods cannot call recursively.

Options:

  1. Only A
  2. Only B
  3. A and B
  4. C and D

2) How many values a method can return in C#?
  1. Any number of values
  2. Only 1 value
  3. Depends on the argument passed
  4. Depends on class

3) If the return type of method is "void" then what is returns?
  1. It returns void type values
  2. It does not return any value
  3. "void" is not the valid return type
  4. It returns three string values

4) There are following options are given below, which of them CANNOT occur more than one time in a C# program?
  1. Program entry point
  2. Namespace
  3. Class
  4. Methods

5) What kind of arguments we used to get updated value from methods?
  1. ref
  2. out
  3. default argument
  4. actual argument

Options:

  1. Only C
  2. Only D
  3. A and B
  4. Only A

6) Is it possible to have arguments with default values in C# methods?
  1. Yes
  2. No

7) Is it required the arguments should NOT initialize before it pass to ref in the method?
  1. Yes
  2. No

8) In the case of "out" parameters, is it required the arguments should initialize before returning to the calling methods?
  1. Yes
  2. No

9) The optional or default argument must be placed at the end of the argument list in the method.
  1. Yes
  2. No

10) There are following statements are given below, which of them are correct about the static method?
  1. Static methods are called by class name.
  2. The static keyword is not required to create a static method.
  3. A non-Static method cannot be called a static method.
  4. Static methods can never be public.

Options:

  1. Only A
  2. Only C
  3. Only D
  4. A and C

11) There are the following keywords that are given below, which of them are NOT access specifiers in C#?
  1. Static
  2. Public
  3. Protected
  4. Internal

Options:

  1. Only A
  2. Only D
  3. A and D
  4. B and C

12) Can be access to private methods outside the class?
  1. Yes
  2. No

13) What is the correct output of given code snippets?
public class Ex
{
    public void sayhello()
    {

        Console.WriteLine("Hello World");
    }
    static public void Main()
    {
        sayhello();
    }
}
  1. Hello World
  2. Runtime Exception
  3. Syntax error
  4. Array index out of bound

14) What is the correct output of given code snippets?
public class Ex
{
    private static void sayhello()
    {
        Console.WriteLine("Hello World");
    }
    static public void Main()
    {
        sayhello();
    }
}
  1. Hello World
  2. Runtime Exception
  3. Syntax error
  4. Array index out of bound

15) What is the correct output of given code snippets?
public class Ex
{
    public static int calculateFactorial(int num)
    {
        int i       = 0;
        int fact    = 1;

        for (i = 1; i >= num; i++)
        {
            fact = fact * i;        
        }

        return fact;
    }
    static public void Main()
    {
        int num     = 5;
        int result  = 0;

        result = calculateFactorial(num);

        Console.WriteLine("Result : "+result);
    }
}
  1. Result : 120
  2. Result : 1
  3. Runtime Exception
  4. Syntax error

16) What is the correct output of given code snippets?
using System;

public class Ex
{
    public static float calculateFactorial(int num)
    {
        int i       = 0;
        float fact    = 1;

        for (i = 1; i <= num; i++)
        {
            fact = fact * i;        
        }

        return fact;
    }
    static public void Main()
    {
        int num     = 5;
        int result  = 0;

        result = calculateFactorial(num);

        Console.WriteLine("Result : "+result);
    }
}
  1. Result : 120
  2. Result : 1
  3. Runtime Exception
  4. Syntax error

17) What is the correct output of given code snippets?
using System;

public class Sample
{
    public static void printTable(int num)
    {
        int i       = 0;
        
        for (i = 1; i <= 10; i++)
        {
            Console.Write(num * i + " ");        
        }
    }
    static public void Main()
    {
        Sample S = new Sample();
        S.printTable(3);
    }
}
  1. 2 4 6 8 10 12 14 16 18 20
  2. 2 4 6 8 10 12 14 16 18
  3. Compiler Error
  4. Runtime Exception

18) What is the correct output of given code snippets?
using System;

public static void printTable(int num)
{
    int i       = 0;
        
    for (i = 1; i <= 10; i++)
    {
        Console.Write(num * i + " ");        
    }
}
public class Sample
{
    
    static public void Main()
    {
        printTable(3);
    }
}
  1. 2 4 6 8 10 12 14 16 18 20
  2. 2 4 6 8 10 12 14 16 18
  3. Compiler Error
  4. Runtime Exception

19) In C# can we create more than one method with the same name?
  1. Yes
  2. No

20) What is the correct output of given code snippets?
using System;

public class Sample
{
    static public void printChar()
    {
        Console.Write("* ");
    }
    static public int printChar(char ch)
    {
        Console.Write(ch+" ");
        return 1;
    }
    static public void Main()
    {
        printChar();
        printChar('#');
    }
}
  1. Syntax Error
  2. * #
  3. Runtime Error
  4. # *

21) What is the correct output of given code snippets?
using System;

public class Sample
{
    public static void MyMethod(int A, ref int X, ref int Y)
    {
        X = A * A;
        Y = A * A * A;
    }

    static public void Main()
    {

        int AA = 5;
        int xx = 0;
        int yy = 0;

        MyMethod(AA, ref xx, ref yy);
        Console.WriteLine("Values: {0},{1}", xx, yy);
    }
}
  1. 25 25
  2. 0 0
  3. 25 125
  4. 125 25

22) If we want to create a static method MyMethod() is to receive a string and a char and it returns a int then what is the correct way to define this method?
  1. public int static MyMethod(string s, char c) {...}
  2. public int MyMethod(string s, char c) {...}
  3. public static int MyMethod(string s, char c) {...}
  4. public static MyMethod(string s, char c) {...}

23) If we want to create a static method MyMethod() that accept int or sometime string then what is the correct way to define this method?
  1. public static void MyMethod(int X) {...}
  2. public static void MyMethod(object X) {...}
  3. public static void MyMethod(string X) {...}
  4. public static void MyMethod(int X, string Y) {...}

24) What is the correct output of given code snippets?
using System;

public class Sample
{
    public static void MyMethod(int A, ref int X, ref int Y)
    {
        X = A * A;
        Y = A * A * A;

        return 0;
    }

    static public void Main()
    {

        int AA = 5;
        int xx = 0;
        int yy = 0;

        MyMethod(AA, ref xx, ref yy);
        Console.WriteLine("Values: {0},{1}", xx, yy);
    }
}
  1. Syntax Error
  2. 25 125
  3. Runtime Exception
  4. 125 125

25) What is the correct output of given code snippets?
using System;

public class Sample
{
    public static int MyMethod(int A, ref int X, ref int Y)
    {
        X = A * A;
        Y = A * A * A;

        return X,Y;
    }

    static public void Main()
    {

        int AA = 5;
        int xx = 0;
        int yy = 0;

        MyMethod(AA, ref xx, ref yy);
        Console.WriteLine("Values: {0},{1}", xx, yy);
    }
}
  1. Syntax Error
  2. 25 125
  3. Runtime Exception
  4. 125 125





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.