Samsung Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

output - 4,1
reason -
Class without any data members and member function such type of class is known as empty class. Size of object of empty class is always 1 byte.

When we create object of any class at that time object always gets 3 characteristics i.e.

State
Behaviour
Identity
When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.

- pramod August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

On my machine sizeof(int) gives 4 .
and sizeof(x) is also coming 4 . So , in this case doesn't it have state behaviour and identity ? .
Also , when i add a double to x , sizeof(x) becomes 16. ( sizeof(double) is giving 8 )
??
If size of state and behaviour is increasing then they shouldn't be zero when only int is there.

- praveenkcs28 February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

should be x instead of X in size of operator.

- pankaj October 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

question also contains why ? u have to write reason in space provided .....

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

Very Helpful. Thanks

- teachMyself August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Space is provided to distinguish between objects of the empty classes.
like if we create an array of empty class' objects.
then for compiler something should be assigned in memory to differentiate array[0] and array[1];

but what is the purpose of empty class is another big question :)

- Siddhesh August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i had writeen this code ...and i get ans as 2,1...

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

#include<iostream>
using namespace std;

class z
{
};


class x
{
int a;
};



int main()
{
std::cout << sizeof(x) << '\n';
std::cout << sizeof(z) << '\n';
}

Run and check, you will get answers as 4, 1 as said before.

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

it also depends on the compiler, both are correct

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

http : // stackoverflow . com/questions/2362097/why-is-the-size-of-an-empty-class-in-c-not-zero

- rasmiranjanbabu March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What will happen if there are multiple variables in a class?
class z
{char a;
float b;
int k;
};
what will sizeof(z) will return and why?

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

The sizeof(z) here will return a value of 7 as char=1 ,int=2 and float=4 bytes but then again ,the output may vary from compiler to compiler.

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

due to padding the sizeof(z) will be 12

- Swag November 15, 2014 | 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