Classes and Objects Flashcards

1
Q

1) Classes as Data Types

A class is basically a data type that can hold both d____ and m_______!

A

data
methods

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

2) You can see that there are two aspects to a class:

1: The d_____ that it holds;
2: The t_______s it can perform.

A

data
tasks

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

3) Objects

In order to use the methods of a class, you need to create an o__________ of that class.

e.g. as we have been doing in the test classes.

A

object

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

4) It’s useful to think of a class as a t____________ from which objects are generated.

an object refers to an individual i_______ of that class.

A

template
instance

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

5) Object-oriented programming consists of defining one or more classes that may i_______ with each other.

A

interact

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

6) The process of creating an object is called i_______________.
To create an object we use a very special m_________ of the class called a c____________

A

instantiation
method
constructor

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

7) The constructor method always has the same n________ as the class.

e.g. Oblong myOblong;

A constructor does NOT r_______ a v_________!

The function of the constructor is to reserve some space in the computer’s m________ to hold the required object.
The new object is stored in a d__________ memory location and the object reference holds the memory address.

A

name
return a value
memory
different

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

8) Encapsulation

Encapsulation is the technique of making attributes (variables ,etc) accessible ONLY to the m________ of the same class.
This is a key feature of object-oriented languages.
Encapsulation protects the data in the class by making sure that no program can assign values to the attributes d_______.

A

methods
directly

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

9) null

In Java, when a reference is first created without assigning it to a new object in the same line, it is given a special value of null; a null value
indicates that no s______ is allocated.

e.g.
Oblong myOblong; // at this point myOblong has a null value, it is simply a name for a location in m________

myOblong = new Oblong(5.0, 7.5); // create a new Oblong object with length 5.0 and height 7.5

A

storage
memory

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

10) What is the key advantage of object-oriented programming over structured programming?

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

11) What are the differences between primitive data types and object data types?

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

12) How does a new object affect memory space?

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

13) How many ways can you create a string object?

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

14) What is parameter passing by reference?

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

15) The String Class

Strings are in fact o______ created from a s_____ class!

A

objects
string

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

16) The String Class

Java actually allows us to declare a string object in the same way as we
declare variables of s________ type such as int or char.

e.g. int car; (variable declaration)
String name; (variable declaration)
String city; (object declaration)

A

simple

17
Q

17) The String Class

Can we do this?

String str;
Str = new String();

A

Yes

18
Q

18) The String Class

Can we do this?

String str = new String();

A

Yes

19
Q

19) Can we give a value to the string at the time we create it?

e.g.
String str = new String(“Hello”);

A

Yes! We can.
A faster way of doing the same thing is by typing:

String str = “Hello”

20
Q

20) Methods of the String class

There are many useful methods in the string class.

Name three:

One of these methods is: .substring(1, 4)
When we call the substring method, we are asking for a certain range of characters, in this case, we want to know the characters from index positions 1 to 4

A

toLowerCase
toUpperCase
charAt
length

21
Q

21) Comparing Strings

When comparing two objects, such as Strings, we should do so by using a method called

equals.

We should not use the e_________ operator (==); this should be used for comparing primitive types only.

A

equality

22
Q

22) Comparing Strings

If, for example, we had declared two strings, firstString and secondString, we would compare these in, say, an
if statement as follows:

if(firstString.equals(secondString))
{
// more code here
}

What would happen if we used == instead of using ‘equals’?

A

We would get an error as all you are doing is finding out whether the objects occupy the same address space in memory,
not comparing the actual value of the string attributes of the objects.

23
Q

23) Can an object of type String can also be used within a switch statement?

A

Yes

We can use it to check to see if it is equal to one of several possible String values.

e.g.
import java.util.Scanner;
public class StringCheckWithSwitch
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
String symbol;
// get symbol from user
System.out.println(“Enter the symbol(square/circle/triangle)”);
symbol = keyboard.next();
// use String object in switch
switch(symbol)
{
case “square”: System.out.println(“ATTACK”); break;
case “circle”: System.out.println(“BLOCK”); break;
case “triangle”: System.out.println(“JUMP”); break;
default: System.out.println(“Invalid Choice”);
}
}
}