.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
abhays
Clintonzz
mattyclown
MakarandK
caryfloric

Home >> Interview Question >> C# >> Post New Question Subscribe to Interview Questions

What will be the output of the following program?.
class A {
public virtual void print() {
Console.WriteLine("A");
}
}
class B:A {
public override void print() {
Console.WriteLine("B");
}
}

class C:B {
public override void print() {
Console.WriteLine("C");
}
}

class D:C {
public override void print() {
Console.WriteLine("D");
}
}
static void Main(string[] args) {
D d=new D();
A a=d;
B b=d;
C c=d;
a.print();
b.print();
c.print();
d.print();
Console.ReadLine();

Posted By :Santosh     Posted Date :October 15, 2009    Points :10   Category :C# 
DDDD

You can also find related Interview Question to What will be the output of the following program?. class A { public virtual void print() { Console.WriteLine("A"); } } class B:A { public override void print() { Console.WriteLine("B"); } } class C:B { public override void print() { Console.WriteLine("C"); } } class D:C { public override void print() { Console.WriteLine("D"); } } static void Main(string[] args) { D d=new D(); A a=d; B b=d; C c=d; a.print(); b.print(); c.print(); d.print(); Console.ReadLine();  below: 

This is the progam using inheritence, what is the order of calling constructors and what is the output?
Public Class ConstructorEx
Public Class Class1
Dim a As Integer
Sub New()
a = 1
Console.WriteLine("a in initialized in Class1")
End Sub
End Class

Public Class Class2
Inherits Class1
Dim b As Integer
Sub New()
b = 1
Console.WriteLine("b in initialized in Class2")
End Sub
End Class
Public Class Class3
Inherits Class2
Dim c As Integer
Sub New()
c = 1
Console.WriteLine("c in initialized in Class2")
End Sub
End Class

Shared Sub main()
'Creation of the object of type Class3
Dim c As New Class3()
Console.ReadLine()
End Sub
End Class

  
In this example creation of the object of type class3 calls the Class3 constructor and that constructor calls Class2 Constructor Class2 Calls Class1 Constructor.So first Class1 constructor will execute and then control will be trasfered to class2 constructor,After executing the class2 constructor the control will be treansfered to Class3 constructor.
means

Class1
|---->Sub New()---------------------|
| 'First it will execute <-----|
| End Sub |
| |
| Class2 |
|--->Sub New()----------------------|
| 'Second it will execute<-------|
| End Sub |
| |
| Class3 |
----Sub New()-----------------------|
'Third this will executes<-----|
End Sub

Means In the Creation of the object of type C, First Class1 Constuctor initializes the data, then class2 then class3.

So output of this program is
a in initialized in Class1
b in initialized in Class2
c in initialized in Class3
(More...)

Consider the following code:

public class Example extends Thread
{
public void run()
{
System.out.println ("Hello");
}

public static void main (String args[])
{
new Example();
}
}

Select the correct option regarding the preceding code ?

  
1, The code will not compile.
2, The code will compile but will not execute.
3, The code will compile, execute, and display Hello as the output.
4, The code will compile, execute, but will not display anything.

Answer : 4 (More...)

Consider the following two classes:

public class Example1
{
public static int num;
static
{
num=5;
}
}


public class Example2
{
public static void main(String[] args)
{
System.out.println(Example1.num);
}
}

Select the correct option regarding the preceding classes.?

  
1, The Example2 class will execute without any output.
2, The Example2 class will execute with the following output:5

3, The Example2 class will not compile as it does not calls the constructor of the Example 1 class.
4, The Example2 class will not execute as it does not calls the constructor of the Example 1 class.

Answer : 2 (More...)

What is public, static and void?

  
public :The keyword public is an access modifier that tells the C# compiler that the Main method is accessible by anyone.

static :The keyword static declares that the Main method is a global one and can be called without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created.

void : The keyword void is a type modifier that states that the Main method does not return any value. (More...)

How to print out all the variables in the Session?

  
Yes, Using "Session.Keys" we can get all session variables

In VB.NET

Dim strSesVar as string
For Each strSesVar In Session.Keys
Response.Write(strSesVar + " : " + Session(strSesVar).ToString() + "
")
Next


In C#

foreach (string strSesVar in Session.Keys)
{
Response.Write(strSesVar + " : " + Session[strSesVar].ToString() + "
");
} (More...)

How can I programmatically print XPS Files ?

  
The main steps to use the three-parameter AddJob(String, String, Boolean) method are as follows. (More...)

What is the difference between NULL AND VOID pointer?

  
NULL can be value for pointer type variables.
VOID is a type identifier which has not size.
NULL and void are not same. Example: void* ptr = NULL; (More...)

Why does my Windows application pop up a console window every time I run it?

  
Make sure that the target type set in the project properties setting is set to Windows Application, and not Console Application. If you're using the command line, compile with /target:winexe, not /target:exe. (More...)

Different between console application and windows application?

  
The windows application is graphical user interface(GUI),console application is character user interface(CUI). (More...)

Differences between Windows And Console Application?

  
http://www.dotnetspark.com/Forum/42-diff-bw-console-application-and-windows.aspx (More...)

byte A=100;
byte B=200;
byte C=A+B;
Console.Write(C);

  

Compile Time Error.

Cannot implicitly convert type 'int' to 'byte'
(More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend