Question 31
MediumWhat is the main difference between an abstract class and an interface in Java (before Java 8)?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is the main difference between an abstract class and an interface in Java (before Java 8)?
An abstract class can have fields, constructors and implemented methods; an interface had only abstract methods and constants
There is no difference
An abstract class can't have any methods
An interface can have constructors
Sign up free to see the answer and a step-by-step explanation.
What is a default method in a Java interface?
A private method of a class
A method with a body in the interface, which implementing classes inherit unless they override it
A static constructor
A method that must be overridden
Sign up free to see the answer and a step-by-step explanation.
Can an abstract class have a constructor?
Only in C++
No, never
Yes; it runs when a concrete subclass is created
Only if it has no methods
Sign up free to see the answer and a step-by-step explanation.
In C++, what happens if a derived class doesn't override a pure virtual function of its base?
The compiler creates an empty body
The base function is called
The derived class is also abstract and can't be instantiated
The program crashes at run time
Sign up free to see the answer and a step-by-step explanation.
Which is the best reason to program to an interface rather than a concrete class?
Implementations can be swapped (e.g. for testing) without changing the code that uses them
Interfaces store more data
It avoids writing classes
It makes code run faster
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;
class Shape {
public:
virtual double area() = 0;
void info() { cout << "Area=" << area(); }
};
class Rect : public Shape {
public:
double area() override { return 4 * 5; }
};
int main() {
Rect r;
r.info();
}
Area=20
Compilation error
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self, amt): ...
class Card(Payment):
def pay(self, amt): return amt * 1.02
try:
Payment()
except TypeError:
print("cannot", end=" ")
print(Card().pay(100))
102.0
TypeError
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self): ...
@abstractmethod
def g(self): ...
class B(A):
def f(self): return 1
try:
B()
print("ok")
except TypeError:
print("abstract")
1
ok
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;
class I {
public:
virtual void run() = 0;
virtual ~I() {}
};
class X : public I { public: void run() override { cout << "X"; } };
class Y : public I { public: void run() override { cout << "Y"; } };
int main() {
I *jobs[3] = { new X(), new Y(), new X() };
for (I *j : jobs) { j->run(); delete j; }
}
XXX
Compilation error
Sign up free to see the answer and a step-by-step explanation.
A payment module calls processor.charge(amount) without knowing whether it's UPI, card or net banking. Which OOP principle does this show?
Multiple inheritance
Operator overloading
Encapsulation only
Abstraction
Sign up free to see the answer and a step-by-step explanation.
Area=45
Area=0
cannot 100
cannot 102.0
None
abstract
XYX
YYY