Ch 5 - Class Design Flashcards

1
Q
true/false, the following code compiles:
public class Zoo {
	public Zoo() {
		super();
		System.out.println("Zoo created");
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the least amount of classes in a .java file you can have that are public?

A

none

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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);
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
true/false, the following code compiles:
public class Zoo {
	public Zoo() {
		System.out.println("Zoo created");
		super();
}}
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many interfaces marked public can you have in a single .java file?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
The following class does not have an access modifier:
class Rodent {}
What does this mean?
A

It has the default package private modifier, which indicates the class can only be accessed by a class within the same package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Any class that inherits another class is know as what?

A

A child class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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!");
}}
A

false. Although the Lion class extends the Animal class, it does not have direct access to the “age” member of Animal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the name of the object that all java classes inherit from?

A

java.lang.Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you prevent a class from being inherited?

A

By using the ‘final’ specifier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

true/false, when the java compiler sees that your class doesn’t extend another class, it automatically inserts code to extend java.lang.Object.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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;
}}
A

The parent constructor in java.lang.Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many classes can one class directly inherit from?

A

Just one. However, multiple inheritance can be achieved by one class inheriting from another, which inherits from another, …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many classes can a .java file contain?

A

As many as you want.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How many classes can have the public access modifier in a single .java file?

A

one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
true/false, the following code compiles:
public class Zoo {
	public Zoo() {
// Calling the parent constructor
		super();
		System.out.println("Zoo created");
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
true/false, the following code compiles:
public class Zoo {
	public Zoo() {
// Calling the parent constructor
		super();
		super();
		System.out.println("Zoo created");
}}
A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
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();
	}
}
A
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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

true/false, in Java, the child constructor is always executed before the parent constructor.

A

false. The parent constructor is always executed before the child constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
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();
}}
A

Primate

Ape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
true/false, the following code compiles:
public class Animal {
	public Animal(String s) {
	}
}
public class Elephant extends Animal {
	public Elephant() {
		super("Stephen");
	}
}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

true/false, a child class may access the private members of the parent class.

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

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().

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
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");
}
A

false. You can only call one or the other, not both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

true/false, the super() call may be used after the first statement of the constructor

A

false. The super() call may not be used after the first statement of the constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
true/false, you can call multiple other constructors from within one constructor:
public Animal(int age) {
	this("Stephen");
	this(48);
}
A

false. You can only make one other constructor call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q
true/false, the following code compiles:
public class Animal {
	public Animal(String s) {
	}
}
public class Elephant extends Animal {
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
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");
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

true/false, the following code compiles:

pulic Rabbit(int age) {
super();
super.setAge(10);
}

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

true/false, you can use the “super()” command from within a normal method to call a constructor.

A

false. You can only use “super()” from within a constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q
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());
}}
A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

true/false, when overriding a method, the method in the child class must be more accessible than the method in the parent class.

A

false. It must be at least as accessible or more accessible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

When inheriting a class, what must the parent class members’ access modifiers be set to for us to see them?

A

public or protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q
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");
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q
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");
}}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is the difference between “super” and “super()”?

A
"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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

true/false, when overriding a method, the method in the child class can have a different signature as the method in the parent class.

A

false. It must have the same signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

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.

A

false. It can be the same type or a subclass of the method in the parent class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q
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());
}}
A
  1. 0

70. 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q
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");
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

true/false, the following code compiles:

pulic Rabbit(int age) {
super.setAge(10);
super();
}

A

false. When using “super()” to call a parent constructor, it must be the first uncommented line in the constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q
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;
	}
}
A

false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q
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;
}}
A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q
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;
}}
A

false. The getWeight() in the child class throws an exception that the parent does not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q
true/false, the following code compiles:
public class Camel {
	public int getNumberOfHumps() {
		return 0;
	}
}
public class BactrianCamel extends Camel {
	protected int getNumberOfHumps() {
		return 2;
	}
}
A

false. The getNumberOfHumps() method in the child class must be at least as accessible as the parent method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q
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;
	}
}
A

