3AKIFT POS Test (Group B)

2023-01-31

Max. 50 points

Name:

Task Max. Achieved
1 10
2 15
3 10
4 15
Sum 50
Grading: >=45: 1, >=39: 2, >=33: 3, >25: 4, <=25: 5
  1. Answer the following statements indicating whether they are True or False.
    0-5 correct: 0 points, 6 correct: 2 points, 7 correct: 4 points, 8 correct: 6 points, 9 correct: 8 points, 10 correct: 10 points.
    Statement True False
    C++ is a strict superset of C.
    In C++, the scope resolution operator :: allows accessing identifiers within a given namespace.
    A C++ set is a container that stores key / value pairs.
    A C++ struct cannot have private members.
    Composition is a "has-a" relationship.
    The C++ Core Guidelines recommend to use struct only if the data members can vary independently.
    A C++ template allows functions and classes to operate with generic types.
    Operator overloading is the mechanism of basing an object or class upon another object ("is-a" relationship).
    A default constructor is a constructor which can be called with no arguments.
    When providing a destructor, one should also provide (or delete) the copy constructor and copy assignment.
  2. The following C++17 snippets are executed as a part of int main(.) (unless main is part of the snippet). All neccessary #includes are present.
    What do these code snippets write to stdout? Write their exact output below each snippet.
    If a snippet cannot be compiled, would crash or its output is undefined, it is enough to write “ERROR”. If there is no visible output, write "-".
    3 points for each correct answer.
    1. int val(123);
      std::cout << "Hello " << val() << std::endl;
      ERROR
    2. std::vector<int> v{1, 2, 3};
      for (auto it = v.rbegin(); it != v.rend(); ++it) std::cout << *it << " ";
      std::cout << std::endl;
      3 2 1
    3. int i{3};
      int& j{i};
      int k{i};
      j = 5;
      std::cout << i << ' ' << j << ' ' << k << std::endl;
      5 5 3
    4. template <class Type>
      void print_vector(const std::vector<Type>& v) {
        for (auto it = v.rbegin(); it != v.rend(); ++it) {
          std::cout << *it << std::endl;
        }
      }
      
      int main() {
        std::vector<int> v{1, 2, 3};
        print_vector(v);
        return 0;
      }
      3
      2
      1
    5. std::set<int> s{7, 5, 16, 8};
      s.insert(5);
      s.insert(6);
      std::cout << s.size() << std::endl;
      5
  3. Define a template struct TreeNode that serves as an element of a binary tree data structure. The objects created by the struct should contain a value of any given type as well as a pointer to the left and right child TreeNodes.
    struct treenode diagram
    A minimalistic diagram showing the struct.
    Define the struct as it is seen in the diagram above. You also have to add two constructors. The first takes a single argument holding the value of a node. The second takes three arguments: the value of a node and a pointer to the left and right child nodes. Do not allow implicit conversion to your new type. (10 points)
    1 point for declaring the template type
    1 point for correctly using the template type throughout the struct
    1+1 points for defining the struct including the semicolon at the end
    1+1+1 points for declaring the data members including default values
    1 points for complete explicit single argument ctor
    2 points for complete three argument ctor
    template <class Type>
    struct TreeNode {
      Type value;
      TreeNode* left = nullptr;
      TreeNode* right = nullptr;
      explicit TreeNode(Type value) : value{value} {};
      explicit TreeNode(Type value, TreeNode* next, TreeNode* prev)
        : value{value}, next{next}, prev{prev} {};
    };
  4. Define a template class Vector with operations to insert elements at the beginning and at the end. You do not need to define any operations, just declare them. Declare the big 3. It is enough to define the destructor. Make the compiler generate the default constructor. Properly use encapsulation. Give all data members reasonable default values. (15 points)
    1 point for declaring the template type
    1 point for correctly using the template type throughout the class
    1+1 points for defining the class including the semicolon at the end
    1+1+1 points for declaring the private data members
    1+1 for declaring the desired insert operations
    1 point for default ctor
    2 points for complete dtor incl. definition
    1+1 point for copy ctor and copy assignment declarations
    1 point for making the API public
    template <class T>
    class Vector {
    public:
      void push_back(T value);
      void push_front(T value);
      Vector() = default;
      ~Vector() { delete[] data_; };
      Vector(const Vector& v);
      Vector& operator=(const Vector& other);
    private:
      T* data_ = nullptr;
      size_t size_;
      size_t reserved_;
    };