Lecture3- Branches Flashcards

1
Q

What is a Boolean expression?

A

A statement that evaluates to true or false.

Examples: 4 == 7 → false, 2 < 3 → true, x > 20 (depends on x)

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

What are relational (comparison) operators in Java?

A

Operators that compare two values and return true or false.

Operators include: * > (Greater than) * < (Less than) * >= (Greater than or equal to) * <= (Less than or equal to) * == (Equal to) * != (Not equal to) ⚠ Note: = is assignment, == is equality.

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

What are branches in Java?

A

Statements that let the program choose between alternative actions based on conditions.

Example: if (temperature > 30) { System.out.println(“It’s hot outside!”); } else { System.out.println(“It’s cool outside.”); }

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

What is the syntax for an if statement?

A

if ( condition )
statement;

doesn’t require curly brackets

Example: if (max == 5) { y = x + y; }

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

What is a block statement in Java?

A

A group of multiple statements enclosed in {}.

  • Indentation alone is not enough! thru it may be for an if statment of there is only one line of code in the body.

Example: if (mouseX < width/2) { fill(255); rect(0, 0, width/2, height); } ⚠ Indentation is NOT enough; use {}!

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

What is an if-else statement?

A

Executes one block of code if the condition is true, otherwise executes another.

Example: if (userAge < 25) { insurancePrice = 4800; } else { insurancePrice = 2200; }

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

What are nested if statements?

A

An if statement inside another if or else.

Example: if (x > 0) { if (y > 0) { System.out.println(“x and y are positive”); } } ⚠ Braces {} help prevent confusion when using else.

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

What is a cascading if-else?

A

A series of if-else conditions that check multiple cases.

Example: if (numYears == 1) { System.out.println(“First year!”); } else if (numYears == 10) { System.out.println(“A decade!”); } else { System.out.println(“Nothing special.”); }

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

What are Java’s logical operators?

A

Operators that combine Boolean expressions.

Operators include: * ! (NOT) * && (AND) * || (OR) Example: if (temp > 98.6 || hasRash) { System.out.println(“Go to the doctor!”); }

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

What does ! (NOT) do?

A

Reverses a Boolean value.

Example: boolean isRaining = false; System.out.println(!isRaining); // Output: true

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

How do you compare strings in Java?

A

Use .equals() for content comparison.

Example: if (str1.equals(str2)) { System.out.println(“Strings are equal”); } ⚠ Do NOT use == for content comparison.

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

What is the purpose of a switch statement in Java?

A

It’s an alternative to if statements. The switch statement provides another way to decide which statement to execute next by evaluating an expression and attempting to match the result to one of several possible cases.

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

What is the general syntax of a switch statement?

A

