Arthur Arthur - 1 month ago 7
Java Question

what statement is true for this piece of code? (JAVA)

a x = new b(t,s);
h.g(n,d);



  1. method g must be public.

  2. t is necessarily a simple parameter (as double, int etc.)

  3. b can be an abstract class.

  4. h is necessarily an object.

  5. d can be a name of a method.

  6. b is necessarily a class that inherits from a (not necessarily directly).

  7. a can be a class that inherits from b.



i think its 6. but i cant disqualify 4.

which statement is true?

Answer

It wouldn't be helpful to give you the answers since understanding this assignment will definitely help you a lot in future assignments, but let me give a few hints:

  1. method g must be public. Can you think of an example of a case where it could have a different access modifier (e.g. if it's protected or private) and have it still compile?
  2. t is necessarily a simple parameter (as double, int etc.) What other types are there? Could it be one of them? Why or why not?
  3. b can be an abstract class. Is it possible to instantiate an abstract class?
  4. h is necessarily an object. What other types are there? Could it be one of those as well? Why or why not? Also, can you rule this out based on the facts that were mentioned in the comments?
  5. d can be a name of a method. What would it mean to pass the name of a method in this context? Note that this question is Java-specific and it may differ by programming language.
  6. b is necessarily a class that inherits from a (not necessarily directly). A good first step here is to enter a code sample in an IDE and see if it compiles. Create a class B that inherits from class A and try to do what this question states you can do. If it's possible, find out if it's necessary and make sure you can explain why (or why not).
  7. a can be a class that inherits from b. Same as for #6.
Comments