Given example of C++ class initialization and destruction. What will be printed?
 #include <iostream>

class A {
public:
    int x;
    int y;
    bool z;
    
    A() : x(1), y(2), z(true) {}
};

int main() {
    A* obj = new A;
    delete obj;

    int* array = new int[10];
    
    if (obj->z && obj->x)
        std::cout << "foo" << std::endl;
    else if (obj->z && obj->y)
        std::cout << "bar" << std::endl;

    return 0;
}
Explanation
The memory allocated for obj is overwritten by the dynamic allocation of memory for the array. Therefore, in this case, the address for obj->x, obj->y, obj->z will contain non-zero data.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes