midterm stuff Flashcards

1
Q

why do we need getter/accessor methods?

A

because of private fields. we need them to see whats happening on the console in the runner

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

where should the main method go? the class or the runner?

A

runner

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

true/false: you shouldn’t use == with doubles because of limited storage rounding

A

true, a double of 1.00000000000000000000001 will read as == 1 when in reality it should be < or >

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

true/false: a 2d array is just an array of arrays

A

true

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

EFFICIENCY:

is the circumference method in:

private double r = 123412312;

public double diameter()
{
return r*2;
}

public double circumference()
{
return 2rMath.PI;
}

efficient? why/why not?

A

no, as the diameter() method is not used. on an AP test, there is no wasted code. so if you see something like that diameter method, it should be used in the solution to the circumference. its also simpler on you as youre less likely to make mistakes on more complicated already given methods.

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

is Math.PI a method?

A

no, property (no parenthesis)

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

true/false: 2d arrays start both rows and columns at index 0

A

true

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

what do the first and second set of brackets in a 2d array:
arr[][]
indicate?

A

first = ROW starting at index 0

second = COLUMN starting at index 0

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

two ways of initializing 2d arrays:

A
  1. int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}};
  2. int[][] arrTwo = new int[][] {{1, 2, 3}, {4, 5, 6}};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

row-major vs. column major order

A

row:
row/first bracket iterator = OUTSIDE loop and column is the inside loop

column:
column/second bracket iterator = outside loop and row is the inside loop

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

what happens when you print a non-string object?

A

you get the name of the object@the memory location

ex. Store@6bc7c054

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

what is the purpose of the toString() method?

A

turning things into strings, particularly objects which, when printed, will only return their name@memory location which is very very not helpful.

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

true/false: toString is automatically called by java every time you print smth

A

true, BUT to get the value you might want out of an object you need to OVERRIDE the toString method as so:

public String toString()
{
return (concatenated values and strings here);
}

and can be called with
System.out.print(e);
or
System.out.print(e.toString());

where e is an object,

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

true/false: toStrings can be overridden to explicitly print basic strings

A

true, java does this implicitly with
System.out.print(str);
where
String str = “java”;

or EXPLICITLY with

System.out.print(str.toString());

the toString() method is automatically called whenever a print is called (its part of the print method!)

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

compareTo vs. equals vs. == vs. valueOf()

A

compareTo() :returns a value negative if string is less than another alphabetically, 0 if same characters in same order, positive if greater

.equals: do they have the same value? (might not work well with custom objects unless overridden, ends up doing the same thing as ==)

==: compares the memory location/if theyre the SAME OBJECT and if a primitive type

valueOf(): returns the string version of a given value (called as String.valueOf(whatever valuae))

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

What is Integer or Double? how can they be used?

A

they are wrapper classes that turn integers and doubles into a reference datatype (object) to be given to methods expecting objects.

they are declared like an object:
Integer i = new Integer(2);, for example

wrapper classes are special because they have min and max values

17
Q

what are the aspects of a getter method?

A
  1. non-void return type
  2. no arguments
        - while you can have a method named something like getdistance(double posX, double posY), its not technically a getter because it COMPUTES A NEW VALUE and has arguments 
  3. GENERALLY named get(and then the name of the thing)
  4. DOES NOT COMPUTE A NEW VALUE
18
Q

primitive vs. reference types

A

Primitives: info stored as the value (boolean, int, double)

reference: holds reference to info related to variable (string, object, literally all other variables)

19
Q

true/false: strings are immutable

A

true

20
Q

range of integers

A

negative 2^31 (32 bits, one bit used to represent the sign)
and positive 2^31 - 1 (0 counts as a value!)

21
Q

define underflow and overflow in terms of bits and space

A

when changing a number past the space limits the opposite limit is returned (ex. adding 2000 to the 2^31-1 max results in java returning the min)

22
Q

call by value

A

when a method/constructor is given an argument, the resulting parameter is a COPY, not the original, meaning if you edit the parameter whatever argument was entered remains the same

23
Q

define camel case

A

naming convention where first word is lowercase and next words start with uppercase
thisIsCamelCase

24
Q

what does static mean?

A

belongs to all instances of a class/is part of the class

25
Q

string literal

A

“this”;

26
Q

how to import java classes?

A

3 parts:
1. keyword import
2. package name (usually java.util)
3. class (ex. scanner)

connect package name and class with a dot and end with a semicolon

putting it altogether youll get smth like:
import java.util.Scanner;

27
Q

instance variables vs. local variables

A

instance = fields, variables of the CLASS

local = variables declared within a specific scope

28
Q

how can string be declared?

A

either string literal:
String str = “danctilsaveus”;

or string object

String str = new String(“danctilsaveus”);

29
Q

true/false: in the statement
double x = Math.random()*10;
x is between the ranges of 1-10

A

no, it is 0-9. you must add one to make it one to ten.

30
Q

what is the range of double x = Math.random();?

A

0.0 UP TO BUT NOT INCLUDING 1.0

31
Q
A