C++ Quiz - check how good you are

1.
#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() B() ~A()
A() B() ~B() ~A()
A() ~A()
2.
#55 What does the program print?
namespace n
{
    #include <stdio.h>
};

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

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

using namespace std;

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

using namespace std;

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

class A
{
public:
    explicit operator bool(void) const { return true; }
};

int main()
{
    A a;
    bool var = static_cast<bool>(a); 
    printf("%d\n", var);
}
1
true
0
compile error
6.
#19 What does the program print?
#include <cstdio>

#define GET(v) #v"456"

int main()
{
    const char* var = "123";
    printf("%s\n", GET(var));
}
#v456
compile error
var456
123456
7.
#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() b() ~B() ~A()
B() A() b() ~A() ~B()
A() B() a() ~B() ~A()
compile error
8.
#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
compile error
123
var
9.
#104 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
B() A() b() ~A() ~A() ~B()
A() B() a() ~A() ~B() ~A()
A() B() b() ~A() ~B() ~A()
10.
#91 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(int i = 0; i < vect.size(); ++i)
    {
        cout << vect[i];
    }
}
iii
123
compile error
321
11.
#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);
}
true
var
1
compile error
12.
#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
compile error
123
%d\n
13.
#8 What does the program print?
#include <cstdio>

int main()
{
    int var = 123;
    int* pvar = &var;
    printf("%d\n", *pvar);
}
adres zmiennej var (np. -1076902564)
123
compile error
segmentation fault
14.
#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()
A() B() a() ~B() ~A()
A() B() b() ~B() ~A()
compile error
15.
#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();
}
segmentation fault
A
B
compile error
16.
#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);
}
B() A() b() ~A() ~B()
B() A() a() ~A() ~B()
compile error
A() B() a() ~B() ~A()
17.
#10 What does the program print?
#include <cstdio>

int main()
{
    int* pvar = 0;
    printf("%d\n", pvar);
}
0
segmentation fault
pvar
compile error
18.
#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
B
AB
A
19.
#59 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.print();
}
C
A
B
compile error
20.
#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");
}
segmentation fault
A
B
compile error



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.