Consider the following interface:
public interface IInt{
int thevalue = 0;
}
True/false: any class that implements the this interface inherits this field. Therefore, it can be accessed using s.thevalue or just 'thevalue' inside the class.True:
- Also, since it is static, it can also be accessed using IInt.thevalue or “implementing class”.thevalue.
True/false: As a rule, fields defined in an interface are public, static, and final. (The methods are public and abstract.)
True
True/false: static method cannot be overridden by a non-static method and vice versa
True
True/false: a static method cannot be shadowed by a static method in the subclass.
False
- Static methods are shadowed like static variables and therefore can’t be overridden
True/false: Fields defined in an interface are ALWAYS considered as public, static, and final. (Methods are public and abstract.)
True
- In fact, you cannot even declare a field to be private or protected in an interface.
Which one of these is a proper definition of a class Car that cannot be sub-classed:
5
- Notice the distinction. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass.
The native keyword can only be used on methods, not on classes and instance variables.
True/false: Fields in an interface are implicitly public, static and final.
True
- Although you can put these words in the interface definition but it is not a good practice to do so.
Which of these statements about interfaces are true:
1. Interfaces are abstract by default.
2. An interface can have static methods.
3. All methods in an interface are abstract although you need not declare them to be so.
4, Fields of an interface may be declared as transient or volatile but not synchronized.
5. interfaces cannot be final.
1, 3, 5
Which of the following statements are correct:
1, 2, 3
True/false: if(b instanceof Bird) will fail to compile if b is not a subclass of bird or any of birds subclasses.
True
True/false: a*10/4-30; generates an int which can be returned as a double without any cast.
True
True/false: The return type should always be the same for overridden and overriding method.
False - This is true for primitives, but for Objects the overriding method can take the same or any subclass as return type.
True/false: A super( ) or this( ) call must always be provided explicitly as the first statement in the body of the constructor.
False
True/false: If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).
True
True/false: super(…) can only be called in the first line of the constructor but this(…) can be called from anywhere.
False
True/false: You can either call super(…) or this(…) from a constructor but not both.
True
True/false: An overriding method can be made less restrictive than the overridden method, but not more restrictive.
True
True/false: it is possible to make the overriding method in SubClass invoke the overridden method in BaseClass by calling super.methodname
True - If the class is static it is also possible to use BaseClass.methodname
Which of these statements about interfaces are true:
2, 4, 5
- An interface can extend any number of other interfaces and can be extended by any number of other interfaces. Variables in interfaces are always static and method prototypes in interfaces can never be static.
Which of the following are correct about “encapsulation”:
2, 3
- 4 is dynamic binding, an outcome of polymorphism.
Which of the following statements is/are true:
3
- Interface cannot “implement” anything. It can extend multiple interfaces. The following is a valid declaration :
interface I1 extends I2, I3, I4 { } (5)
True/false: A class implementing an interface must define all the methods of that interface.
False - not if the class is abstract
True/false: Subclasses must define all the abstract methods that the superclass defines.
False - not if the subclass itself is abstract
True/false: A super class must have a non private constructor.
True - Since the constructor is private, the subclass cannot access it and therefore, it needs to be made public. protected or default access is also valid.