Chapter 4: Methods and Encapsulation Flashcards

1
Q

What parts of a method are required for the code to compile?

A
Return type
Method name
Parameter list (can be empty ())
Method body (can be empty {})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the access modifiers in order from most to least accessible?

A

public
protected
[default]
private

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

Can a package private class be accessed from a child class in a different package?

A

No. Package private is default accessibility and can only be accessed within the same package.

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

What does final mean when applied to a method?

A

The method cannot be overridden

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

What is the syntax for a method’s exception list?

A

Keyword throws followed by a comma separated list of exceptions

throws Exception1, Exception2, …

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

What is the correct syntax for varargs?

A

datatype… name

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

What are the rules for using varargs parameter?

A

It must be the last parameter and there can be only one vararg in a given parameter list

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

How do you pass data to a method with a vararg?

A

Data can be passed in as comma separated values or as an array

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

How do you access data from a vararg within a method?

A

Treat it like an array

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

When can you call methods on a null reference variable?

A

When the method is static

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

Can a static member call an instance member?

A

No

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

What runs first, static initializer or intance initializer?

A

Static

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

When is the last time a value can be assigned to a final instance variable?

A

In the constructor

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

When do static initializer blocks run?

A

When the class is first used

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

What do static imports do?

A

Allow importing of static class members

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

Is Java a pass-by-reference language?

A

No. Java is pass-by-value

17
Q

What is the order in which Java will match overloaded methods?

autoboxed type
larger primitive type
varargs
exact type match

A

exact type match
larger primitive type
autoboxed type
varargs

18
Q

Can a class constructor have a different name than the class it’s in?

A

No

19
Q

Can a constructor have a return type?

A

No

20
Q

If a class only has a private constructor, is a default constructor provided?

A

No

21
Q

How do you call other constructors within the same class?

A

Using this()

22
Q

What is the order of initialization?

constructor
superclass
instance variable declaration and instance initializers in the order they appear
static variable declaration and static initializers in the order they appear
A
superclass
static variable declaration and static initializers in the order they appear
instance variable declaration and instance initializers in the order they appear
constructor
23
Q

List five JavaBeans conventions

A

properties are private
getters begin with ‘is’ if the property is a boolean
getters begin with ‘get’ if the property is not boolean
setters begin with ‘set’
methods are camelcase

24
Q

How do you create an immutable class?

A

Make instance variables private
Don’t provide setters
set initial values in the constructor

25
Q

When can you omit the parenthesis around a lambda expression’s parameter?

A

When there is exactly one parameter without an explicitly specified type

26
Q

When do you need to provide a return statement in a lambda expression?

A

When using a code block for the right side of the expression ( {} )