MCQs
class maths { int fact(int n) { int result; if (n == 1) return 1; result = fact(n - 1) * n; return result; } } class Output { static void main(String args[]) { maths obj = new maths() ; Console.WriteLine(obj.fact(4)*obj.fact(2)); } }
4! = 4*3*2*1 & 2! = 2*1 .So, 24*2 = 48.
Output : 48
interface calc { void cal(int i); } class displayA :calc { public int x; public void cal(int i) { x = i * i; } } class displayB :calc { public int x; public void cal(int i) { x = i / i; } } class Program { public static void Main(string[] args) { displayA arr1 = new displayA(); displayB arr2 = new displayB(); arr1.x = 0; arr2.x = 0; arr1.cal(2); arr2.cal(2); Console.WriteLine(arr1.x + " " + arr2.x); Console.ReadLine(); } }
class displayA executes the interface calculate by doubling the value of item . Similarly class displayB implements the interface by dividing item by item.So, variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output : 4, 1
class A { public int i; public void display() { Console.WriteLine(i); } } class B: A { public int j; public void display() { Console.WriteLine(j); } } class Program { static void Main(string[] args) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); Console.ReadLine(); } }
When method display() is called using objects of class 'B'. The method 'display()' for class 'B' is called instead of class 'A' as class 'B' is inherited by class 'A'.
Output :2
class maths { public int x; public double y; public int add(int a, int b) { x = a + b; return x; } public int add(double c, double d) { y = c + d; return (int)y; } public maths() { this.x = 0; this.y = 0; } } class Program{ static void Main(string[] args) { maths obj = new maths(); int a = 4; double b = 3.5; obj.add(a, a); obj.add(b, b); Console.WriteLine(obj.x + " " + obj.y); Console.ReadLine(); }}
namespace ConsoleApplication4{ abstract class A { public int i; public abstract void display(); } class B: A { public int j; public int sum; public override void display() { sum = i + j; Console.WriteLine(+i + "\n" + +j); Console.WriteLine("sum is:" +sum); } } class Program { static void Main(string[] args) { A obj = new B(); obj.i = 2; B obj1 = new B(); obj1.j = 10; obj.display(); Console.ReadLine(); } }}
Abstract method implementation is processed in subclass 'B' .Also the object 'obj' of abstract class 'A' initializes value of i as 2.The object of class 'B' also initializes value of j as 10.Since, the method display() is called using object of class A which is 'obj' and hence i = 2 whereas j = 0 .So, sum = 2.
Output : 2 0
sum is : 2