Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factual questions about java

Tags:

java

My teacher gave out a practice exam on java recently and I'm curious to see how I did on the true/false part. I'm least confident about number one, so any explanation there would help a lot.

  1. In Java, a class can extend any number of abstract classes.

    false. I don't quite understand why, is it just because in an inheriting class the the parent classes could cause a conflict?

  2. In Java, it is illegal to pass a parameter whose type is an abstract class.

    False, abstract classes don't even have constructors...

  3. In Java, an abstract class can have any number of subclasses.

    True. I can't think of anything that would restrict this.

  4. In Java, there is no restriction on the number of interfaces a class can implement.

    True

  5. It is not possible to implement a stack as a doubly-linked list, because a stack requires access to only one end of the list, and a doubly-linked list provides access to both ends of the list.

    true. but it wouldn't be very efficient.

like image 936
devin Avatar asked Feb 13 '26 03:02

devin


1 Answers

1) It is false because Java does not support multiple inheritance. A class can only extend one class, whether it is abstract or not. It can transitively extend multiple classes (e.g., it extends B which extends C so indirectly it also extends C). A class can implement multiple interfaces. There are many reasons why Java does not support multiple inheritance, but it does support multiple interfaces, so it is better in many ways.

2) First of all, abstract classes can have constructors. The claim is false because you are allowed to pass abstract types as parameters. Because of polymorphism, you will be passing a concrete subtype that you instantiated already.

3) That is true

4) That is true to a degree (there is some limit by the JVM implementation, but you'll never hit it in practice)

5) You can easily implement a stack as a doubly linked list, it's a good exercise. It is even efficient since you're still doing everything at O(1).

like image 91
Uri Avatar answered Feb 14 '26 17:02

Uri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!