Strings, Exceptions, and Files Flashcards

1
Q

How do you change the case of a String?

A

String s = “string”;
s.toLowerCase();
s.toUpperCase();

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

What does String1.compareTo(String2) do?

A

it tells you which is first in “alphabetical order”. “Less” or “greater” is determined by the numeric codes (ASCII or Unicode) of the characters in that position. Uppercase is always less than lowercase

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

How could you find one String within another?

A

string1.indexOf(string2);

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

How can you remove leading/trailing whitespace from a String?

A

s.trim();

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

How could you find what characters are between two indices?

A

String1.substring(start, end)

start is the position of the first character you want
end is the position of the first character you don’t want

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

What does String[] split(String) do?

A

break up a String into an array of Strings at the positions of the given substring
“Test 12 34”.split(“ “) gives {“Test”, “12”, “34”}

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

What are Exceptions?

A

represent problems that arise during execution, which a program might want to deal with. Exceptions are objects that give information about what went wrong and where in the program it went wrong.

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

What are some examples of exceptions?

A
  • 5/0 gives an Arithmetic Exception
  • array[-1] = 0 gives an
    ArrayIndexOutOfBoundsException
  • Integer.parseInt(“Not a number”) gives a NumberFormatException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the methods that every exception object has?

A
  • String getMessage() - find more about what happened
  • void printStackTrace() - print out where the program was at the time this exception object was created
  • String toString() - the name of the exception and its message
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are Errors in Java

A

As opposed to Exceptions, Errors are more serious problems that a program is not supposed to manage/handle. When it happens, the program will terminate with a description of the error.

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

What happens when you use an Exception?

A

When something goes wrong, you can “throw” an exception. This object is now automatically returned up the stack, immediately terminating all of these methods until it hits something that “catches” it.

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

How do you use an Exception in a method?

A

all methods that throw an exception or call a method that does this have “throws Exception” at the end of the signature unless the method catches the exception itself.

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

What is the format of a try-catch?

A

try {
… statements
}
catch(TypeOfException e) {
… do this if an exception happens
}
catch(OtherTypeOfException ae) { … }
finally {
… do this every time, whether try successfully completed or an exception was thrown and caught
}
… continue unless some un-caught exception occurs

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

What are RuntimeExceptions?

A

unchecked exceptions. You don’t need to catch them, or declare in your method signature that it could throw them. RuntimeExceptions are thrown when the programmer makes a mistake and calls a method incorrectly. They happen during runtime

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

What are checked exceptions?

A

Types of exceptions that must be handled wherever they may occur in your program; otherwise, your program will not compile

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

What type of exception should you check for when using a fileReader?

A

FileNotFoundException
(java.io)

17
Q

What type of exception should you check for when parsing strings?

A

NumberFormatException
(java.lang)

18
Q

What type of exception should you check for when writing files?

A

IOException
(java.io)

19
Q

How do you read input from a file?

A

FileReader fileReader = new FileReader(filename);
Scanner in = new Scanner(fileReader);
in.nextLine();
in.close();

20
Q

How do you write input to a file?

A

FileWriter fileWriter = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(“”);
out.close();