Chapter 4: Methods and Encapsulation Flashcards

1
Q

What does a basic method signature (method declaration) include?

A

access modifier, optional modifiers, return type, name, parameters, method body public static void main( String [] args){ //method body }

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

Which of these are required? Access modifier (ie Public) specifier (final) Return type Method name Parameter list exception list method body

A

no no yes yes yes no yes

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

What are the four access modifiers?

A

public private protected and default (which is package private, there is actually no keyword for this)

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

Which of these compile? public void walk1(){}

default void walk2(){}

void public walk3(){}

void walk4(){}

A

yes

no default is not a keyword

no access modifier comes before the return type

yes

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

What are the three optional specifiers you need to know for this exam?

A

final used when a method is not allowed to be overridden by a subclass static when you want to bind a method to a class abstract used when not providing a method body

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

Which of these don’t compile and why?

public void walk(){}

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(){}

A

yes

yes

yes

yes

no modifier is not a key word

no modifiers cannot come directly before the return type

yes

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

Which of these compile?

public void walk1(){}

public void walk2(){return;}

public String walk3(){return “”;}

public String walk4(){}

public walk5(){};

String walk6(int a){ if (a==4 return “”;)}

A

yes

yes

yes

no there is not a return statement in the body

no

no there is no return type specified

no tricky, but because a string may not always be returned, if a does not equal 4 for example, the compiler will complain

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

Which of these compiles?

int number(){ int temp = 9; return temp; }

int longMethod(){ int temp =9L return temp ; }

A

The first. You cannot stuff a long into an int. Also, this shows that a method that is supposed to return an int cannot return a long value.

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

Which of these compile? public void walk1(){} public void 2walk(){} public walk3 void(){} public void Walk_$(){} public void() {}

A

yes no you cannot start a method name with a number no, the name cannot come before the return type yes no, there is no method name

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

Is the parameter list required?

A

Yes, but it doesn’t have to contain any parameters

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

Which of these 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){}

A

yes no, no parameter list ie the parenthesis are missing yes no, semi-colon in the place of the comma. Semi-colons are for separating statements, no parameter lists yes

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

How does Java indicate that something went wrong?

A

By throwing an exception

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

Which of these compile? public void example1(){} public void example2() throws IllegalArgumentException{} public void example3() throws IllegalArgumentException, InterruptedException{}

A

All of these compile. You can have as many exceptions as you want–separated by commas–or none at all.

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

What is a method body?

A

Simply a code block { } that contains zero or more Java Statements.

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

Which of these compile? public void walk1(){} public void walk2(); public void walk3(int a ) { int anem = 5;}

A

yes no, no method body yet there is a semicolon. yes

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

What does varargs stand for?

A

variable argument(s) that are treated like an array

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

How are varargs different than arrays?

A

Varargs parameters must be the last element in the method’s parameters list.

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

How many varargs can you have in a method signature?

A

Only one. Varargs must be the last element of a methods parameter list, therefore, there can only be one of them.

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

Which of these compile? public void walk1(int… numbers){} public void walk2(int start, int… numbers){} public void walk3(int…numbers, int start){} public void walk4(int… start, int… numbers){}

A

yes, varargs at the end yes, varargs at the end and separated by a comma no, varargs are not at the end no, you can only have one varargs element in the parameter list of method signature

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

What are the three ways you can call a method with a varargs parameter?

A
  1. You can pass in an array of elements. 2. Or, you can list the elements of the array and Java will create the array for you. 3. Or, you can omit the varargs values in the method call and Java will create an array of zero
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the output? public static void main(String[] args) { walk(1); walk(1,2); walk(1,2,3); walk(1, new int[] {4,5}); walk(1, null); } public static void walk(int start, int… numbers){ System.out.println(numbers.length); } }

A

0 1 2 2 null pointer exception. As null is not an int, Java treats it as an array reference that happens to be null. When walk tries to determine the length of the array it returns a null pointer exception.

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

