2AKIFT POS Test (Group A)

2024-05-14

Max. 100 points

Name:

Task Max. Achieved
1 20
2 20
3 20
4 40
Sum 100
Grading: >87.5: 1, >75: 2, >62.5 : 3, >50: 4, <=50: 5
  1. Answer the following statements indicating whether they are True or False.
    0-5 correct: 0 points, 6 correct: 4 points, 7 correct: 8 points, 8 correct: 12 points, 9 correct: 16 points, 10 correct: 20 points.
    Statement True False
    C++ allows procedural, object oriented, generic and functional programming.
    In C++, the scope resolution operator :: allows accessing identifiers within a given namespace.
    A C++ vector is a container that stores key / value pairs.
    A C++ compiler translates high level code to a lower level (assembly language).
    The assembler combines object files into a unified executable program.
    It is recommended to use magic-based for-loops wherever possible.
    A function declaration serves as a promise to the compiler that the function will exist when linking.
    using namespace std is a good practice in C++ programs.
    It is impossible to write C++ close to the fundamental aspects of hardware like C.
    Range-based for loops provide easy access to each element in a container.
  2. The following C++20 snippets are executed as a part of int main(.) (unless main is part of the snippet). Assume all neccessary #includes to be 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 "-".
    4 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. char name[] = "Sue";
      char* n = name;
      while (*n) {
        std::cout << *n;
        n++;
      }
      Sue
    5. int a = 10;
      int* p = &a;
      int** pp = &p;
      **pp *= **pp;
      std::cout << a;
      100
  3. Implement the function
    std::size_t str_len(char* str);
    that returns the number of characters in str excluding the trailing '\0'. Do not use any library functions. (20 points)
    4 points for declaring a variable for the length with the correct data type
    4 points for a proper loop condition
    4 points for incrementing the pointer
    4 points for incrementing the counter
    4 points for returning the correct result
    std::size_t str_len(char* str) {
      std::size_t len(0);
      while (*str++) {len++;}
      return len;
    }
  4. Declare a struct that stores the name of a book, the number of its pages and its price. Use appropriate data types for each member. Then implement a function
    double total_price(std::vector<Book> books);
    that returns the total price of all books in books. Assume that while all required includes are present, no using directives were used. (40 points)
    5 points for declaring the struct
    5 points for each data member with a proper data type
    5 points for declaring and initializing a varible holding the total price
    5 points for using a proper for loop
    5 points for incrementing the total price
    5 points for returning the correct value
    struct Book {
      std::string name;
      uint32_t pages;
      double price;
    };
    
    double total_price(std::vector<Book> books) {
      double total = 0.0;
      for (const auto& book : books) {
        total += book.price;
      }
      return total;
    }