true. The method fly is successfully overloaded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q
true/false, the following code compiles:
public class Camel {
	protected int getNumberOfHumps() {
		return 0;
	}
}
public class BactrianCamel extends Camel {
	protected int getNumberOfHumps() {
		return 2;
	}
}
A

true. The getNumberOfHumps() method in the child successfully overrides the same method in the parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q
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;
	}
}
A

false. The “eat()” method is not successfully overridden here since the return types, void and int, are not covariant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

true/false, a child method may hide or eliminate a parent method’s exception without issue.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q
true/false, the following code compiles:
public class Camel {
	protected String getNumberOfHumps() {
		return "Undefined";
	}
}
public class BactrianCamel extends Camel {
	private int getNumberOfHumps() {
		return 2;
	}
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q
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;
}}
A

false. The scope of the exception thrown in Snake.getWeight() is narrower than in Reptile.getWeight().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q
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();
	}
}
A

Compiler error on this line:
public void eat() {
The compiler thinks you’ve trying to override a method that should be hidden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is it called when methods replace parent methods in the calls defined in the child class?

A

Method hiding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

true/false, for method hiding, the method in the child class must have the same signature as the method in the parent class.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

true/false, in Java, in a child class, it is possible to override a private method in the parent class.

A

false. However, the child class can define its own version of the method with the same or modified signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q
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();
	}
}
A

Compiler error on this line:
public static void eat() {
The compiler thinks you’re trying to hide a method that should be overridden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

A child method replacing the parent method in calls defined in both the parent and child is called what?

A

Method overriding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

true/false, for method hiding, the method in the child class must have the same visibility than the method in the parent class.

A

false. It must be at least as or more accessible than the method in the parent class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

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.

A

true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q
true/false, the following code compiles:
public class Camel {
	private String getNumberOfHumps() {
		return "Undefined";
	}
}
public class BactrianCamel extends Camel {
	private int getNumberOfHumps() {
		return 2;
	}
}
A

true. This code compiles without issue. If the method in the parent class were public or protected, the code would not compile.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

true/false, for method hiding, if the method returns a value, it must be different than that from the method in the parent class.

A

false. It must be the same or a subclass of that of the parent class, aka covariant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q
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();
	}
}
A

Panda bear is chewing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

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

A hidden method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

When in a child class, you can access the parent version of a hidden variable using what keyword?

A

super

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q
Does the following code compile:
public class Bird {
	public final boolean hasFeathers() {
		return true;
	}
}
public class Penguin extends Bird {
	public boolean hasFeathers() {
		return false;
	}
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

When a parent method is overridden by a child method, the parent method is never used unless explicitly called using what syntax?

A

super.method()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q
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();
	}
}
A

Marsupial walks on two legs: true

Kangaroo walks on two legs: true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

true/false, you can hide a method in a parent class if it’s marked “final”.

A

false. You cannot hide a method in a parent class from the child class if it’s marked “final”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

true/false, java allows variables to be overridden.

A

false. They can be hidden but not overridden.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q
Does the following code compile:
public class Bird {
	public boolean hasFeathers() {
		return true;
	}
}
public class Penguin extends Bird {
	public final boolean hasFeathers() {
		return false;
	}
}
A

yes. It’s ok if the overriding method is marked “final” so long as the method being overridden is not marked “final”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

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)?

A

parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q
Does the following code compile:
public class Bird {
	public boolean hasFeathers() {
		return true;
	}
}
public class Penguin extends Bird {
	public boolean hasFeathers() {
		return false;
	}
}
A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

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.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

true/false, final methods can be overridden.

A

false. Final methods cannot be overridden.

82
Q

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)?

A

child

83
Q
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();
}}
A

Marsupial walks on two legs: false

Kangaroo walks on two legs: true

84
Q

true/false, when creating a method with the “final” keyword, you cannot override this method from a child class.

A

true.

85
Q

How do you hide a variable?

A

In your child class, you define a variable with the same name as a variable in the parent class.

86
Q

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.

A

false. The parent version of a hidden method is always executed if the call to the method is defined in the parent class.

87
Q
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;
	}
}
A

no. You cannot override a method that is marked “final”.

