unit 2 Flashcards

1
Q

Difference between a parameter and an argument (formal parameter vs. actual parameter)

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

What kind of operation does an object method use?

A

dot operation

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

can you use == and < or > on string types?

A

no, you need to use equals or compareTo

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

Fields are assigned _____ if they are not EXPLICITLY INITIALIZED in a constructor

A

the default value for their type

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

int holds one number, array holds many, universal data structure

A

yes

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

What information type do math class methods such as Math.sqrt(); or Math.random() yield?

A

double

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

accessor methods do not have parameters

(true/false)

A

TRUEEEEEEE SO TRUE SO TRUE

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

private double m;

public double getSlope(double m)
{
(IMPLEMENTATION NOT SHOWN)
}
————————-
work? why/why not?

A

you cannot pass what you dont have initialized as a value. we don’t know PARAMETER m, even if we know FIELD m. thus, we cannot pass parameter m (because parameters within METHODS must be known)

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

Consider the following description of the Thing class which includes two constructors.

public Thing() – Constructs a Thing object that uses a default value to represent a color

public Thing(String setColor) – Constructs a Thing object that uses setColor to represent a color

Which of the following code segments, when appearing in a class other than Thing, will create an instance variable of a Thing object with a value of null ?

A
private Thing someThing = new Thing(“Green”);

B
private Thing someThing = new Thing(“”);

C
private Thing someThing = new Thing();

D
private Thing someThing;

E
private Thing(“Green”);

A

D, as all other options would result in a successfully declared and initialized object except E (which would just result in a full compilation error, so someThing would be neither declared or initialized in this case). we want something to be declared, but not initialized, thus returning the default for the type, or a null value.

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

Consider the following description of the VetRecord class which includes two constructors.

public VetRecord(String name, int age, int weight, boolean needsVac) – Constructs a VetRecord object that represents a vet record for a pet with name name, age age, weight weight, and whether they need to be vaccinated needsVac.

public VetRecord(String name, int age, int weight) – Constructs a VetRecord object that represents a vet record for a pet with name name, age age, and weight weight.

A new constructor is to be added to the VetRecord class. Which of the following is NOT a possible header for the new constructor?

A
VetRecord(int age, int weight)

B
VetRecord(int age, boolean needsVac)

C
VetRecord(String name, int age)

D
VetRecord(String name, boolean needsVac)

E
VetRecord(String name, int weight, int age)

A

E, as there is already a constructor with parameters in order of: string, int, int. thus, if this were added then it would cause a “compile-time” (syntax) error

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

Consider the following description of the MagicNumber class which includes one constructor and two methods.

public MagicNumber()– Constructs a MagicNumber object that represents a number

public void displayNumber()– Displays the number to the screen

public void add_2()– Increases the number by 2

When located in a method in a class other than MagicNumber, which of the following code segments will compile without error?

  1. MagicNumber.add_2();
    MagicNumber.displayNumber();
  2. MagicNumber n1 = new MagicNumber();
    n1.add_2();
    n1.displayNumber();
  3. n2.add_2();
    n2.displayNumber();

A
I only

B
II only

C
III only

D
II and III only

E
None of the code segments will compile.

A

B, II only, as though option 1 calls a nonstatic method successfully through dot notation, it does not actually construct an object of the MagicNumber class to do anything with. 3 does the same, using dot notation on a nonexistent object. Option 2, however, successfully declares an object variable to be manipulated.

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

What does the dot operator do?

A

“invokes a nonstatic method of a class onto an object of that same class,” or uses the method that a class creates that isn’t static (shared by all objects, belonging to the class and not a specific instance of that class(instance of a class = object))

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

in the methods:
public void methodA(int arg) – Calls methodB with the value of arg * 10

public void methodB(int arg) – Displays the value of arg + 10

if methodA(4) were called, what would happen?

A

methodA would execute with arg value of 4, which would THEN call methodB with the result and continue on

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

Why would you use a wrapper?

A

to give a primitive type to a method that wants an object

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

How do you call a method?

A

.method for methods OUTSIDE of the class/being called in the main method

method() for methods within the same class

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

when is dot notation NECESSARY?

A

invoking methods in one class on an object in a different class

17
Q

TRUE/FALSE:
fields must have values, and if none is given they get defaults. if parameter slope is within a method and
m = slope;
if m is uninitialized the m will override slope and result in zero

A

true

18
Q

how can you change a field string?

A

when theres a particular method designed to change it, or a MODIFIER method

19
Q

what is the keyword giveaway for modifer methods?

A

set

20
Q
A