What is the output? public static void main(String[] args) { run(11, 22); } public static void run(int… numbers) { System.out.println(numbers[1]); } }

A
  1. you can access a varargs parameter just like accessing an array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How accessible are ‘private’ classes, methods and fields?

A

Only accessible within the same class. Only code in the same class can call private methods or access private fields. Accessing private members of another class will always stop the program from compiling.

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

How accessible are ‘default’ (package private) access:

A

private and other classes in the same package. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.

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

How accessible are protected classes, methods and fields?

A

This does everything the default

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

How accessible are public classes, methods and fields?

A

protected and classes in other packages.

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

Will this compile? public class App { private static int a; public static String b; public static char c; public static void main(String[] args) { boolean[] b = { true, false }; Duck duck = new Duck(); duck.makeNoise(); System.out.println(b.length); } } class Duck { private String noise = “quack”; private void quack() { System.out.println(noise); } private void makeNoise() { quack(); } }

A

No. The makeNoise() method call will stop the program from compiling. It is in a class that is accessible, but the methods themselves are not accessible because they are marked private. If makeNoise() were marked public or protected, it would compile just fine.

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

True or False: When there is no access modifier only classes within the same package have access?

A

True

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

What’s the difference between default access and protected access?

A

default provides the ability for classes of the same package to have access. Protected adds the ability for children to access members of a parent class.

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

What does it mean to extend a super class?

A

Extending means creating a subclass that has access to any protected or public members of the parent class.

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

Can you access protected members from a class in a different package?

A

Yes. But only if the class in question is a child of the parent which belongs to the different class.

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

What can access a protected method?

A

A call from the same package or from another class extending the super.

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

How would you go about accessing a protected method or field from another class? What are the two ways of doing so?

A
  1. It’s in the same package. 2. protected methods and fields can be accessed by children.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What will happen if you try to call a protected method from a class/instance of an object that is defined in a different package?

A

It will work fine if it’s public. Otherwise it won’t work. Unless you have inherited and treated that other class as a super. You can use protected methods from a super.

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

Why is it that a child class does not have access to the protected methods of a new instance of the parent class within the child’s method body? (assuming the parent is in a different package.)

A

The new object is NOT the same object as that which the child is inheriting from. So the protected methods essentially become private

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

What does public access mean?

A

Anyone can access the public methods, classes, fields from anywhere.

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

Can you call static methods without an instance of the class?

A

Yes. Think of the main() method.

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

Other than the main method, what are two purposes for static methods?

A
  1. For utility or helper methods not requiring the state of an object to work well. 2. For state that is shared by all instances of a class, like a counter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Other than the main method, what are two purposes for static methods?

A
  1. For utility or helper methods not requiring the state of an object to work well. 2. For state that is shared by all instances of a class, like a counter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is the output? public class App { public static int k; public static void main(String[] args) { System.out.println(k); App app = new App(); System.out.println(app.k); app = null; System.out.println(app.k); } }

A

0 0 0 It doesn’t matter that app is set to null because k is a static value.

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

What is the output? public class App { public static int k; public static void main(String[] args) { System.out.println(k); App app1 = new App(); App app2 = new App(); app1.k = 1; app2.k = 2; System.out.println(k); } }

A
  1. the app1 and app2 are just distractions from the fact that this is a static variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

What constitutes a ‘member’ of a class?

A

a field or a method of a class

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

What is the output: public class App { private static String name = “Jedd”; public static int k; public static void main(String[] args) { first(); second(); third(); } public static void first() {System.out.println(name);}; public static void second() {System.out.println(name);}; public void third() {System.out.println(name);}; }

A

This won’t compile. You can’t make as static reference to a non static method. A static member cannot call an instance member. BUT, once you made an instance of the class, you could access that static field.

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

