Ch 4 - Methods and Encapsulation Flashcards
Which of the following methods compile?
public void walk1() {}
public void walk2() { return; }
public String walk3() {}
public void walk1() {}
public void walk2() { return; }
public String walk3() {} – does not compile
What optional specifier is used when not providing a method body?
abstract
When a method is defined as public, where can it be called from?
Any class
Which of the following lines below do not compile:
public void walk1() {}
default void walk2() {}
void public walk3() {}
void walk4() {}
public void walk1() {}
default void walk2() {} – does not compile
void public walk3() {} – does not compile
void walk4() {}
What optional specifier is used for defining class methods?
static
Which of the following method signatures do not compile?
public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {}
public void final walk6() {}
final public void walk7() {}
public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} – does not compile
public void final walk6() {} – does not compile
final public void walk7() {}
When a method is defined as protected, where can it be called from?
Can only be called from classes in the same package or subclasses.
Describe the different parts of the method signature and identify which are required and which aren’t.
Access modifier (public/private) - not required
optional specifier (final) - not required
return type (void) - required
method name (nap) - required
parameter list (int minutes) - required but can be empty parenthesis
optional exception list (throws Exception) - not required
method body ({}) - required, but can be empty
When a method is defined as private, where can it be called from?
Only from within the same class
When a method is defined as Default package protected, where can it be called from?
Can only be called from classes in the same package.
Which of the following methods compile?
public String walk1 { return “”; }
public walk2() {}
String walk3(int a) { if (a == 4) return “”; }
public String walk1 { return “”; }
public walk2() {} – does not compile
String walk3(int a) { if (a == 4) return “”; } – does not compile
true/false, a method signature does not have to provide a return type.
false. If there is not return type provided by the method, you must use the ‘void’ key word in the signature.
When a method returns a value, it must be assignable to…
…the return type.
Name the four access modifiers for methods.
public
private
protected
Default (package protected)
What optional specifier is used when a method is not allowed to be overridden by a subclass?
final
true/false, the following code compiles:
package pond.duck;
public class ParentDuck {
String noise = “quack”;
void quack() { System.out.println(noise); }
private void makeNoise() { quack();
}}
package pond.duck;
public class Duckling {
public void makeNoise() {
ParentDuck duck = new ParentDuck();
duck.quack();
System.out.println(duck.noise);
}}
true
What is the output of the following code?
public static void run(int… nums) {
System.out.println(nums[1]);
}
public static void main(String[] args) {
run(11, 22);
}
22
Which of the following methods will compile?
public void walk1(int… nums) {}
public void walk2(int start, int… nums) {}
public void walk3(int… nums, int start) {}
public void walk4(int… start, int… nums) {}
public void walk1(int… nums) {}
public void walk2(int start, int… nums) {}
public void walk3(int… nums, int start) {} // does not compile
public void walk4(int… start, int… nums) {} // does not compile
What would the output of the following program be?
public static void walk(int start, int… nums) {
System.out.println(nums.length);
}
public static void main(String[] args) {
walk(1);
walk(1, 2);
walk(1, 2, 3);
walk(1, new int[] {4,5});
walk(1, null);
}
0
1
2
2
NullPointerException
When using a vararg parameter in a method’s input parameter list, where must it be located?
It must be the last element.
Which of the following methods compile?
public void walk1() {}
public void 2walk() {}
public walk3 void() {}
public void Walk_$() {}
public void() {}
public void walk1() {}
public void 2walk() {} – does not compile
public walk3 void() {} – does not compile
public void Walk_$() {}
public void() {} – does not compile
Which of the following methods compile?
public void walk1() {}
public void walk2();
public void walk3(int a) { int name = 5; }
public void walk1() {}
public void walk2(); – does not compile
public void walk3(int a) { int name = 5; }
true/false, the following code compiles:
package pond.duck;
public class ParentDuck {
private String noise = “quack”;
private void quack() { System.out.println(noise); }
private void makeNoise() { quack();
}}
package pond.duck;
public class Duckling {
public void makeNoise() {
ParentDuck duck = new ParentDuck();
duck.quack(); System.out.println(duck.noise);
}}
false
How many exceptions can you put in the optional exception list?
As many as you want.