AP Computer Science A - Using Objects (Unit 2) Test Review

0.0(0)
studied byStudied by 2 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/137

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

138 Terms

1
New cards
they hold only one piece of data at a time
Do primitive data types hold multiple pieces of data at once, or only one piece of data?
2
New cards
int, double, and boolean
What are the 3 main primitive data types that we use in this class?
3
New cards
-they hold more than one piece of data at a time
-they can hold data of different types
-they have built in methods (tools)
-user can create their own classes
What are the characteristics of class/reference data types?
4
New cards
class/reference data type
What datatype is a string?
5
New cards
-primitive data type variables hold their actual value that they have been set to
-class/reference data type variables hold a reference to the memory location of the actual data
What is the main difference between primitive data types and class/reference data types?
6
New cards
True
True or False:
Primitive data type variables over write the previous value stored
7
New cards
no they don't overwrite the previous value stored inside them, instead they reference a whole separate memory location for the new value
Do reference data type variables overwrite the previous value stored inside them? If not, then what do they do?
8
New cards
the variable holds the value: null, since we never gave it a value
What value do reference/class variables get set if they were only declared and not actually set a value, in the past?
9
New cards
-because when you change a string variable's value, you are creating a new and separate reference for this new value as compared to the old value
-basically, a new memory location is referenced without overwriting the original value
-hence, the original value still exists, and therefore the string's original value wasn't changed, so strings are immutable
Why are strings immutable?
10
New cards
Immutable
unchanging over time/unable to be changed
11
New cards
Null
-a special value that means "no reference"
-Java holds this value automatically for a string variable when the variable has not been set to any value
12
New cards
-String bruh = "bruh";
-String bruh;
bruh = "bruh";
-String bruh = new String("bruh");
What are the three ways to create the string: "bruh"?
13
New cards
-the first "String" tells us the data type of the variable
-alpha is the name of the string variable
-new String helps initialize the creation of a new string variable
-"alpha particles" is the value that the string variable alpha was set to (notice how we put the values in double-quotes inside the parentheses)
When we create the following string, what do the different parts of the line tell us?:
String alpha = new String("alpha particles");
14
New cards
Java allocates a space in memory called a reference, and you can imagine this a a box that is named the variable's name, but the box only holds the memory location of the actual data/value (it does this because the data is most likely large, since we are dealing with a class/reference data type)
What happens we declare and set the value of a class/reference data type variable?
15
New cards
class data types
What are reference data types also known as?
16
New cards
-String empty = null; //this is just an in-line comment and you don't need it in your program, but it's just to tell you that you DO NOT put the null in double quotes or single quotes
-String empty; //this is just an in-line comment and you don't need it in your program, but it's just to tell you that empty was automatically set the value of null, because we never set a value originally
What are the two ways that we set the value of a string variable to null?
17
New cards
String concatenation
-when we concatenate a string onto another string variable's value
-we do this by using the + operator
18
New cards
-Java will force the primitive data type to a string in order to concatenate with the other string
-for example:
int x = 3;
int y = 8;
System.out.print("I have " + x + y + " dollars");
-in the above example, we would actually be printed out, I have 38 dollars, and this is because Java reads from left to right so it sensed that we have a string first inside the print statement, so when it detected the ints, it forced them to strings (so 3 became "3" and 8 became "8")
-after Java had everything inside the print statement in terms of strings, it concatenated everything together
-we could have fixed this problem by either writing the print statement like this: System.out.print(x + y + " is how much I have");, and in this situation, Java read the mathematical operation between the ints first, so it did that first, then forced the sum of x and y to a string to concatenate it with the other string
-we could have also done this to fix it: System.out.print("I have " + (x + y) + " dollars);, and in this situation, by order of operations, Java first read the mathematical operation and then performed it first, and then it forced the sum of x and y onto a string and then concatenated it with the other two strings
What happens when you concatenate a class data type such as a string with a primitive data such as an int?
19
New cards
Escape characters
quick tricks that allow additional items to be placed inside print statements
quick tricks that allow additional items to be placed inside print statements
20
New cards
-to include a literal double quote inside a string, use: "\ and \"
-to include a new line inside a string, use: \n
-to include a tab inside a string, use: \t
-to include a literal single quote inside a string, use: '\ and \'
-to include a literal backslash inside a string, use: \\
What are the 5 main strings that we need to know for this class?
21
New cards
-to put a double quote inside a string, we put "\ before the first double quote of the string, and we put \" right before the second double quote of the string
-following the instructions from above will output your string inside double quotes
How do we put a literal double quote inside a string?
22
New cards
-to put a single quote inside a string, we put '\ before the first double quote of the string, and we put \' right before the second double quote of the string
-following the instructions from above will output your string inside single quotes
How do we put a literal single quote inside a string?
23
New cards
-D is the correct answer, since if we look at the print statement, we can see that we print j, then we go to a new line, we then tab once and then print a, then we force a new line, tab twice, and then print v, and then finally, we force another new line, tab three times, then print a (all of this should match the output from the code)
-C is not correct since we are tabbing on each line before adding a new line, and tabbing one one line doesn't mean that the number of tabs will be transferred to the next line as well (instead, you start the beginning of the line on each new line)
Which of the following print statements will print the following code? Why?:
j
a
v
a
A. System.out.println("j\ta\t\tv\t\t\ta");
B. System.out.println("j\na\n\nv\n\n\na");
C. System.out.println("j\t\na\t\t\nv\t\t\t\na");
D. System.out.println("j\n\ta\n\t\tv\n\t\t\ta");
24
New cards
it's really important because primitive data types like ints and doubles will usually involve number calculations, so you have to make sure that if you are doing a number calculation inside your print statement, then either you put the number calculation at the beginning of the print statement so that Java recognizes it first and does it first, or you can put the calculation anywhere inside the print statement as long as it is in its own set of parentheses so that Java will recognize the order of operations
How important is it to order things carefully like primitive data type variables and string variables when concatenating?
25
New cards
-we need to escape it, inside the string literal using another backslash before the backslash that we want to print
-for example, the following print statement will print a SINGLE backslash: System.out.print("\\");
What do we have to do in order to print a backslash (\) in a print statement?
26
New cards
-they usually print string literals (a string that we put in double quotes but don't set to a variable)
-they can also print the values stored inside string variables
What type of strings do print statements print?
27
New cards
true
True or False:
Each letter in a string is stored separately
28
New cards
indexes
What are the locations for storing each letter in a string, called?
29
New cards
-they start at 0 and they end at the 1 before the length of the string (length of the string minus 1)
-for example, in the string "bruh", the length of this string is 4 and "b" is stored at index 0, "r" is at index 1, "u" is at index 2, and "h" is at index 3
What value do indexes start at? Where do they end?
30
New cards
tools that we can use to help us change the data
What are methods?
31
New cards
the String data type has built in methods
Which reference/class data type has built in methods?
32
New cards
an exception that you get when you don't take into account that the index counting starts at 0 and not 1
What is an index out of bound exception?
33
New cards
a null pointer exception/error
What exception/error do you get when you try to call a method on a string that has a value of null?
34
New cards
False
True or False:
A method can be called on a string variable that has not been declared
35
New cards
Dot operator
-the following operator is used to call methods: .
-the following operator is used to call methods: .
36
New cards
variableName.method();
-basically, put the variable's name then the dot operator then the name of the method with the two parentheses after it (sometimes parameters are inputted into the parantheses and other times they are not)
What is the general format to use string methods?
37
New cards
a set of two parantheses, like so: ()
What are do methods always have after them?
38
New cards
yes
Do spaces count as indexes?
39
New cards
-it tells us the length of the string that is stored in the variable that was attached before the dot operator, as an int
-for example:
String name = "Bob"
System.out.println(name.length());
-the code from above would print out 3 since the string stored in the variable name, "Bob", only had 3 characters
What does the .length() string type method do?
40
New cards
-it checks if both strings have the same characters, spaces, etc., and then it returns either a true boolean value or a false boolean value depending on if the strings are equal or not (true shows that the strings were equal and false shows that they weren't)
-in the parentheses after the method name, a parameter must be passed in
-for example:
String a = "cool"
String b = "kool"
System.out.println(a.equals(b));
-the code from above would print out the value: false, because the string "cool" is not EXACTLY the same as the string "kool" (note that the variable b was the parameter that we checked variable a against)
What does the .equals() string type method do?
41
New cards
-a method that pulls off a section of a string from the starting index parameter to the stopping index parameter minus 1, both of which need to be specified
-for example:
String w = "bruh"
System.out.println(w.substring(0,2));
-the code from above would print out the characters from the starting index 0 to the stopping index, which was specified as 2, minus 1. Hence, the method will print out characters from the string that was stored in variable w, from index 0 to 1, so br would be printed out
-another thing to notice from the code above is how the starting and stopping indexes were specified as parameters inside the parantheses
What does the .substring(start, stop) string type method do?
42
New cards
-Java prints all the characters, spaces, etc. from the starting index minus 1 and then all the way to the end of the string
-for example:
String w = "bruh"
System.out.println(w.substring(2));
-in the code above, ruh would be printed out since u is what the second index stores, and then Java just prints everything after the second index
When using the substring.(start, stop) string type method, what happens if you only specify the starting index and not the stopping index?
43
New cards
-this method changes all letters in the string variable (that comes before the dot operator) to upper case letters, while keeping the rest of the string the same
-for example:
String w = "br5uh"
System.out.println(w.toUpperCase());
-in the code above, BR5UH would be printed out since all the letters were converted to upper case letters while keeping everything else the same
What does the .toUpperCase() string type method do?
44
New cards
-this method changes all letters in the string variables (that comes before the dot operator) to lower case letters, while keeping the rest of the string the same
-for example:
String a = "jAvA51"
System.out.println(w.toLowerCase());
-in the code above, java51 would be printed out since all the letters were converted to lower case letters while keeping everything else the same
What does the .toLowerCase() string type method do?
45
New cards
-it compares two one string stored in a string variable with a string literal, alphabetically
-the string variable that holds a string is placed before the dot operator and the string literal is placed as a parameter inside the parentheses in double quotes
-if the string literal parameter, alphabetically, comes after the string value stored in the string variable that is being compared, then a negative int is outputted whose magnitude is determined depending on how far the strings are alphabetically apart
-if the string literal parameter, alphabetically, comes before the string value stored in the string variable that is being compared, then a positive int is outputted, whose magnitude is determined depending on how far/close the strings are alphabetically
What does the .compareTo(otherString) string type method do?
46
New cards
-that the string literal parameter, alphabetically, comes after the string value stored in the string variable that is being compared
-the magnitude of the negative int is determined depending on how far/close the strings are alphabetically
When using the .compareTo(otherString) string type method, what does it mean if a negative int is outputted?
47
New cards
-that the the string literal parameter, alphabetically, comes before the string value stored in the string variable that is being compared
-the magnitude of the positive int is determined depending on how far/close the strings are alphabetically
When using the .compareTo(otherString) string type method, what does it mean if a positive int is outputted?
48
New cards
-it tells us which index a specific character from some string, belongs to
-we place the variable (that contains a string value) before the dot operator and then inside the parentheses, we put in a character from the string stored in the variable, in double-quotes, as a parameter
-for example:
String a = "epic"
System.out.print(w.indexOf("i"))'
-from the code above, we are outputted 2 because "i" is stored in index 2 in the string "epic"
What does the .indexOf(string) string type method tell us?
49
New cards
we can store it in the appropriate type of variable
If a string method such as .indexOf() outputs an integer value, then what can we do with this value?
50
New cards
the output would be 1 even though the letter "a" is repeated twice in the string "variable", and this is because the .indexOf(str) method only looks at where its parameter(s) first appeared in the string
What would be the output of the following code, and why?:
String b = "variable"
System.out.println(b.indexOf("a"));
51
New cards
the output would be 3 and this is because when finding where mini-strings such as "cu" are in the larger string such as "calculator", Java looks for the mini string inside the larger string and it reports the FIRST INDEX that the mini string starts at inside the larger string
What would be the output of the following code, and why?:
String c = "calculator"
System.out.println(c.indexOf("cu");
52
New cards
we will be printed a -1 to the console
What output does .indexOf(str) string method give us when we print it, whenever the method cannot find the index to which its parameter corresponds to in the string stored inside the variable? Basically an example like this:
String m = "math"
System.out.print(m.indexOf("y"));
53
New cards
an object oriented programming (OOP) language
What type of programming language is Java?
54
New cards
object-oriented programming
-programming that uses objects in code to represent real world entities
-this type of programming allows us to map our program to the real situations that out programs represent, and so it becomes easier to solve problems
-Java is an example of an _________-__________ _______________ language
55
New cards
-state and behavior
-for example, a string is an object and its state are things like its character length, etc., and its behavior would be methods like .getLength(), etc. that we can use on it
What two characteristics do objects in the real world and in code, share?
56
New cards
they are the representation of an object's state, and they are the values that the object stores or characteristics of the object itself
What are attributes/states?
57
New cards
classes
What are the blueprints from which objects are created?
58
New cards
Encapsulation
the technique of controlling and protecting data of an object, and controlled access of data can be provided through public or private methods of an object
59
New cards
state
What is another word for the attribute of an object?
60
New cards
methods define behaviors and allow us to execute the behaviors of an object (basically, methods are the same as behaviors)
How are methods related to the behavior of an object?
61
New cards
an "instance" of the class
What is another word for an object that is created from a class?
62
New cards
the commands/functions that use on the attributes/states of the object
What are the methods/behaviors of an object?
63
New cards
they're usually variables
Objects are generally what thing in code?
64
New cards
-being open (allowing scans)
-bein close (not allowing scans/inputs from the user)
What are the state/attributes of the scanner class?
65
New cards
.nextLine();, .nextInt();, .nextDouble();, etc.
What are some of the behaviors/methods of the scanner class?
66
New cards
Keyword "new"
-a word that tells Java that we need to reserve some space in memory for the new object that we are creating
-class/reference data types (except strings) need this word in order to tell the computer to create a new spot in memory, but primitive data types don't need this
67
New cards
using constructors
How are non-primitive data type objects created?
68
New cards
a class is the blueprint/template for creating an object inside of it
How is a class related to an object?
69
New cards
they have the same behaviors/methods because they're all following the same template (they're all in the same class)
What do all objects of certain class have the same of? Why?
70
New cards
what states will be possible for the object (what types of data it will store, how many, and what restrictions there will be on values, etc.)
What does the class of an object determine about the object itself?
71
New cards
String planet = new String("earth");
How can we create a string object called planet, and make it so that it stores the value "earth", using a constructor?
72
New cards
Some constructors take in parameters, while others don't
Do all constructors take in parameters, or do only some?
73
New cards
using call by value (meaning that the constructors gets a copy of that value, not the actual value itself)
How are parameters passed to the constructor?
74
New cards
constructors with the same name, but different parameters, so that they create different objects from one another with different characteristics
What are overloaded constructors?
75
New cards
the rectangle class
What is a common class that has overloaded constructors?
76
New cards
-Java knows which constructor to use, and when, by looking at the signature of the constructor, which is made up of the name of the constructor and the constructor's parameter list
When using overloaded constructors, how does Java know which constructor to use, and when?
77
New cards
-the name of the constructor, which is ALWAYS THE SAME AS THE NAME OF THE CLASS THAT THE CONSTRUCTOR IS IN
-the constructor's parameter list, which is the value of the variables passed into the constructor when creating it
What two things make up the signature of a constructor?
78
New cards
into packages
What are classes in Java organized into?
79
New cards
java.lang
In which package are standard Java packages for things like strings, part of?
80
New cards
we have to import it
What must we do to a package before we can use the classes inside that package?
81
New cards
by writing the following code: import Java.lang
How do we import the Java.lang package?
82
New cards
by writing the following code: import edhesive.shapes.*
How do we import the edhesive shapes package?
83
New cards
then we import all the classes in that package
What does it mean if add an asterisk * after the name of a package while importing it?
84
New cards
ye
Are constructors special types of methods that allow the programmer to create a new object?
85
New cards
each class defines its own constructors
Does every class define its own constructors or does each class in Java have the same constructors?
86
New cards
new
When creating a new object using a constructor, what keyword must be used?
87
New cards
nope
Do any two constructors have BOTH the same name and the same parameters (order of parameters and type of parameters)?
88
New cards
by looking at the data type of each parameter in the constructor and the order of the parameters in each constructor
How can Java differentiate between constructors of the same name (overloaded constructors)?
89
New cards
-we get an empty string
-for example, if you start at index 1 but the inputted stopping value is also 1, then remember that when using substring, Java prints everything from the starting index to the stopping index's value minus 1, so when we input both the starting and stopping index as 1, Java tries to print from index 1 to 0, and nothing exists here, so we get an empty string
What do we get if both parameters in the substring method are at the same index?
90
New cards
the # of parameters that the constructor can take in, the data types of the parameters, and the variable names of the parameters inputted
What are the formal parameters of a constructor?
91
New cards
1.0
When using constructors from the shapes package, what is the default side length/radius length?
92
New cards
the side length/radius is automatically set to the default value of 1.0
When using a constructor from the shapes package, what happens when a side length/radius has not been specified?
93
New cards
-the first parameter is an int that specifies the number of side lengths
-the second parameter is a double that specifies the side length of each side
-note that if the second parameter isn't inputted then some shape with the specified amount of sides will have a default of side length of 1.0 on each side, and if either parameter aren't specified then an equilateral triangle with a side length of 1.0 on each side is created
When suing the RegularPolygon constructors, what do the first and second parameters specify?
94
New cards
a header and a body
What are the two parts of a method?
95
New cards
-the visibility modifier: public or private (the visibility always comes first)
-the return data type: int, double, etc. (this comes right after the visibility modifier)
What parts make up the header in a method?
96
New cards
the header
In a method, does the header or body come first (reading from left to right)?
97
New cards
-the name of the method
-the formal parameter list, which is basically the two parentheses with the parameters inside them (the formal parameter list is written after the name of the method)
What parts make up the body of a method?
98
New cards
-the visibility modifier comes first, then the return data type, then the method name, and then the formal parameter list
-for example: public int getFaceValue();
-note that not every method needs to have a parameter inputted, and so the formal parameter list can just be represented by the two parentheses sometimes
What is the general structure of a method?
99
New cards
the creation of an object that the method can be used on
Except certain methods such as those from the math class, what do most methods require before using them?
100
New cards
yessir
Does Java have built in methods for strings?