Chapter 4 Methods and Encapsulation Flashcards

1
Q

What access modifiers Java offers for methods ?

A
  1. public - The method can be called from any class.
  2. private - The method can only be called from within the same class.
  3. protected - The method can only be called from classes in the same package or subclasses.
  4. Default (Package Private) Access - The method can only be called from classes in the same package. This one is tricky because there is no keyword for default access. You simply omit the access modifier.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What optional specifiers Java offers (six) ?

A
  1. static - Used for class methods.
  2. abstract - Used when not providing a method body.
  3. final - Used when a method is not allowed to be overridden by a subclass.
  4. synchronized - On the OCP but not the OCA exam.
  5. native - Not on the OCA or OCP exam. Used when interacting with code written in another language such as C++.
  6. strictfp - Not on the OCA or OCP exam. Used for making floating-point calculations portable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between default and protected access modifiers from methods point of view ?

A
  • Protected - The method can only be called from classes in the same package or subclasses.
  • Default - The method can only be called from classes in the same package.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can instance members call static members and vice versa ?

A

When referenced from outside the class, methods are called using the classname—for example, StaticClass.method(). Instance members are allowed to call static members, but static members are not allowed to call instance members.

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

What is pass-by-value ?

A

It means that calls to methods create a copy of the parameters. Assigning new values to those parameters in the method doesn’t affect the caller’s variables.

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

What means overloaded methods ?

A

Overloaded methods are methods with the same name but a different parameter list. Java calls the most specific method it can find. Exact matches are preferred, followed by wider primitives. After that comes autoboxing and finally varargs.

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

When the default no-argument constructor is called ?

A

The default no-argument constructor is called when no constructor is coded.

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

Can multiple constructors call each other ?

A

Multiple constructors are allowed and can call each other by writing this(). If this() is present, it must be the first statement in the constructor.

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

Do you know the order of initialization ?

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 in the file.
  4. The constructor.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What rules for JavaBeans naming conventions do you know ?

A
  • Properties are private;
  • Getter methods begin with is if the property is a boolean;
  • Getter methods begin with get if the property is not a boolean;
  • Setter methods begin with set;
  • The method name must have a prefix of set/get/is, followed by the first
    letter of the property in uppercase, followed by the rest of the property name;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are parentheses around parameter list required in lamda expressions ?

A

The parentheses are only optional when there is one parameter and it doesn’t have a type declared.

print(() -> true); // 0 parameters
print(a -> a.startsWith(“test”)); // 1 parameter
print((String a) -> a.startsWith(“test”)); // 1 parameter
print((a, b) -> a.startsWith(“test”)); // 2 parameters
print((String a, String b) -> a.startsWith(“test”)); // 2 parameters

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

Will this compile:

public void methodA () { return; }
public void methodB () { return null; }

A

public void methodA () { return; } // YES
public void methodB () { return null; } // NO - Void methods cannot return a value

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

What is encapsulation and what is immutability ? How they differ ?

A

Encapsulation requires using methods to get and set instance variables so other classes are not directly using them. Instance variables must be private for this to work. Immutability takes this a step further, allowing only getters, so the instance variables do not change state.

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

Will this compile and if not why:

public class TestClass { public testClass() {} }

A

It won’t compile. The compiler will say “Return type for the method is missing”. It looks like default constructor but it isn’t because its name should match the name of the class (case-sensitive).

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

True or False: Both instance and static initializers are able to access static variables ?

A

True

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

Which is valid:

s -> s.isEmpty()

s -> { s.isEmpty() }

s -> { s.isEmpty(); }

s -> { return s.isEmpty(); }

String s -> s.isEmpty()

(String s) -> s.isEmpty()

A

s -> s.isEmpty() - OK

s -> { s.isEmpty() } - return keywork is required inside braces for lambda bodies

s -> { s.isEmpty(); } - return keywork is required inside braces for lambda bodies

s -> { return s.isEmpty(); } - OK

String s -> s.isEmpty() - it is missing the parentheses around the parameter list

(String s) -> s.isEmpty() - OK

17
Q
A