Java SE 11: Encapsulation Flashcards

1
Q

static is a ______ that we apply to a method or a variable

A

modifier

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

_____ means that the method or variable belongs to the class and is shared by all objects in that class

A

static

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

a static modifier means it’s not unique to an object instance, and it can be accessed without _________ the class. So we don’t need to use the ___ keyword, and no constructor gets called when we access static methods or variables

A

a static modifier means it’s not unique to an object instance, and it can be accessed without instantiating the class. So we don’t need to use the NEW keyword.

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

A constructor gets called when we access static methods or variables.

True or False

A

False

You don’t need to use the new keyword, and no constructor gets called when we access static methods or variables

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

An _______ vairable is unique to an individial object

A

Instance

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

In the following ItemSizes class does a ItemSizes object need to be instatied in order to access it’s members?

Yes or No

public class ItemSizes {

public static String mSmall = “Mens Small”

}

}

A

No

since the member is static it can be accessed without having to create the ItemSizes class

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

an ______ _____ is a non-static method

A

instance method

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

Is the setSize() method an instance method or static method?

public class Item {

public String size;

public void setSize(String sizeArg) {

size = sizeArg;

}

}

A

Instance method

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

How do you make a variable a constant?

A

using the final keyword before the variable type name

ex. final int COLUMNS

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

constant variables can be changed after they have been initialized.

True or False

A

False

Constants cannot be changed after they have been initialzed.

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

The name of variables declared class ______ should always be uppercase

A

constants

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

What is called when the compiler changes the type of variable to a type that supports a larger size?

A

Promotion

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

Some promotions are done automatically by the compiler

True or False

A

True

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

Feature of promotion is to avoid ________ values.

A

Truncated

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

We use ____ _______ to lower the range of a value

A

type casting

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

Type casting a value has no effect on the amount of memory the value consumes.

True or False

A

False

Type casting reduces the amount of memory used by the value. Ex. change and long value to an int

17
Q

is the example below a promotion or type casting?

int i = 3;

long l = i;

double d = 3;

A

Promotion

18
Q

In the example below will the num3 value be truncated?

Yes or No

int num1 = 55555;

int num2 = 66666;

long num3 = num1 * num2;

A

Yes

19
Q

In the example below the result num3 is 3.7 billion which is too large to fit in an int type. What do i need to change to ensure the final number is truncated.

int num1 = 55555;

int num2 = 66666;

long num3 = num1 * num2;

A

Either num1 or num2 needs to be changed to a long type.

20
Q

How would you convert the number represented as a string to a primitive type?

String sNum = “150”;

int intNum = ________________ ;

A

int nNum = Integer.parseInt(sNum);

21
Q

How would you convert the boolean represented as a string to a boolean type?

String nNum = “true”;

boolean b = ______________;

A

boolean b = Boolean.parseBoolean(sNum);

22
Q

How would you modify the code below to make the setID method private but still accessible?

public class item {

print int id;

print String desc;

public item () {

desc= “–description required–”;

}

public void setId() {

id = Item.nextId++;

}

A

public class item {

print int id;

print String desc;

public item () {

setID();

desc= “–description required–”;

}

private void setId() {

id = Item.nextId++;

}

23
Q

In encapsulation, the variables of a class will be _____ from other classes, and can be accessed only through the methods of their current class.

A

Hidden

24
Q

Encapsulation uses access control to hide the fields, so we can provide safe access through ______ and ______ methods.

A

….. getter and setter methods.

25
Q

Encapsulation encourages good _____ ______design

A

object-oriented

26
Q

If you make attributes private, how can another object access them?

A

One object can access the private attributes of a second object. If the second object provides public methods for each of the operations that are to be performed on the value of an attribute.

IE. Getter and setter methods

27
Q

The below is an example of a Setting Method with ________

public void setColorCode(char newCode) {

if (newCode == ‘R’) {

colorCode = newCode;

return;

}

if (newCode == ‘G’) {

colorCode = newCode;

return;

}

if (newCode == ‘B’) {

colorCode = newCode;

return;

}

System.out.println(“Invalid colorCode. Use R, G, or B”);

}

A

Checking

28
Q
A