What is the output? public class App { private String name = “Jedd”; public static int k; public static void main(String[] args) { first(); second(); third(); } public static void first() {System.out.println(name);} public static void second() {System.out.println(name);} public static void third() {System.out.println(name);} }

A

This won’t compile. You cannot call an instance member from a static member. The only way to fix this would be to make the methods instance methods as well.

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

If you have a static method, can you call another static member? (member meaning method or field) If yes, how would you do so?

A

Yes. You can just use the class name. public class App { private static String name = “Jedd”; public static void first() {} public static void second() {} public static void third() {System.out.println(name);} public static void main(String[] args) { App.first(); second(); App.third(); //also works if you just say first, second or third without App in front. Class name is inferred by the compiler } }

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

It you have a static method, can you call an instance member? If yes, how would you do it?

A

No. At least not without instantiating an object.

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

If you have an instance method, can you call a static method or variable? If yes, how would you do it?

A

By using the class name or a reference variable. For example: public class App { private static String name = “Jedd”; USING REFERENCE VARIABLE public void first() {System.out.println(name);} USING CLASS NAME public void second() {System.out.println(App.name);} public static void main(String[] args) { App app = new App(); app.first(); app.second(); } }

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

If you have an instance method, can you call another instance method or variable?

A

Yes. By using a reference variable. For example: public class App { private static String name = “Jedd”; private String lastName = “Whateve”; // This is fine because you have to make an instance before you can call this method. So class variable will be available. public void first() {System.out.println(name);} // This works because both are instance variables. So you can call this with a reference variable. public void second() {System.out.println(lastName);} public static void main(String[] args) { App app = new App(); app.first(); app.second(); } }

50
Q

What is a constant variable? Which modifier is applied to these?

A

A variable that is never meant to change throughout the life of the program. ‘final’. This means it can never change.

51
Q

Will this compile? public class App { final static Integer NUMBER = 2; public static void main(String[] args) { NUMBER = 2; System.out.println(NUMBER); } }

A

No. You cannot modify a variable with the ‘final’ modifier. (AKA a constant)

52
Q

Do all of these work? private final static int NUMBER = 2; private static final int NUMBER = 2; final private static int NUMBER = 2;

A

yes. Doesn’t seem to care

53
Q

Does this compile? public class App { private static final ArrayList values = new ArrayList<>(); public static void main(String[] args) { values.add(2); values.forEach(System.out::println); } }

A

Yes, this works. We can call methods on reference variables. Final, here, is only going to stop you from trying to reassign the final values to point at a different object.

54
Q

Does this compile? public class App { private static int one; private static final int two; private final static int three; private static final int four; static { one = 1; two = 2; three = 3; four = three + one; } public static void main(String[] args) { System.out.println(one); } }

A

Yes. This seems fishy because you are changing the value of a final variable, but since it’s all done “up front”.

55
Q

Will this compile? public class App { private static int one; private static final int two; private final static int three; private static final int four; static { one = 1; two = 2; three = 3; } public static void main(String[] args) { System.out.println(one); } }

A

No. The compiler will let you know that you haven’t initialized four.

56
Q

Will this compile? public class App { private static int one; private static final int two; private final static int three; static final private int four; static { one = 1; two = 2; three = 3; four = three + one; } public static void main(String[] args) { System.out.println(one); } }

A

Yes. It’s fine with you initializing all the static variables in the static initializer.

57
Q

What is the key difference between Regular imports and static imports?

A

Static imports are for importing static members of classes where as regular imports are just for importing classes.

58
Q

Will this compile? public class App { private static int one=0; private static final int two=1; private final static int three=2; static final private int four=3; static { one = 1; two = 2; three = 3; four = three + one; } public static void main(String[] args) { System.out.println(one); } }

A

no. even though it is in the static initializer you cannot change the value of field marked final.

59
Q

Why doesn’t this work? import static java.util.Arrays; //does not compile import static java.util.Arrays.asList; static import java.util.Arrays.*; // does not compile public class BadStaticImports{ public static void main(String[] args){ Arrays.asList(“one”); // does not compile } }

A

The first tries to use a static import to import a class. Remember that static imports are only for importing static members of a class, not the class themselves. Regular imports are for importing a class. The second doesn’t compile because static can’t come before the word import. The third doesn’t compile because we have not imported the Arrays class, we have only imported it’s static member, asList, so you can’t refer to the class here.

60
Q

Will this compile? import static statics.A.Type; import static statics.B.Type;

A

No. You cannot import two classes with the same name. It would work if you just imported *A* and *B*

61
Q

Is Java a pass by value language? What does this mean?

A

Yes. This means that a copy of the variable is made and the method receives that copy.

62
Q

What is the output? public class App { public static void main(String… strings) { int num = 4; newNumber(num); System.out.println(num); } public static void newNumber(int num) { num = 8; } }

A
  1. This demonstrates pass by value because only a copy of num is passed into the newNumber(). num never has it’s value changed.
63
Q

What is the output? public class App { public static void main(String… strings) { String name = “Jedd”; newName(name); System.out.println(name); } public static void newName(String name) { name = “Not Jedd”; } }

A

This demonstrates that pass by value applies to reference variables as well. Just as with primitives, the variable assignment is only to the method parameter and doesn’t affect the caller.

64
Q

What is the a caller in the context of method parameters and the caller?

A

The caller is the method doing the calling.

65
Q

What is the output? public static void main(String… strings) { int number = 1; String letters = “abc”; number(number); letters = letters(letters); System.out.println(number + “ “ + letters); } public static int number(int number) { number++; return number; } public static String letters(String letters) { letters += “d”; return letters; }

A

1 abcd Note that number(number); is largely ignored by the caller. The method is called and ran, but the value isn’t assigned to a variable. This is different with letters because letters(letters) is actually stored in a variable.e. Creating a new string with the reference variable.

66
Q

What is method overloading?

A

It is when there are different method signatures with the same name but different type parameters

67
Q

What is the one thing that cannot change when creating an overloaded method?

A

The name. The name must remain the same but you are welcome to change the method type, parameter types, as well as the number of parameters. You can also have different access modifiers, specifiers, and exception lists.

68
Q

Do all of these compile? public void fly(int numMiles){} public void fly(short numFeet)[] public boolean fly() {return false;} void fly(int numMIles, short numFeet){} public void fly(short numFeet, int numMiles) throws Exception{}

A

Yes

69
Q

What’s wrong here? public void fly(int numMiles){} public int fly(int numMiles){}

A

The overloaded method won’t compile because it only differs from the original by return type. The parameter lists are the same so they are duplicate methods as far as Java is concerned. You must change the parameter list.

70
Q

What is wrong here? public void fly(int numMiles){} public static void fly(int numMiles){}

A

Same problem. The parameter list is the same.

71
Q

Will this compile? public void fly(int[] lengths){} public void fly(int… lengths){}

A

No. Remember that Java treats varargs as an array. This means the method signature for these two are identical.

72
Q

What happens of you pass an Integer variable as an argument to a method where the parameter expects the primitive int?

A

Autoboxing. Works just fine.

73
Q

With overloading, what happens if you have the primitive and the Integer versions as an overloaded method? Like this: public void fly(int numMiles){}; public void fly(Integer numMiles){};

A

Java will match the int numMiles version. Java tries to use the most specific parameter list it can find. Autoboxing is extra work.

74
Q

What is the output and why? public static void main(String… strings) { App app = new App(); app.fly(56); app.fly(“test”); } void fly(Object o) { System.out.println(“object “); } void fly(String string) { System.out.println(“string “); }

A

object string Java will choose the most specific reference type it can. So, the second call outputs string because the input is clearly a string, so it will specifically choose the string version of the overloaded method. The first selects object only after looking for the option to Autobox to Integer. When it doesn’t find that it goes to Object.

75
Q

What is the output and why? public static void main(String… strings) { App app = new App(); app.fly(123); app.fly(123L); } void fly(int i) { System.out.println(“int “); } void fly(long l) { System.out.println(“long “); }

A

int long The first call passes an int and sees an exact match. The second call passes a long and also sees an exact match.

76
Q

What is the output and why? public static void main(String… strings) { App app = new App(); app.fly(123); app.fly(123L); } // void fly(int i) { // System.out.println(“int “); // } void fly(long l) { System.out.println(“long “); }

A

long long Java has no problem calling a larger primitive. However, it will not do so unless a better match is found.

77
Q

Which method would be chosen for glide(1,2); Exact match by type. Larger primitive type. Autoboxed type. Varargs

A

Exact match by type.

78
Q

In what order will Java match overloaded methods? (4 options)

A

Exact match by type. Larger primitive type. Autoboxed Type. Varargs.

79
Q

Why doesn’t this compile? public static void main(String… strings) { App app = new App(); app.fly(4); app.fly(4L); } void fly(Long… l) {} void fly(String[] args) {} }

A

Java can handle converting 4 to a long, but it cannot do a second conversion to convert that long to a Long.

80
Q

Why doesn’t this compile? public static void main(String… strings) { App app = new App(); app.fly(4); app.fly(4L); } void fly(Long… l) {} void fly(String[] args) {} }

A

Java can handle converting 4 to a long, but it cannot do a second conversion to convert that long to a Long.

81
Q

Why doesn’t this compile? public static void main(String… strings) { App app = new App(); app.fly(4); app.fly(4L); } void fly(Long… l) {} void fly(String[] args) {} }

