Java Questions 31 - 40  «Prev  Next»

Java Questions 34

  1. What must a class do that implements an interface?

    Answer:
    Unless the implementing class is abstract, the implementing class must provide implementations for all methods defined in the interface.

  2. What is the purpose of the interface contract?

    Answer:
    Implementing an interface defines a role you can play, despite how different it might be from some other class implementing the same interface.

  3. Can an interface extend more than one interface?

    Answer:
    Yes, an interface is free to extend multiple interfaces.

  4. Does an abstract class need to implement all of the methods of an interface?

    Answer:
    If a class is marked abstract and implements an interface, the class can choose to implement a) any b) all or c) none of the methods from any of the interfaces.

  5. Can an interface implement another interface?

    Answer:
    An interface cannot implement an interface.

  6. Can an interface implement another interface?

    Answer:
    No. An interface can extend one or more interfaces.

  7. How does ClassLoader work in Java?

    Answer:
    In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations (i.e.) Filesystem, Network location.
    There are three main types of ClassLoaders in Java. 1. Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
    2. Extension ClassLoader: It loads class files from jre/lib/ext location.
    3. Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them

  8. When overriding a method what must you pay attention to that is not important in overloading?

    Answer:
    What you are allowed to declare as a return type depends on whether you are 1) overriding or 2) overloading.

  9. If you overload a method in a subclass, can you declare a different return type?

    Answer:
    Yes
    public class Parent {
    
     public static void main(String[] args) {
      Parent p1 = new Child();
      int a =  p1.add(3, 5);
      System.out.println("a = " + a );
      Child c1 = new Child();
      float b = c1.add(2.1f, 3,5.3f);
      System.out.println("b= " + b );
     }
     int add( int a, int b) {
      return a + b;
     }
    }
    
    class Child extends Parent{
     float add (float a, int b, float c) {
      return a + b + c;
     }	
    }
    

    Program Output:
    a = 8
    b= 10.4
    

  10. What must you do in order to overload a method?

    Answer:
    Change the argument list.