Be Prepared Ch 1 - 2.3 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Why shouldn’t you use a for each loop sometimes?

A

You cant modify stuff when using a for each loop. as the for each loop is making a copy of the stuff. Rather use a regular for loop

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

Free responses will typically be less than _____ lines of code

A

12

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

Appropriate Casting

A

Int a,b;

Double ratio;

Ratio = (double)a/b; // or a/(double) b;

//but not ratio = (double) (a/b) – this is a cast applied too late!

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

There are two forms fo the ++ and – operators in Java. What are they and how are they different?

A

The prefix form increments (or decrements) the variable before its value is used in the rest of the expression; the postfix form increments (or decrements) it afterwards.

While(i<=n) sum+= i++; is the same as

While (I <= n)

{

Sum+= I;//i

i++;

}

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

== and != compare the _________ of the objects, not the ______ To compare the ____ use the equals or compareTo method

A

addresses; values; values

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

== or != must be used when you need to compare an object to ____

A

null

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

“Everything” in Java not explicitly set to something is initialized to a ____ value.

A

zero;

For references (anything that holds an object) that is null.

For int/short/byte that is a 0.

For float/double that is a 0.0

For booleans that is a false.

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

Everything in Java, except _____, not explicitly set to something is initialized to a zero value.

A

Local Variables;

You need to explicitly initialize any local variables (by either initialization or assignment)

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

assignment

A

throwing away the old value of a variable and replacing it with a new one

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

initialization:

A

it’s a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.

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

declaration:

A

a _____ states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.

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

De Morgan’s Laws

A

!(a&&b) is the same as !a||!b

!(a||b) is the same as !a&&!b

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

Short-circuit evaluation

A

For the java logical operators, && and ||, if the value of the first operand is suffiecient to dertemine the result, the the second operand is not evaluated.

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

Bit-wise logical operators

A

Not in ap exam. But can cause synatx errors when comparing bits, or bolleans . .. Do not comply with short-ciruit evaluation when used in that way

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

For-each loop

A

Int[] scores = [87,85,76];

For (int score : scores)

System.out.println(score+” “);

Note: if you need access to indices, use a for or while loop

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

a “break;” statement takes you out of the inner loop but not out of the ____.

A

outer loop

17
Q

Strings are _____

A

immutable

no string methods can change the string. An assignment statement str1 = str2; copies the reference from str2 into rts1, so they both refer to the same memory location.

18
Q

A literal string

A

is a string of characters within double quotes. May include “escape sequences” \n, ", \ etc.

Ex: “Hello” or “Hello\n”

19
Q

String Methods

A

Length

Equals

compareTo

Substring()

Substring(int start)

indexOf(String find)

indexOf(String find, int fromIndex)

20
Q

compareTo -

A

returns a positive number if str1 is greater than str2 (lexicograhpically), zero if they are equal, anda negative number if str1 is less than str2

21
Q

indexOf(String find)

A

Returns the index within this string of the first occurrence of the specified substring.

returns -1 if find is not found in str

22
Q

indexOf(String find, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

returns -1 if find is not found in str

23
Q

If you have a string method called process(String msg, String delim)
, how does process change msg?

A

It doesnt.

Rhyme = “…something.\n …bob…bob..\n..”

String rhyme2 = process(rhyme, “\n”); << does not change rhyme!

Process receives and works with a copy of a reference to the original string (see Be Prepared Section 3.3) the method can reassign the copy, as it does here, but the original reference still refers to the same string. This consideration, combined with the immutability of strings, assures us that rhyme remains unchanegd after the call process(rhyme)