Question
Download Solution PDFclass exam {int arr[10]; };
class e1: public exam {};
class e2: public exam {};
class derived : public e1, public e2 {};
int main (void)
{cout<< sizeof (derived);
return 0; }
assume that an integer takes 16 bits then predict output of above code.
Answer (Detailed Solution Below)
Detailed Solution
Download Solution PDFThe correct answer is option 2: 80
Key Points
- Given class:
class exam { int arr[10]; }; / 10 integers
Since each int is 16 bits = 2 bytes, total = 10 × 2 = 20 bytes - Inheritance hierarchy:
class e1 : public exam {}; / inherits exam class e2 : public exam {}; / also inherits exam class derived : public e1, public e2 {}; / contains two exam instances (through e1 and e2)
So, `derived` has two independent copies of exam (due to separate inheritance paths). - Size calculation:
- Each exam = 20 bytes
- derived = 20 (from e1) + 20 (from e2) = 40 bytes
- But since we're using `int arr[10]` and sizeof(int) = 2 bytes, total per exam = 10 × 2 = 20 bytes
- Total: 20 + 20 = 40 bytes
- However, due to padding and alignment done by the compiler (usually to align data on 4-byte or 8-byte boundaries), each exam class might be padded to 40 bytes in some compilers
- Thus, total could be 40 + 40 = 80 bytes in such cases
Hence, the correct answer is: option 2: 80
Last updated on Jul 3, 2025
-> NIELIT Scientific Assistant answer key 2025 has been released at the official website.
-> NIELIT Scientific Assistant admit card 2025 has been released.
-> NIELIT Scientific Assistant city intimation slip 2025 has been released at the official website.
-> NIELIT Scientific Assistant exam 2025 is scheduled to be conducted on June 28.
-> A total number of 113 revised vacancies have been announced for the post of Scientific Assistant in Computer Science (CS), Information Technology (IT), and Electronics & Communication (EC) streams.
-> Online application form, last date has been extended up to from 17th April 2025.
->The NIELT has revised the Essential Qualifications for the post of Scientific Assistant. Candidates must possess (M.Sc.)/ (MS)/ (MCA) / (B.E.)/ (B.Tech) in relevant disciplines.
-> The NIELIT Scientific Assistant 2025 Notification has been released by the National Institute of Electronics and Information Technology (NIELIT).