Classes, Objects & Methods Flashcards

1
Q

Class Syntax

A

class Name {

}

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

When is a main method required in a class?

A

If the class is the starting point of you program.

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

What types of applications don’t require a main method?

A

Applets

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

How do you access instance variables from an object?

A

object.variable

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

How does the Java compiler sort class definitions?

A

It puts each class into its own .class file.

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

Is it necessary for classes to be in the same source file?

A

No.

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

Do the contents of the variables in one object differ from another?

A

Yes, each instance of a class stores its own contents.

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

Object Declaration Syntax

A

object Name = new object();

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

What does the new operator do?

A

dynamically allocates memory for an object and returns a reference to it.

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

Reference Variables

A

Variables referring to objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Vehicle car1 = new Vehicle();
Vehicle car2 = car1;

Are car1 and car2 referencing the same or different objects?

A

Same

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

What are methods used for?

A

Manipulating and providing access to data inside classes.

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

What method begins execution of a program?

A

The main method

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

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

Method Syntax

A

type name(param) {

}

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

When is the method Return type void?

A

If the method doesn’t return a value.

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

How can you cause the immediate termination of a void method prior to its ending brace? What happens to program control?

A

Return;

Program control returns to the caller.

17
Q

How to return a value in a method?

A

return value;

18
Q

What methods won’t return values?

A

Void methods

19
Q

What method will initialize an object, provide initial values to instance variables & perform other startup procedures?

A

A constructor

20
Q

What do you name a constructor?

A

The same name as its class

21
Q

What return type should a constructor have?

A

It has no return type

22
Q

Do all classes have constructors?

A

Yes, even if not defined, due to a default constructor in the Object class that initializes all member variables to their default values.

23
Q

What are the default values of all member variables?

A

Zero, null & false

24
Q

Can you add parameters to constructors?

A

Yes

25
Q

What does garbage collection do?

A

Reclaims objects automatically

26
Q

When is memory occupied by an object released?

A

When no references to it exist

27
Q

Under what two conditions will garbage collection run?

A

There are objects to recycle

There is a need to recycle them

28
Q

What method is used to ensure an object terminates cleanly?

A

Finalize()

29
Q

When is finalize() called? Who calls it?

A

Java Runtime calls finalize() just before it recycles the object.

30
Q

What do you include in a finalize() method?

A

Specify actions that must be performed before the object is destroyed.

31
Q

What implicit argument exists in a method as a reference to the invoking object?

A

this

32
Q

What distinction does this.variable clarify?

A

When the name of a parameter or local variable is the same as the name of an instance variable. “this” provides access to the hidden instance variable.

33
Q

Instance variable vs local variable

A

Local variables are declared in the method or block

Instance variables are declared in the class. They always have a default value.

34
Q

Aliasing

A

When one object reference is assigned to another. The address is copied, not the object itself, so it’s a different name for the same object.

35
Q

Can an object reference refer to nothing?

A

Yes, null is a reference to nothing.