c++ - Interview questions? -


hey went interview known global firm position of c++ developer. had multiple choice test of 30 questions answered of them correctly of them tricky , badly worded . had lot of time wrote down 1 of questions in opinion badly worded there exact copy of question . want know if me or question intentionally badly worded. believe every point count because of competition position.

question :

what shall flowing call return ?

sizeof(obj1);   

a. size of member functions of obj1 in bytes
b. size of data in obj1.
c. size of member functions , data of obj1.
d. none of answers correct.

i knew answer should size of object in bytes , chose option c . in opinion object contain member functions (code segment) , data (static , dynamic allocation). tester marked wrong answer .

the answer d because non of above correct. padding not part of data. consider following 2 examples:

#include <iostream>  struct test {     int x;     int y;     double d;     test(){} };  int main() {     test t;     std::cout << sizeof(t) << std::endl;      std::cin.get();     return 0; } 

will return 16 bytes of data types + padding while:

#include <iostream>  struct test {     int x;     double d;     int y;     test(){} };  int main() {     test t;     std::cout << sizeof(t) << std::endl;      std::cin.get();     return 0; } 

return 24 bytes data types + padding. int not larger because data alignment different. not b nor other answers, ergo d closest.


Comments