C++ Quiz - check how good you are

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

int main()
{
    int var = 3;

    printf("C");

    if(var)
        if(var > 1)
            if(var > 3)
                printf("A");
                printf("B");
}
C
CB
CAB
compile error
2.
#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");
}
AD
ABDF
compile error
ACDE
3.
#13 What does the program print?
#include <cstdio>

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

using namespace std;

int main()
{
    int& r = 0;

    for(int i = 0; i < 3; ++i)
    {
        r = i;
        cout << r;
    }
}
012
0123
compile error
123
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);
}
compile error
B() A() b() ~A() ~B()
B() A() a() ~A() ~B()
A() B() a() ~B() ~A()
6.
#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
segmentation fault
B
A
7.
#7 What does the program print?
#include <cstdio>

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

int main()
{
    const char* var = "123";
    printf("%s\n", var);
}
segmentation fault
var
123
compile error
9.
#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();
}
A
compile error
C
B
10.
#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() a() ~B() ~A()
A() B() b() ~B() ~A()
11.
#72 What does the program print?
#include <iostream>

int main()
{
    cout << "A" << "B" << "C" << endl;
}
AAA
CBA
ABC
compile error
12.
#54 What does the program print?
namespace n
{
    #include <stdio.h>
};

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

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

using namespace std;

int main()
{
    int i = 5;
    cout << ++++++i << endl;
}
5
7
compile error
8
14.
#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() B() ~A() ~B()
A() B() ~B() ~A()
B() ~B()
A() ~A()
15.
#8 What does the program print?
#include <cstdio>

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

struct A
{
    int f;
};
typedef A AA;

int main()
{
    A a1;
    struct A a2;
    AA a3;

    a1.f = 1;
    a2.f = 2;
    a3.f = 3;

    printf("%d", a1.f + a2.f + a3.f);
}
6
4
5
compile error
18.
#17 What does the program print?
#include <cstdio>

#define GET(v) v;

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

#define GET(v) #v"456"

int main()
{
    const char* var = "123";
    printf("%s\n", GET(var));
}
var456
123456
#v456
compile error
20.
#100 What does the program print?
#include <iostream>
#include <set>

using namespace std;

struct classcomp
{
  bool operator() (const int& lhs, const int& rhs) const {return lhs>rhs;}
};

int main()
{
    set<int, classcomp> s;
    s.insert(11);
    s.insert(2);
    s.insert(33);

    for(set<int>::iterator it = s.begin(); it != s.end(); ++it)
    {
        cout << *it;
    }
}
33211
21133
33112
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.