AP Flashcards
WHEN GIVEN A METHOD USE THE METHOD MAKE UR CODE EFFICIENT PLEASE GOOD GOLLY GOD GOSH JEEZ LoUIS
can you use == for doubles?
NAHHHH, only for ints and booleans (because giant doubles are approximations of real numbers)
point of wrappers
such as arrraylists, to put ints and doubles into an object-only area
if you fail to initalize a local variable in a method, this is the only case where…
the variable does NOT get initalized to null/default automatically
4/23/24 camera roll
“removed.add(memberList.get(i))
memberList.remove(i)
“
can be simplified down to:
remove.add(memberList.remove(i));
super.method or method.super?
super.method(PARAMETER(s))
can you overload an inherited method?
yes!
you can declare variables in a loop and have it just keep getting redeclared and adjust for every object buster
what do you use for a boolean, .equals or ==?
==
why cant you edit a for each loop?
because such as in for ( int num : nums), num is a variable declared in the header, or a COPY of nums. editing num edits the instance variable
modifier methods are what make an object…
mutable (integer class is immutable)
lists can be mutable if youre in a class and you have a _____ via a for each loop
modifer method
true/false: subclasses inherit the constructors of superclasses
false, they need to specify their own constructors (unless the subclass has no constructors, in which java creates an automatic no args)
12-2-10: When will the method stringRecursion produce a run time error?
public void stringRecursion(String s)
{
if (s.length() < 16)
{
System.out.println(s);
}
stringRecursion(s + “*”);
}
A. It will never produce a run time error.
B. It will always produce a run time error.
C. Only when the length of the input string is greater than or equal to 16.
D. Only when an empty string is input.
E. Whenever the input string length is less than 16.
B, the length conditional is within the if but the increases in length via extra *’s are outside the if
12-2-17: Consider the following method. Assume that String s = “rain”; and int b = 4; have been executed. What are the values of s and b after test(s,b) is executed?
public static void test(String str, int y)
{
str = str + “bow”;
y = y * 2;
}
A. s=”rainbow”; b=8;
B. s=”rain”; b=8;
C. s=”rainbow”; b=4;
D. s=”rain”; b=4;
E. s=”bow”; b=4;
D, strings are immutable so changes mean you create a new string, s is unaffected. primitive types are COPIED, and nothing is done to b as they are PASSED BY VALUE (only the value is passed to the method, not the reference variable)
when is pass by value used?
in method calls, wherein the VALUE of the PRIMITIVE parameters, not the actual reference/memory location itself are used. methods use a copy basically
when is pass by reference used?
while java technically never uses pass by reference, the pass by value of objects is effectively the same thing
an object reference is passed as a method parameter and can be altered OUTSIDE the confines of that method by something within the method
12-2-18: Which of the following is/are true about using insertion sort versus using merge sort?
I. Insertion sort requires more storage space than mergesort.
II. Insertion sort is only more efficient than mergesort in the case that we have a very small and nearly sorted array.
III. Insertion sort is almost always less efficient than mergesort.
A. I only
B. II only
C. III only
D. I and III
E. II and III
E✔️ Merge sort uses the “divide and conquer” approach to sort an array. This will end up being more efficient than insertion sort in the case where we have long unordered array. However if we have a very small almost sorted array, then insertion sort will outperform merge sort.
rank the sort types in order of efficiency
merge > insertion > selection
how do == and .equals() differ?
FOR STRINGS:
== determines whether or not the names refer to the same OBJECT, whereas .equals() determines whether the VALUE is the same (same vs. equivalent) (cannot use keyword new in the second reference variable)
for everything else:
can .super() be used to call a grandparent class’s method?
not unless the parent has a super of that same overridden method
given:
int q = 4;
changeN(q);
System.out.println(q);
SEP METHOD:
public static in changeN(int q)
q++;
return q;
what will the code print?
4
13-2-11: Assume the following classes.
public class Animal
{
// constructors not shown
public void eat()
{ // code not shown
}
}
public class Bear extends Animal
{
// constructors not shown
public void growl()
{ // code not shown
}
}
Assume that the following declaration is in a different class.
Animal b = new Bear();
Which of the following will compile without error?
I. b.eat();
II. b.growl;
III. ((Bear) b).growl();
1 and 3:
I works since the declared type is Animal and Animal has an eat method. III works because the cast tells the compiler to treat b is a Bear and Bear has a growl method.
II does NOT work because java looks for methods based on the DECLARATION type, and as such will not be able to find specific methods only found in bear.
declared type vs actual type
the declared type (the left side of the = statement) is the type at declaration, and the type which java will parse, looking for the methods used in code. if found, the code will compile, but if not, the code will NOT compile (see bear notecard)
the actual type/type at RUN TIME is the type that the object actually, well, is (right side of the = sign). at run time, as the code managed to compile, java will either find the method or keep going up all the way to the object class to find a method (but it WILL find one!)
if there is a method in the declared type that allows the code to compile, but that same method is overridden in the actual type, then it uses the ACTUAL type duringe execution! (thus, RUN TIME type!)
9-6-4: Given the following class definitions and a declaration of Book b = new Dictionary() which of the following will cause a compile-time error?
public class Book
{
public String getISBN()
{
// implementation not shown
}
// constructors, fields, and other methods not shown }
public class Dictionary extends Book
{
public String getDefinition()
{
// implementation not shown
}
}
A. b.getISBN();
B. b.getDefintion();
C. ((Dictionary) b).getDefinition();
B - ✔️ At compile time the declared type is Book and the Book class does not have or inherit a getDefintion method.
how does static play into polymorphism?
in an instance like
Animal eshanTanseer = new Dog();
you would call
Animal.makeSound();
or if you called
eshanTanseer.makeSound();
it would give the same result as the animal static makesound method, NOT the dog one, as it was declared as an animal. as the static methods are tied to classes,
it doesn’t actually matter what this individual instance is as static methods are tied to the CLASSES, when java sees animal declaration, it goes for an animal static method, regardless of whether this INDIVIDUAL INSTANCE ( N O T CL A S S ) of animal is ALSO a dog during runtime
private int j;
public constructor(int j)
j = j
what happens here?
in the scope of the constructor the PARAMETER takes precedence. thus, the copy of the PARAMETER gets set equal to the field, the default value of which
insertion sort (shifts/swaps), selection sort (shifts/swaps)
shifts
swaps
null values are compared by…
== (treat it like a primitive, it is the value therefore must be the same thing)
object1 = object2
int[]1 = int[]2
vs.
int1 = int2
int1 gets a copy of int2
object1 now references the same memory location of object2
object2’s changes will affect object1 and vise versa, whereas the ints will not do so
int[]s are objects so same things ith them
what are the only things that cannot be called with this.?
static stuff, because, yk, no actual object that is “this”, only classes
true/false: static methods can change instances
no, but instances can change static stuff (and I do mean all static “stuff”)
13-4-9: Consider the following method and if int[] a = {8, 3, 1}, what is the value in a[1] after m1(a); is run?
public int m1(int[] a)
{
a[1]–;
return (a[1] * 2);
}
A. 4
B. 16
C. 7
D. 2
E. 3
D, the return doesn’t actually effect it