88
Q
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();
}}
A

[parentTail=4]

[tail=8,parentTail=4]

89
Q

true/false, an abstract method contains no implementation.

A

true

90
Q
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";
	}
}
A

true

91
Q
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();
}}
A

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.
92
Q
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();
}}
A

[parentTail=4]

[tail=8,parentTail=4]

93
Q

true/false, an abstract class can be instantiated.

A

false.

94
Q

true/false, you cannot define an abstract method as private.

A

true

95
Q

true/false, a concrete class must implement all of the abstract methods inherited from the abstract class that it is extending.

A

true

96
Q
true/false, the following code compiles:
public abstract class Whale {
	private abstract void sing();
}
A

false. An abstract method may not be marked private.

97
Q

true/false, an abstract method can be defined in a class that is not abstract.

A

false. Abstract methods must be defined in an abstract class.

98
Q
true/false, the following code compiles:
public abstract class Animal {
	public abstract String getName();
}
public class Walrus extends Animal { }
A

false. A concrete class must implement all inherited abstract methods from the abstract class that it is extending.

99
Q

true/false, an abstract class may extend another abstract class.

A

true

100
Q

true/false, abstract methods must not provide a method body/implementation in the abstract class in which it is declared.

A

true

101
Q

true/false, abstract classes may not be marked private, protected, or final.

A

true

102
Q

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.

A

true

103
Q
true/false, the following code compiles:
public class Chicken {
	public abstract void peck();
}
A

false. You cannot have an abstract method in a class that is not abstract.

104
Q

true/false, an abstract class is required to have at least one abstract method.

A

false. An abstract class doesn’t need to have any abstract methods.

105
Q
Why does the following code not compile?
public abstract class Turtle {
	public abstract void swim() {}
	pubic abstract int getAge() {
		return 10;
	}
}
A

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.

106
Q

Why is it that you cannot mark an abstract method or class as final?

A

Abstract classes and methods are meant to be extended or overwritten, and marking them as final takes away the ability to do that.

107
Q

true/false, an abstract class may include nonabstract methods and variables.

A

true

108
Q
true/false, the following code compiles:
public abstract class Goat {
	public abstract final void chew();
}
A

false. abstract methods are not allowed to be marked final.

109
Q
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";
}}
A

false. The class Lion must implement all abstract methods that it inherits, and it is missing the method roar().

110
Q

The first nonabstract subclass that extends an abstract class is known as what?

A

A concrete class.

111
Q

true/false, the first concrete class that extends an abstract class must provide an implementation for at least one of the inherited abstract methods.

A

false. it must provide an implementation for all of the inherited abstract methods.

112
Q

true/false, you can instantiate an an abstract class.

A

false

113
Q
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!");
}}
A

true

114
Q
true/false, the following code compiles:
public final abstract class Tortoise {}
A

false

115
Q

true/false, you can define an abstract method in a non abstract class.

A

false. Abstract methods may only be defined in abstract classes.

116
Q
true/false, the following code compiles:
public abstract class Cow {}
A

true

117
Q

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.

A

true

118
Q
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!");
	}
}
A

true

119
Q

true/false, an abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.

A

true

120
Q
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!");
}}
A

true.

121
Q

true/false, abstract methods may not be declared private or final.

A

true

122
Q
true/false, the following code compiles:
public abstract class Eel {
	public static void main(String[] args) {
		final Eel eel = new Eel();
	}
}
A
false. The following line fails to compile:
final Eel eel = new Eel();
You cannot have a private member in an abstract class.
123
Q
true/false, the following code compiles:
public abstract class Chicken {
	public abstract void peck();
}
A

true

124
Q
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";
	}
}
A

false. The class Bird must implement all of the abstract methods defined in the Animal class.

125
Q

true/false, abstract classes may be defined with any number, including zero, of abstract and nonabstract methods.

A

true

126
Q
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 {
}
A

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.

127
Q
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!");
	}
}
A

false. When overriding a method in the parent, you cannot reduce visibility.

128
Q

true/false, an interface may not be marked final.

A

true.

