What should you do?

You have the following code (line numbers are included for reference only):

You need to ensure that new instances of Connection can be created only by other classes by calling the Create method. The solution must allow classes to inherit from Connection.
What should you do?

A. Option A
B. Option B
C. Option C
D. Option D

microsoft-exams

9 thoughts on “What should you do?

  1. UPDATED : Option 4 is answer. Try to run this program. only this case runs.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;

    namespace Rextester
    {
    //public abstract class Connection
    //public static class Connection
    public class Connection
    {
    //private Connection(){}
    protected Connection(){}

    public static Connection Create()
    {
    return new Connection();
    }
    }
    public class Program : Connection
    {
    public static void Main(string[] args)
    {
    Connection abc = Connection.Create();
    //Your code goes here
    Console.WriteLine(“Hello, world!”);
    }
    }
    }

  2. I kinda thought C as well. There’s no private constructor defined. This code (if it’s to be a singleton connection) would need it. It would also need a static connection field. The Q is a bit lame.

  3. Correct answer is A.

    “The solution must allow classes to inherit from Connection.”, this rules out B as no classes can inherit from static classes.

    “You need to ensure that new instances of Connection can be created only by other classes by calling the Create method.”. This is a tricky statement but C ensures no other class can see it. D would allow inherited classes to call it (similar to abstract class) and abstract still has access to it. A would enforce other classes to call to create it, but static classes can still call there own static methods. So I would go back to A.

      1. Option 4 is answer. Try to run this program. only this case runs.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text.RegularExpressions;

        namespace Rextester
        {
        //public abstract class Connection
        //public static class Connection
        public class Connection
        {
        private Connection(){}
        //protected Connection(){}

        public static Connection Create()
        {
        return new Connection();
        }
        }
        public class Program : Connection
        {
        public static void Main(string[] args)
        {
        Connection abc = Connection.Create();
        //Your code goes here
        Console.WriteLine(“Hello, world!”);
        }
        }
        }

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.