InterView Programming

For more basic questions Click here

What is the output of below Programme?   
class a
    {
        public void test()

        { Console.WriteLine("Class A"); }
    }

    class b:a
    {
        //public void test()

        //{ Console.WriteLine("Class B"); }
    }

    class c:b
    {
        public void test()

        { Console.WriteLine("Class C"); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            a _a = new a();
            b _b = new b();
            c _c = new c();
            _a.test();
            _b.test();
            _c.test();
      _a = new c();
      _a.test();
        }
    }

Output: 
Class A
Class A
Class C
Class A

What is the output of below Programme?   
class a
    {
        public virtual void test()
        { Console.WriteLine("Class A"); }
    }

    class b:a
    {
        public override void test()

        { Console.WriteLine("Class B"); }
    }

    class c:b
    {
        public override void test()
        { Console.WriteLine("Class C"); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            a _a = new a();
            b _b = new b();
            c _c = new c();
            _a.test();
            _b.test();
            _c.test();
            Console.WriteLine("------------------");

            _a = new b();
            _a.test();
            _b = new c();
            _b.test();
            Console.Read();
        }
    }

 Output: 
Class A
Class B
Class C
------------------
Class B
Class C

What is the output of below Programme?  
class a
    {
        public void test()
        { Console.WriteLine("Class A"); }
    }

    class b:a
    {
        public new virtual void test()
        { Console.WriteLine("Class B"); }
    }

    class c:b
    {
        public override void test()
        { Console.WriteLine("Class C"); }
    }


    class Program
    {
        static void Main(string[] args)
        {
            a _a = new a();
            b _b = new b();
            c _c = new c();
            _a.test();

            _b.test();

            _c.test();

            Console.WriteLine("------------------");
            _a = new b();
            _a.test();
            _b = new c();
            _b.test();
            Console.Read();
        }
    }
 Output: 

Class A
Class B
Class C
------------------
Class A
Class C

Note
1.  The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.
2.  The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into derived class.
3. The new keyword is used to hide a method, property, indexer, or event of base class into derived class.
  exp: see the output of         _a = new b();
                         _a.test();
   in 3rd Programme.

Enum Type Output question
enum color : int { red,green=5,bluepink=4yellowwhiteblackbrown}

Console.WriteLine(color.black); //black
Console.WriteLine((int)color.red); //0
Console.WriteLine((int)color.yellow); //5
Console.WriteLine((int)color.blue); //6
Console.WriteLine((int)color.brown); //8

What is the Output of below Code
interface IService
{
    void print(string val = "Hello world");
}
public class Service : IService
{
    public void print(string val = "Hello India")
    {
        Console.Write(val);
    }
}

static void Main()
{
    Service obj = new Service();
    obj.print(); // Hello India

    IService iObj = obj;
    iObj.print();  // Hello World
}

What is the Output of below Code

Find Integer values from array
string[] stringArray = new string[] {"X""B""Z""Y""A","2""4""1","3","6"};
            List<intarr = new List<int>();
            bool res;
            int a;
            for (int i = 0i < stringArray.Lengthi++)
            {
                res = int.TryParse(stringArray[i], out a);
                if (res)
                {
                    arr.Add(Convert.ToInt32(stringArray[i]));
                }
            }
arr will contain all int values from stringArray.

What will be the output of below code?

public class Program
    {
        static void Main()
        {
            A obj = new B();
            obj.display("hello");
            obj.display1("hello");

            B obj1 = new B();
            obj1.display("hello");
            obj1.display1("hello");
            Console.Read();
        }
    }

    public class A
    {
        public void display(string message)
        {
            Console.WriteLine("A.Disaplay => " + message);
        }

        public virtual void display1(string message)
        {
            Console.WriteLine("A.Disaplay1 => " + message);
        }
    }

    public class B : A
    {
        public void display(string message)
        {
            Console.WriteLine("B.Disaplay => " + message);
        }
        public override void display1(string message)
        {
            Console.WriteLine("B.Disaplay1 => " + message);
        }
    }
Answer:
A.Disaplay => hello
B.Disaplay1 => hello
B.Disaplay => hello
B.Disaplay1 => hello

