ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
template <typename T> struct Box { static int count; Box() { count++; } };
template <typename T> int Box<T>::count = 0;
int main() {
Box<int> a, b;
Box<double> c;
cout << Box<int>::count << Box<double>::count;
}- A. 33
- B. 30
- C. 21
- D. 11
Correct answer: C. 21
Each template instantiation is a separate class with its own static member, so Box<int>::count and Box<double>::count are independent. (Contrast Java, where erasure leaves one shared static.)
More OOP with C++ and Java questions
- What is printed? cpp struct N { int v; N(int x = 7) : v(x) { } }; void show(N n) { cout << n.v; } int main() { N a; N b = 3; show(4)…
- What is printed? java class P { void f(int x) { System.out.print("P-int "); } } class C extends P { void f(long x) {…
- What is printed? java BigDecimal a = new BigDecimal("1.0"), b = new BigDecimal("1.00"); Set<BigDecimal> h = new HashSet<>(List.of(a, b))…
- What is printed? java Stream.of("a", "bb", "ccc") .filter(s -> { System.out.print("f" + s.length()); return s.length() > 1; }) .map(s -> {…
- What is printed? java Stream.of(1, 2, 3).peek(System.out::print).map(x -> x 2); System.out.print("end");
- What is printed? java Map<Integer, Long> m = Stream.of("a", "bb", "cc", "ddd", "e") .collect(Collectors.groupingBy(String::length…