C++ Quiz - check how good you are

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

struct S
{
    static int x;
};

int S::x = 123;

int main()
{
    printf("%d\n", S::x);

    return 1;
}
%d\n
123
nie skompiluje się
segmentation fault
2.
#86 What does the program print?
#include <iostream>

using namespace std;

int i = 6;

int main()
{
    int i = i;
    cout << i << endl;
}
0
niezdefiniowana wartość
6
compile error
3.
#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
123
compile error
4.
#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);
}
4
compile error
2
3
5.
#6 What does the program print?
#include <cstdio>

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

int main()
{
    int var = 3;

    if(var) printf("A"); var ? printf("B") : printf("C");
    printf("D"); !var ? printf("E") : printf("F");
}
compile error
AD
ACDE
ABDF
7.
#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();
}
A
compile error
B
segmentation fault
8.
#64 What does the program print?
#include <cstdio>

union A
{
    struct
    {
        int m:8;
        int n:4;
    };
    int h;
};

int main()
{
    A a;

    a.h = 0;
    a.m = 0;
    a.n = 1;

    printf("%d", a.h > 0 ? 2 : 0);
}
0
2
1
compile error
9.
#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;
}
compile error
bool
true
segmentation fault
10.
#73 What does the program print?
#include <iostream>

using namespace std;

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

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

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

int main()
{
    int var = 123;
    A a(var);
}
var
123
segmentation fault
compile error
13.
#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();
}
B
A
C
compile error
14.
#43 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;
    b.print();
}
AB
A
BA
B
15.
#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ść
16.
#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);
}
A() B() a() ~B() ~A()
compile error
B() A() b() ~A() ~B()
A() B() b() ~B() ~A()
17.
#89 What does the program print?
#include <iostream>

using namespace std;

int main()
{
    unsigned i = 0;
    cout << --++--++i << endl;
}
compile error
-1
0
4294967295
18.
#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
segmentation fault
compile error
B
19.
#87 What does the program print?
#include <iostream>

using namespace std;

int i = 5;

int main()
{
    int i[i];
    cout << sizeof(i) << endl;
}
5
20
compile error
1
20.
#13 What does the program print?
#include <cstdio>

int main()
{
    int* pvar = new int(123);
    printf("%d\n", *pvar);
    delete pvar;
}
segmentation fault
pvar
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.