Sap Labs Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

The problem is that in the line
You have explicitly called the destructor for s object and then at the closing brace of the main function destructor is again been called i.e. what the C++ guarantees.
And if freed memory is again freed, it is undefined behaviour by the Standard.

- Luv June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

three problems
1) u have called destructor explicitly and will be also called at the end of prog
so trying to free memory twice.......
2) after calling destructor s->~A() u is assigned to S
3) copy constructor is not present.... default copu constructor do shallow copy
here deep copy is reqd
A s("Object s");
A t=s;
only above two lines in main will also give error.............

- Anonymous June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

And don't forget the use of delete instead of delete[]

- eugene.yarovoi June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Problem:
- you are trying to allocate memory for array and deleting only for one object
- calling delete through destructor twice.

Solution:
- use delete p[]
- in destructor, have a check whether the element p is null or not, then delete it.

- sjain October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Compiler errror....explicit call of destructor i.e is s.~A().

- govind.chauhan143 June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

explicit destructor call is allowed in C++, it's not an error

- Luv June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Explicit constructor is only allowed in case of placement new. Otherwise it is not allowed

- Saurabh December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will show core dump, because you have explicitly deallocate the memory of object "s" and in next statement u are assigning the object "s" to another object, so shallow copy can not takes place because s has no memory.
Another reason for core dump is one memory has been deallocated two times .

- Aalok June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One mistake in destructor ie
instead of delete p; it should be delete [] p;

- Vivek Kumar July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no error will be reported.
The only error is absence of copy constructor, so the two line
// A t=s; and u=s;
will not work.

- TopCoder August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no error will be reported.
The only error is absence of copy constructor, so the two line
// A t=s; and u=s;
will not work.

- TopCoder August 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the only problem in this code is that it did not have a copy and assignment constructors and my code is here.....


#include <iostream>
#include <string.h>
#include<cstdio>
using namespace std;


class A
{
char *p;
public:
A(const char* str)
{
cout<<"parameterized constructor"<<endl;

p=new char[strlen(str)+1];
strcpy(p,str);
}

~A()
{
delete p;
}

A& operator =(const A &a)
{
cout<<"assignment constructor"<<endl;

p=new char[strlen(a.p)+1];
strcpy(p,a.p);
}
void print(){ cout<<"string= "<<p<<endl;}

A(const A &a){
cout<<"copy constructor"<<endl;
p=new char[strlen(a.p)+1];
strcpy(p,a.p);
}
};

int main()
{
A s("Object s");
s.print();
A t = s;
s.~A();
A u("Object u");
u=s;
return 0;
}

- bhavesh October 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <iostream>
#include <cstring>

using namespace std;

class A
{
	private:
		char* p;
	
	public:
		A(const char* str)
		{
			if (str == NULL)
			{
				throw "Null pointer exception.";
			}

			p = new char[strlen(str) + 1];
			strcpy(p, str);
			cout << "Parameterized constructor: " << p << endl;
		}

		~A()
		{
			if(p != NULL)
			{
				cout << "Destructor: " << p << endl;
			}

			delete p;
			p = NULL;
		}


		A& operator=(const A &a)
		{
			if (a.p == NULL)
			{
				throw "Null pointer exception.";
			}

			if (p != NULL)
			{
				delete p;
			}

			p = new char[strlen(a.p) + 1];
			strcpy(p, a.p);
			cout << "Assignment operator: " << p << endl;
			return *this;
		}

		A(const A& a)
		{
			if (a.p == NULL)
			{
				throw "Null pointer exception.";
			}

			p = new char[strlen(a.p) + 1];
			strcpy(p, a.p);
			cout << "Copy constructor: " << p << endl;
		}

};

int main()
{
	try
	{
		A s("Object s");
		A t = s;
		s.~A();
		A u("Object u");
		u = s;
	}
	catch(const char* msg)
	{
		cout << msg << endl;
		return -1;
	}

	return 0;
}

- Alex September 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cstring>

using namespace std;

class A
{
	private:
		char* p;
	
	public:
		A(const char* str)
		{
			if (str == NULL)
			{
				throw "Null pointer exception.";
			}

			p = new char[strlen(str) + 1];
			strcpy(p, str);
			cout << "Parameterized constructor: " << p << endl;
		}

		~A()
		{
			if(p != NULL)
			{
				cout << "Destructor: " << p << endl;
			}

			delete p;
			p = NULL;
		}


		A& operator=(const A &a)
		{
			if (a.p == NULL)
			{
				throw "Null pointer exception.";
			}

			if (p != NULL)
			{
				delete p;
			}

			p = new char[strlen(a.p) + 1];
			strcpy(p, a.p);
			cout << "Assignment operator: " << p << endl;
			return *this;
		}

		A(const A& a)
		{
			if (a.p == NULL)
			{
				throw "Null pointer exception.";
			}

			p = new char[strlen(a.p) + 1];
			strcpy(p, a.p);
			cout << "Copy constructor: " << p << endl;
		}

};

int main()
{
	try
	{
		A s("Object s");
		A t = s;
		s.~A();
		A u("Object u");
		u = s;
	}
	catch(const char* msg)
	{
		cout << msg << endl;
		return -1;
	}

	return 0;
}

- Anonymous September 12, 2015 | 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