C++ Quiz - check how good you are

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

using namespace std;

int main()
{
    int var1 = 123;
    int var2 = 456;
    int& r = var1;
    r = var2;
    cout << r << endl;
}
456
123
r
compile error
2.
#32 What does the program print?
#include <cstdio>

class A
{
public:
    A(int var) { printf("%d", var);  }
};

int main()
{
    bool var = true;
    A a(var);
}
1
var
compile error
true
3.
#16 What does the program print?
#include <cstdio>

#define GET(v) #v

int main()
{
    const char* var = "123";
    printf("%s\n", GET(var));
}
segmentation fault
var
compile error
123
4.
#42 What does the program print?
#include <cstdio>

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

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

int main()
{
    B b;
}
A() ~A()
A() B() ~B() ~A()
B() ~B()
A() B() ~A() ~B()
5.
#103 What does the program print?
#include <iostream>
#include <vector>

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()
compile error
B() A() a() ~A() ~B()
B() A() b() ~A() ~B()
6.
#81 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    for(int i = 0; i < 3; ++i)
    {
        const int& r = i;
        cout << r;
    }
}
compile error
123
012
0123
7.
#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();
}
A
B
C
compile error
8.
#26 What does the program print?
#include <cstdio>

int main()
{
    int i = 123;
    printf("%d\n", i--);
}
123
121
122
compile error
9.
#52 What does the program print?
extern "C" {
    #include <stdio.h>

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

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

#define ENABLE

int main()
{
#if ENABLE
    printf("enabled");
#else
    printf("disabled");
#endif
}
compile error
enabled
enableddisabled
disabled
11.
#73 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    cout << "A" << 123 << "C" << endl;
}
A123C
compile error
AC
123
12.
#10 What does the program print?
#include <cstdio>

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

using namespace std;

int i = 6;

int main()
{
    int i = i;
    cout << i << endl;
}
0
compile error
6
niezdefiniowana wartość
14.
#29 What does the program print?
#include <cstdio>

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

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

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

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

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

int main()
{
    A a(123);
}
compile error
segmentation fault
123
var
18.
#15 What does the program print?
#include <cstdio>

int main()
{
    const char* var = "123";
    printf("%s\n", var);
}
var
compile error
segmentation fault
123
19.
#50 What does the program print?
#include <cstdio>

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

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

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

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

struct B
{
    virtual void print() { printf("B"); }
};

struct C : public A, public B
{
    void print() { printf("C"); };
};

int main()
{
    C c;
    A* pa = &c;
    pa->print();
}
compile error
B
C
A



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.