Question 51
MediumA Java method is overloaded as print(Object o) and print(String s). Which is called by print(null)?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
A Java method is overloaded as print(Object o) and print(String s). Which is called by print(null)?
print(Object o)
Neither
Compilation error always
print(String s), the more specific one
Sign up free to see the answer and a step-by-step explanation.
Animal a = new Dog(); compiles in Java, but Dog d = new Animal(); does not. Why?
Dog has more fields
Every Dog is an Animal (safe upcast), but an Animal is not necessarily a Dog
Constructors are not inherited
Animal is abstract
Sign up free to see the answer and a step-by-step explanation.
Why is downcasting (base to derived) risky?
It deletes the object
The object might not really be of the derived type, causing a ClassCastException or undefined behaviour
It changes the object's class permanently
It is always a compile error
Sign up free to see the answer and a step-by-step explanation.
What does dynamic_cast<Derived*>(basePtr) return in C++ if the object is not a Derived?
It throws for pointers
A new Derived object
The same pointer
nullptr
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
struct A {
virtual void f() { cout << "A.f "; }
void g() { cout << "A.g"; }
};
struct B : A {
void f() override { cout << "B.f "; }
void g() { cout << "B.g"; }
};
int main() {
A* p = new B();
p->f();
p->g();
delete p;
return 0;
}
A.f B.g
B.f A.g
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
struct A {
A() { who(); }
virtual void who() { cout << "A"; }
};
struct B : A {
void who() override { cout << "B"; }
};
int main() {
B b;
return 0;
}
A
Error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
class Parent { static void show() { System.out.println("Parent static"); } }
class Child extends Parent { static void show() { System.out.println("Child static"); } }
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}
Parent static Child static
Error
Sign up free to see the answer and a step-by-step explanation.
How does C++ usually implement virtual functions?
Each polymorphic class has a table of function pointers (vtable), and each object holds a pointer to it (vptr)
By recompiling at run time
Using templates
By copying every function into each object
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
#include <iostream>
using namespace std;
void f(int) { cout << "int "; }
void f(double) { cout << "double"; }
int main() {
f(5);
f(5.0f);
return 0;
}
double double
int double
Sign up free to see the answer and a step-by-step explanation.
What is covariant return type in method overriding?
The overriding method may return a subtype of the parent method's return type
The overriding method must return void
Overloaded methods must return the same type
The return type may be any unrelated type
Sign up free to see the answer and a step-by-step explanation.
A.f A.g
B.f B.g
B
AB
Parent static
Child static
Error
int int