A

Java can handle converting 4 to a long, but it cannot do a second conversion to convert that long to a Long.

82
Q

What is a constructor?

A

A special method that matches the name of the class and has no return type..not even void.

83
Q

Are these valid constructors? (Assuming the class name is Bunny). public bunny() {} public void Bunny(){}

A

No. The first has a lower case b so doesn’t match the case specific class name. The second has a return type of void, so that won’t work.

84
Q

How do you call on a constructor?

A

Bunny bunny = new Bunny(); You have to call the constructor with the new keyword.

85
Q

What does java do when it sees the ‘new’ keyword?

A

Allocates memory for a new object. It also looks for a new constructor and calls it.

86
Q

What does the ‘this’ keyword do?

A

It tells java you want to reference an instance variable.

87
Q

Describe what’s happening here: public class Bunny{ private String color; public Bunny (String color) { this.color = color; } }

A

The constructor, if called, would create an instance of the bunny class, and use the constructor parameter to set the instance variable

88
Q

What’s the problem here? What is the output as is and how would you fix it? public class Bunny{ private String color; private int height; private int length; public Bunny(int length, int theHeight) { length = this.length; height = theHeight; this.color = “white”; } public static void main( String… args){ Bunny b = new Bunny(1,2); System.out.println(b.length + “ “ + b.height + “ “ + b.color); }}

