//#1: What will be the output of the following code
int DoSomething(int a, int b)
{
return 0;
}
float DoSomething(int c, int d);
{
return 0;
}
int a = DoSomething(10, 20);
float b = DoSomething(30, 40);
Ans: Method Overloading cannot be done by Return types
//#2: OutPut for following Program
class A
{
public virtual void Func1()
{
Console.WriteLine(“A:Func1”);
}
public void Func2()
{
Console.WriteLine(“A:Func2”);
}
}
class B : A
{
public override void Func1()
{
Console.WriteLine(“B:Func1”);
}
public new void Func2()
{
Console.WriteLine(“B:Func2”);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
A a = new B();
a.Func1();//B:Func1
a.Func2();//A:Func2
B a = new B();
a.Func1();//B:Func1
a.Func2();//B:Func2
}
}
// Note: B a = new A(); // Not allowed
#3: Ignore Case Code in C#
string s1 = “SHArad”;
string s2 = “sharad”;
var result = String.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
#4: State difference in throw & throw Ex
try
{
}
catch (Exception ex)
{
throw;
throw ex;
}
private static void ThrowException1() {
try
{
DivByZero();
// line 34
}
catch{
throw
;
// line 36
}
}
private static void ThrowException2() {
try
{
DivByZero();
// line 41
}
catch(Exception ex) {
throw
ex;
// line 43
}
}
Exception 1:
at UnitTester.Program.DivByZero() in <snip>\Dev\UnitTester\Program.cs:line 49
at UnitTester.Program.ThrowException1() in <snip>\Dev\UnitTester\Program.cs:line 36
at UnitTester.Program.TestExceptions() in <snip>\Dev\UnitTester\Program.cs:line 19
Exception 2:
at UnitTester.Program.ThrowException2() in <snip>\Dev\UnitTester\Program.cs:line 43
at UnitTester.Program.TestExceptions() in <snip>\Dev\UnitTester\Program.cs:line 25
Thus, “throw” maintains the full hierarchy in the stack trace and gives complete information about the exception occurred in the code. Whereas “throw ex” pretends that exceptions occurred on the line where “throw ex” was written and removes all the hierarchy above the method containing the “throw ex” expression.
//#5: Write the output of the following Program
public class Employee
{
public string FirstName { get; set; }
}
Main(){
Employee e1 = new Employee();
e1.FirstName = “Suresh”;
ProcessEmployee(e1);
Console.WriteLine(e1.FirstName);
void ProcessEmployee(Employee e1)
{
e1.FirstName = “Abhijeet”;
}
}
Ans: Answer will be Abhijeet
//#6: OutPut of following Code
int i = 0;
int n;
n = i++;
Console.WriteLine(n);
n = ++i;
Console.WriteLine(n);
Ans:
0
2
//#7: Output of following Code
var s = “Shashikant”;
s = 10;
Console.WriteLine(s);
Ans: Error : Cannot implicitly convert int to string
//#8: Write a program to find largest of 3 unique integers in a single statement.
//int a, b, c;
int a = 5, b = 3, c = 2;
Console.WriteLine((a > b ? a : b) > c ? (a > b ? a : b) : c);
//#9: Write a program to print unique elements in an array.
int[] items = { 2, 3, 5, 3, 7, 5 };
- static void Main(string[] args)
- {
- int[] items = { 2, 3, 5, 3, 7, 5 };
- int n = items.Length;
- Console.WriteLine(“Unique array elements: “);
- for(int i=0; i<n;i++)
- {
- bool isDuplicate = false;
- for(int j=0;j<i;j++)
- {
- if(items[i] == items[j])
- {
- isDuplicate = true;
- break;
- }
- }
- if(!isDuplicate)
- {
- Console.WriteLine(items[i]);
- }
- }
- Console.ReadLine();
- }
//#10: Association, Composition, & Aggregation
Association
Association is a relationship among the objects. Association is “*a*” relationship among objects. In Association, the relationship among the objects determine what an object instance can cause another to perform an action on its behalf. We can also say that an association defines the multiplicity among the objects. We can define a one-to-one, one-to-many, many-to-one and many-to-many relationship among objects. Association is a more general term to define a relationship among objects.
Aggregation
Aggregation is a special type of Association. Aggregation is “*the*” relationship among objects. We can say it is a direct association among the objects. In Aggregation, the direction specifies which object contains the other object. There are mutual dependencies among objects.
Composition
Composition is special type of Aggregation. It is a strong type of Aggregation. In this type of Aggregation the child object does not have their own life cycle. The child object’s life depends on the parent’s life cycle. Only the parent object has an independent life cycle. If we delete the parent object then the child object(s) will also be deleted. We can define the Composition as a “Part of” relationship. E.g. “Company Location” under “Company”
//#11: Shadowing vs Overriding
Shadowing (method hiding)
A method or function of the base class is available to the child (derived) class without the use of the “overriding” keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.
Example
- Public class BaseClass
- {
- public string GetMethodOwnerName()
- {
- return “Base Class”;
- }
- }
- public class ChildClass : BaseClass
- {
- public new string GetMethodOwnerName()
- {
- return “ChildClass”;
- }
- }
Test Code
- static void Main(string[] args)
- {
- ChildClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }
Output
ChildClass
Overriding
Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.
Example
- public class BaseClass
- {
- public virtual string GetMethodOwnerName()
- {
- return “Base Class”;
- }
- }
- public class ChildClass : BaseClass
- {
- public override string GetMethodOwnerName()
- {
- return “Child Class”;
- }
- }
In overriding, the base class can be accessed using the child object’s overridden method.
Overriding Example
- static void Main(string[] args)
- {
- BaseClass c = new ChildClass();
- Console.WriteLine(c.GetMethodOwnerName());
- }
Output
ChildClass
//#12: Diamond Box Problem in C#
The “diamond problem” is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which class of the method does D inherit: that of B, or that of C? So this is an ambiguity problem in multiple inheritances in c#. So that c# does not support multiple inheritances. It also called an ambiguity problem in c#.
namespace CSharpConApp.DiamondProblem1
{
interface IMother
{
void Loan();
}
interface IFather
{
void Loan();
}
public class Mother:IMother
{
public void Loan()
{
Console.WriteLine(“Loan taken by mother.”);
}
}
public class Father:IFather
{
public void Loan()
{
Console.WriteLine(“Loan taken by father”);
}
}
public class Child:IMother,IFather
{
void IMother.Loan()
{
Console.WriteLine(“Mother’s loan paid by child”);
}
void IFather.Loan()
{
Console.WriteLine(“Father’s loan paid by child”);
}
}
public class DiamondProblem1
{
static void Main(string[] args)
{
Child child = new Child();
((IMother)child).Loan();
((IFather)child).Loan();
Console.ReadLine();
}
}
}
***