Which four lines of code should you use in sequence?

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.)

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.

Explanation:
Box 1:

Box 2:

Box 3:

Box 4:

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.



Leave a Reply 17

Your email address will not be published. Required fields are marked *


Dhruv

Dhruv

Box 4 should be

((Logger)logger).LogCompleted();

Mr D

Mr D

Box 4 is ((Logger)logger).LogCompleted();
Tested by console

lucky

lucky

Hi Mr D. Are you sure that Box is exactly ((Logger)logger).LogCompleted();?

Leonardo

Leonardo

((Logger)logger).LogCompleted();

Maurice

Maurice

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();

Rik

Rik

try again
box 4 is : ((Logger)logger).LogCompleted();

Gleb

Gleb

…and you didn’t notice, that the last string in console was “Completed” instead of “Finished”

Ivan

Ivan

I confirm, the Box 4 must be((Logger)logger).LogCompleted();
In case of using logger.LogCompleted(); console output will be “Completed”

JZED

JZED

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”);
}

}

}

Elvis

Elvis

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();

Guest

Guest

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();

Fydra

Fydra

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.

harsh

harsh

just now verified by writing code in VS
it has to be ((Logger)logger).completed();

Begia

Begia

BaseLogger logger = new Logger();
logger.Log(“Log started”);
logger.Log(“base : Log continuing”);
((Logger)logger).LogCompleted();

Arun Rana

Arun Rana

BaseLogger logger = new Logger();
((Logger)logger).Log(“Log started”);
logger.Log(“base : Log continuing”);
((Logger)logger).LogCompleted();