A

0,2,white The problem is the: length = this.length. The order needs to be reversed to this.length = length. The first will set length to what the instance variable is, and it defaults to zero.

89
Q

Every class in Java has a constructor whether you code one or not. What would it look like?

A

public ClassName(){} No parameters.

90
Q

When are things auto-‘generated’ by Java? For example, the default constructor for classes, when would java generate this?

A

This happens at the compile step. This means the .java file will not have the generated method whereas the .class will.

91
Q

Is the default constructor created even if you have supplied a constructor for the class?

A

No.

92
Q

Why provide a private constructor for a class?

A

Having a private constructor tells the compiler not to provide a default no-argument constructor. It also prevents other classes from instantiating the class. This is useful when a class only has static methods or the class wants to control all calls to create new instances of itself.

93
Q

Can you overload class constructors?

A

Yes

94
Q

What is the output and why doesn’t it do what we think it should? public class App { private String color; private int height; private int length; public App(int length) { new App(1,2); } public App(int length, int theHeight) { this.length = length; this.height = theHeight; } public static void main(String… args) { App app = new App(2); System.out.println(app.color + “ “ + app.length + “ “ + app.height); } }

A

null 0 0 This doesn’t do what we want to because when the constructor with one parameter is called, it creates an object with the default weight and color. It then constructs a different object with the desired weight and color and ignores the new objects. Essentially, we constructed the first object, and then constructed a new object without ever referencing the new object. So the new object, built with 1 and 2 as values, is not really reachable. So probably gets garbage collected almost immediately as it has no pointers.

