Midterm Review

This is not meant to be a comprehensive review of everything you need to know for the midterm. Please study everything covered so far.
Exercise 1:
What will be the output of the following C++ code?
#include <iostream>
void modify(int &x) { x += 5; }
int main() {
    int a = 10;
    modify(a);
    std::cout << "a: " << a << std::endl;
    return 0;
}
    
Answer:

a: 15

Exercise 2:
Why does the following C++ code fail to compile, and how can it be fixed?
class Example {
    const int value;
public:
    void setValue(int v) { value = v; }
};
    
Answer:

value is marked const but setValue attempts to modify value. Remove const from the declaration.

Exercise 3:
What will be the output of the following C++ code snippet?
#include <iostream>
int main() {
    int *ptr = new int(42);
    std::cout << *ptr << std::endl;
    delete ptr;
    return 0;
}
    
Answer:

42

Exercise 4:
Consider the following code that uses multiple catch blocks. What will be the output when the program is executed?
#include <iostream>
#include <stdexcept>
int main() {
    try {
        throw 10;
    } catch (const std::exception &e) {
        std::cout << "Caught std::exception: " << e.what() << std::endl;
    } catch (int n) {
        std::cout << "Caught int: " << n << std::endl;
    }
    return 0;
}
    
Answer:

The exception thrown is of type int, so the output will be:
Caught int: 10

Exercise 5:
Examine the following code. What is the output and what does it illustrate about constant pointers versus pointers to constant values?
#include <iostream>
int main() {
    int x = 10, y = 20;
    int * const ptr1 = &x;      // constant pointer to int
    const int * ptr2 = &y;      // pointer to constant int
    // ptr1 = &y; // Error: cannot change the address stored in ptr1
    *ptr1 = 15;                // Allowed: modifying the value at x
    // *ptr2 = 25; // Error: cannot modify the value pointed to by ptr2
    std::cout << x << " " << y << std::endl;
    return 0;
}
    
Answer:

The output is: 15 20. This demonstrates that for ptr1 (a constant pointer), you cannot change the address it points to, but you can modify the value at that address. In contrast, for ptr2 (a pointer to constant data), you cannot modify the value pointed to, even though the pointer itself can point to a different address.

Exercise 6:
Consider the following class that overloads the addition operator for complex numbers. What is the output when the code is executed?
#include <iostream>
class Complex {
    double real, imag;
public:
    Complex(double r, double i) : real(r), imag(i) {}
    Complex operator+(const Complex &other) const {
        return Complex(real + other.real, imag + other.imag);
    }
    void print() const {
        std::cout << real << " + " << imag << "i" << std::endl;
    }
};

int main(){
    Complex a(1.0, 2.0), b(3.0, 4.0);
    Complex c = a + b;
    c.print();
    return 0;
}
    
Answer:

The output will be:
4 + 6i

Exercise 7:
What will be the output of the following template function when called in main?
#include <iostream>
template<typename T>
T max(T a, T b) {
    return a > b ? a : b;
}

int main() {
    std::cout << max(3, 7) << std::endl;
    std::cout << max(3.14, 2.72) << std::endl;
    return 0;
}
    
Answer:

The output will be:
7
3.14

Exercise 8 (OOP in C++ - Inheritance and Virtual Functions):
What will be the output of the following code?
#include <iostream>
class Animal {
public:
    virtual void speak() { std::cout << "Some generic animal sound" << std::endl; }
    virtual ~Animal() = default;
};

class Dog : public Animal {
public:
    void speak() override { std::cout << "Woof!" << std::endl; }
};

int main() {
    Animal* pet = new Dog();
    pet->speak();
    delete pet;
    return 0;
}
    

Output:
Woof!

Exercise 9 (OOP in C++ - Abstract Base Class):
Why can’t an object of type Shape be instantiated?
#include <iostream>
class Shape {
public:
    virtual double area() const = 0;  // Pure virtual function
};

class Circle : public Shape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() const override { return 3.14159 * radius * radius; }
};

int main() {
    // Shape s;  // Uncommenting this line will cause a compile-time error
    Circle c(5);
    std::cout << c.area() << std::endl;
    return 0;
}
    

Shape is an abstract class due to the pure virtual function area(). Abstract classes cannot be instantiated.

Exercise 10 (Smart Pointers - Unique Pointer):
What will be printed when the following code is executed?
#include <iostream>
#include <memory>
class Widget {
public:
    Widget() { std::cout << "Widget created" << std::endl; }
    ~Widget() { std::cout << "Widget destroyed" << std::endl; }
};

int main() {
    std::unique_ptr<Widget> ptr = std::make_unique<Widget>();
    return 0;
}
    

Output:
Widget created
Widget destroyed

Exercise 11 (Smart Pointers - Shared and Weak Pointers):
Consider the following code. What is the output of use_count() and why?
#include <iostream>
#include <memory>
int main(){
    std::shared_ptr<int> sp = std::make_shared<int>(100);
    std::weak_ptr<int> wp = sp;
    std::cout << "Shared count: " << sp.use_count() << std::endl;
    return 0;
}
    

Output:
Shared count: 1
because weak_ptr does not increase the reference count.

Exercise 12 (UNIX Bash Commands - Listing Files):
What does the following command do?
ls -al
    

Lists all files and directories (including hidden files) in the current directory in long format, showing permissions, owner, group, file size, and timestamp.

Exercise 13 (UNIX Bash Commands - Recursive Grep):
Explain what the following command does:
grep -r "main" .
    

Searches recursively (-r) for the string "main" in all files in the current directory.

Exercise 14 (UNIX File Systems - Permission String):
What does the permission string rwxr-xr-- mean for a file?

- Owner: rwx – read, write, and execute
- Group: r-x – read and execute (no write)
- Others: r-- – read only

Exercise 15 (UNIX File Systems - Permissions):
Which command would you use to give the owner read and write access but only read access for everyone else for the file document.txt?
chmod 644 document.txt