1/159
Looks like no tags are added yet.
What is a switch statement?
Statement that executes the first case whose constant expression matched the value of the switch expression
What is the general form of a switch statement?
switch (expression) {
case constantExpr1:
// Statements
break;
case constantExpr2:
// Statements
break;
default: // If no other case matches
// Statements
break;
}
What is the default case?
If no other cases match (else)
What is a switch statement equivalent to and can be replaced by?
Multi-branch if-else statement
What data type can be in the switch statement’s expression?
Int, char, string
What data type cannot be in the switch statement’s expression?
boolean, floating-point type
Can the case expression be a variable?
No, it must be a constant expression
Does the order of the cases matter?
No, but good style is to have them in some order
What happens when you omit the break statement for the switch statements?
The program finds a match for the test variable, all statements within the switch statement execute from that point forward
What is a boolean data type?
A quantity that only has true or false values
What is a use of boolean data type variables?
It can be used to simplify if-else statements. Assign the boolean variable with the result of an expression. Insert variable into if().
What is a conditional operator?
A shortcut to a single if-else statement
What is the format of a conditional operator?
variable = (condition)? exprWhenTrue: exprWhenFalse
If x =2, what is the output?-
(x == 2) ? 5 : 9 * x
5
What is the operator precedence? (9)
Logical NOT (!)
Multiplication, division, modulus
Addition, subtraction
Relational (< >)
Equality ( == !=)
Logical AND (&&)
Logical OR (||)
Conditional (?:)
Assignment (=)
Can floating-point numbers be compared with ==? Why or why not?
No, because they are not exactly represented in limited available memory bits
What is the method to see if two strings are equal?
str1.equals(str2)
What does str1.equals(str2) return if the two string ARE equal?
true
What is the method to relationally compare two strings?
str1.compareTo(str2)
How does relational string comparison work?
Comparison begins at index 0, when a character of the two strings is not equal, those characters will be relationally compared based on the Unicode. (sees which unicode is greater or less than)
Is A or a greater?
a is greater. The unicode of a = 97, the unicode of A = 97
What letter of strings is usually less than others in the alphabet?
A
What shorter strings greater than or less than longer strings (if characters are equal)?
less than
ex. banana < bananarama
What does str1.compareTo(str2) output when str1 is less than str2?
A negative number
What boolean expression to check if str1 is less than str2?
str1.compareTo(str2) < 0
What does str1.compareTo(str2) output when str1 is equal to str2?
0
What boolean expression to check if str1 is equal to str2?
str1.compareTo(str2) == 0
What does str1.compareTo(str2) output when str1 is greater than str2?
Positive number is returned
What boolean expression to check if str1 is greater than str2?
str1.compareTo(str2) > 0
What method checks if two strings are equal to each other while ignoring case?
str1.equalsIgnoreCase(str2)
What method compares two strings while ignoring case?
str1.compareToIgnoreCase(str2)
What is an index?
Each string position number
What does an index start with?
0
What method determines the character at a specified index?
charAt()
What would someString.charAt(x) output?
Outputs the character of someString at index x
When using charAt() method, what happens when an index is inputted that doesn’t exist?
Error is outputted
How to find the last character index if the length is known?
Length - 1
What method returns the strings length?
string.length()
What would this do?-
stringVariable + “hello”
It would return a new string that adds hello to the end of stringVariable
What would this do?-
stringVariable1 += stringVariable2
It would concatenate stringVariable2 to the end of stringVariable1
What method concatenates two strings?
concat()
What would s1.concat(s2) return?
Return a new string that adds s2 to the end of s1
Would this code change s1 variable?-
s1.concat(s2)
No
How to change this code so that s1 is changes to the concatenated version-
s1.concat(s2)
s1 = s1.concat(s2)
What should all character method be preceded by?
Character.
What method checks if a character is a letter?
Character.isLetter(‘ ‘)
What method checks if a character is a digit?
Character.isDigit(‘ ‘)
What method checks if a character is whitespace?
Character.isWhitespace(‘ ‘)
What method uppercases a character?
Character.toUpperCase(‘ ‘)
What method lowercases a character?
Character.toLowerCase(‘ ‘)
What character finds the index of a string or character inside a string?
indexOf()
What is the syntax of indexOf()?
stringVariable.indexOf(textToFind)
What does indexOf() return?
Returns index of first occurrence of the character or string
What does indexOf() return when the string or character is not found?
-1
What does this return?-
s1.indexOf(textToFind, startIndex)
Returns the index of the first occurence of textToFind starting at index startIndex
What method returns the index of the last occurence of the text to find?
s1.lastIndexOf(textToFind)
What method extracts a specific portion of a string and returns it as a new string object?
substring()
What is the basic syntax of substring() method?
s1.substring(startIndex, endIndex)
What does s1.substring(startIndex, endIndex) return?
Returns substring starting from index startIndex to index (endIndex - 1)
(endIndex is exclusive)
What does s1.substring(startIndex) return?
Returns substring of s1 starting from index startIndex to end of the string
What method can replace a string with text?
replace()
What is the basic syntax of the replace() method?
s1.replace(oldText, newText)
What type of data is a string?
String is a class
What each created String defined as?
A class object
What is a String variable?
A named object of the String class
What does it mean that String variables are a reference?
It is a variable that holds a memory address, not the String itself
What does comparing two Strings using the == operator do?
It is comparing the computer memory locations/address number of the strings. Dont do it!!!
If String a = “Hi!” and String b = a, what is the output of a == b?
True, since it is comparing the same memory address
What is a null String?
String variable that is assigned to null keyword. Object is referred to nothing in memory
What is the endsWith() and startsWith() method?
Takes a String argument and returns true or false if a String object does or does not end or start with the specified argument
What is the Integer.parseInt() method?
Takes a String argument and returns its integer valeu
What is the Double.parseDouble() method?
Takes a String argument and returns its double value
How to create a StringBuilder object?
StringBuilder sb1 = new StringBuilder(“ “);
What is the append() method?
Adds characters to the end of a StringBuilder object
sb1.append(“textToAdd”);
What is the insert() method?
Adds characters at a specific location within a StringBuilder object
What is the setCharAt() method?
Changes a character at a specified position within a StringBuilder object
What is the difference in bits of a lowercase and uppercase character?
Only one bit
What would this code output?-
char anotherLetter = ‘a’ + 5;
f, since it is adding the unicode of a + 5. that unicode in a character would be f.
What would this code output?-
int integer = ‘a’ + 5;
The unicode of ‘a’ + 5
What would this code output if letter = ‘a’-
char anotherLetter = letter + 5;
It would be an error
What is the long way to declare a String?
String newStringvar = new String(“text”);
What is an iteration?
Each time through a loop’s statements
What is a while loop?
Program that repeatedly executes a list of sub-statements while the loop’s expression evaluates to true
What is the basic format of a while loop?
while (expression) {
// Loop body: Executes if expression evaluated to true
// After body, execution jumps back to the "while"
}
For a while loop, where are the two places you can first get input into a variable?
At the end of loop body
before the loop
Which method of getting input into a variable (at end of loop, before the loop) will ensure that loop will always executes at least once?
At the end of the loop
What is an infinite loop?
A loop that never stops iterating?
What are two common causes of an infinite loop?
Forgetting to update a variable
Creating a loop expression whose evaluation to false isn’t reachable
What is a sentinel value?
Special value indicating the end of a list
What is a do-while loop?
A loop that first executes the loop body’s statements, then checks the loops condition
What is the format of a do-while loop?
do {
// Loop body
} while (loopExpression);
What is the minimum amount of times a do-while loop will iterate?
Will iterate at least once
What is a for loop?
Loop with variable initialization, loop expression, and loop variable update
What is the format of a for loop?
for (initialExpression; conditionExpression; updateExpression) {
// Loop body
}
What is the increment operator?
++variable, increments the variable by 1
What is the decrement operator?
- - variable, subtracts the variable by 1
When should you use a for loop and when should you use a while loop?
For loop - when number of iterations is known
While loop- when number of iterations is not known
What is a common use of nested loops?
Can be used to generate all combinations of some items
How to find inner loop total iterations?
outer loop iterations x each inner loop iteration
What is a break statement?
break;
causes immediate exit of loop