ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
template <typename T> struct Is { static const int v = 0; };
template <> struct Is<int> { static const int v = 1; };
template <typename T> struct Is<T*> { static const int v = 2; };
int main() {
cout << Is<int>::v << Is<int*>::v << Is<double>::v << Is<const int>::v;
}- A. 1201
- B. 1000
- C. 1200
- D. 1202
Correct answer: C. 1200
int uses the full specialisation, int* matches the partial specialisation for pointers, and double falls to the primary template. const int is a different type from int, so it also uses the primary template.
More OOP with C++ and Java questions
- What is printed? cpp struct L { L() { cout << "L"; } }; void f() { static L l; cout << "f"; } int main() { f(); f(); f(); }
- What is printed? cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int main() { T(); cout << "1"; const T& r = T(); cout <<…
- Which lines fail to compile? cpp struct U { U() { } U(const U&) = delete; }; void take(U u) { } int main() { U a; // L1 U b = a; // L2 U&…
- Which line fails to compile? cpp struct P { int v = 1; int get() { return v; } int cget() const { return v; } }; int main() { const P p{}…
- Which lines fail to compile? cpp struct S { int x = 1; static int y; static int f() { return y; } // L1 static int g() { return x; } // L2…
- Which lines fail to compile? cpp struct B { virtual void f(int) { } virtual void g() const { } void h() { } }; struct D : B { void f(int)…