Bloomberg LP Interview Question for Software Engineer / Developers






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

If there is a default constructor then both are same, otherwise different.
According to the latest C++ 2003 standard,
new T calls default initializer and,
new T() calls value initializer.

No constructor :
new T:
It causes default initialization of non-POD(*1) types.
POD types remain uninitialized. That is, indeterminate value.
new T():
For non-POD types default initialization.
POD types are zero initialized.

If there is a constructor which doesn't list the variables, then the same thing happens, default initialization for non-POD and zero/random values for POD according to new()/new.

*1. POD is Plain Old Data. That is a C-like struct. Ctors/Dtors/Funcs. etc make it non-POD.

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

If T is a user defined class/struct, there is no difference.
However,
int *o = new int; // *o has not been initialized
int *o = new int(); // *o has been default initialized, ie *o == 0

- Anonymous May 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Tested, the output is the same

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int *o = new int;
8 int *p = new int();
9 cout << *o << endl;
10 cout << *p << endl;
11 }

./a.out
0
0


~

- Anonymous June 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

really it is compiler dependent. check out of below code
A is -842150451 B is -842150451
x is 009294A0 y is 0092A038

int main_contr()
{
	class a
	{
		int aaa;
	public:
		a()
		{

		}
		int geta()
		{
			return aaa;
		}
	};
	a* A = new a;
	a* B = new a();
	cout << "A is " << A->geta() << " B is " << B->geta();
	int* x = new int;
	int* y = new int();
	cout << "\nx is " << x << " y is " << y;
	return 0;
}

- Sirius April 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey I tested that code too.. result is different..
int *w=new int; prints garbage and the other one prints zero

- rocks September 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you have an idea about why this should happen.

- Anonymous March 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If T is POD, there is no difference between
new T;
new T ();
in both cases all members initialized.

But I was surprised to find that:
T t;

leaves all class members uninitialized

- Leo March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

both have the same functionality...in first case the constructor is called implicitly while in the second case constructor is explicitly called

- camSun May 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If this is the case then below code should not compile:

class test
{
public:
	explicit test() {
		cout<<"constructor called"<<endl;
	}
};

void main()
{
	test *ptr1 = new test;
	test *ptr2 = new test();
	
	getchar();
}

- jassi May 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you got it

- camSun May 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud

- prashant May 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

explicit has no effect on default constructors

- Anonymous January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- siva.sai.2020 May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no you are wrong!! this code compiles and runs too!!

- Anonymous November 10, 2012 | Flag


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