Ch 5 - Class Design Flashcards
true/false, the following code compiles: public class Zoo { public Zoo() { super(); System.out.println("Zoo created"); }}
true
What is the least amount of classes in a .java file you can have that are public?
none
true/false, the followng code compiles: public class Animal { private int age; public Animal(int age) { super(); this.age = age; }} public class Zebra extends Animal { public Zebra(int age) { super(Age); } public Zebra() { this(4); }}
true
true/false, the following code compiles: public class Zoo { public Zoo() { System.out.println("Zoo created"); super(); }}
false
How many interfaces marked public can you have in a single .java file?
1
The following class does not have an access modifier: class Rodent {} What does this mean?
It has the default package private modifier, which indicates the class can only be accessed by a class within the same package.
Any class that inherits another class is know as what?
A child class
true/false, the following code compiles: public class Animal { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; }} public class Lion extends Animal { private void roar() { System.out.println("The " + age + " year old lion says Roar!"); }}
false. Although the Lion class extends the Animal class, it does not have direct access to the “age” member of Animal.
What is the name of the object that all java classes inherit from?
java.lang.Object
What is referred to as the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.
inheritance
How do you prevent a class from being inherited?
By using the ‘final’ specifier.
true/false, when the java compiler sees that your class doesn’t extend another class, it automatically inserts code to extend java.lang.Object.
true
In the following code, what is the line containing the word super() calling? public class Animal { private int age; public Animal(int age) { super(); this.age = age; }}
The parent constructor in java.lang.Object.
How many classes can one class directly inherit from?
Just one. However, multiple inheritance can be achieved by one class inheriting from another, which inherits from another, …
How many classes can a .java file contain?
As many as you want.
How many classes can have the public access modifier in a single .java file?
one
true/false, the following code compiles: public class Zoo { public Zoo() { // Calling the parent constructor super(); System.out.println("Zoo created"); }}
true
true/false, the following code compiles: public class Zoo { public Zoo() { // Calling the parent constructor super(); super(); System.out.println("Zoo created"); }}
false
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant { public static void main(String[] args) { Animal a = new Animal(); } }
false. Since the class Animal doesn't have a no argument constructor, the following line in the Elephant class does not compile: Animal a = new Animal();
true/false, in Java, the child constructor is always executed before the parent constructor.
false. The parent constructor is always executed before the child constructor.
What is the output of the following code? class Primate { public Primate() { System.out.println("Primate"); }} class Ape extends Primate { public Ape() { System.out.println("Ape"); }} public class Chimpanzee extends Ape { public static void main(String[] args) { new Chimpanzee(); }}
Primate
Ape
true/false, if no super() call is declared in a constructor, Java will insert a no-argument super() as the first statement of the constructor.
true
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant extends Animal { public Elephant() { super("Stephen"); } }
true
true/false, a child class may access the private members of the parent class.
false
true/false, the first statement of every constructor is a call to another constructor within the class using this() or a call to a constructor in the direct parent using this().
false. The first statement of every constructor is a call to another constructor within the class using this() or a call to a constructor in the direct parent using super().
true/false, you can call the parent constructor as well as another constructor in your same class from within one constructor, eg: public Animal(int age) { super("Stephen"); this("Stephen"); }
false. You can only call one or the other, not both.
true/false, the compiler automatically inserts a call to the no-argument constructor super() if the first statement is not a call to the parent constructor.
true
true/false, the super() call may be used after the first statement of the constructor
false. The super() call may not be used after the first statement of the constructor.
true/false, if the parent doesn’t have a no-argument constructor, the compiler requires an explicit call to a parent constructor in each child constructor.
true
true/false, you can call multiple other constructors from within one constructor: public Animal(int age) { this("Stephen"); this(48); }
false. You can only make one other constructor call.
true/false, if the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor in the child class.
true
true/false, the following code compiles: public class Animal { public Animal(String s) { } } public class Elephant extends Animal { }
false. Since the Elephant class does not define a constructor, the compiler will try to insert a default no-argument one for it that includes a ‘super()’ call.
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + super.getAge()); System.out.print(" and " + super.size + " meters long"); System.out.print(" with " + this.numberOfFins + " fins"); }}
true
true/false, the following code compiles:
pulic Rabbit(int age) {
super();
super.setAge(10);
}
true
true/false, you can use the “super()” command from within a normal method to call a constructor.
false. You can only use “super()” from within a constructor.
What is the output of the following code? public class Canine { public double getAverageWeight() { return 50; } }} public class Wolf extends Canine { public double getAverageWeight() { return getAverageWeight() + 20; } public static void main(String[] args) { System.out.println(new Canine.getAverageWeight()); System.out.println(new Wolf().getAverageWeight()); }}
Infinite loop on this line: return getAverageWeight() + 20; This happens because we did not use the "super" keyword to explicitly call the getAverageWeight() method in the parent class.
true/false, when overriding a method, the method in the child class must be more accessible than the method in the parent class.
false. It must be at least as accessible or more accessible.
When inheriting a class, what must the parent class members’ access modifiers be set to for us to see them?
public or protected
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + getAge()); System.out.print(" and " + size + " meters long"); System.out.print(" with " + numberOfFins + " fins"); }}
true
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + super.getAge()); System.out.print(" and " + super.size + " meters long"); System.out.print(" with " + super.numberOfFins + " fins"); }}
false. The following line fails to compile:
System.out.print(“ with “ + super.numberOfFins + “ fins”);
The this and super keywords may both be used for methods or variables defined in the parent class, but only this may be used for members defined in the current class.
What is the difference between “super” and “super()”?
"super" is used to reference a member in the parent class, and it can be used in methods outside the constructor. "super()" is used to reference a constructor in the parent class and can only be called from within the child class constructor. It must be the first uncommented line in the constructor.
true/false, when overriding a method, the method in the child class can have a different signature as the method in the parent class.
false. It must have the same signature.
true/false, when overriding a method, if the method returns a value, it must be the same type of the method in the parent class.
false. It can be the same type or a subclass of the method in the parent class.
What is the output of the following code? public class Canine { public double getAverageWeight() { return 50; } }} public class Wolf extends Canine { public double getAverageWeight() { return super.getAverageWeight() + 20; } public static void main(String[] args) { System.out.println(new Canine.getAverageWeight()); System.out.println(new Wolf().getAverageWeight()); }}
- 0
70. 0
true/false, the following code compiles: class Fish { protected int size; private int age; public Fish(int age) { this.age = age; } public int getAge() { return age; }} public class Shark extends Fish { private int numberOfFins = 8; public Shark(int age) { super(age); this.size = 4; } public void displaySharkDetails() { System.out.print("Shark with age: " + this.getAge()); System.out.print(" and " + this.size + " meters long"); System.out.print(" with " + this.numberOfFins + " fins"); }}
true
true/false, when overriding a method, the method in the child class may not throw a checked exception thta is new or broader than the class of any exception thrown in the parent class method.
true
true/false, the following code compiles:
pulic Rabbit(int age) {
super.setAge(10);
super();
}
false. When using “super()” to call a parent constructor, it must be the first uncommented line in the constructor.
true/false, the following code compiles: public class Bird { public void fly() { System.out.println("Bird is flying"); } public void eat(int food) { System.out.println("Bird is eating " + food + " units of food."); } } public class Eagle extends Bird { public int fly(int height) { System.out.println("Bird is flying at " + height + " meters"); return height; } pubic int eat(int food) { System.out.println("Bird is eating " + food + " units of food"); return food; } }
false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() throws InsufficientDataException { throw new InsufficientDataException(); } protected double getWeight() throws Exception { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws InsufficientDataException { return 2; }}
true
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() { throw new InsufficientDataException(); } protected double getWeight() { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws InsufficientDataException { return 2; }}
false. The getWeight() in the child class throws an exception that the parent does not.
true/false, the following code compiles: public class Camel { public int getNumberOfHumps() { return 0; } } public class BactrianCamel extends Camel { protected int getNumberOfHumps() { return 2; } }
false. The getNumberOfHumps() method in the child class must be at least as accessible as the parent method.
true/false, the following code compiles: public class Bird { public void fly() { System.out.println("Bird is flying"); } } public class Eagle extends Bird { public int fly(int height) { System.out.println("Bird is flying at " + height + " meters"); return height; } }
true. The method fly is successfully overloaded.
true/false, the following code compiles: public class Camel { protected int getNumberOfHumps() { return 0; } } public class BactrianCamel extends Camel { protected int getNumberOfHumps() { return 2; } }
true. The getNumberOfHumps() method in the child successfully overrides the same method in the parent.
true/false, the following code compiles: public class Bird { public void eat(int food) { System.out.println("Bird is eating " + food + " units of food."); } } public class Eagle extends Bird { pubic int eat(int food) { System.out.println("Bird is eating " + food + " units of food"); return food; } }
false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.
true/false, a child method may hide or eliminate a parent method’s exception without issue.
true
true/false, the following code compiles: public class Camel { protected String getNumberOfHumps() { return "Undefined"; } } public class BactrianCamel extends Camel { private int getNumberOfHumps() { return 2; } }
false. The getNumberOfHumps() method in the child class is not as accessible to the same method in the parent class. Also, the return types are not covariant.
true/false, the following code compiles: public class InsufficientDataException extends Exception {} public class Reptile { protected boolean hasLegs() throws InsufficientDataException { throw new InsufficientDataException(); } protected double getWeight() throws InsufficientDataException { return 2; }} public class Snake extends Reptile { protected boolean hasLegs() { return false; } protected double getWeight() throws Exception { return 2; }}
false. The scope of the exception thrown in Snake.getWeight() is narrower than in Reptile.getWeight().
What is the output of the following code: public class Bear { public static void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Compiler error on this line:
public void eat() {
The compiler thinks you’ve trying to override a method that should be hidden.
What is it called when methods replace parent methods in the calls defined in the child class?
Method hiding.
true/false, for method hiding, the method in the child class must have the same signature as the method in the parent class.
true
true/false, in Java, in a child class, it is possible to override a private method in the parent class.
false. However, the child class can define its own version of the method with the same or modified signature.
true/false, for method hiding, the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
true
What is the output of the following code: public class Bear { public void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public static void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Compiler error on this line:
public static void eat() {
The compiler thinks you’re trying to hide a method that should be overridden.
A child method replacing the parent method in calls defined in both the parent and child is called what?
Method overriding.
true/false, for method hiding, the method in the child class must have the same visibility than the method in the parent class.
false. It must be at least as or more accessible than the method in the parent class.
true/false, for method hiding, the method defined in the child class must be marked as static if it is marked as static in the parent class.
true.
true/false, the following code compiles: public class Camel { private String getNumberOfHumps() { return "Undefined"; } } public class BactrianCamel extends Camel { private int getNumberOfHumps() { return 2; } }
true. This code compiles without issue. If the method in the parent class were public or protected, the code would not compile.
true/false, for method hiding, if the method returns a value, it must be different than that from the method in the parent class.
false. It must be the same or a subclass of that of the parent class, aka covariant.
What is the output of the following code: public class Bear { public static void eat() { System.out.println("Bear is eating"); } } public class Panda extends Bear { public static void eat() { System.out.println("Panda bear is chewing"); } public static void main(String[] args) { Panda.eat(); } }
Panda bear is chewing
When a child class defines a static method with the same name and signature as a static method defined in a parent class, what is it called?
A hidden method
When in a child class, you can access the parent version of a hidden variable using what keyword?
super
Does the following code compile: public class Bird { public final boolean hasFeathers() { return true; } } public class Penguin extends Bird { public boolean hasFeathers() { return false; } }
no. It doesn’t matter if the method in the child class is marked “final” or not. If the method in the parent is marked that way, it cannot be overridden.
When a parent method is overridden by a child method, the parent method is never used unless explicitly called using what syntax?
super.method()
What is the output of the following code? class Marsupial { public boolean isBiped() { return false; } public void getMarsupialDescription() { System.out.println("Marsupial walks on two legs: " + isBiped()); } } public class Kangaroo extends Marsupial { public boolean isBiped() { return true; } public void getKangarooDescription() { System.out.println("Kangaroo hops on two legs: " + isBiped()); } public static void main(String[] args) { Kangaroo joey = new Kangaroo(); joey.getMarsupialDescription(); joey.getKangarooDescription(); } }
Marsupial walks on two legs: true
Kangaroo walks on two legs: true
true/false, you can hide a method in a parent class if it’s marked “final”.
false. You cannot hide a method in a parent class from the child class if it’s marked “final”.
true/false, java allows variables to be overridden.
false. They can be hidden but not overridden.
Does the following code compile: public class Bird { public boolean hasFeathers() { return true; } } public class Penguin extends Bird { public final boolean hasFeathers() { return false; } }
yes. It’s ok if the overriding method is marked “final” so long as the method being overridden is not marked “final”.
When working with overridden variables, if you’re referencing the variable from within the parent class, the variable in which class is used (parent or child)?
parent.
Does the following code compile: public class Bird { public boolean hasFeathers() { return true; } } public class Penguin extends Bird { public boolean hasFeathers() { return false; } }
yes
true/false, at runtime the child version of an overridden method is always executed for an instance regardless of whether the method call is defined in a parent or child class method.
true
true/false, final methods can be overridden.
false. Final methods cannot be overridden.
When working with overridden variables, if you’re referencing the variable from within the child class, the variable in which class is used (parent or child)?
child
What is the output of the following code? public class Marsupial { public static boolean isBiped() { return false; } public void getMarsupialDescription() { System.out.println("Marsupial walks on two legs: " + isBiped()); }} public class Kangaroo extends Marsupial { public static boolean isBiped() { return true; } public void getKangarooDescription() { System.out.println("Kangaroo hops on two legs: " + isBiped()); } public static void main(String[] args) { Kangaroo joey = new Kangaroo(); joey.getMarsupialDescription(); joey.getKangarooDescription(); }}
Marsupial walks on two legs: false
Kangaroo walks on two legs: true
true/false, when creating a method with the “final” keyword, you cannot override this method from a child class.
true.
How do you hide a variable?
In your child class, you define a variable with the same name as a variable in the parent class.
true/false, at runtime the parent version of a hidden method is always executed if the call to the method is defined in the child class.
false. The parent version of a hidden method is always executed if the call to the method is defined in the parent class.
Does the following code compile: public class Bird { public final boolean hasFeathers() { return true; } } public class Penguin extends Bird { public final boolean hasFeathers() { return false; } }
no. You cannot override a method that is marked “final”.
What is the output of the following code? public class Rodent { protected int tailLength = 4; public void getRodentDetails() { System.out.println("[parentTail=" + tailLength + "]"); }} public class Mouse extends Rodent { protected int tailLength = 8; public void getMouseDetails() { System.out.println("[tail=" + tailLength + ",parentTail="+super.tailLength+"]"); } public static void main(String[] args) { Mouse mouse = new Mouse(); mouse.getRodentDetails(); mouse.getMouseDetails(); }}
[parentTail=4]
[tail=8,parentTail=4]
true/false, an abstract method contains no implementation.
true
true/false, the following code compiles: public abstract class Animal { protected int age; public void eat() { System.out.println("Animal is eating"); } public abstract String getName(); } public class Swan extends Animal { public String getName() { return "Swan"; } }
true
What is the output of the following code? public class Parent1 { public boolean isBiped() { return false; } public void getMarsupialDescription() { System.out.println("Parent.myMethod1 - " + isBiped()); }} public class Child1 extends Parent1 { public boolean isBiped() { return true; } public void getKangarooDescription() { System.out.println("Child.myMethod1 - " + isBiped()); } public static void main(String[] args) { Child1 c1 = new Child1(); c1.getMarsupialDescription(); c1.getKangarooDescription();
Parent1 p1 = new Parent1(); p1.getMarsupialDescription(); }}
Parent.myMethod1 - true
Child.myMethod1 - true
Parent.myMethod1 - false
- you only override an instance method or hide a static method when you’re inheriting the method from the parent.
What is the output of the following code? public class Rodent { protected static int tailLength = 4; public void getRodentDetails() { System.out.println("[parentTail=" + tailLength + "]"); }} public class Mouse extends Rodent { protected static int tailLength = 8; public void getMouseDetails() { System.out.println("[tail=" + tailLength + ",parentTail="+super.tailLength+"]"); } public static void main(String[] args) { Mouse mouse = new Mouse(); mouse.getRodentDetails(); mouse.getMouseDetails(); }}
[parentTail=4]
[tail=8,parentTail=4]
true/false, an abstract class can be instantiated.
false.
true/false, you cannot define an abstract method as private.
true
true/false, a concrete class must implement all of the abstract methods inherited from the abstract class that it is extending.
true
true/false, the following code compiles: public abstract class Whale { private abstract void sing(); }
false. An abstract method may not be marked private.
true/false, an abstract method can be defined in a class that is not abstract.
false. Abstract methods must be defined in an abstract class.
true/false, the following code compiles: public abstract class Animal { public abstract String getName(); } public class Walrus extends Animal { }
false. A concrete class must implement all inherited abstract methods from the abstract class that it is extending.
true/false, an abstract class may extend another abstract class.
true
true/false, abstract methods must not provide a method body/implementation in the abstract class in which it is declared.
true
true/false, abstract classes may not be marked private, protected, or final.
true
true/false, implementing an abstract method in a subclass follows the same rules for overriding a method. The name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.
true
true/false, the following code compiles: public class Chicken { public abstract void peck(); }
false. You cannot have an abstract method in a class that is not abstract.
true/false, an abstract class is required to have at least one abstract method.
false. An abstract class doesn’t need to have any abstract methods.
Why does the following code not compile? public abstract class Turtle { public abstract void swim() {} pubic abstract int getAge() { return 10; } }
The two methods do not compile because abstract methods are not allowed to have a body, even if it’s just open and close curly braces.
Why is it that you cannot mark an abstract method or class as final?
Abstract classes and methods are meant to be extended or overwritten, and marking them as final takes away the ability to do that.
true/false, an abstract class may include nonabstract methods and variables.
true
true/false, the following code compiles: public abstract class Goat { public abstract final void chew(); }
false. abstract methods are not allowed to be marked final.
true/false, the following code compiles: public abstract class Animal { public abstract String getName(); } public abstract class BigCat extends Animal { public abstract void roar(); } public class Lion extends BigCat { public String getName() { return "Lion"; }}
false. The class Lion must implement all abstract methods that it inherits, and it is missing the method roar().
The first nonabstract subclass that extends an abstract class is known as what?
A concrete class.
true/false, the first concrete class that extends an abstract class must provide an implementation for at least one of the inherited abstract methods.
false. it must provide an implementation for all of the inherited abstract methods.
true/false, you can instantiate an an abstract class.
false
true/false, the following code compiles: public abstract class Animal { public abstract String getName(); } public abstract class BigCat extends Animal { public abstract void roar(); } public class Lion extends BigCat { public String getName() { return "Lion"; } public void roar() System.out.println("Roar!"); }}
true
true/false, the following code compiles: public final abstract class Tortoise {}
false
true/false, you can define an abstract method in a non abstract class.
false. Abstract methods may only be defined in abstract classes.
true/false, the following code compiles: public abstract class Cow {}
true
true/false, in an abstract class, you can define a method with a body, so long as it’s not marked abstract and not marked final.
true
true/false, the following code compiles: public abstract class Whale { protected abstract void sing(); } public class HumpbackWhale extends Whale { protected void sing() { System.out.println("Humpback Whale is singing!"); } }
true
true/false, an abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
true
true/false, the following code compiles: public abstract class Animal { public abstract String getName(); } public abstract class BigCat extends Animal { public String getName() { return "Lion"; } public abstract void roar(); } public class Lion extends BigCat { public void roar() System.out.println("Roar!"); }}
true.
true/false, abstract methods may not be declared private or final.
true
true/false, the following code compiles: public abstract class Eel { public static void main(String[] args) { final Eel eel = new Eel(); } }
false. The following line fails to compile: final Eel eel = new Eel(); You cannot have a private member in an abstract class.
true/false, the following code compiles: public abstract class Chicken { public abstract void peck(); }
true
true/false, the following code compiles: pubic abstract class Animal { public abstract String getName(); } public class Bird extends Animal { } public class Flamingo extends Bird { public String getName() { return "Flamingo"; } }
false. The class Bird must implement all of the abstract methods defined in the Animal class.
true/false, abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.
true
true/false, the following code compiles: public abstract class Animal { public abstract String getName(); } pubic class Walrus extends Animal { } public abstract class Eagle extends Animal { }
false. The code fails to compile because the Walrus class does not implement all of the abstract methods that it inherits from the Animal class. The Eagle class does not need to implement the methods in Animal, even though it extends it, because it too is an abstract class.
true/false, the following code compiles: public abstract class Whale { protected abstract void sing(); } public class HumpbackWhale extends Whale { private void sing() { System.out.println("Humpback Whale is singing!"); } }
false. When overriding a method in the parent, you cannot reduce visibility.
true/false, an interface may not be marked final.
true.
true/false, the following code compiles: public interface WalksOnTwoLegs {} public class TestClass { public static void main(String[] args) { WalksOnTwoLegs example = new WalksOnTwoLegs(); } }
false. You can not directly instantiate an interface.
true/false, the following interfaces are equivalent:
public interface CanFly { void fly(int speed); } public abstract interface CanFly { public abstract void fly(int speed); }
true. The compiler would automatically convert the first interface into the second.
true/false, the following code compiles: public abstract interface Int1 { public static final int MIN = 2; public abstract int getMaxDepth(); }
true. All interface variables are assumed to be static, and all interface methods are assumed to be abstract.
true/false, the following code compiles: public interface HasTail { public int getTailLength(); } public interface HasWhiskers() { public int getNumberOfWhiskers(); } public interface Seal extends HasTail, HasWhiskers { }
public class LeopardSeal implements HasTail, HasWhiskers { }
false. As a concrete class implementation, the class LeopardSeal should implement the getTailLength and getNumberOfWhiskers methods.
What is an abstract data type that defines a list of abstract public methods that any class implementing it must provide?
An interface
true/false, an interface is required to have at least one method.
false. An interface need not have any methods.
What specifiers get automatically inserted into an interface definition if not provided by the developer?
public and abstract
true/false, the following code compiles:
public final interface WalksOnTwoLegs {}
false. An interface may not be marked final.
true/false, a class may not inherit multiple interfaces.
false. They can implement may interfaces.
true/false, an interface that extends another interface, as well as an abstract class that implements an interface, inherits all of the abstract methods as its own abstract methods.
true
true/false, all top-level interfaces are assumed to have public or default access.
true.
true/false, the following code compiles:
public interface WalksOnTwoLegs {}
true
true/false, the following code compiles:
public interface HasTail { public int getTailLength(); } public interface HasWhiskers() { public int getNumberOfWhiskers(); } public interface Seal extends HasTail, HasWhiskers { }
true
Which of the following lines of code compile:
private final interface CanCrawl { private void dig(int depth); protected abstract double depth(); public final void surface(); }
None of them:
private final interface CanCrawl { // interfaces cannot be marked final
private void dig(int depth); // all interface methods are assumed to be public.
protected abstract double depth(); // all interface methods are assumed to be public.
public final void surface(); // interface methods may not be marked final.
}
true/false, interfaces can be instantiated directly.
false.
true/false, the first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all of the inherited abstract methods.
true.
true/false, the following code compiles: public interface Herbivore { public String eatPlants(); } public interface Omnivore { public void eatPlants(); public void eatMeat(); } public class Bear implements Herbivore, Omnivore { public void eatMeat() { System.out.println("Eating meat"); } public void eatPlants() { System.out.println("Eating plants"); } }
false. if the method name and input parameters are the same but the return types are different between the method defined in each of the interfaces, the class or interface attempting to inherit both will not compile.
true/false, the following code compiles: public class Hyena {}
public interface HasFur extends Hyena {}
false. An interface cannot extend or implement a class.
true/false, an interface can extend another interface.
true
true/false, the following code compiles:
public interface myInterface {
public int MYINTEGER = 0;
}
true
true/false, the following code compiles: public interface Herbivore { public int eatPlants(); } public interface Omnivore { public void eatPlants(); } public abstract class AbstractBear implements Herbivore, Omnivore {}
false. An interface or class that tries to implement two different interfaces, each containing a method with the same signature but different return type, will not compile.
true/false, the value of an interface variable must be set when it is declared since it is marked as final.
true
true/false, the following code compiles:
public interface CanRun {}
public class Chetah implements CanRun {}
true
true/false, the following code compiles: public interface Herbivore { public void eatPlants(int numPlants); } public interface Omnivore { public void eatPlants(); public void eatMeat(); } public class Bear implements Herbivore, Omnivore { public void eatMeat() { System.out.println("Eating meat"); } public void eatPlants() { System.out.println("Eating plants"); } }
false. In this case, the eatPlants methods in each interface have different method signatures, meaning that each of them must be explicitly implemented. To make this code compile, you would change the Bear class to look like this: public class Bear implements Herbivore, Omnivore { public void eatMeat() { System.out.println("Eating meat"); } public void eatPlants() { System.out.println("Eating plants"); } public void eatPlants(int numPlants) { System.out.println("Eating " + numPlants + " plants"); } }
true/false, the following code compiles:
public interface myInterface {
private int MYINTEGER = 0;
}
false. interface varaibles are assumed to be public.
true/false, interface variables are assumed to be public.
true
true/false, the following code compiles:
public interface myInterface {
public int MYINTEGER;
}
false. Interface variables are assumed to be static and must be assigned a value when declared since it is marked as final.
true/false, the following code compiles:
public interface CanRun {} public class Chetah extends CanRun {}
false. A class cannot extend an interface.
true/false, a class can extend an interface.
false. A class implements an interface, not extends it.
true/false, the following code compiles: public interface Herbivore { public int eatPlants(); } public interface Omnivore { public void eatPlants(); } public interface Supervore extends Herbivore, Omnivore {}
false. An interface or class that tries to implement two different interfaces, each containing a method with the same signature but different return type, will not compile.
true/false, the following code compiles:
public interface myInterface {
protected int MYINTEGER = 0;
}
false. interface varaibles are assumed to be public.
true/false, the following code compiles: public interface Herbivore { public void eatPlants(); } public interface Omnivore { public void eatPlants(); public void eatMeat(); } public class Bear implements Herbivore, Omnivore { public void eatMeat() { System.out.println("Eating meat"); } public void eatPlants() { System.out.println("Eating plants"); } }
true. Because the eatPlants() method is in both interfaces with the same signature, it is ok to implement it just once in the Bear class. The method actually is implemented twice.
true/false, an interface can implement another interface.
false.
true/false, interface methods are assumed to be static and final.
false. An interface method cannot be marked final.
true/false, interface variables are assumed to be static and final.
true.
true/false, the following code compiles: public interface Carnivore { public int getRequiredFoodAmount() { return 13; } }
false. In an interface, for a method to have a body, it must be a default method.
true/false, in an interface, if a method is marked with the “default” keyword, it does not need to provide a body.
false. All default methods must provide a method body.
true/false, the following code compiles: public interface HasFins { public default boolean doFinsHaveScales() { return true; } } public interface SharkFamily extends HasFins { public boolean doFinsHaveScales() { return false; } }
false. The doFinsHaveScales method in the SharkFamily interface either needs to include the keyword “default” or remove the method body.
true/false, a static method defined in an interface is not inherited in any classes that implement the interface.
true.
true/false, the following code compiles: public interface Hop { public static int getJumpHeight() { return 8; } }
true
true/false, a class that implements two interfaces containing static methods with the same signature will compile.
true. This is true because static methods in interfaces are not inherited by the subclass and must be accessed with a reference to the interface name.
true/false, a default method cannot be marked as static, final, or abstract.
true.
true/false, if a class implements two interfaces that have default methods with the same name and signature, the compiler will error out. However, if the implementing class overrides the duplicate methods, the code will compile.
true.
true/false, the following code compiles: public interface IsWarmBlooded { boolean hasScales(); public default double getTemperature() { return 10.0; } }
true.
true/false, a default method will not compile if it marked as private or protected.
true.
true/false, a default method within an interface defines an abstract method with a default implementation.
true.
true/false, the following code compiles: public interface HasFins { public default int getNumberOfFins() { return 4; } } public interface SharkFamily extends HasFins { public default int getNumberOfFins() { return 8; } }
true
true/false, the following code compiles:
public interface Carnivore {
public default void eatMeat();
}
false. A default method must have a method body.
true/false, if a class implements two interfaces that have default methods with the same name and signature, the compiler will error out.
true.
true/false, the following code compiles: public interface Walk { public default int getSpeed() { return 5; } } public interface Run { public default int getSpeed() { return 10; } } public class Cat implements Walk, Run { public int getSpeed() { return 1; } public static void main(String[] args) { System.out.println(new Cat().getSpeed()); } }
true. It compiles because we’ve overridden the duplicate default method getSpeed() in the Cat class.
true/false, the following code compiles: public interface Hop { static int getJumpHeight() { return 8; } }
true
true/false, the following code compiles: public interface Hop { public static int getJumpHeight() { return 8; } } public class Bunny implements Hop { public void printDetails() { System.out.println(Hop.getJumpHeight()); } }
true.
true/false, a default method may be defined in regular class or an interface.
false. A default method may only be defined within an interface.
true/false, the following code compiles: public interface HasFins { public default double getLongestFinLength() { return 20.0; } } public interface SharkFamily extends HasFins { public double getLongestFinLength(); }
true
true/false, a default method is a method defined within an interface with the default keyword in which a method body is provided.
true.
true/false, a default method is not assumed to be static, final, or abstract.
true. The default method may be overridden by the implementing class.
true/false, the following code compiles: public interface Hop { protected static int getJumpHeight() { return 8; } }
false, a static method in an interface cannot be marked private or protected.
true/false, default methods in an interface are assumed to have protected access.
false, they are assumed to be public.
true/false, a static method in an interface is assumed to be public and will not compile if marked as private or protected.
true.
true/false, the following code compiles: public interface Walk { public default int getSpeed() { return 5; } } public interface Run { public default int getSpeed() { return 10; } } public class Cat implements Walk, Run { public static void main(String[] args) { System.out.println(new Cat().getSpeed()); } }
false. The code fails to compile on this line: public class Cat implements Walk, Run { You cannot implement two identical default methods from different interface files, unless you also override those methods.
true/false, the following code compiles: public interface Hop { public static int getJumpHeight() { return 8; } } public class Bunny implements Hop { public void printDetails() { System.out.println(getJumpHeight()); } }
false. The code fails to compile on this line:
System.out.println(getJumpHeight());
You must have an explicit reference to the name of the interface.
true/false, to reference a static method in an interface, a reference to the name of the interface must be used.
true.
What is the output of the following code? public class Primate { public boolean hasHair() { return true; } } public interface HasTail { public boolean isTailStriped() { public boolean isTailStriped; } } public class Lemur extends Primate implements HasTail { public boolean isTailStriped() { return false; } public int age = 10; public static void main(String[] args) { Lemur lemur = new Lemur(); System.out.println(lemur.age); HasTail hasTail = lemur; System.out.println(hasTail.age); Primate primate = lemur; System.out.println(primate.isTailStriped()); } }
Compiler error on this line:
System.out.println(hasTail.age);
And compiler error on this line:
System.out.println(primate.isTailStriped());
What is the output of the following code? public class Reptile { public String getName() { return "Reptile"; } } public class Alligator extends Reptile { public String getName() { return "Alligator"; } } public class Crocodile extends Reptile { public String getName() { return "Crocodile"; } } public class ZooWorker { public static void feed(Reptile reptile) { System.out.println("Feeding: " + reptile.getName()); } public static void main(String[] args) { feed(new Alligator()); feed(new Crocodile()); feed(new Reptile()); }}
Feeding: Alligator
Feeding: Crocodile
Feeding: Reptile
What is the property of an object to take on many different forms?
Polymorphism
true/false, when dealing with polymorphism, once an object has been assigned a new reference type, only the methods and variables available to that reference type are callable on the object without an explicit cast.
true.
true/false, the following code compiles:
public class Rodent { } public class Capybara extends Rodent { public static void main(String[] args) { Rodent rodent = new Rodent(); Capybara capybara = (Capybara) rodent; } }
true, the code compiles, but a ClassCastException is thrown at runtime from this line:
Capybara capybara = (Capybara) rodent;
What is the output of the following code? public class Bird { public String getName() { return "Unknown"; } public void displayInformation() { System.out.println("The bird name is: " + getName()); } } public class Peacock extends Bird { public String getName() { return "Peacock"; } public static void main(String[] args) { Bird bird = new Peacock(); bird.displayInformation(); } }
The bird name is: Peacock
A java object may be accessed which three ways?
- Using a reference with the same type as the object.
- Using a reference that is a super class of the object.
- Using a reference that defines an interface the object implements, either directly or through a superclass.
What is the output of the following code? public class Primate { public boolean hasHair() { return true; } } public interface HasTail { public boolean isTailStriped() { public boolean isTailStriped; } } public class Lemur extends Primate implements HasTail { public boolean isTailStriped() { return false; } public int age = 10; public static void main(String[] args) { Lemur lemur = new Lemur(); System.out.println(lemur.age); HasTail hasTail = lemur; System.out.println(hasTail.isTailStriped()); Primate primate = lemur; System.out.println(primate.hasHair()); } }
10
false
true
true/false, the following code compiles: public class Bird {} public class Fish { public static void main(String[] args) { Fish fish = new Fish(); Bird bird = (Bird) fish; } }
false. Compiler error on this line: Bird bird = (Bird) fish; The Fish and Bird classes are not related through any class hierarchy.
What is known as a method in which the specific implementation is not determined until runtime?
A virtual method.