Chapter 5 - Methods Flashcards

(19 cards)

1
Q

Optional specifiers for methods
Can they be in any order?

A

Yes

static
abstract
final
default
synchronized

There can be multiple specifiers listed on a method but certain combinations are not allowed if it doesn’t make sense to the compiler

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

Can you declare multiple throws exception in method declaration

A

Yes

Separated by comma

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

Local variable modifiers?

A

There is only one e

final

Remember a final variable needs to be initialized during declaration or initialized once before it’s used elsewhere

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

Effectively final variable

A

It’s a local variable not declared as final but never modified with in its block after initialization

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

Optional specifiers for instance variable

A

final - must be initialized with each instance of the class. Unlike other instance variable these final instance variables do not get defaults . They are to be explicitly initialized during instance creation

volatile - instructs JVM that the value can be modified by other threads

transient - used to indicate that an instance variable should not be serialized

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

Rules for creating a method with varargs

A

1) method can have at most one vararg parameter

2) it should be the last parameter if declared. Only one is allowed

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

Static members

A

Static members can be called from another static method or an instance method

But a static method can not use or call an instance member

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

void walk(int… args){
print(“hello”);
}

walk();

Does that work?

A

Yes.

You do t have to pass any vararg parameters. Java will create an array of length zero

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

Protected member access

A

Check where the code is executed. The class where code executes needs to have access. It doesn’t matter if object has access or not. It matters only where it gets executed

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

Output?

public class Snake{
public static String name=“venky”;
}

Snake s=null;
print(s.name);

A) doesn’t compile
B) null
C) NullPointerException
D) Venky

A

D

You may think it should throw the exception. Since it is static variable you don’t need an object. You could write Snake.name
JVM knows the reference type of null object and uses static member as if it’s referenced through class name

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

Static imports

A

import static Java.util.Arrays.asList;

Please note if we have an instance/static method with same name declared as a member , the instance or static method takes precedence over an imported one.

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

Does below compile
import static Java.util.Arrays;
import static Java.util.Arrays.asList;
static import Java.util.Arrays.;
import static Java.util.Arrays.
;

Arrays.asList(“one”);

A

Only 2 and 4 compiles

1 doesn’t compile since it’s a regular import and should not use keyword static

3 doesn’t t compile because static word comes before import which is wrong

5 doesn’t compile because class Arrays is not imported which is not required actually but if used it has to be imported.

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

Limits of autoboxing and numeric promotion

Long x=6;

A

Doesn’t compile. Java can not do both autoboxing and numeric promotion. Either it does promotion to long or autoboxing to Integer. Neither works for Long on left side

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

Unboxing and numeric promotion in one line

Integer a=18;
long x=a;

A

Compiles fine

Here unboxing and numeric promotion can be done at the same time [ where as autoboxing and numeric promotion both can’t be done at same time ]

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

Unboxing a null

Long x=null;
long y=x;

A

Compiles but throws an exception

Internally JVM does x.longValue()

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

Autoboxing of arrays

Long[] a={23,67};
Integer[] b={23,67};
Double[] c={12, 3};

A

1 doesn’t compile because it can’t do both autoboxing and numeric promotion

2 compiles because it needs to do only autoboxing

3 doesn’t compile because the data types not matching. Values are auto boxed to Integer which is not same as Double

17
Q

Overloaded methods definition

A

Different method signature (name and parameter list)

They can have different combinations of access modifiers and optional specifiers and exceptions list

18
Q

Order of preference for glide(1,2)

A

glide(int a, int b)
glide(long a, long b)
glide(Integer a, Integer b)
glide(int… a)

Note: glide(Long a, Long b) doesn’t work

19
Q

What returns ?

int juggle(boolean a, boolean… b){
return b.length;
}

juggle(true);
juggle(true, true);
juggle(true,true,true);
juggle(true,{true,true});
juggle(true, new boolean[4]);
juggle(true, new boolean[]{true,true});

A

0
1
2
Doesn’t compile . Not valid array
4
2