95
Q

What is the output and why does it work? public class App { private String color; private int height; private int length; public App(int length) { this(1,2); } public App(int length, int theHeight) { this.length = length; this.height = theHeight; } public static void main(String… args) { App app = new App(2); System.out.println(app.color + “ “ + app.length + “ “ + app.height); } }

A

null 1 2 When you use ‘this’ as if it were a method, Java calls another constructor on the same instance of the class. So, you’ve created an object in memory, and then called the same objects other constructor as a way to set the values.

96
Q

Why doesn’t the constructor called compile? public class App { private String color; private int height; private int length; public App(int length) { App(1,2); } public App(int length, int theHeight) { this.length = length; this.height = theHeight; } public static void main(String… args) { App app = new App(2); System.out.println(app.color + “ “ + app.length + “ “ + app.height); } }

A

It has to have ‘new’ in front of it. so public App(int length) {

new App(1,2);

}

97
Q

Why doesn’t this compile? public class App { private String color; private int height; private int length; public App(int length) { System.out.println(“Hello”); this(1,2); } public App(int length, int theHeight) { this.length = length; this.height = theHeight; } public static void main(String… args) { App app = new App(2); System.out.println(app.color + “ “ + app.length + “ “ + app.height); } }

A

If you choose to call this(), it must be the first non commented statement in the constructor.

98
Q

Can you set final fields with an class constructor?

A

Yes. But remember final fields can be set exactly one time during the initialization process. As construction is part of the initialization process this is just fine.

99
Q

