C# Test 6

More C# Tests

Question 1: What will be the output of the following program?

static void Main()
{
    Func<string, string> greet = delegate (string name)
    {
        return "Hi " + name;
    };

    Console.Write(greet("Sachin"));
}

Question 2: What will be the output of the following program?

public static void Main()
{
	Action DoSomething = () => Console.WriteLine("Hello!");
       
    DoSomething();
}

Question 3: When will be T will be replaced with the actual type in the following program?

class ValueProcessor
{
    // Implementation 
}

Question 4: What will be the output of the following program?

class Program
{
    static void Main(string[] args)
    {
        DataProcessor<string> p2 = new DataProcessor<string>();
        p2.Process(100);
    }

}


class Processor<T>
{
    public void Process(T value)
    {
        Console.Write(value.GetType().Name);
    }
}

class DataProcessor<U> : Processor<int>
{

}

Question 5: Which of the following generic constraints restricts the generic type parameter to an object of the class that implements IEnumerable interface?

Question 6: What will be the output of the following code?

ValueTuple person = (1, "Tom");
Console.Write(person.Item2);

Question 7: Which of the following types can implement interfaces?

Question 8: Can we declare a protected member in an interface?

Question 9: Which of the following types can participate in inheritance in C#?

Question 10: What will be the output of the following program?

class Person
{
    public void Introduction(){ 
        Console.WriteLine("I am a person.");
    }

    public string Introduction(){ 
        return "I am a person.";
    }
}

Question 11: What does the following code do?

File.WriteAllText(@"C:\MyFile.txt", "This is file content.");

Question 12: What will be the output of Console.Write(sizeof(int));?

Question 13: What is the difference between interface and abstract class?

Question 14: How to restrict a class to be a base class?

Question 15: How to access members of the base class from within a derived class?