Topic 1.1: Java Basics - Define the scope of variables  Flashcards

1
Q

By default in java the variable with the closest scope takes precedence and hides access to the variable with the same name but in a longer scope.

A

how is
shadowing
in variables resolved by java

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

what is the place of declaration and the place of initialisation of
local variables

A

Place of declaration: Can be defined in a constructor, method, or code block.
Place of initialization: Local variables should be initialized at the point of declaration. (they will not be given default values if not explicitly initialised)

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

Place of declaration: Can be defined in a constructor, method, or code block.
Place of initialization: Local variables should be initialized at the point of declaration. (they will not be given default values if not explicitly initialised)

A

what is the place of declaration and the place of initialisation of
local variables

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

what is the place of declaration and the place of initialisation of
Instance Variables (Fields)

A

Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Typically initialized within the constructor.

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

what 2 ways can
shadowing
in variables be resolved by the programmer

A

to resolve this the programmer may:
* change the name of the variables with a shared name
* use the this keyword to explicitly access the instance variables

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

name 3 situations of
Shadowing
in Variables

A

Three situations of this occuring are:
* A parameter shadows a field.
* A local variable shadows a field.
* A local variable shadows a parameter.

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

Scope: Throughout the class, accessible from any constructor, method, or code block.
Lifetime: As long as the program is running.

A

what is the scope and lifetime of
Class Variables (Static Variables)

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

Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Can be initialized at declaration or using a static block.

A

what is the place of declaration and the place of initialisation of
Class Variables (Static Variables)

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

Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Typically initialized within the constructor.

A

what is the place of declaration and the place of initialisation of
Instance Variables (Fields)

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

how is
shadowing
in variables resolved by java

A

By default in java the variable with the closest scope takes precedence and hides access to the variable with the same name but in a longer scope.

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

to resolve this the programmer may:
* change the name of the variables with a shared name
* use the this keyword to explicitly access the instance variables

A

what 2 ways can
shadowing
in variables be resolved by the programmer

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

what is the place of declaration and the place of initialisation of
Class Variables (Static Variables)

A

Place of declaration: Defined within the class but outside constructors and methods.
Place of initialization: Can be initialized at declaration or using a static block.

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

define
Shadowing
in Variables

A

This occurs when two separate variables share the same name and are both in scope

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

Place of declaration: Declared within the header of the constructor or method.

Place of initialization: Initialized when the constructor or method is invoked.

A

what is the place of declaration and the place of initialisation of
Constructor and Method Parameters (Formal Parameters)

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

what is the scope and lifetime of
Constructor and Method Parameters (Formal Parameters)

A

Scope: Limited to the constructor or method block they are declared in.

Lifetime: Exist as long as the constructor or method is executing.

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

Scope: Throughout the class, accessible from any constructor, method, or code block.

Lifetime: As long as the instance exists.

A

what is the scope and lifetime of
Instance Variables (Fields)

17
Q

Three situations of this occuring are:
* A parameter shadows a field.
* A local variable shadows a field.
* A local variable shadows a parameter.

A

name 3 situations of
Shadowing
in Variables

18
Q

Scope: Limited to the code block they are declared in.
Lifetime: From declaration to the end of the code block.

A

what is the scope and lifetime of
local variables

19
Q

what is the scope and lifetime of
local variables

A

Scope: Limited to the code block they are declared in.
Lifetime: From declaration to the end of the code block.

20
Q

This occurs when two separate variables share the same name and are both in scope

A

define
Shadowing
in Variables

21
Q

Scope: Limited to the constructor or method block they are declared in.

Lifetime: Exist as long as the constructor or method is executing.

A

what is the scope and lifetime of
Constructor and Method Parameters (Formal Parameters)

22
Q

what is the scope and lifetime of
Instance Variables (Fields)

A

Scope: Throughout the class, accessible from any constructor, method, or code block.

Lifetime: As long as the instance exists.

23
Q

what is the place of declaration and the place of initialisation of
Constructor and Method Parameters (Formal Parameters)

A

Place of declaration: Declared within the header of the constructor or method.

Place of initialization: Initialized when the constructor or method is invoked.

24
Q

what is the scope and lifetime of
Class Variables (Static Variables)

A

Scope: Throughout the class, accessible from any constructor, method, or code block.
Lifetime: As long as the program is running.