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.
Which of the following methods compile?
public void walk1() {}
public void walk2 {}
public void walk3(int a) {}
public void walk4(int a; int b) {}
public void walk5(int a, int b) {}
public void walk1() {}
public void walk2 {} – does not compile
public void walk3(int a) {}
public void walk4(int a; int b) {} – does not compile
public void walk5(int a, int b) {}
How many vararg parameters can you have in a method’s input parameters list?
only one
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.swan;
import pond.duck.ParentDuck;
public class Cygnet {
public void makeNoise() {
ParentDuck duck = new ParentDuck();
duck.quack();
System.out.println(duck.noise);
}}
false
The protected access modifier adds the ability to what?
Access members of a parent class.
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.inland;
import pond.shore.Bird;
public class Bird2 {
public void watchBird() {
Bird bird = new Bird();
bird.floatInWater();
System.out.println(bird.text);
}}
false, protected members are only visible to sub classes and classes in the same package.
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.swan;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void swim() {
floatInWater();
System.out.println(text);
}}
true
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.goose;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void swim() {
Bird2 other = new Bird2();
other.floatInWater();
System.out.println(other.text);
}}
package pond.duck;
import pond.goose.Bird2;
public class Bird3 {
public void watch() {
Bird2 bird2 = new Bird2();
bird2.floatInWater();
}}
false
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.swan;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void swim() {
Bird2 other = new Bird2();
other.floatInWater();
System.out.println(other.text);
}}
true
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.goose;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void watchBird() {
floatInWater();
System.out.println(text);
}}
true, Bird2 is a subclass of Bird.
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.goose;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void swim() {
Bird2 other = new Bird2();
other.floatInWater();
System.out.println(other.text);
}
pubic void swim2() {
Bird other = new Bird2();
other.floatInWater();
System.out.println(other.text);
}}
false.
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.shore;
public class Bird2 {
public void watchBird() {
Bird bird = new Bird();
bird.floatInWater();
System.out.println(bird.text);
}}
true, Bird2 is in the same package as Bird.
true/false, the following code compiles:
package pond.shore;
public class Bird {
protected String text = “floating”;
protected void floatInWater() {
System.out.println(text);
}}
package pond.swan;
import pond.shore.Bird;
public class Bird2 extends Bird {
public void swim() {
Bird other = new Bird();
other.floatInWater();
System.out.println(other.text);
}}
false. The code does not compile because the members of Bird are not being accessed in Bird2 via inheritance.
true/false, a static method can call an instance method or member.
false Well, it can if the method instantiates the object.
What is the output of the following lines of code?
public class Counter {
private static int count;
public Counter() { count++; }
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
System.out.println(count);
}}
3
true/false, a static member can call an instance member.
false
true/false, the following code will compile:
package pond.duck;
public class Bird {
public String name = “helpful”;
public void swim() {
System.out.println(“swim”);
}}
package pond.goose;
import pond.duck.Bird;
public class Goose {
public void swim() {
Bird bird = new Bird();
bird.swim();
System.out.println(bird.name);
}}
true
What is the output of the following code?
public class Koala {
public static int count = 0;
}
public class KoalaTester {
public static void main(String[] args) {
Koala k = new Koala();
System.out.println(k.count);
k = null;
System.out.println(k.count);
}}
0
0
true/false, an instance method can call a static method or member.
true
Does the following code compile, and if not, why?
public class Gorilla {
public static int count;
public static void addGorilla() { count++; }
public void babyGorilla() { count++; }
public void announceBabies() {
addGorilla();
babyGorilla();
}
public static void announceBabiesToEveryone() {
addGorilla();
babyGorilla();
}
public int total;
public static double average = total / count;
}
The code does not compile here:
public static void announceBabiesToEveryone() {
addGorilla();
babyGorilla(); // does not compile
}
It also does not compile on this line:
public static double average = total / count;
You cannot make a static reference to a non static member.
Having a static method eliminates what?
…the need for the caller to instantiate the object just to call the method.
When a member has default (package private) permissions, what does it mean?
It means that a member is private to classes in the same package. In other words, only classes in the same package can access it.
true/false, a static method can call another static method or member.
true
What is the output of the following code?
public class Koala {
public static int count = 0;
}
public class KoalaTester {
public static void main(String[] args) {
Koala.count = 4;
Koala k1 = new Koala();
Koala k2 = new Koala();
k1. count = 6;
k2. count = 5;
System.out.println(Koala.count);
}}
5
What is the output of the following code?
public class Static {
private String name = “Static class”;
public static void first() {}
public static void second() {}
public static void third() {
System.out.println(name);
}
public static void main(String[] args) {
first();
second();
third();
}}
compiler error on this line:
public static void third { System.out.println(name); }
the variable name is not static.
What is the output of the following code?
public class Static {
private String name = “Static class”;
public static void first() {}
public static void second() {}
public void third() {
System.out.println(name);
}
public static void main(String[] args) {
first();
second();
third();
}}
compiler error on this line:
third();
You cannot call a non static method from a static method, such as main.
Does this code compile?
private static final ArrayList values = new ArrayList<>();
public static void main(String[] args) { values.add(“changed”); }
yes, “values” is a reference variable. All the compiler can do is make sure that we don’t try to reassign “values” to a different object.