Chapter 7 Flashcards

1
Q

Name all components in the following method declaration:

public final void nap(int minutes) throws InterruptedException {}

A
  • Access modifier
  • Optional specifier
  • Return type
  • Method name
  • Parantheses
  • Parameter(s)
  • Exception
  • Method body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the method name and parameter list also called?

A

Method signature

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

What are all access modifiers?

A
  • Private
  • Default (Package-private)
  • Protected
  • Public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are all optional specifiers?

A
  • static
  • abstract (when a method body is not provided)
  • final (used when a method is not allowed to be overridden)
  • synchronized (used with multithreaded code)
  • native (not on exam)
  • strictfp (not on exam)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the rules for method names?

A

Same as variable names

  • Can contain letters, numbers, underscore and $
  • Cannot start with a number
  • Cannot be a reserved word
  • Single underscore is not allowed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between an array and varargs?

A

Varargs have one additional rule in that it must be the last element in a method’s parameter list.

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

What is the scope of the private access modifier?

A

Only accessible within the same class

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

What is the scope of the default access modifier?

A

Private plus other classes in the same package

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

What is the scope of the protected access modifier?

A

Default access plus child classes

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

What is the scope of the public access modifier?

A

Protected access plus other packages

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

What does the following code print?

public class Koala {
    public static int count = 0;
}
class TestKoala {
    public static void main(String[] args) {
        Koala k = new Koala();
        System.out.println(k.count);
        k = null;
        System.out.println(k.count);
    }
}
A

0 twice

page 267

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

What does it mean when we say Java is a “pass-by-value” language?

A

This means that a copy of the variable is made and the method receives that copy. Assignments made in the method do not affect the caller.

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

What is printed in the following code?

public static void main(String[] args) {
  String name = "Willie";
  speak(name);
  System.out.println(name);
}

public static void speak(String name) {
name = “Wulm”;
}

A

“Willie”

The variable assignment is only to the method parameter and doesn’t affect the caller.

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

What are the rules for method overloading?

A
  • The method must have an equal name
  • The method must have different parameters (different type(s) or more parameters)
  • Everything else can vary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can I overload a method with an array parameter with a method with a varargs parameter?

A

No. Java treats varargs as if they were an array. You can pass an array to one of the methods and Java would not know which method to call.

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

Can I overload a method with a primitve type parameter with a method with its corresponding wrapper class type parameter?

A

Yes. Java tries to use the most specific parameter it can find. If the primitive int method version is not present, it will autobox to the method with the wrapper class type parameter.

17
Q

Is this a valid overloaded method? why?

public void walk(List< String> strings) {}
public void walk(List< Integer> integers) {}

A

no.

Java has a concept called type erasure where generics are used only at compile time. That means the compiled code looks like this:

public void walk(List strings) {}
public void walk(List integers) {}

(page 281)

18
Q

Is this a valid overloaded method?

public void walk(int[] ints) {}
public void walk(Integer[] integers) {}

A

yes.

They specify actual types and don’t participate in type erasure.

(page 282)

19
Q

What is the order Java uses to choose the right overloaded method given the following command glide(1, 2) and the following methods:

String glide(Integer i, Integer i)
String glide(int i, int i)
String glide(long i, long i)
String glide(int... nums)
A
  1. String glide(int i, int i)
  2. String glide(long i, long i)
  3. String glide(Integer i, Integer i)
  4. String glide(int… nums)

(page 282)

20
Q

Does the following code work?

public static void play(Long l) {}

play(4);

A

No.

Java is happy to convert the int 4 to a long 4 or an Integer 4 (autoboxing). It cannot handle converting to a long and then to a Long.

(page 283)

21
Q

What does encapsulation mean?

A

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

22
Q

What is a getter method also called?

A

Accessor method

23
Q

What is a setter method also called?

A

Mutator method

24
Q

Does the following code compile?

public static void main(String[] args) {
test(1);
}

private static void test(double val) {}

A

Yes

25
Q

Does the following code compile?

public static void main(String[] args) {
test(1);
}

private static void test(short val) {}

A

No:

error: incompatible types: possible lossy conversion from int to short