129
Q
true/false, the following code compiles:
public interface WalksOnTwoLegs {}
public class TestClass {
	public static void main(String[] args) {
		WalksOnTwoLegs example = new WalksOnTwoLegs();
	}
}
A

false. You can not directly instantiate an interface.

130
Q

true/false, the following interfaces are equivalent:

public interface CanFly {
	void fly(int speed);
}
public abstract interface CanFly {
	public abstract void fly(int speed);
}
A

true. The compiler would automatically convert the first interface into the second.

131
Q
true/false, the following code compiles:
public abstract interface Int1 {
	public static final int MIN = 2;
	public abstract int getMaxDepth();
}
A

true. All interface variables are assumed to be static, and all interface methods are assumed to be abstract.

132
Q
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 { }

A

false. As a concrete class implementation, the class LeopardSeal should implement the getTailLength and getNumberOfWhiskers methods.

133
Q

What is an abstract data type that defines a list of abstract public methods that any class implementing it must provide?

A

An interface

134
Q

true/false, an interface is required to have at least one method.

A

false. An interface need not have any methods.

135
Q

What specifiers get automatically inserted into an interface definition if not provided by the developer?

A

public and abstract

136
Q

true/false, the following code compiles:

public final interface WalksOnTwoLegs {}

A

false. An interface may not be marked final.

137
Q

true/false, a class may not inherit multiple interfaces.

A

false. They can implement may interfaces.

138
Q

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.

A

true

139
Q

true/false, all top-level interfaces are assumed to have public or default access.

A

true.

140
Q

true/false, the following code compiles:

public interface WalksOnTwoLegs {}

A

true

141
Q

true/false, the following code compiles:

public interface HasTail {
	public int getTailLength();
}
public interface HasWhiskers() {
	public int getNumberOfWhiskers();
}
public interface Seal extends HasTail, HasWhiskers { }
A

true

142
Q

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();
}
A

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.
}

143
Q

true/false, interfaces can be instantiated directly.

A

false.

144
Q

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.

A

true.

145
Q
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");
	}
}
A

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.

146
Q
true/false, the following code compiles:
public class Hyena {}

public interface HasFur extends Hyena {}

A

false. An interface cannot extend or implement a class.

147
Q

true/false, an interface can extend another interface.

A

true

148
Q

true/false, the following code compiles:
public interface myInterface {
public int MYINTEGER = 0;
}

A

true

149
Q
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 {}
A

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.

150
Q

true/false, the value of an interface variable must be set when it is declared since it is marked as final.

A

true

151
Q

true/false, the following code compiles:

public interface CanRun {}

public class Chetah implements CanRun {}

A

true

152
Q
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");
	}
}
A
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");
	}
}
153
Q

true/false, the following code compiles:

public interface myInterface {
private int MYINTEGER = 0;
}

A

false. interface varaibles are assumed to be public.

154
Q

true/false, interface variables are assumed to be public.

A

true

155
Q

true/false, the following code compiles:
public interface myInterface {
public int MYINTEGER;
}

A

false. Interface variables are assumed to be static and must be assigned a value when declared since it is marked as final.

156
Q

true/false, the following code compiles:

public interface CanRun {}
public class Chetah extends CanRun {}
A

false. A class cannot extend an interface.

157
Q

true/false, a class can extend an interface.

A

false. A class implements an interface, not extends it.

158
Q
true/false, the following code compiles:
public interface Herbivore {
	public int eatPlants();
}
public interface Omnivore {
	public void eatPlants();
}
public interface Supervore extends Herbivore, Omnivore {}
A

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.

159
Q

true/false, the following code compiles:

public interface myInterface {
protected int MYINTEGER = 0;
}

A

false. interface varaibles are assumed to be public.

160
Q
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");
	}
}
A

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.

161
Q

true/false, an interface can implement another interface.

A

false.

162
Q

true/false, interface methods are assumed to be static and final.

A

false. An interface method cannot be marked final.

163
Q

true/false, interface variables are assumed to be static and final.

A

true.

164
Q
true/false, the following code compiles:
public interface Carnivore {
	public int getRequiredFoodAmount() {
		return 13;
	}
}
A