Swap two Numbers without using third Variable-
Int a10;int b20
aa+b;
ba-b
aa-b;
C.W.(“a= ”+a+” b=  ”+b

What is the output of below Programme?   
class MyCapsule
    {
      private char myChar;
      public void MyCharFunc() { myChar = 'A'Console.Write(myChar); }
    }

    class Program:MyCapsule
    {
        static void Main(string[] args)
        {
            MyCapsule o = new MyCapsule();
            o.MyCharFunc();
            Console.Read();
        }
    }
OutPut = A

What is the output of below Programme?   
class MyCapsule
    {
      private char myChar;
      public void MyCharFunc(char ch) {
          Console.Write(ch);
      }
    }

    class Program:MyCapsule
    {
        static void Main(string[] args)
        {
            MyCapsule o = new MyCapsule();
            o.MyCharFunc('A');
            Console.Read();
        }
    }
OutPut = A

What is the output of below Programme?   
class MyCapsule
    {
        public static int i = 5;
        public void test()
        {
            i = i + 5;
            Console.WriteLine(i);
        }
    }

    class Program
    {
        static void Main()
        {
            MyCapsule var = new MyCapsule();
            var.test();
            MyCapsule var1 = new MyCapsule();
            var1.test();
            Console.ReadKey();
        }
    }
Output- 10
             15
Note: static variable shared the value of it among all instances of the class.

What is the output of below Programme?   
public class A
    {
        public A()
        {
            Console.WriteLine("Class A");
        }
    }

    public class B :A
    {
        public B()
        {
            Console.WriteLine("Class B");
        }

        private int myVar;

        public int MyProperty
        {
            get {
                return myVar; }
            set {
                    A a = new A();
                    myVar = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B b = new B(); //output
            b.MyProperty = 8//output
            Console.Read();
        }
    }
Output- Class A
             Class B
             Class A 

What is the output of below Programme?   
public abstract class A
    {
        public virtual void test1()
        {
            Console.WriteLine("hell");
        }
    }

    public class B : A
    {
        public override void test1()
        {
            Console.WriteLine("Paradise");
        }
    }

   public class Program
    {
        static void Main(string[] args)
        {
            A a = new B();
            a.test1();
            Console.ReadKey();
        }
    }
Output- Paradise

A simple recursive function in c#
class Program
    {
        static void RecuriveFunction(int x)
        {
            if (x > 10)  //Condition to stop recursion
                return;  //Exit out of function
            else
            {
                Console.WriteLine(x);
                RecuriveFunction(x + 1);  //Call myself
            }
        }
     
        static void Main()
        {
            RecuriveFunction(1);
            Console.ReadKey();
        }
    }
    
Fibonacci number
public class Program
    {
       public void Print()
       {
           int ab;
           a = b = 1;
           while (b <= 1000)
           {
               Console.WriteLine(b);
               b = a + b;
               a = b - a;
           }
       }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Print();
            Console.ReadKey();
        }
    }
Output: 

1
2
3
5
8
13
21
34
55
89
144
233
377
610
987

Create a Pyramid Programe

 

 Digital Clock

Enter your Full Name and Get First Name as Output



Convert Inch Value into Feet



Enter any Number and get Reverse Number as Output


 Check Palindrome Number


Pyiramid Programme


 Convert Second Value in Hours and Minuts


Create a Table from 2-10


Get the characters of a string variable(string str = "asdfretb") in alphabatically order with the character position by using DataTable.

for Ex.
string name= "adb"
it should display like :
a   1
b   3
d   2

public class Program
    {
       void print()
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Name",typeof(string));
           dt.Columns.Add("Position",typeof(int));
           string str = "asdfretb";
           int len = str.Length;
           for (int i = 0; i < len; i++)
           {
               DataRow dr = dt.NewRow();
               dr[0] = str.Substring(i, 1);
               dr[1] = i;
               dt.Rows.Add(dr);
           }
           foreach (DataRow _dr in dt.Select("", "NAME ASC"))
           {
               Console.WriteLine("\t" + _dr["Name"] +"\t"+ _dr[1]);
           }
       }
      
        static void Main(string[] args)
        {
            Program p = new Program();
            p.print();
            Console.ReadKey();
        }
    }

Output - 

We know that Base class constructor called first. But if we creating object with parameters, and base class have both constructor default and parameterized, then which constructor of baseclass called first.

Ans: Base class default constructor called first.

Then what you can do that base class parameterized constructor call first.

Ans: We can use "Base" keyword




0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives