C++ Quiz - check how good you are

1.
#53 What does the program print?
extern "C" {
    #include <stdio.h>

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

int main()
{
    print("B");
}
spowoduje wyciek pamięci
B
A
compile error
2.
#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
***
**
3.
#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();
}
BA
A
AB
B
4.
#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() b() ~B() ~A()
A() B() a() ~B() ~A()
5.
#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();
}
segmentation fault
compile error
A
B
6.
#60 What does the program print?
#include <cstdio>

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

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

struct C : public A, public B
{
    using A::print;
};

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

int main()
{
    int** pvar = 0;
    printf("%d\n", *pvar);
}
compile error
0
segmentation fault
nieokreślona wartość
8.
#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();
}
segmentation fault
B
A
compile error
9.
#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;
    }
}
321
ititit
compile error
123
10.
#31 What does the program print?
#include <cstdio>

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

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

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

using namespace std;

int main()
{
    unsigned i = 0;
    cout << --++--++i << endl;
}
4294967295
compile error
-1
0
13.
#74 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    int var = 123;
    int& r = var;
    cout << r << endl;
}
var
123
compile error
r
14.
#41 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()
{
    A* pa = new B();
    delete pa;
}
A() B() ~A() ~B()
A() ~A()
A() B() ~B() ~A()
A() B() ~A()
15.
#16 What does the program print?
#include <cstdio>

#define GET(v) #v

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

using namespace std;

int main()
{
    int i = 5;

    for(i = 0; i < 3; ++i)
    {
    }

    cout << i << endl;
}
5
3
0
2
17.
#87 What does the program print?
#include <iostream>

using namespace std;

int i = 5;

int main()
{
    int i[i];
    cout << sizeof(i) << endl;
}
20
1
5
compile error
18.
#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
A
B
19.
#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() ~B() ~A()
A() ~A()
A() B() ~A()
A() B() ~A() ~B()
20.
#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;
}
r
456
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.