C++ Quiz - check how good you are

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

using namespace std;

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

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

void f(A* a) { a->fun(); }

int main()
{
    B b;
    f(&b);
}
compile error
A() B() a() ~B() ~A()
B() A() b() ~A() ~B()
A() B() b() ~B() ~A()
2.
#13 What does the program print?
#include <cstdio>

int main()
{
    int* pvar = new int(123);
    printf("%d\n", *pvar);
    delete pvar;
}
segmentation fault
123
pvar
compile error
3.
#85 What does the program print?
#include <iostream>

using namespace std;

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

int main()
{
    int* pvar = 0;
    printf("%d\n", *pvar);
}
%d\n
compile error
segmentation fault
0
5.
#55 What does the program print?
namespace n
{
    #include <stdio.h>
};

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

int main()
{
    print("B");
}
B
segmentation fault
m
compile error
6.
#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();
}
BA
AB
B
A
7.
#26 What does the program print?
#include <cstdio>

int main()
{
    int i = 123;
    printf("%d\n", i--);
}
123
122
121
compile error
8.
#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;
    }
}
012
compile error
123
0123
9.
#99 What does the program print?
#include <iostream>
#include <set>

using namespace std;

struct classcomp
{
  bool operator() (const int& lhs, const int& rhs) const {return lhs<rhs;}
};

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

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

int main()
{
    int var = 0;

    if(var)
        goto end;

    printf("A");

    end:
        printf("B");
}
compile error
B
AB
A
11.
#65 What does the program print?
#include <cstdio>
#include <assert.h>

int main()
{
    assert(0);
    printf("A");
}
abort
compile error
A
0
12.
#72 What does the program print?
#include <iostream>

int main()
{
    cout << "A" << "B" << "C" << endl;
}
compile error
AAA
CBA
ABC
13.
#19 What does the program print?
#include <cstdio>

#define GET(v) #v"456"

int main()
{
    const char* var = "123";
    printf("%s\n", GET(var));
}
var456
compile error
#v456
123456
14.
#28 What does the program print?
#include <cstdio>

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

int main()
{
    printf("%%d\n", 456);
}
%d
456
%%d
%%d\n
16.
#64 What does the program print?
#include <cstdio>

union A
{
    struct
    {
        int m:8;
        int n:4;
    };
    int h;
};

int main()
{
    A a;

    a.h = 0;
    a.m = 0;
    a.n = 1;

    printf("%d", a.h > 0 ? 2 : 0);
}
0
2
1
compile error
17.
#66 What does the program print?
#include <cstdio>
#include <assert.h>

int main()
{
    assert(123);
    printf("A");
}
abort
A
compile error
123
18.
#20 What does the program print?
#include <cstdio>

#define GET(v) v"456"

int main()
{
    printf("%s\n", GET("123"));
}
123
v456
compile error
123456
19.
#2 What does the program print?
#include <cstdio>

class S
{
    static int x;
};

int S::x = 123;

int main()
{
    printf("%d\n", S::x);

    return 1;
}
segmentation fault
123
%d\n
compile error
20.
#73 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    cout << "A" << 123 << "C" << endl;
}
AC
A123C
compile error
123



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.