DRAG DROP
You are developing a C# console application that outputs information to the screen. The
following code segments implement the two classes responsible for making calls to the
Console object:
When the application is run, the console output must be the following text:
Log started
Base: Log continuing
Finished
You need to ensure that the application outputs the correct text.
Which four lines of code should you use in sequence? (To answer, move the appropriate
classes from the list of classes to the answer area and arrange them in the correct order.)
Answer: See the explanation.
Note:
* The abstract keyword enables you to create classes and class members that are
incomplete and must be implemented in a derived class.
* An abstract class cannot be instantiated. The purpose of an abstract class is to provide a
common definition of a base class that multiple derived classes can share.
Box 4 should be
((Logger)logger).LogCompleted();
yeap
https://msdn.microsoft.com/en-us/library/ms173153.aspx
Box 4 is ((Logger)logger).LogCompleted();
Tested by console
Hi Mr D. Are you sure that Box is exactly ((Logger)logger).LogCompleted();?
((Logger)logger).LogCompleted();
The answer displayed above is correct i tested it myself in visual studio answer is:
BaseLogger logger = new Logger();
logger.Log(“Log Started”);
logger.Log(“Base: Log Continuning”);
logger.LogCompleted();
try again
box 4 is : ((Logger)logger).LogCompleted();
…and you didn’t notice, that the last string in console was “Completed” instead of “Finished”
I confirm, the Box 4 must be((Logger)logger).LogCompleted();
In case of using logger.LogCompleted(); console output will be “Completed”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
BaseLogger logger = new Logger();
logger.Log(“Log started!”);
logger.Log(“Base: Log Continuing”);
((Logger)logger).LogCompleted();
Console.ReadLine();
}
}
public abstract class BaseLogger
{
public virtual void Log(string message)
{
Console.WriteLine(“Base: ” + message);
}
}
public class Logger : BaseLogger
{
public override void Log(string message)
{
Console.WriteLine(message);
}
public void LogCompleted()
{
Console.WriteLine(“Finished”);
}
}
}
This is the point thing at the keyword new in the method Logger.LogCompleted() that hides method LogCompleted() inherited from BaseLogger class . If would replace the new keyword with override this answer would be true:
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log Countinuing”);
logger.LogCompleted();
In this question we must in this way answer:
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“Base: Log Countinuing”);
((Logger)logger).LogCompleted();
The 4th is
((Logger)logger).LogCompleted();
because in the first line, we declared the logger to be a “base logger”. So when we call LogComplete, we automatically call if trom the BaseLogger class, as this is the type of our ‘logger’-variable.
So ‘logger.LogComplete()’ is short for actually this code:
((BaseLogger)logger).LogComplete();
6,5,1,7
BaseLogger logger = new Logger();
logger.Log(“Log Started”);
logger.Log(“Base: Log Continuning”);
logger.LogCompleted();
Cause “public NEW void Log.Complited”
With “new” sword logger don`t know about base LogCompleted in BaseLogger class.
just now verified by writing code in VS
it has to be ((Logger)logger).completed();
6517
BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“base : Log continuing”);
((Logger)logger).LogCompleted();
BaseLogger logger = new Logger();
((Logger)logger).Log(“Log started”);
logger.Log(“base : Log continuing”);
((Logger)logger).LogCompleted();