ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
struct B { virtual ~B() { } };
int main() {
B* p = nullptr;
try { cout << typeid(*p).name(); }
catch (const bad_typeid&) { cout << "T"; }
}- A. Undefined behaviour: a null dereference
- B. An empty line
- C. B
- D. T
Correct answer: D. T
For an expression of polymorphic type, typeid(*p) with a null pointer p is defined to throw std::bad_typeid instead of dereferencing it.
More OOP with C++ and Java questions
- What is printed? java class R implements AutoCloseable { String n; R(String n, boolean fail) { if (fail) throw new…
- What happens when this is compiled? java interface I { int f(); } abstract class A { abstract long f(); } class C extends A implements I {…
- What happens when this is compiled? java interface I { default void f() { System.out.print("I"); } } abstract class A { public abstract…
- What is printed? java int[] arr = {1, 2, 3}; for (int x : arr) x = 2; System.out.print(Arrays.toString(arr));
- What is printed? java Set<String> s = new LinkedHashSet<>(List.of("a", "b", "c")); s.remove("a"); s.add("a"); s.add("b")…
- What is printed? java TreeMap<String, Integer> m = new TreeMap<>(); for (String k : new String[]{"9", "10", "100", "2"}) m.put(k…