C++ Quiz - check how good you are

1.
#29 What does the program print?
#include <cstdio>

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

int main()
{
    int var = 3;

    printf("C");

    if(var)
        if(var > 1)
            if(var > 3)
                printf("A");
                printf("B");
}
compile error
CB
C
CAB
3.
#26 What does the program print?
#include <cstdio>

int main()
{
    int i = 123;
    printf("%d\n", i--);
}
122
compile error
123
121
4.
#76 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int var = 123;
    int& r = var;
    int& r2 = r;
    cout << r2 << endl;
}
123
r
compile error
r2
5.
#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
5
6
compile error
6.
#83 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int i = 5;
    cout << ++i++ << endl;
}
6
5
compile error
7
7.
#106 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(const A* a) { a->fun(); }

int main()
{
    B b;
    f(&b);
}
B() A() b() ~A() ~B()
compile error
A() B() a() ~B() ~A()
A() B() b() ~B() ~A()
8.
#57 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.print();
}
compile error
C
B
A
9.
#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);
}
A() B() a() ~B() ~A()
B() A() b() ~A() ~B()
A() B() b() ~B() ~A()
compile error
10.
#4 What does the program print?
#include <cstdio>

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

int main()
{
    printf("%%d\n", 456);
}
segmentation fault
compile error
456
%d
12.
#5 What does the program print?
#include <cstdio>

int main()
{
    int var = 123;
    printf("%d\n", var);
}
var
segmentation fault
compile error
123
13.
#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
BA
B
AB
14.
#27 What does the program print?
#include <cstdio>

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

int main()
{
    int var = 123;
    int* pvar = &var;
    printf("%d\n", *pvar);
}
123
segmentation fault
adres zmiennej var (np. -1076902564)
compile error
16.
#46 What does the program print?
#include <cstdio>

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

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

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

#define GET(v) #v"456"

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

class A
{
public:
    explicit A(bool var) { printf("bool");  }
};

int main()
{
    bool var = true;
    A a = var;
}
segmentation fault
true
compile error
bool
19.
#63 What does the program print?
#include <cstdio>

union A
{
    int f;
    int h;
};

int main()
{
    A a;

    a.f = 1;
    a.h = 2;

    printf("%d", a.f + a.h);
}
2
compile error
4
3
20.
#100 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;
    }
}
compile error
33211
33112
21133



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.