C++ Quiz - check how good you are

1.
#84 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int i = 5;
    cout << (++i)++ << endl;
}
compile error
5
7
6
2.
#91 What does the program print?
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vect;
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);

    for(int i = 0; i < vect.size(); ++i)
    {
        cout << vect[i];
    }
}
321
iii
123
compile error
3.
#80 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int& r = 0;

    for(int i = 0; i < 3; ++i)
    {
        r = i;
        cout << r;
    }
}
0123
123
compile error
012
4.
#37 (C++11) What does the program print?
#include <cstdio>

class A
{
public:
    explicit operator bool(void) const { return true; }
};

int main()
{
    A a;
    bool var = static_cast<bool>(a); 
    printf("%d\n", var);
}
0
compile error
true
1
5.
#58 What does the program print?
#include <cstdio>

struct A
{
    void print() { printf("A"); }
};

struct B
{
    void print(int val) { printf("B"); }
};

struct C : public A, public B
{
    void print(const char* p = 0) { printf("C"); }
};

int main()
{
    C c;
    c.print();
}
A
C
compile error
B
6.
#62 What does the program print?
#include <cstdio>

struct A
{
    int f;
};
typedef A AA;

int main()
{
    A a1;
    struct A a2;
    AA a3;

    a1.f = 1;
    a2.f = 2;
    a3.f = 3;

    printf("%d", a1.f + a2.f + a3.f);
}
4
compile error
5
6
7.
#54 What does the program print?
namespace n
{
    #include <stdio.h>
};

void print(const char * m) { n::printf(m); }

int main()
{
    print("B");
}
B
m
segmentation fault
compile error
8.
#44 What does the program print?
#include <cstdio>

class A
{
public:
    void print() { printf("A"); }
};

class B : public A
{
public:
    void print() { printf("B"); }
};

int main()
{
    B b;
    A* a = &b;
    a->print();
}
A
AB
B
BA
9.
#56 What does the program print?
#include <cstdio>

struct A
{
    void print() { printf("A"); }
};

struct B
{
    void print(int val) { printf("B"); }
};

struct C : public A, public B
{

};

int main()
{
    C c;
    c.A::print();
}
compile error
B
C
A
10.
#40 What does the program print?
#include <cstdio>

class A
{
public:
    A() { printf("A() "); }
    ~A() { printf("~A() "); }
};

class B : public A
{
public:
    B() { printf("B() "); }
    ~B() { printf("~B() "); }
};

int main()
{
    A* pa = new B();
    delete pa;
}
A() B() ~A() ~B()
A() B() ~A()
A() ~A()
A() B() ~B() ~A()
11.
#71 What does the program print?
#include <cstdio>

int main()
{
    int var = 3;

    if(var) printf("A"); var ? printf("B") : printf("C");
    printf("D"); !var ? printf("E") : printf("F");
}
compile error
ACDE
AD
ABDF
12.
#75 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int var = 123;
    int& r = var;
    cout << *r << endl;
}
compile error
r
123
var
13.
#85 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int i = 5;
    cout << ++++++i << endl;
}
7
compile error
5
8
14.
#97 What does the program print?
#include <iostream>
#include <set>

using namespace std;

int main()
{
    set<int> s;
    s.insert(11);
    s.insert(2);
    s.insert(33);

    for(set<int>::iterator it = s.begin(); it != s.end(); ++it)
    {
        cout << *it;
    }
}
21133
33112
33211
11233
15.
#39 What does the program print?
#include <cstdio>

class A
{
public:
    operator bool(void) const { return true; }
};

int main()
{
    A a;
    int var = a;
    printf("%d\n", var);
}
true
1
compile error
0
16.
#29 What does the program print?
#include <cstdio>

int main()
{
    bool b = true;
    int i = b;
    printf("%d\n", i);
}
1
true
0
compile error
17.
#101 What does the program print?
#include <iostream>

using namespace std;

struct A
{
    A() { cout << "A() "; fun(); }
    ~A() { cout << "~A() "; fun(); }
    virtual void fun() { cout << "a() "; }
};

struct B :  A
{
    void fun() { cout << "b() "; }
};

int main()
{
    B b;
    b.fun();
}
compile error
A() a() a() ~A() a()
A() a() b() ~A() a()
A() b() b() ~A() b()
18.
#48 What does the program print?
#include <cstdio>

class B;

class A
{
    friend class B;
    void print() { printf("A"); }
};

class B
{
public:
    void print()
    {
        A a;
        a.print();
    }
};

int main()
{
    B b;
    b.print();
}
B
compile error
segmentation fault
A
19.
#49 What does the program print?
#include <cstdio>

class A
{
    void print() { printf("A"); }
};

class B
{
public:
    void print()
    {
        A a;
        a.print();
    }
};

int main()
{
    B b;
    b.print();
}
A
B
segmentation fault
compile error
20.
#43 What does the program print?
#include <cstdio>

class A
{
public:
    void print() { printf("A"); }
};

class B : public A
{
public:
    void print() { printf("B"); }
};

int main()
{
    B b;
    b.print();
}
B
A
BA
AB



Wszystkie pytania i kody są właśnością autora i nie mogą być powielane bez jego zgody. Kody były kompilowane i uruchamiane w środowisku Linux (x86, C++98 gcc 4.4.7, C++11 gcc 4.7.2). Fragmenty kodu są w standardzie C++98 chyba, że podano inaczej w treści pytania. Dokładałem wszelkiej staranności aby pytania były pozbawione błędów, ale jeżeli jakieś błędy zostaną wykryte proszę o kontakt. Pozdrawiam Autor.