AP Flashcards

1
Q

WHEN GIVEN A METHOD USE THE METHOD MAKE UR CODE EFFICIENT PLEASE GOOD GOLLY GOD GOSH JEEZ LoUIS

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

can you use == for doubles?

A

NAHHHH, only for ints and booleans (because giant doubles are approximations of real numbers)

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

point of wrappers

A

such as arrraylists, to put ints and doubles into an object-only area

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

if you fail to initalize a local variable in a method, this is the only case where…

A

the variable does NOT get initalized to null/default automatically

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

4/23/24 camera roll

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

“removed.add(memberList.get(i))

memberList.remove(i)

can be simplified down to:

A

remove.add(memberList.remove(i));

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

super.method or method.super?

A

super.method(PARAMETER(s))

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

can you overload an inherited method?

A

yes!

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

you can declare variables in a loop and have it just keep getting redeclared and adjust for every object buster

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

what do you use for a boolean, .equals or ==?

A

==

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

why cant you edit a for each loop?

A

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

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

modifier methods are what make an object…

A

mutable (integer class is immutable)

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

lists can be mutable if youre in a class and you have a _____ via a for each loop

A

modifer method

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

true/false: subclasses inherit the constructors of superclasses

A

false, they need to specify their own constructors (unless the subclass has no constructors, in which java creates an automatic no args)

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

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.

A

B, the length conditional is within the if but the increases in length via extra *’s are outside the if

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

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;

A

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)

17
Q

when is pass by value used?

A

in method calls, wherein the VALUE of the PRIMITIVE parameters, not the actual reference/memory location itself are used. methods use a copy basically

18
Q

when is pass by reference used?

A

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

19
Q

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

A

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.

20
Q

rank the sort types in order of efficiency

A

merge > insertion > selection

21
Q
A
22
Q

how do == and .equals() differ?

A

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:

23
Q

can .super() be used to call a grandparent class’s method?

A

not unless the parent has a super of that same overridden method

24
Q

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?

A

4

25
Q

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();

A

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.

26
Q

declared type vs actual type

A

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!)

27
Q

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();

A

B - ✔️ At compile time the declared type is Book and the Book class does not have or inherit a getDefintion method.

28
Q

how does static play into polymorphism?

A

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

29
Q

private int j;

public constructor(int j)
j = j

what happens here?

A

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

30
Q

insertion sort (shifts/swaps), selection sort (shifts/swaps)

A

shifts
swaps

31
Q

null values are compared by…

A

== (treat it like a primitive, it is the value therefore must be the same thing)

32
Q

object1 = object2
int[]1 = int[]2

vs.

int1 = int2

A

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

33
Q

what are the only things that cannot be called with this.?

A

static stuff, because, yk, no actual object that is “this”, only classes

34
Q

true/false: static methods can change instances

A

no, but instances can change static stuff (and I do mean all static “stuff”)

35
Q

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

A

D, the return doesn’t actually effect it

36
Q
A