---
# System prepended metadata

title: CS3307 2019 Sample Final Exam

---

# CS3307 2019 Sample Final Exam

## Multiple choice

1. In C++, a non-null pointer always “points to” data allocated on the heap.
    - No
2. If a variable `i` is declared as:
    ```c++
    const int i = 0;
    ```
    then the value of i cannot be changed in subsequent code.
    - Tru
3. The syntax const `int *bob` indicates that `bob` is a:
    - Regular pointer to constant int type
4. Under what circumstances should you declare a function as `const`?
    - When the function does not modify `*this`
5. Space is always allocated on the stack when:
    - A function is called
6. Space is always allocated on the heap when:
    - `new` is invoked
7. Consider the following C++ code snippet:
    ```c++
    int i = 4;
    int j = 5;
    
    int *k = &i;
    int& l = j;
    
    *k = 9;
    l = 44;
    
    cout << i << " ";
    cout << j << " ";
    cout << *k << " ";
    cout << l << endl;
    ```
    What will be the output of the snippet? Assume `cout` and `endl` have been imported.
    - `9 44 9 44`
8. Consider the following function:
    ![](https://i.imgur.com/5h7gTLV.png)
    
    Which of the following statements about the class/type `T` must be true in order for the code to compile and run without crashing?
    - It must be some kind of numeric type.
9. In C++, the destructor for an object is always called when the object is destroyed.
    - True
10. In C++, only operations that are declared to be virtual can be overridden in derived classes.
    - False
11. All member functions in a C++ class have a `this` pointer.
    - False
    > Still needs to be confirm
12. The story points of a user story:
    - Are used to indicate the approximate size and complexity of a story.
13. In a UML class diagram, there can never be more than one association or relationship between any two classes in the diagram.
    - False
14. Which of the following statements is false?
    - All of the above statements are true, not false. 
15. Why does the Interface Segregation Principle aim to avoid fat interfaces?
    - Any class inheriting the interface must provide an implementation for all interface functions, which may be empty or meaningless.
16. Which of the following are true about a successful implementation of the Singleton design pattern?
    - It ensures that exactly one copy of the object always exists during execution.
    > Still needs to confirm
17. Which of the following statements about the Decorator pattern are true?
    - It can be used to add and remove behaviour dynamically at run time.
18. Creational design patterns:
    - Are used to encapulate the creation of objects. 
19. The Adapter pattern:
    - All of the above is true 
20. The Builder pattern is used to:
    - Create objects through a similar process of steps, but allow the actual representation to vary.


## Fill in the blanks

21. Consider the following class declarations
![](https://i.imgur.com/qVnSVPr.png)
    For each of the following, indicate the method called (by its number in the comments above) if we were to replace line 21 with the line given. If the line would not compile, indicate WNC.
    > The whole question is a bait... Since it doesn't have `public` in both class, therefore all the methods are `private` by default, so all the blanks should be **WNC**. According to the prof.
    
22. Given the following class definitions
    ```c++
    #include<iostream> 
    using namespace std;
    
    class X {
        public:
        virtual void a() { cout << "X::a"; }
        void b() { cout << "X::b"; }
        virtual void c() { cout << "X::c"; }
        virtual void c(int r) { cout<<"X::c(r)"; }
        void d() { cout << "X::d"; }
    };
    
    class Y : public X { 
        public: void a() { cout << "Y::a"; } 
        void b() { cout << "Y::b"; } 
        virtual void d() {cout<<"Y::d";} 
        void e() { cout << "Y::e"; }
    };
    
    class Z : public Y { 
        public:
        void c() { cout << "Z::c"; } 
    };
    ```
    - `X * xx = new X;  compiles` 
    - `X * xy = new Y; compiles`
    - `X * xz = new Z; compiles`
    - `Y * yx= new X; Error invalid conversion from X* to Y*`
    - `Y * yy = new Y; compiles`
    - `Y * yz = new Z; compiles` 
    - `Z * zx = new X; Error: invalid conversion from X* to Z*`
    - `Z * zz = new Z; compiles`
    
    All outputs
    ```
    X::a
    X::b
    X::c
    
    Y::a
    X::b
    X::c
    X::c(r)
    X::d
    
    Y::a
    X::b
    Z::c
    
    Y::a
    X::c
    
    Y::a
    Z::c
    X::c(r)
    
    Y::a
    Z::c
    ```
    
    