- Home
- Programming
- OCP Java SE8
36.
What is the output of the following application?
package race;
interface Drive {
int SPEED = 5;
default int getSpeed() { return SPEED; }
}
interface Hover {
int MAX_SPEED = 5;default int getSpeed() { return MAX_SPEED; }
}
public class Car implements Drive, Hover {
public static void main(String[] gears) {
class RaceCar extends Car {
@Override public int getSpeed() { return 10; }
};
System.out.print(new RaceCar().getSpeed());
}
}
package race;
interface Drive {
int SPEED = 5;
default int getSpeed() { return SPEED; }
}
interface Hover {
int MAX_SPEED = 5;default int getSpeed() { return MAX_SPEED; }
}
public class Car implements Drive, Hover {
public static void main(String[] gears) {
class RaceCar extends Car {
@Override public int getSpeed() { return 10; }
};
System.out.print(new RaceCar().getSpeed());
}
}
- A.5
- B.10
- C.The code does not compile due to the definition of Racecar .
- D.The code does not compile for some other reason.
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
Both the Drive and Hover interfaces define a default method getSpeed() with the same signature. In fact, both getSpeed() methods return the same value of 5 . The class Car implements both interfaces, which means it inherits both default methods. Since the compiler does not know which one to choose, the code does not compile, and the answer is Option D. Note that if the Car class had overridden the getSpeed() method, then the code would have compiled without issue and printed 10 at runtime. In particular, the local class Racecar defined in the main() method compiles without issue, making Option C incorrect. |
37.
Fill in the blanks: It is possible to extend an ____________but not an ____________.
- A.interface, abstract class
- B.abstract class, enum
- C.enum, interface
- D.abstract class, interface
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
An interface can be extended by another interface and a class can be extended by another class, making the second part of Options A, C, and D incorrect. Option B is correct because an enum cannot be extended. Note that Option C is also incorrect for this reason. |
38.
Which of the following results is not a possible output of this program?
package sea;
enum Direction { NORTH, SOUTH, EAST, WEST; };
public class Ship {
public static void main(String[] compass) {
System.out.print(Direction.valueOf(compass[0]));
}
}
package sea;
enum Direction { NORTH, SOUTH, EAST, WEST; };
public class Ship {
public static void main(String[] compass) {
System.out.print(Direction.valueOf(compass[0]));
}
}
- A.WEST is printed.
- B.south is printed.
- C.An ArrayIndexOutOfBoundsException is thrown at runtime.
- D.An IllegalArgumentException is thrown at runtime.
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
If the program is called with a single input WEST , then WEST would be printed at runtime. If the program is called with no input, then the compass array would be of size zero, and an ArrayIndexOutOfBoundsException would be thrown at runtime. Finally, if the program is called with a string that does not match one of the values in Direction , then an IllegalArgumentException would be thrown at runtime. The only result not possible is south , since the enum value is in uppercase, making Option B the correct answer. |
39.
Which of the following is not an advantage of using enumerated types in Java?
- A.Ensure consistency of data across an application.
- B.Offer ability to create new enumerated values at runtime.
- C.Provide access to fixed constants whose value does not change during the course of the application.
- D.Support cases where a value can only take one of a limited number of options.
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Enumerated types support creating a set of reusable values whose values are fixed and consistent across the entire application. For these reason, Options A, C, and D are incorrect. Option B is the false statement because enum values are defined at compile time and cannot be changed or added at runtime. |
40.
Given the following enum declaration, how many lines contain compilation errors?
package rainbow;
enum Light {}
public enum Color extends Light {
RED, BLUE, ORANGE, GREEN
protected Color() {}
}
package rainbow;
enum Light {}
public enum Color extends Light {
RED, BLUE, ORANGE, GREEN
protected Color() {}
}
- A.None, the code compiles as is.
- B.One
- C.Two
- D.Three
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The program contains three compilation problems. First off, the enum Color extends the enum Light , but enums cannot extend other enums so the definition is invalid. Second, the enum value list must end with a semicolon ( ; ) if the enum definition contains anything other than the enum values. Since it includes a constructor, a semicolon ( ; ) is required after GREEN . Finally, enum constructors must be private , meaning the protected constructor for Color does not compile. For these three reasons, Option D is the correct answer. |