C# object oriented programming Question and Answer.

Abhishek Malaviya
4 min readJul 10, 2023

--

As C# developers, we need to practice the OOP concept. We’ll discuss some interesting questions. I hope you’ll enjoy it and brush up on your knowledge. Here we are not following any coding standard, so please ignore it.

Question 1: What will output of below code?

    public class Class1
{

public string Name { get; set; }

public Class1()
{
Name = "Abhishek";
}

public static void Main()
{
Class1 objClass1 = new Class1() { Name="Siva" };
Console.WriteLine(objClass1.Name);
}
}

Answer: Siva.

Question 2: Will this code compile?

    public class clsB
{
public clsB(string s)
{
Console.WriteLine($"base :{s}");
}
}

public class clsD:clsB
{
public clsD(string s)
{

}
}

Answer: No.

Error CS7036 There is no argument given that corresponds to the required parameter 's' of 'clsB.clsB(string)' 

Inheritance is compiled time because it is related to specification.

Base class does not has default constructor you need to use base keyword to call base class constructor. Below code will be compiled.

    public class clsB
{
public clsB(string s)
{
Console.WriteLine($"base :{s}");
}
}

public class clsD:clsB
{
public clsD(string s):base("base")
{

}
}

Question 3: What will be output of following code?

    public class clsB
{
public clsB()
{
Console.WriteLine("base default constr");
}

public clsB(string s)
{
Console.WriteLine($"base param constr :{s}");
}
}

public class clsD:clsB
{
public clsD(string s)
{
Console.WriteLine($"drive param constr :{s}");
}
public static void Main()
{
clsD objclsD = new clsD("i am child class");
}
}

Answer

base default constr
drive param constr : i am child class

Here you can see base class default constructor is calling by default. But what if you want to call base class parameter constructor ?

Answer: use base keyword to

    public class clsB
{
public clsB()
{
Console.WriteLine("base default constr");
}

public clsB(string s)
{
Console.WriteLine($"base param constr :{s}");
}
}

public class clsD:clsB
{
public clsD(string s) : base("i am base class")
{
Console.WriteLine($"drive param constr :{s}");
}
public static void Main()
{
clsD objclsD = new clsD("i am child class");
}
}

Output

base param constr : i am base class
drive param constr : i am child class

Question 4: Will this code compile?

    public class TestClass 
{
public void Multiply(int firstNumber, int secondNumber)
{
Console.WriteLine($"Result: {firstNumber * secondNumber}");
}

public int Multiply(int firstNumber, int secondNumber)
{
return firstNumber * secondNumber;
}
}

Answer: No

Error CS0111 Type 'TestClass' already defines a member called 'Multiply' with the same parameter types .

Method overloading depends on the number, type, and order of parameters. The return type should be the same for method overloading.

Question 5: Will this code compile?

    public class TestClass
{
public TestClass()
{
Console.WriteLine("I am a TestClass constructor");
}
}

public static void Main()
{
using(TestClass testClass=new TestClass())
{

}
}

Answer: No

Error CS1674 'TestClass': type used in a using statement must be implicitly convertible to 'System.IDisposable'. 

Using clause mainly use for unmanaged resources that wrap operating system resources, such as files, windows, network connections, or database connections.

Question 6: Will this code compile?

   class TestClass
{
public TestClass()
{
Console.WriteLine("I am a TestClass constructor");
}
}

public class TestClass1 : TestClass
{
public TestClass1()
{
Console.WriteLine("I am a TestClass1 constructor");
}
}

Answer: No

Error CS0060 Inconsistent accessibility: base class 'TestClass' is less accessible than class 'TestClass1' 

By default, the class access modifier is internal. So the base class TestClass access modifier is internal, which is less accessible than the child public class TestClass1.

Question 7: Will this code compile?

   public class TestClass
{
public TestClass()
{
Console.WriteLine("I am a TestClass constructor");
}
}

public class TestClass1 : TestClass
{
public TestClass1()
{
Console.WriteLine("I am a TestClass1 constructor");
}
}

TestClass1 testClass1 = new TestClass();

Answer: No.

Error CS0266 Cannot implicitly convert type 'TestClass' to 'TestClass1'. An explicit conversion exists (are you missing a cast?) 

Question 8: What will be the output?

public class TestClass
{
public void GetMessage()
{
Console.WriteLine("This is a base class message");
}
}

public class TestClass1 : TestClass
{
public void GetMessage()
{
Console.WriteLine("This is a child class message");
}
}

TestClass testClass = new TestClass1();
testClass.GetMessage();

Answer: This is a base class message

Question 9: What will be the output?

public class TestClass
{
public TestClass()
{
Console.WriteLine("I am a TestClass constructor");
}

static TestClass()
{
Console.WriteLine("I am a TestClass static constructor");
}
}

public class TestClass1 : TestClass
{
public TestClass1()
{
Console.WriteLine("I am a TestClass1 constructor");
}

static TestClass1()
{
Console.WriteLine("I am a TestClass1 static constructor");
}
}


TestClass testClass = new TestClass1();

Answer:

Output

Question 10: Will this code compile?

public class TestClass
{
public static TestClass()
{
Console.WriteLine("It is static constructor");
}
}

Answer: No.

Error CS0515 'TestClass.TestClass()': access modifiers are not allowed on static constructors 

By default static constructor is private. Access modifiers are not allowed on static constructors.

I’ll add some more questions in the future. Until then, happy coding!

--

--

Responses (1)