3AKIFT POS Test (Group A)

2023-01-11

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
    Every C code can be directly compiled as C++.
    In C++, files automatically create namespaces.
    The C++ standard library offers containers like list, map and set.
    A C++ struct offers roughly the same features as a C++ class.
    Encapsulaton allows limiting direct access to an object's data members.
    The C++ Core Guidelines recommend to use struct only if the data members can vary independently.
    A C++ template is a guideline describing best practices for writing code.
    Operator overloading allows to use the built in C++ operators on own types.
    A default constructor is always automatically generated.
    It is a best practice to provide a destructor, even without providing a 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;
      Hello 123
    2. std::vector<int> v{1, 2, 3};
      for (const auto& e : v) std::cout << e << " ";
      std::cout << std::endl;
      1 2 3
    3. void f(int i) {
        i = 5;
      }
      
      int main() {
        int i{3};
        int& j{i};
        f(i);
        f(j);
        std::cout << i << ' ' << j << std::endl;
        return 0;
      }
      3 3
    4. template <class Type>
      void print_vector(const std::vector<Type>& v) {
        for (const auto& e : v) {
          std::cout << e << std::endl;
        }
      }
      
      int main() {
        std::vector<int> v{1, 2, 3};
        print_vector(v);
        return 0;
      }
      1
      2
      3
    5. std::list<int> l{7, 5, 16, 8};
      l.push_front(1);
      l.push_back(3);
      std::cout << l.size() << std::endl;
      6
  3. Define a template struct ListNode that serves as an element of a doubly linked list. The objects created by the struct should contain a value of any given type as well as a pointer to the next and previous ListNodes. Define 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 previous and next 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
    1 points for complete explicit single argument ctor
    2 points for complete three argument ctor
    template <typename T>
    struct ListNode {
      T value;
      ListNode* next = nullptr;
      ListNode* prev = nullptr;
      explicit ListNode(T value) : value{value} {};
      explicit ListNode(T value, ListNode* next, ListNode* 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_;
    };