AP Flashcards
(36 cards)
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