1. What happens when you attempt to compile and run the
following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public: string a(string b) {
return b.substr(0,2);
}};
class B {
public: string a(string b) {
return b.substr(2,2);
}};
int main() {
A a;
B b;
cout << a.a(b.a(“ABCD”));
return 0;
}
It prints ABCD
It prints CD
It prints AB
Compilation fails
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
int data[3];
public:
int cnt;
void put(int v) { data[cnt++] = v; }
};
int main() {
A a;
a.cnt = 0;
a.put(1);
a.put(1);
cout << a.cnt;
, return 0;
}
It prints 0
It prints 2
Compilation fails
It prints 1
3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
public:
int data[3];
private:
int cnt;
public:
void put(int v) { data[cnt++] = v; }
int take() { int c = cnt; cnt = 0; return c; }
};
int main() {
A a;
a.take();
a.put(a.take());
a.put(1);
cout << a.data[0];
return 0;
}
It prints 2
Compilation fails
It prints 1
It prints 0
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
int cnt;
void put(int v) { cout << cnt++; }
};
int main() {
A a;
following code?
#include <iostream>
#include <string>
using namespace std;
class A {
public: string a(string b) {
return b.substr(0,2);
}};
class B {
public: string a(string b) {
return b.substr(2,2);
}};
int main() {
A a;
B b;
cout << a.a(b.a(“ABCD”));
return 0;
}
It prints ABCD
It prints CD
It prints AB
Compilation fails
2. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
int data[3];
public:
int cnt;
void put(int v) { data[cnt++] = v; }
};
int main() {
A a;
a.cnt = 0;
a.put(1);
a.put(1);
cout << a.cnt;
, return 0;
}
It prints 0
It prints 2
Compilation fails
It prints 1
3. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
public:
int data[3];
private:
int cnt;
public:
void put(int v) { data[cnt++] = v; }
int take() { int c = cnt; cnt = 0; return c; }
};
int main() {
A a;
a.take();
a.put(a.take());
a.put(1);
cout << a.data[0];
return 0;
}
It prints 2
Compilation fails
It prints 1
It prints 0
4. What happens when you attempt to compile and run the
following code?
#include <iostream>
using namespace std;
class A {
int cnt;
void put(int v) { cout << cnt++; }
};
int main() {
A a;