Classes, Objects, and Methods Flashcards

1
Q

What is the difference between a class and an object?

A

A class is a logical abstraction that describes the form and behavior of an object.

An object is a physical instance of the class.

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

How is a class defined?

A

A class is defined by using the keyword class.

Inside the class statement, you specify the code and data that comprise the class.

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

What does each object have its own copy of?

A

Each object of a class has its own copy of the class’ instance variables.

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

Using two separate statements, show how to declare an object called counter of a class called MyCounter.

A

MyCounter counter;
counter = new MyCounter();

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

Show how a method called myMeth( ) is declared if it has a return type of double and has two int parameters called a and b.

A

double myMeth(int a, int b) { // …

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

How must a method return if it returns a value?

A

A method that returns a value must return via the return statement, passing back the return value in the process.

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

What name does a constructor have?

A

A constructor has the same name as its class.

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

What does new do?

A

The new operator allocates memory for an object and initializes it using the object’s constructor.

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

What is garbage collection and how does it work?

A

Garbage collection is the mechanism that recycles unused objects so that their memory can be reused.

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

What is this?

A

The this keyword is a reference to the object on which a method is invoked. It is automatically passed to a method.

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

Can a constructor have one or more parameters?

A

Yes.

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