C++ | Class and Object | Question 5
Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects.
Predict the output following program.
#include<iostream> using namespace std; class Test { static int x; int *ptr; int y; }; int main() { Test t; cout << sizeof(t) << " "; cout << sizeof(Test *); } |
chevron_right
filter_none
(A) 12 4
(B) 12 12
(C) 8 4
(D) 8 8
Answer: (C)
Explanation: For a compiler where pointers take 4 bytes, the statement “sizeof(Test *)” returns 4 (size of the pointer ptr).
The statement “sizeof(t)” returns 8. Since static is not associated with each object of the class, we get (8 not 12).
Quiz of this Question
Recommended Posts:
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 2
- C++ | Class and Object | Question 6
- C++ | Class and Object | Question 3
- C++ | Class and Object | Question 4
- Java | Class and Object | Question 4
- Java | Class and Object | Question 7
- Java | Class and Object | Question 1
- Java | Class and Object | Question 2
- Java | Class and Object | Question 5
- Java | Class and Object | Question 4
- Java | Class and Object | Question 6
- Object Slicing in C++
- Exception handling and object destruction | Set 1
- Where is an object stored if it is created inside a block in C++?



