Morgan Stanley Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

- Anonymous November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

well in multi thread programs i think we should declare th object in shared memory

- Anonymous June 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in java singleton classes can be created by declaring the constructor as private

- baskar July 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A singleton is an object-oriented design pattern in which only one instance of the class can be instantiated at a given time. An example would include a class that handles the printer queue. Instead of creating a new instance of the class, you would call a class function to retrieve the current instance of the class.

An example of this in Java:

public class MySingleton
{
    // reference to current instance
    private static MySingleton instance = null;

    // making the constructor private ensures only we can call this
    private MySingleton()
    {
        // do stuff
    }

    // the synchronized keyword makes it thread safe
    public static synchronized getInstance()
    {
        if(instance == null)
        {
            instance = new MySingleton();
        }
        
        return instance;
    }
}

Application code would use the MySingleton class like this:

MySingleton singleton = MySingleton.getInstance();

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

^^^ Correction to code above.

public static synchronized getInstance()

should be:

public static synchronized MySingleton getInstance()

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a design pattern to ensure that only one instance of class get created
1. hide constructor
2. private static instance
3. public static getInstance
4. synchronized (need to double check when using mutex) to avoid creating multiple copies.

- Anonymous September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make sure the methods in the singleton class be thread-safe (use synchronized declaration )

- Anonymous May 18, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More