VMWare Inc Interview Question for Java Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
3
of 5 vote

class Singleton
{
	private static Singleton instance;
	private Singleton()
	{
		...
	}

	public static synchronized Singleton getInstance()
	{
		if (instance == null)
			instance = new Singleton();

		return instance;
	}
	...
	public void doSomething()
	{
		...	
	}
}

- nueman fernandez June 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

add synchronization

- tarun June 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice example of ill-formed singleton class. Just imagine what will happen in case if 100 threads will call to this method – it will block all of them one by one just to check that singleton instance is not null.

- gevorgk June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Isn't that better than not synchronizing it and winding up with more than one instances of the class?

- teli.vaibhav June 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@teli.vaibhav: consider this:
A printer that takes print command from several threads. Using more than one instance of printer object is not a good idea.

- Prithvi June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Singleton {
	private static volatile transient Singleton instance;

	private Singleton() {
	}

	public static Singleton getInstance() {
		if (instance == null) {
			synchronized(Singleton.class) {
				if(instance == null) {
					instance = new Singleton();
				}
			}
		}
	}
}

- Anonymous July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

nice but what happens if some one clone your object

- Mahipal November 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code doesn't cover the case of instance!=null. Just adding for other users.

- pawanKjajara February 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SingleT {

private:

SingleT(){}
SingleT(const SingleT& obj){}
const SingleT& operator= (const SingleT& rhs){}

public:

static SingleT* obj;

static SingleT* creator(){

if (!obj)
{
obj = new SingleT();
}
return obj;
}

};


Obviously, I have not taken care of thread safety here. you can google it and will get easily.

- prashant June 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a> public class SingleInst {
public static final SingleInst INSTANCE = new SingleInst();
private SingleInst() {
...
}
... // Remainder code...
}

B> // Singleton with static factory
public class SingleInst {
private static final SingleInst INSTANCE = new SingleInst();
private SingleInst() {
...
}
public static SingleInst getInstance() {
return INSTANCE;
}
... // Remainder code..
}

c> With New java version - you could use enums too..

- pranab June 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Singleton
{
private static Singleton singleInstance;

static
{
singleInstance = new SingleInstance();

}

private Singleton()
{
// This can not be called
}

public Singleton getInstance()
{
return singleInstance;
}
}

- ankit0913 June 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let me correct the above one:

public class Singleton
{
private static Singleton singleInstance;

static
{
singleInstance = new Singleton();

}

private Singleton()
{
// This can not be called
}

public Singleton getInstance()
{
return singleInstance;
}
}

- ankit0913 June 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

nice but what happens if some one clone your object

- Mahipal November 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Singleton {
   private static final Singleton instance;   
 
   private Singleton(){}
 
   public static Singleton getInstance() {
     if (instance == null)
       instance = new Singleton();
     return instance;
   }
 }

As you can see, the constructor is private, so we are unable instantiate it in the normal fashion. What you have to do is call it like this:

1

public Singleton singleton = Singleton.getInstance();

When you do this, the getInstance() method then checks to see if the parameter ‘instance’ is null. If it is, it will create a new one by calling the private constructor. After that, it just returns it. Of course, if it is not null, it just returns the existing instance of it. This insures that there is only one copy of the object within your program.

- amnesiac June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we extend a singleton class? In that case, could we consider singleton class as doing its singleton job?

- Sriram August 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Singleton {
public static Singleton instance;

private Singleton(){

}

public static Singleton getInstance(){
if(instance != null){
return instance;
}
synchronized(Singleton.class){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
}

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

for complete singleton java code refer to - javadiscover.com/2013/02/how-to-create-fully-singleton-design.html

- anand September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

CTest.hpp

#include <iostream>

class CTest {
private:
	int m_nVal;
	static CTest * m_pInstance;

	//! Constructor.
	CTest() : m_nVal(0) {
		std::cout << "CTest(): " << m_nVal << std::endl;
	}

	//! Disable copy constructor.
	CTest(const CTest&) = delete;
	//! Disable assignment operator.
	CTest& operator = (const CTest&) = delete;

public:
	//!Distructor
	~CTest() {
		std::cout << "~CTest()" << std::endl;
	}
	static CTest& getInstance();
};

CTest.cpp

#include "CTest.hpp"
#include <mutex>

std::once_flag o_flg;
CTest* CTest::m_pInstance = NULL;
CTest& CTest::getInstance() {
	call_once(o_flg, []() {
		if (!m_pInstance) {
			m_pInstance = new CTest();
		}
	}
	);
	return *m_pInstance;
}

main.cpp

#include "CTest.hpp"
#include <thread>
#include <vector>

void createInst() {
	CTest& mInst = CTest::getInstance();
}

int main() {
	{
		std::vector<std::thread> tLst;
		for (size_t i = 0; i < 100; i++) {
			tLst.push_back(std::move(std::thread(createInst)));
		}

		for (auto& t : tLst) t.join();
	}

	std::cin.get();
	return 0;
}

- Passerby_A March 06, 2014 | 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