C# Test 7

More C# Tests

Question 1: Value type variables in C# are derived from the class System.ValueType?

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

static void Main()
{
    Predicate<int> GetBool = delegate (int val)
    {
        if (val <= 0)
            return false;
        else
            return true;
    };
    Console.Write(GetBool(-1));
} 

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

class Program
{
    static void Main(string[] args)
    {
        Processor<int> p = new Processor<int>();
        p.BaseValue = 1;
        int result = p.Add(10);
            
        Console.Write(result);
    }
}

class Processor<T>
{
    public int Add(int value) {
        return this.BaseValue + value;
    }
    public T BaseValue { get; set; }
}

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

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

        DataProcessor<string> p2 = new DataProcessor<string>();
        p2.Process("TEST");
    }
}

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

class DataProcessor<U> : Processor<U>
{

} 

Question 5: How would you rewrite the following condition?

int a = 15, b = 9, c;
if (a > b) 
    c = b;
else
	c = a;

Question 6: Which of the following is not supported in C#?

Question 7: Which of the following statements is FALSE?

Question 8: A class can inherit one or more Structs.

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

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

    public void Introduction(string name){ 
        Console.WriteLine("My name is " + name);
    }
        
    public void Introduction(string name, int age = 0){ 
        Console.WriteLine($"My name is {name} and I am {age} years old.");
    }
        
}

Person p = new Person();
p.Introduction("Steve");

Question 10: Which of the following keyword is used to indicate that a field might be modified by multiple threads that are executing at the same time?

Question 11: An async method can have which of the following return types?

Question 12: Which of the following operator does not throw an exception if the cast fails?

Question 13: Which of the following is also called static polymorphism?

Question 14: Which of the following is true about C# structures vs C# classes?

Question 15: Which operator invokes a constructor of a class?