CPE 212 Sample Exam II This is just one page of a sample exam I am working to st
ID: 3717780 • Letter: C
Question
CPE 212 Sample Exam II
This is just one page of a sample exam I am working to study for my final at 3PM today, I have posted the others as well, I will put "CPE 212 Sample Exam II" in each post so it's easy to find the others.
16 got cut off:
16. What is the output of the following code segment assuming it appears in the function main()?
if (true){
D dddd;
}
Explanation / Answer
16. What is the output of the following code segment assuming it appears in the function main()?
if (true){
D dddd;
}
Answer: ABD) None of the answers provided.
Explanation: The output of the given code will be ADda.
The reason is we are creating object for class D, which is a subclass of A
So, the constructor of A is called first then the constructor of D, followed by the destructor of D and then the destructor of A.
11. What is the output of the statement A aa; assuming it appears in the function main()?
Answer: The output of the above statement will be Aa.
First the statement in the constructor is executed followed by the destructor.
12. What is the output of the statement B bb; assuming it appears in the function main()?
Answer: ABba
Explanation: As B is inherited from A, creating an object to B, first executes the constructor of A, then the constructor of B followed by the destructors of b and a.
13. What is the output of the statement C cc; assuming it appears in the function main()?
Answer: AABBCcba
Explanation: As C is inherited from B which is inturn inherited from A, first the constructor of A(print A) is called then the constructor of B(prints AB) followed by the constructor of C (prints BC), next the destructors of C, B and A are called respectively. Hence the output is AABBCcba
14. What is the output of the statement B bbb(2); assuming it appears in the function main()?
Answer: AABBba
Explanation: We know that B is inherited from A so obviously first constructor of A is executed then the constructor of B is executed, here we are passing 2 as parameter while creating object for B hence the parameterized constructors of A and B are called and prints AABB next the destructors are called and prints ba
So the output is AABBba
15. What is the output of the following statement assuming it appears in the function main()
C* ccc = new C(2);
Answer: ABCC
Explanation: We are creating a pointer to Class C which is inherited from B which is inturn inherited from A. As we are passing 2 as parameter the parametrized constructor is called and prints ABCC. Observe that here the destructors are not called because we are creating a pointer and this will not loose its scope until we delete the pointer.