```java
switch (expression) {
case value1:
statement-list1;
break;
case value2:
statement-list2;
break;
case value3:
statement-list3;
break;
default:
statement-list4;
break;
}
~~~

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

What is the purpose of the break statement in a switch statement?

A

The break statement causes control to transfer to the end of the switch statement. Without it, the flow of control will continue into the next case.

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

What happens if a break statement is omitted from a case in a switch statement?

A

If a break statement is omitted, the flow of control continues into the next case, which may be appropriate in certain situations but is typically avoided to ensure only the statements of one case are executed.

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

What is the role of the default case in a switch statement?

A

The default case has no associated value and is executed if no other case value matches. If no default case exists and no values match, control falls through to the statement after the switch.

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

Can a switch expression be any type?

A

A switch expression can be a byte, short, char, int, or a String. It cannot be a boolean, float, or double. Relational expressions cannot be used with a switch statement.

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

What is a string in Java?

A

A string is a sequence of characters in memory. Each character in the string has an index, starting with 0.

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

How are two strings considered equal in Java?

A

Two strings are equal if:
- Both strings have the same number of characters.
- Each corresponding character is identical.
- Case matters (e.g., “Book” ≠ “book”).

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

What does str1.equals(str2) do in Java?

A

It returns true if str1 and str2 have the same number of characters and the same case.

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

How does the == operator compare strings in Java?

A

The == operator compares the memory addresses of the strings, not their contents.

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

What does str1.compareTo(str2) return?

A

It returns:
- A negative number if str1 is lexicographically less than str2.
- 0 if str1 is equal to str2.
- A positive number if str1 is lexicographically greater than str2.

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

How do you compare strings while ignoring case in Java?

A

Use str1.equalsIgnoreCase(str2) or str1.compareToIgnoreCase(str2).

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

What does str1.length() return?

A

It returns the number of characters in the string str1.

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

What does str1.concat(s2) do in Java?

A

It creates a new string by appending s2 to the end of str1.

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

What does str1.charAt(indexNumber) do?

A

It returns the character at the specified index of the string str1.

27
Q

What does str1.indexOf(item) return?

A

It returns the index of the first occurrence of item in str1. If item is not found, it returns -1.

28
Q

What does str1.lastIndexOf(item) do?

A

It returns the index of the last occurrence of item in str1. If not found, it returns -1.

29
Q

What does str1.substring(startIndex) do?

A

It returns a substring starting from startIndex to the end of the string.

30
Q

What does str1.substring(startIndex, endIndex) do?

A

It returns a substring starting at startIndex and ending at endIndex - 1.

31
Q

What does str1.replace(findStr, replaceStr) do?

A

It returns a new string where all occurrences of findStr are replaced with replaceStr.

32
Q

What is a method in Java?

A

A method is a group of statements that perform a specific task. Methods have a name followed by parentheses (). The values inside the parentheses are called parameters.

33
Q

What is Math.pow(a, b) used for?

A

Math.pow(a, b) is a method to compute ‘a’ raised to the power of ‘b’.

34
Q

What does Math.random() do in Java?

A

Math.random() provides a random number.

35
Q

What categories are mathematical methods in the Math class organized into?

A

The Math class methods are categorized as:
- Trigonometric methods
- Exponent methods
- Service methods

36
Q

Do you need to import the Math class in Java?

A

No, the Math class is in the java.lang package and is implicitly imported into all Java programs.

37
Q

What is an example of a useful constant provided by the Math class?

A

An example is Math.PI, which provides the value of Pi.

38
Q

what does the Math.random() do?

A

returns a double random number between 0 (inclusive) and 1
(exclusive).

39
Q

how do we modify the Math.random() command

A

a + (Math.random() * b);

returns a random double betwen a and (a+b)-1

Ex.
Math.random() * 10 will return a double between 0.000 to 9.0000,

Math.random() * 10 +1 will give 1-10

If you want an integer, just type cast the whole thing

40
Q

What does import java.util.Random; allow you to do?

A

It provides the ability to create a Random object and use all its properties.

41
Q

What is the default behavior of a Random object for seeding?

A

By default, it uses a number based on the current time as the seed, creating a different sequence with each program run.

42
Q

How do you create a Random object in Java?

A

Use Random rand = new Random();.

43
Q

How do you set a specific seed for a Random object?

A

Use the setSeed() method, e.g., rand.setSeed(5);.

44
Q

How do you generate a random number within a specific range in Java?

A

Use the nextInt() method, e.g., rand.nextInt(25); (returns a value from 0-24).

45
Q

How do you get a random number in the range 10-15?

A

Use rand.nextInt(6) + 10;.

46
Q

What is the char data type used for in Java?

A

The char data type is used to hold a single character.

47
Q

How is a char stored in Java?

A

A char is stored as a number using a character encoding, such as ASCII or Unicode.

48
Q

Can relational operators be used on char data in Java?

A

Yes, relational operators can be used on char data, as each character has a unique numeric Unicode value.

49
Q

What is the result of the following code?

```java
char a = ‘a’;
System.out.println(++a);
~~~

A

The result is b.

50
Q

Can a char be cast into a numeric type in Java?

A

Yes, a char can be cast into any numeric type, and vice versa. When a char is cast into a numeric type, its Unicode value is used.

51
Q

What does indexOf(ch) do?

A

Returns the index of the first occurrence of ch in the string. Returns -1 if not matched.

52
Q

What does indexOf(ch, fromIndex) do?

A

Returns the index of the first occurrence of ch after fromIndex in the string. Returns -1 if not matched.

53
Q

What does indexOf(s) do?

A

Returns the index of the first occurrence of string s in this string. Returns -1 if not matched.

54
Q

What does indexOf(s, fromIndex) do?

A

Returns the index of the first occurrence of string s in this string after fromIndex. Returns -1 if not matched.

55
Q

What does lastIndexOf(ch) do?

A

Returns the index of the last occurrence of ch in the string. Returns -1 if not matched.

56
Q

What does lastIndexOf(ch, fromIndex) do?

A

Returns the index of the last occurrence of ch before fromIndex in this string. Returns -1 if not matched.

57
Q

What does lastIndexOf(s) do?

A

Returns the index of the last occurrence of string s. Returns -1 if not matched.

58
Q

What does lastIndexOf(s, fromIndex) do?

A

Returns the index of the last occurrence of string s before fromIndex. Returns -1 if not matched.

59
Q

What is a text box used for in programming?

A

A text box is used to display multiple lines of text.

60
Q

How is a multi-line text box defined in programming?

A

A multi-line text box starts with three double quotations ("""), followed by optional whitespace and a line terminator. The last line ends with three double quotations (""") again.

61
Q

How does indentation work in multi-line text boxes?

A

Indentation can be used in a multi-line text box, and the compiler removes the maximum common left space characters from all the lines.

62
Q

What happens to the right trailing spaces in a multi-line text box?

A

Right trailing space is trimmed by default. To keep the right trailing space, the escape character \s should be used.

63
Q

Can other escape characters be used in a multi-line text box?

A

Yes, other escape characters can also be used in a multi-line text box.