false. In an interface, for a method to have a body, it must be a default method.

165
Q

true/false, in an interface, if a method is marked with the “default” keyword, it does not need to provide a body.

A

false. All default methods must provide a method body.

166
Q
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;
	}
}
A

false. The doFinsHaveScales method in the SharkFamily interface either needs to include the keyword “default” or remove the method body.

167
Q

true/false, a static method defined in an interface is not inherited in any classes that implement the interface.

A

true.

168
Q
true/false, the following code compiles:
public interface Hop {
	public static int getJumpHeight() {
		return 8;
	}
}
A

true

169
Q

true/false, a class that implements two interfaces containing static methods with the same signature will compile.

A

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.

170
Q

true/false, a default method cannot be marked as static, final, or abstract.

A

true.

171
Q

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.

A

true.

172
Q
true/false, the following code compiles:
public interface IsWarmBlooded {
	boolean hasScales();
	public default double getTemperature() {
		return 10.0;
	}
}
A

true.

173
Q

true/false, a default method will not compile if it marked as private or protected.

A

true.

174
Q

true/false, a default method within an interface defines an abstract method with a default implementation.

A

true.

175
Q
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;
	}
}
A

true

176
Q

true/false, the following code compiles:
public interface Carnivore {
public default void eatMeat();
}

A

false. A default method must have a method body.

177
Q

true/false, if a class implements two interfaces that have default methods with the same name and signature, the compiler will error out.

A

true.

178
Q
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());
	}
}
A

true. It compiles because we’ve overridden the duplicate default method getSpeed() in the Cat class.

179
Q
true/false, the following code compiles:
public interface Hop {
	static int getJumpHeight() {
		return 8;
	}
}
A

true

180
Q
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());
	}
}
A

true.

181
Q

true/false, a default method may be defined in regular class or an interface.

A

false. A default method may only be defined within an interface.

182
Q
true/false, the following code compiles:
public interface HasFins {
	public default double getLongestFinLength() {
		return 20.0;
	}
}
public interface SharkFamily extends HasFins {
	public double getLongestFinLength();
}
A

true

183
Q

true/false, a default method is a method defined within an interface with the default keyword in which a method body is provided.

A

true.

184
Q

true/false, a default method is not assumed to be static, final, or abstract.

A

true. The default method may be overridden by the implementing class.

185
Q
true/false, the following code compiles:
public interface Hop {
	protected static int getJumpHeight() {
		return 8;
	}
}
A

false, a static method in an interface cannot be marked private or protected.

186
Q

true/false, default methods in an interface are assumed to have protected access.

A

false, they are assumed to be public.

187
Q

true/false, a static method in an interface is assumed to be public and will not compile if marked as private or protected.

A

true.

188
Q
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());
	}
}
A
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.
189
Q
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());
	}
}
A

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.

190
Q

true/false, to reference a static method in an interface, a reference to the name of the interface must be used.

A

true.

191
Q
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());
	}
}
A

Compiler error on this line:
System.out.println(hasTail.age);
And compiler error on this line:
System.out.println(primate.isTailStriped());

192
Q
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());
}}
A

Feeding: Alligator
Feeding: Crocodile
Feeding: Reptile

193
Q

What is the property of an object to take on many different forms?

A

Polymorphism

194
Q

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.

A

true.

195
Q

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;
	}
}
A

true, the code compiles, but a ClassCastException is thrown at runtime from this line:
Capybara capybara = (Capybara) rodent;

196
Q
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();
	}
}
A

The bird name is: Peacock

197
Q

A java object may be accessed which three ways?

A
  1. Using a reference with the same type as the object.
  2. Using a reference that is a super class of the object.
  3. Using a reference that defines an interface the object implements, either directly or through a superclass.
198
Q
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());
	}
}
A

10
false
true

199
Q
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;
	}
}
A
false. Compiler error on this line:
Bird bird = (Bird) fish;
The Fish and Bird classes are not related through any class hierarchy.
200
Q

What is known as a method in which the specific implementation is not determined until runtime?

A

A virtual method.