473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Sizeof problems(interview questions)


Class A
{
int a;
};

cout<<"the size of A is "<< sizeof(A)<<endl;

Q: what is the result?

Now Add in a function, int getVal();

What's the result now?

Explain why

Jul 23 '05 #1
10 3566
ma****@gmail.com wrote:
Class A
{
int a;
};
Syntax error. What is `Class'? ('class', we know; C++ is case sensitive.)
cout<<"the size of A is "<< sizeof(A)<<endl;
Assuming that `A' has been declared correctly above, the answer is
`whatever sizeof(A) *says* it is.
Q: what is the result?

Now Add in a function, int getVal();

What's the result now?
Similarly.

Explain why

Because doing your own homework is something called, well, not cheating.

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
On 7 Jul 2005 18:25:02 -0700, ma****@gmail.com wrote in comp.lang.c++:

Class A
{
int a;
};

cout<<"the size of A is "<< sizeof(A)<<endl;

Q: what is the result?
What did you answer? The only answer that the C++ language provides
is that sizeof(A) is greater than or equal to sizeof(int). In
numbers, 1 or greater.
Now Add in a function, int getVal();

What's the result now?
The answer to the first question is equally accurate for the second,
still >= sizeof(int), still >= 1.
Explain why


Because. Further explanation is pointless, as is trying to impress
whoever it is came up with this as a useful question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3
Come on. I am providing a question for people to practise/discuss, not
asking for the answer for myself.

If you feel confident, just give your answer instead of laughing at
people.

Jul 23 '05 #4
You haven't answered the question yet.
Greater or equal to sizeof(int)?
Does the later "adding " affect the result at all?

Jul 23 '05 #5
Me
ma****@gmail.com wrote:
class A
{
int a;
};

cout<<"the size of A is "<< sizeof(A)<<endl;

Q: what is the result?
Prints out a number >= sizeof(int)
Now Add in a function, int getVal();

What's the result now?
Prints out the same thing.
Explain why


A is still POD.

Jul 23 '05 #6
On 7 Jul 2005 19:33:38 -0700, ma****@gmail.com wrote in comp.lang.c++:
You haven't answered the question yet.
Greater or equal to sizeof(int)?
Does the later "adding " affect the result at all?


Usually I don't even answer people who can't even be bothered to quote
correctly. If you make the choice to use the broken Google Groups
interface instead of a proper newsreader, it is still up to you to
quote properly.

And yes, I answered both questions. Learn how to read. And how to
quote.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #7
Me wrote:
ma****@gmail.com wrote:
class A
{
int a;
};
[..]


A is still POD.


No, it ain't. 'a' is private, and that makes it non-POD.

V
Jul 23 '05 #8
ma****@gmail.com wrote:
You haven't answered the question yet.


Well, answers were given. I assume you asked the questions because you don't
know the answers, so how do you know that the answers you were given are
inadequate? What sort of answers would satisfy you?

DW
Jul 23 '05 #9
Me
Victor Bazarov wrote:
Me wrote:
ma****@gmail.com wrote:
class A
{
int a;
};
[..]


A is still POD.


No, it ain't. 'a' is private, and that makes it non-POD.


Bleh. Well, so anyway, the reason why adding a non-virtual function
can't add space is 9.2/12 "so might requirements for space for managing
virtual functions (10.3) and virtual base classes (10.1)." neither of
which apply here.

Jul 23 '05 #10
ma****@gmail.com wrote:
You haven't answered the question yet.
Greater or equal to sizeof(int)?
Does the later "adding " affect the result at all?


He DID answer your question. Let me reformulate, since you don't seem
to understand. The C++ language says this:

- sizeof(int) >= 1
- The size of any class is greater or equal to size of every member in
the class.
- If the class has any virutal methods then its size may be greater.

That's it. There's no way of knowing more precisely what sizeof(A) is.
If someone at an interview expected you to provide a more precise
answer without specifying which platform they are talking about then
they are clueless and you shouldn't bother trying to get the job they
are offering.
Bart.

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
1
by: Rick | last post by:
I'm having problems with EnterpriseServices transactions running against Oracle 9iR2. I am inconsistently getting Oracle ORA-24761: Transaction Rolled Back results mid-transaction. If I start...
27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
71
by: cj | last post by:
Dear friends, I have one more questions for everyone in the newsgroup: I am preparing for an interview on UNIX/C++. Could you please identify some of the most important questions which might be...
26
by: pandapower | last post by:
Hi, These were some of the interview questions asked,can someone discuss whats the solution. 1.I have a singly linked list and i have to print the 5th element from the last.I have to print it...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
14
by: Eugeny Myunster | last post by:
Hello all, How can i emulate sizeof() only for integers?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.