For more basic questions Click here
What is the output of below line?
What is the output of below Programme?
Output:
What is the output of below line?
//Cannot assign <null> to an implicitly-typed variable
var a = null;
if you have two interfaces that contain methods with the same name and signature, you can explicitly implement one of the interfaces to avoid ambiguity. Here's how you can do that:
interface Ia { void foo(); }
interface Ib { void foo(); }
public class C : Ia, Ib
{
void Ia.foo() => Console.WriteLine("Ia: hello");
void Ib.foo() => Console.WriteLine("Ib: hello");
}
main()
{
// To call the Ia version
Ia _ia = new C();
_ia.foo(); // Outputs: Display from Ia
// To call the Ib version
Ib _ib = new C();
_ib.foo(); // Outputs: Display from Ib
}
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
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();
}
}
Class A
Class B
Class C
------------------
Class B
Class C
What is the output of below Programme?
Output:
------------------
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();
}
}
Class A
Class B
Class C
------------------
Class A
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
What is the Output of below Code
Enum Type Output question
enum color : int { red,green=5,blue, pink=4, yellow, white, black, brown}
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
arr will contain all int values from stringArray.
What will be the output of below code?
Answer:
Output:
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
Create a Pyramid Programe
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
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.
string[] stringArray = new string[] {"X", "B", "Z", "Y", "A","2", "4", "1","3","6"};
List<int> arr = new List<int>();
bool res;
int a;
for (int i = 0; i < stringArray.Length; i++)
{
res = int.TryParse(stringArray[i], out a);
if (res)
{
arr.Add(Convert.ToInt32(stringArray[i]));
}
}
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);
}
}
A.Disaplay => hello
B.Disaplay1 => hello
B.Disaplay => hello
B.Disaplay1 => hello
Swap two Numbers without using third Variable-
Output- Class A
Class B
Class A
What is the output of below Programme?
Int a= 10;int b= 20;
a= a+b;
b= a-b
a= a-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
15
Note: static variable shared the value of it among all instances of the class.
What is the output of below Programme?
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();
}
}
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 a, b;
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();
}
}
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
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
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 -
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