Java Interfaces: Essential Basics Quiz Quiz

Explore key concepts behind interfaces in Java, including their purpose, declaration, and what they can contain. This quiz covers fundamental interview topics about interfaces with practical examples for beginners.

  1. Definition of Interfaces in Java

    What best describes an interface in Java?

    1. A type of variable
    2. A built-in Java keyword
    3. A storage location for data
    4. A set of rules that a class must follow

    Explanation: An interface is described as a set of rules a class must follow, enabling shared behaviors. Variables, keywords, and data storage are not the main concepts of interfaces, making those options incorrect.

  2. Purpose of Using Interfaces

    Why are interfaces used in Java?

    1. To store static values only
    2. To allow multiple classes to share the same behavior
    3. To speed up program execution
    4. To make classes inherit only one parent

    Explanation: Interfaces enable different classes to share the same behaviors, as shown in the example with devices like TV and sound systems. The other options are unrelated to the main purpose of interfaces.

  3. Declaring an Interface

    How do you declare a simple interface named 'RemoteControl' in Java?

    1. object RemoteControl { }
    2. class RemoteControl { }
    3. RemoteControl interface { }
    4. interface RemoteControl { }

    Explanation: The correct way to declare an interface uses the 'interface' keyword. 'class' and 'object' are for classes or objects, not interfaces, and 'RemoteControl interface' is incorrect syntax.

  4. Interface Example Scenario

    In the remote control example, what does the interface provide to devices like TVs?

    1. The detailed way to perform each action
    2. A constructor for TV objects
    3. A list of variables only
    4. Actions they must support, like power on or off

    Explanation: The interface provides actions (methods) such as power on or off for devices to implement. It does not specify how actions are performed or provide variables or constructors.

  5. Implementing Interface Methods

    What must a class do when it implements a Java interface?

    1. Ignore the interface methods
    2. Inherit non-static data members
    3. Provide bodies for all methods defined in the interface
    4. Automatically inherit private variables

    Explanation: Classes must provide bodies (implementations) for all interface methods. The other options are incorrect because interface methods are not ignored, and interface variables are not private or data members.

  6. Methods in Interfaces

    By default, what is true about methods defined inside a Java interface before Java 8?

    1. They are all private
    2. They initialize instance variables
    3. They are abstract and have no body
    4. They must have a method body

    Explanation: Before Java 8, interface methods are abstract and do not have method bodies. They are not private, do not initialize variables, and do not require a body.

  7. Access Modifiers in Interfaces

    Which access modifier applies to interface methods by default?

    1. Public
    2. Private
    3. Final
    4. Protected

    Explanation: Interface methods are public by default to ensure they can be implemented by any class. 'Protected', 'Private', and 'Final' are not the default for interface methods.

  8. Adding Methods in Interfaces (Java 8 and Later)

    What new types of methods were allowed in Java interfaces starting with Java 8?

    1. Default and static methods
    2. Only instance variables
    3. Synchronized blocks
    4. Constructors

    Explanation: From Java 8, interfaces can have default and static methods. Instance variables, constructors, and synchronized blocks are still not permitted as part of an interface's method set.

  9. Variables in Java Interfaces

    What type of variables can you define inside a Java interface?

    1. Public static final variables (constants)
    2. Dynamic local variables
    3. Instance variables with private access
    4. Only protected variables

    Explanation: Interfaces can have public static final variables (constants). They cannot contain instance variables, protected variables, or dynamic local variables.

  10. Implementing Multiple Interfaces

    What benefit do interfaces give classes regarding shared behaviors?

    1. They let classes inherit constructors from other classes
    2. They allow classes to share the same set of behaviors by following the same set of rules
    3. They prevent use of abstract classes
    4. They force all classes to have the same implementation

    Explanation: Interfaces help classes share behaviors through the same method signatures. They do not force identical implementations, enable constructor inheritance, or prevent using abstract classes.