What are the 4 steps (from start to finish) of initialization? (assuming an object is instantiated

A
  1. If there is a superclass, initialize it first. 2. Static variable declarations and static initializers in the order they appear in the file. 3. Instance variable declarations and instance initializers in the order they appear on the file. 4. The constructor.
100
Q

What is the output? public class App { private String name = “Jedd”; {System.out.println(name);} private static int COUNT = 0; static {System.out.println(COUNT);} static {COUNT += 10; System.out.println(COUNT);} public App(){ System.out.println(“Constructor just ran”); } public static void main(String…strings) { App app = new App(); } }

A

0 10 Jedd Constructor just ran Superclass static declarations and initializers instance declarations and initializers

101
Q

What is the output? public class App { static {printNumber(2);} static void printNumber(int num) {System.out.println(num + “ “ );} App(){printNumber(5); System.out.println(“in the constructor method”);} static {printNumber(4);} {printNumber(6);} static { new App(); System.out.println(“creating an instance using the constructor”);} {printNumber(8);} public static void main(String…strings) { System.out.println(“in the main method”); } }

A

2 4 6 8 5 in the constructor method creating an instance using the constructor in the main method public class App { //1 because static static {printNumber(2);} static void printNumber(int num) {System.out.println(num + “ “ );} //5 This is the constructor method App(){printNumber(5); System.out.println(“in the constructor method”);} //2 because static static {printNumber(4);} //3 simply calls the static method…so still static {printNumber(6);} // even though this is static, it’s still a constructor. So it will come after static and instance declarations and expressions static { new App(); System.out.println(“creating an instance using the constructor”);} //4 simply calls the static method again…so still static {printNumber(8);} //6 public static void main(String…strings) { System.out.println(“in the main method”); } }

102
Q

What’s the output: public class App { static {printNumber(1);} static void printNumber(int num) { System.out.println(num + “ “); } { printNumber(6); } App(int number) { System.out.println(“Start of constructor”); printNumber(number); System.out.println(“in the constructor method”); } static { System.out.println(“Beginning of static call”); new App(5); } public static void main(String… strings) { System.out.println(“in the main method”); } }

A

1 Beginning of static call 6 Start of constructor 5 in the constructor method in the main method

103
Q

What’s the point of encapsulation?

A

it’s helpful because it prevents callers from making uncontrolled changes to your class.

Encapsulation means we set up the class so only methods in the class with the variables can refer to the instance variables. Callers are required to use these methods.

104
Q

Are getters and setters private or public?

A

Public. These all a caller to reach instance data and manipulate it.

105
Q

Under JavaBeans naming conventions, what are instance variables called?

A

Properties

106
Q

JavaBeans rules for naming conventions:

Properties are (level of access) ____.

A

private

107
Q

JavaBeans rules for naming conventions:

Getter methods begin with __ or ___ if the property is a boolean.

A

“is” or “get”.

public boolean isHappy()

108
Q

JavaBeans rules for naming conventions:

Getter methods begin with ___ if the property is not a boolean.

A

“get”

public int getNumEggs(){}

109
Q

JavaBeans rules for naming conventions:

Setters begin with ____

A

“set”

public void setHappy(boolean happy)

110
Q

The method name must have a prefix ___/___/__, followed by the first letter of the property in uppercase, followed by the rest of the property name.

A

get/set/is

public void setNumEggs(int num){numEggs = num;}

111
Q

Which lines DO NOT follow JavaBeans naming conventions:

  1. private boolean playing;
  2. private String name;
  3. public Boolean getPlaying(){}
  4. public boolean isPlaying(){}
  5. public String name(){return name;}
  6. public void updateName(String n) {name=n;}
  7. public void setname(String n){name=n;}
A
  1. This should be getName()
  2. This should also be getName()

7 Should be setName(){}. (not setname)

112
Q

Why are immutable classes helpful?

A

You know you can pass them around without anything being changed by getters or setters.

113
Q

Are immatble classes allowed to have values?

A

Yes, but they can’t be changed after an instance has been made. The only way to do this is with constructors. Omit the getters and setters! Why would you want getters and setters on an immutable class?

114
Q

What is the output? Why is this class not immutable?

private StringBuilder builder;

public static void main(String… strings) {

StringBuilder sb = new StringBuilder(“initial”);

App problem = new App(sb);

sb.append(“ added”);

StringBuilder gotBuilder = problem.getBuilder();

gotBuilder.append(“ more”);

System.out.println(problem.getBuilder());}

public App(StringBuilder b) {

builder = b;}

public StringBuilder getBuilder() {

return builder;}

A

Output: initial added more

The problem is that we aren’t making a new StringBuilder in the constructor. So we end up passing the same String builder around throughout the class.

If you change the constructor to this:

public App(StringBuilder b) {

builder = new StringBuilder(b);}

the output becoming initial more

115
Q

What is functional programming?

A

A way of writing code that’s more declarative than imperative.

116
Q

What is a lambda expression?

A

A block of code that gets passed around. You can think of a lambda like an anonymous method.

117
Q

Can you pass lambdas around like a variable?

A

yes

118
Q

What is the output?

public static void main(String… strings) {

List<string> words = new ArrayList&lt;&gt;();</string>

words. add(“hello”);
words. add(“hi”);
words. add(“bi”);
words. add(“no”);
words. forEach(System.out::println);
words. removeIf(a -> !a.contains(“hello”));
words. forEach(System.out::println);

}

A

hello

hi

bi

no

hello

119
Q

Which interface do you need to know how to implement for the OCA exam?

A

Predicate

120
Q
A