Ch7 Methods and Encapsulation 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 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
3
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
4
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
5
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
6
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
7
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
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
10
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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) {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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

String glide(int i, int i)
String glide(long i, long i)
String glide(Integer i, Integer i)
String glide(int… nums)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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.

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

Does the following code compile?

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

private static void test(double val) {}

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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

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