Question 71
HardIn Java, which method is called by obj.equals(x) when obj is declared as Object but is really a String?
- A.
String.equals
- B.
Neither
- C.
It depends on x
- D.
Object.equals
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
In Java, which method is called by obj.equals(x) when obj is declared as Object but is really a String?
String.equals
Neither
It depends on x
Object.equals
Sign up free to see the answer and a step-by-step explanation.
In Java, given void f(Object o) and void f(String s), which runs for f((Object) "hi")?
f(String s)
Compilation error
Both
f(Object o)
Sign up free to see the answer and a step-by-step explanation.
What is covariance in method overriding?
Return types must be identical
Methods may change names
An override may return a subtype of the parent method's return type
An override may take a supertype parameter
Sign up free to see the answer and a step-by-step explanation.
Which feature lets C++ templates achieve compile-time polymorphism?
Each instantiation generates code for the specific types, chosen at compile time
dynamic_cast
RTTI
vtables
Sign up free to see the answer and a step-by-step explanation.
What does dynamic_cast do that static_cast doesn't, for pointers to polymorphic classes?
It changes the object
It checks the real type at run time and returns nullptr if the cast is invalid
It is faster
It works on any type
Sign up free to see the answer and a step-by-step explanation.
What does 'ad-hoc polymorphism' refer to?
Templates and generics
Inheritance
Function or operator overloading
Virtual dispatch
Sign up free to see the answer and a step-by-step explanation.
In Python, why does 1 + '2' raise TypeError but '1' * 2 work?
str implements mul for integers, but int's add doesn't accept strings and str has no matching radd
Python can't add numbers
Strings are numbers
Sign up free to see the answer and a step-by-step explanation.
What is the 'expression problem'?
Lambda syntax
The difficulty of adding both new types and new operations without changing existing code
Operator precedence
Parsing arithmetic expressions
Sign up free to see the answer and a step-by-step explanation.
Which is true of final methods in Java?
They are always static
They must be abstract
They can't be called
They can't be overridden, so calls to them can be bound statically
Sign up free to see the answer and a step-by-step explanation.
What is a pure virtual destructor in C++ and why might it be used?
A destructor with no body
A destructor that can't be called
A static destructor
A destructor declared = 0 (but still defined) to make a class abstract when it has no other pure virtual functions
Sign up free to see the answer and a step-by-step explanation.