Methods & Classes II Flashcards

1
Q

What are advantages of using classes?

A

Links Data with code manipulation

Controls access

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

Three Access Modifiers & Default Access

A

Public
Protected - only applicable to inheritance
Private - accessible only by members of its class

Default access is public

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

What standard methods control access to members?

A

Get()

Put()

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

Can you pass objects to methods?

A

Yes, it’s common

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

Call by value

A

Copies the value of an argument into a parameter

Primitive are passed by value

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

Call by Reference

A

The reference to an argument is passed to the parameter.

Objects are passed by reference

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

Will changes to an object inside a method affect the original object?

A

Yes

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

Method Overloading

A

When two or more methods within the same class share the same name with different parameters.

E.g. Constructors

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

How is the correct method chosen if it is overloaded?

A

By matching arguments with parameters.

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

Recursion

A

A method calling itself

E.g. A factorial

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

What happens during recursion?

A

New local variables & parameters are allocated storage on the stack & the method is executed with these new variables.

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

Does a recursive call make a new copy of the method?

A

No, just the arguments are new.

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

What happens when there are too many recursive calls to a method?

A

A stack overrun

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

Why do recursion rather than iteration?

A

Some types of algorithms can be more clear & simple

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

How do you end the loop of recursive calls within a method?

A

You must include a conditional statement to force the method to return without recursion.

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

When can you access a class member without referencing an object?

A

If that member is declared static.

You don’t need to create an object prior to accessing a static member.

Ex: className.member

Static variables are essentially global variables

17
Q

Which can be static - methods or variables?

A

Both

18
Q

What restrictions do static methods have?

A

They can
directly call only other static methods
access only static data
do not have a this reference

19
Q

When is a static block executed?

A

When a class is first loaded, before it is used for anything else & before objects are constructed.

20
Q

Can classes be nested?

A

Yes - a nested class is declared within another class.

21
Q

What are inner classes?

What can they access?

A

Non-static nested classes

They can access all variables & methods of the outer class.

22
Q

What are the advantages of nesting a class?

A

It creates a localized class that is not known outside its block

23
Q

Varargs method

A

A method that takes a variable number of arguments depending on its precise usage.

24
Q

Varargs method syntax

A

int …v

Specified by ellipses …

The arguments are stored in an array referenced by the name given.

25
Q

Can you combine normal & variable parameters?

A

Yes, but the varargs must be the last declared & there can only be one.

26
Q

Ambiguity errors

A

Can occur due to varargs & overloading. Often exposes conceptual flaws in the code.