C++ Quiz - check how good you are

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

int main()
{
    int var = 3;

    switch(var)
    {
        case 3:
            printf("A");
        case 4:
            printf("B");
        default:
            break;
    }
}
compile error
A
AB
B
2.
#26 What does the program print?
#include <cstdio>

int main()
{
    int i = 123;
    printf("%d\n", i--);
}
121
122
compile error
123
3.
#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
BA
B
4.
#93 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(vector<int>::const_iterator it = vect.begin(); it != vect.end(); ++it)
    {
        cout << *it;
    }
}
compile error
123
321
segmentation fault
5.
#47 What does the program print?
#include <cstdio>

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

int main()
{
    A* a = 0;
    a->print();
}
A
segmentation fault
0
compile error
6.
#92 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(vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
    {
        cout << *it;
    }
}
123
321
compile error
ititit
7.
#69 What does the program print?
#include <cstdio>

int main()
{
    int var = 3;

    if(var)
        printf("A");
        printf("B");
        if(var > 1)
            printf("C");
            printf("D");
            if(var > 3)
                printf("E");
                printf("F");
}
ABCDF
AC
ABCD
compile error
8.
#13 What does the program print?
#include <cstdio>

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

int main()
{
    cout << "A" << "B" << "C" << endl;
}
AAA
ABC
compile error
CBA
10.
#94 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(vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
    {
        cout << "*";
        vect.erase(it);
    }
}
***
**
*
segmentation fault
11.
#14 What does the program print?
#include <cstdio>

int main()
{
    int* pvar = new int(123);
    printf("%d\n", *pvar);
    delete *pvar;
}
compile error
adres zmiennej pvar
segmentation fault
123
12.
#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();
}
compile error
segmentation fault
B
A
13.
#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
B
A
segmentation fault
14.
#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");
}
A
B
compile error
segmentation fault
15.
#96 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(vector<int>::iterator it = vect.begin(); it != vect.end();)
    {
        cout << "*";
        it = vect.erase(it);
    }
}
segmentation fault
***
*
**
16.
#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();
}
A() b() b() ~A() b()
A() a() a() ~A() a()
compile error
A() a() b() ~A() a()
17.
#23 What does the program print?
#include <cstdio>

#define ENABLE 0

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

using namespace std;

int main()
{
    unsigned i = 0;
    cout << --++--++i << endl;
}
4294967295
-1
0
compile error
19.
#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();
}
C
compile error
B
A
20.
#45 What does the program print?
#include <cstdio>

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

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

int main()
{
    B b;
    A* a = &b;
    a->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.