Exam 2 - CS 150

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/159

encourage image

There's no tags or description

Looks like no tags are added yet.

160 Terms

1
New cards

What is a switch statement?

Statement that executes the first case whose constant expression matched the value of the switch expression

2
New cards

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;

}


3
New cards

What is the default case?

If no other cases match (else)

4
New cards

What is a switch statement equivalent to and can be replaced by?

Multi-branch if-else statement

5
New cards

What data type can be in the switch statement’s expression?

Int, char, string

6
New cards

What data type cannot be in the switch statement’s expression?

boolean, floating-point type

7
New cards

Can the case expression be a variable?

No, it must be a constant expression

8
New cards

Does the order of the cases matter?

No, but good style is to have them in some order

9
New cards

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

10
New cards

What is a boolean data type?

A quantity that only has true or false values

11
New cards

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().

12
New cards

What is a conditional operator?

A shortcut to a single if-else statement

13
New cards

What is the format of a conditional operator?

variable = (condition)? exprWhenTrue: exprWhenFalse

14
New cards

If x =2, what is the output?-

(x == 2) ? 5 : 9 * x


5

15
New cards

What is the operator precedence? (9)

  1. Logical NOT (!)

  2. Multiplication, division, modulus

  3. Addition, subtraction

  4. Relational (< >)

  5. Equality ( == !=)

  6. Logical AND (&&)

  7. Logical OR (||)

  8. Conditional (?:)

  9. Assignment (=)


16
New cards

Can floating-point numbers be compared with ==? Why or why not?

No, because they are not exactly represented in limited available memory bits

17
New cards

What is the method to see if two strings are equal?

str1.equals(str2)

18
New cards

What does str1.equals(str2) return if the two string ARE equal?

true

19
New cards

What is the method to relationally compare two strings?

str1.compareTo(str2)

20
New cards

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)

21
New cards

Is A or a greater?

a is greater. The unicode of a = 97, the unicode of A = 97

22
New cards

What letter of strings is usually less than others in the alphabet?

A

23
New cards

What shorter strings greater than or less than longer strings (if characters are equal)?

less than

ex. banana < bananarama

24
New cards

What does str1.compareTo(str2) output when str1 is less than str2?

A negative number

25
New cards

What boolean expression to check if str1 is less than str2?

str1.compareTo(str2) < 0

26
New cards

What does str1.compareTo(str2) output when str1 is equal to str2?

0

27
New cards

What boolean expression to check if str1 is equal to str2?

str1.compareTo(str2) == 0

28
New cards

What does str1.compareTo(str2) output when str1 is greater than str2?

Positive number is returned

29
New cards

What boolean expression to check if str1 is greater than str2?

str1.compareTo(str2) > 0

30
New cards

What method checks if two strings are equal to each other while ignoring case?

str1.equalsIgnoreCase(str2)

31
New cards

What method compares two strings while ignoring case?

str1.compareToIgnoreCase(str2)

32
New cards

What is an index?

Each string position number

33
New cards

What does an index start with?

0

34
New cards

What method determines the character at a specified index?

charAt()

35
New cards

What would someString.charAt(x) output?

Outputs the character of someString at index x

36
New cards

When using charAt() method, what happens when an index is inputted that doesn’t exist?

Error is outputted

37
New cards

How to find the last character index if the length is known?

Length - 1

38
New cards

What method returns the strings length?

string.length()

39
New cards

What would this do?-

stringVariable + “hello”


It would return a new string that adds hello to the end of stringVariable

40
New cards

What would this do?-

stringVariable1 += stringVariable2


It would concatenate stringVariable2 to the end of stringVariable1

41
New cards

What method concatenates two strings?

concat()

42
New cards

What would s1.concat(s2) return?

Return a new string that adds s2 to the end of s1

43
New cards

Would this code change s1 variable?-

s1.concat(s2)

No

44
New cards

How to change this code so that s1 is changes to the concatenated version-

s1.concat(s2)

s1 = s1.concat(s2)

45
New cards

What should all character method be preceded by?

Character.

46
New cards

What method checks if a character is a letter?

Character.isLetter(‘ ‘)

47
New cards

What method checks if a character is a digit?

Character.isDigit(‘ ‘)

48
New cards

What method checks if a character is whitespace?

Character.isWhitespace(‘ ‘)

49
New cards

What method uppercases a character?

Character.toUpperCase(‘ ‘)

50
New cards

What method lowercases a character?

