Unit 3 Flashcards

1
Q

What are the operators == and != used for?

A

determining whether something is equal or inequal (and whether object names/reference variables would refer to the same thing or not via boolean
(ex. code lines
Object n1 = new Object()
and
Object bro = n1
makes bro and n1 refer to the same thing and thus would be ==) )

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

list the relational operators (numerical):

A

< Less Than

> Greater Than

<= Less than or equal to

> = Greater than or equal to

== equals
!= not equals

write them like you would say them “less than or equal to” means less than comes first, then equal to always comes after

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

what would !(c==d) be equal to?
what about !(a>c)
or
!(a && b)?

A

c!=d and a<=b

!a || !b

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

what are the cases in which demorgans law would ! a variable?

A

only with boolean expressions

!(a && b) —–> !a || !b

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

How would you see if a number is even or odd using a remainder and a relational operator?

A

number % 2 == 0
true for even, false for not. similar thing for seeing if something is uneven (if you want a true value)
number % 2 > 0

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

Where is reaminder often used? PLACEHOLDER

A

Use it to check for odd or even numbers. If num % 2 != 0 is true, num is odd and if num % 2 == 0 is true then num is even.

You can also use remainder to check if any number is evenly divisible by any other: If num1 % num2 == 0 is true then num1 is evenly divisible by num2.

Use it to get the last digit from an integer number: num % 10 gives us the rightmost digit of num.

Use it to get the number of minutes left when you convert a total number of minutes to hours and minutes:

int totalMinutes = 345;
int hours = totalMinutes / 60; // Number of whole hours, i.e. 5
int minutes = totalMinutes % 60; // Number of minutes left over, i.e. 45
Use it whenever you have limit in the value, and you need to wrap around to zero if the value goes over the limit: the value of num % limit will always be in the range from 0 (inclusive) to limit (exclusive) as long as num and limit are both positive.

PLACEHOLDER

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

What must the keyword if always be followed by in an if statement

A

a boolean within a parenthesis (boolean expression)
then Do statement;
or
{
Do statement
}

NEVER PUT THE SEMICOLON IN AN IF STATEMENT WITHIN THE same LINE

DO NOT DO
If (expression);

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

What is a “dangling else” set of code assigned to?

A

attachedto the closest unassigned if statement (indentation means nada zilch zero) BUT you can have curly braces to separate the ifs and assign the else to a specific, further if statement like so:

if (boolean expression)
{
if (boolean expression)
Do this statement;
}
else // belongs to first if
Do that statement;

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

must align squiggly braces with header of if expression, everything inside a tab forward, and if its an if… else… else should be aligned with if

true/false

A

true, though technically a personal style and not NECESSARY, do it for comp sci A you bum

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

when do you not need squiggly braces for a conditional?

A

when you have only one line of code, just having indenting is fine (but use squigglies because this might get messy)

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

define short-circuit evaluation

A

an order of programming which avoids error, as if a parameter is not met from left to right at the first expression, the program will not continue

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

Why do you need a “public static” for MOST practice problem methods?

A

so that the method is accessible/public to different classes, and static means it belongs to all instances of a class/is part of the class

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

in the code segment
String s1 = new String(“Hello”);
String s2 = new String(“Bye”);
how would you set an alias name for s2, s3?

A

String s3 = s2;

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

how do == and .equals() differ when used on strings?

A

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

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

why compare objects to null?

A

to see if they exist, and thus not to cause an error when moving further into the conditional (because an object that doesn’t exist cant be compared to anything)

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

why should you ALWAYS use num % 2 != 0 to check if num is odd?

A

because what happens if num is zero? that’s what i thought

17
Q

in the code:

int x = 5;

if (x <10)
return 0;
else if (x<11)
return 1;
else if (x<12)
return 2;

what will be returned?

A

2, as the else ifs override each other

18
Q

does an if ALWAYS require an else?

A

nah

19
Q

in the code

int x = 10;
if (x>5)
return 0:
else
return 1;

if (x>6)
return 12;
else
return “c”;

what is returned?

A

12, as the second line of if else overrides the first

20
Q

generally, in loops, when you want to count to a number and you start at 0, you use ______ (<, <=) and when you want to count to a number and you start at 1, you use _____ (<, <=)

A

<

<=

21
Q
A