Week 4 Part 2 Flashcards

1
Q

T/F - We declare the main method static so that the JVM can invoke ClassName.Main() without creating an instance of the class

A

True

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

T/F - Static variables can only be used in some of the classes methods

A

False. They can be used in all them as they have “class scope”.

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

ClassName.StaticMethod() - Does this work?

A

Yes

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

How do private static class members differ from public ones?

A

They can only be accessed by through the methods of the class

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

Can static methods access a class’ instance variables and instance methods?

A

No. Because static methods exist before any objects of the class have been instantiated.

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

Can the “this” reference be used in a static method? Why?

A

No. Because static methods are loaded before anything else. There’s no “this.var” to grab

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

T/F - A static variable is given a default value if it is not initalized

A

True

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

A static method calls an instance method of the same class by using only the method name, what happens?

A

Compilation error

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

A static method attempts to access an instance variable in the same class by using only the variable name, what happens?

A

Compilation error

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

What can a static method directly relate to?

A

Other static members that have been loaded with the class

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

Can instance methods relate to static methods? how about other instance methods?

A

They can relate to both.

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

public class JustTheStatic {
public String me = “Mine”;
public static void main(String[] args){
System.out.println(“Now” + me); }

What happens here? Does it run?

A

It does not run. Despite being a part of a static class “me” is not static. Therefore when main attempts to run “me” is not yet in memory.

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

T/F - Static objects in Java are immutable

A

False

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

T/F - String objects are immutable

A

True. (Cannot be modified after they are created)

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

When we concatenate two strings together what happens? (Hint - immutable)

A

A new string is created containing the concatenated values. Original string values are not modified.

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

Typically, the garbage collector _____ eventually reclaim the memory for any objects that are eligible

A

MIGHT

17
Q

T/F - The JVM guarantees the garbage collector will execute, at the right time

A

False

18
Q

T/F - The garbage collector will collect each eligible object when the time comes

A

False. May collect no objects or simple a subset of eligible ones

19
Q

Principle of least privilege - Is it important? What is it?

A

It’s fundamental to good software engineering. It suggests that code should only be granted enough privileges to accomplish its designated tasks.

20
Q

How does the principle of least privilege make your code better?

A

Prevents code from accidentally modifying variable values and calling methods that should not be accessible.

21
Q

final variables can be initialized when they are declared or by each __________, so that each object of the class has a different value

A

constructor

22
Q

If a class with final vars has multiple constructors, what would each one be required to do?

A

Initialize each final variable (assuming it’s not done by the class)

23
Q

What happens if a final variable is not initalized?

A

A compilation error

24
Q

When a class has references to objects of other classes as members

A

Composition (has-a relationship)
IE. AlarmClock objects need current time and the time when it is to sound its alarm, therefore it would have 2 references to time objects and in AlarmClock