Character.toLowerCase(‘ ‘)

51
New cards

What character finds the index of a string or character inside a string?

indexOf()

52
New cards

What is the syntax of indexOf()?

stringVariable.indexOf(textToFind)

53
New cards

What does indexOf() return?

Returns index of first occurrence of the character or string

54
New cards

What does indexOf() return when the string or character is not found?

-1

55
New cards

What does this return?-

s1.indexOf(textToFind, startIndex)

Returns the index of the first occurence of textToFind starting at index startIndex

56
New cards

What method returns the index of the last occurence of the text to find?

s1.lastIndexOf(textToFind)

57
New cards

What method extracts a specific portion of a string and returns it as a new string object?

substring()

58
New cards

What is the basic syntax of substring() method?

s1.substring(startIndex, endIndex)

59
New cards

What does s1.substring(startIndex, endIndex) return?

Returns substring starting from index startIndex to index (endIndex - 1)

(endIndex is exclusive)

60
New cards

What does s1.substring(startIndex) return?

Returns substring of s1 starting from index startIndex to end of the string

61
New cards

What method can replace a string with text?

replace()

62
New cards

What is the basic syntax of the replace() method?

s1.replace(oldText, newText)

63
New cards

What type of data is a string?

String is a class

64
New cards

What each created String defined as?

A class object

65
New cards

What is a String variable?

A named object of the String class

66
New cards

What does it mean that String variables are a reference?

It is a variable that holds a memory address, not the String itself

67
New cards

What does comparing two Strings using the == operator do?

It is comparing the computer memory locations/address number of the strings. Dont do it!!!

68
New cards

If String a = “Hi!” and String b = a, what is the output of a == b?

True, since it is comparing the same memory address

69
New cards

What is a null String?

String variable that is assigned to null keyword. Object is referred to nothing in memory

70
New cards

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

71
New cards

What is the Integer.parseInt() method?

Takes a String argument and returns its integer valeu

72
New cards

What is the Double.parseDouble() method?

Takes a String argument and returns its double value

73
New cards

How to create a StringBuilder object?

StringBuilder sb1 = new StringBuilder(“ “);

74
New cards

What is the append() method?

Adds characters to the end of a StringBuilder object

sb1.append(“textToAdd”);

75
New cards

What is the insert() method?

Adds characters at a specific location within a StringBuilder object

76
New cards

What is the setCharAt() method?

Changes a character at a specified position within a StringBuilder object

77
New cards

What is the difference in bits of a lowercase and uppercase character?

Only one bit

78
New cards

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.

79
New cards

What would this code output?-

int integer = ‘a’ + 5;

The unicode of ‘a’ + 5

80
New cards

What would this code output if letter = ‘a’-

char anotherLetter = letter + 5;

It would be an error

81
New cards

What is the long way to declare a String?

String newStringvar = new String(“text”);

82
New cards

What is an iteration?

Each time through a loop’s statements

83
New cards

What is a while loop?

Program that repeatedly executes a list of sub-statements while the loop’s expression evaluates to true

84
New cards

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"

}


85
New cards

For a while loop, where are the two places you can first get input into a variable?

  1. At the end of loop body

  2. before the loop


86
New cards

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

87
New cards

What is an infinite loop?

A loop that never stops iterating?

88
New cards

What are two common causes of an infinite loop?

  1. Forgetting to update a variable

  2. Creating a loop expression whose evaluation to false isn’t reachable


89
New cards

What is a sentinel value?

Special value indicating the end of a list

90
New cards

What is a do-while loop?

A loop that first executes the loop body’s statements, then checks the loops condition

91
New cards

What is the format of a do-while loop?

  • do {

  •    // Loop body

  • } while (loopExpression);


92
New cards

What is the minimum amount of times a do-while loop will iterate?

Will iterate at least once

93
New cards

What is a for loop?

Loop with variable initialization, loop expression, and loop variable update

94
New cards

What is the format of a for loop?

for (initialExpression; conditionExpression; updateExpression) {

// Loop body

}

95
New cards

What is the increment operator?

++variable, increments the variable by 1

96
New cards

What is the decrement operator?

- - variable, subtracts the variable by 1

97
New cards

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

98
New cards

What is a common use of nested loops?

Can be used to generate all combinations of some items

99
New cards

How to find inner loop total iterations?

outer loop iterations x each inner loop iteration

100
New cards

What is a break statement?

break;

causes immediate exit of loop