Strings and Chars Flashcards

1
Q

What are Strings?

A

A data type used for storing text; a collection of characters that maintain their order

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

What characters are valid in a string?

A

All characters are valid, but to get certain characters you must use escape sequences (eg \n)

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

What are the escape sequences?

A

”: "
:
new line: \n
tab: \n
and more

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

In Java, what types can be turned into a String?

A

All types

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

What is concatenation?

A

Adding up strings to make a longer string using the + or += symbols

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

How does the compiler decide when to do addition or concatenation?

A

The operation a + b only means addition if both a and b are numbers of some kind. Otherwise, if the other side is not a String, it is converted to one

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

What are objects?

A

Data types that begin with a capital letter; larger chunks of data made up of the primitive data types

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

What is stored on the stack when you use objects? Why?

A

A reference to the data’s location on the heap. At compile-time, the computer doesn’t know how much space an object will need.

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

What is the function in processing that lets you draw text on the canvas?

A

text(String, x, y);
(draws a string with its lower left corner at (x, y). Characters are mostly above the y value, but characters with descenders will go lower.)

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

What are the other text functions in processing?

A

textSize(p): sets the size of the text, in pixels
fill(): controls the text colour
textWidth(String): how many pixels wide will it be (based on textSize)
textAscent(): how many pixels “above the line”
textDescent(): how many pixels “below the line”

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

How can you get user input in your processing programs?

A

Using JOption Pane.
Add import javax.swing.JOptionPane; at the very top of your program, and use JOptionPane.showInputDialog(“prompt”) to get a String from the user

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

How can you turn something else into a String?

A

use str(x) (this is done automatically for functions that have strings as arguments)

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

How can you change a String containing characters that form a valid int or float literal, how can you obtain an actual int or float?

A

int() and float() ((int)string and (float)string don’t work)

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

What happens if you try to convert a string that is not a valid number into a number data type?

A

it will print NaN

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

Describe the structure of a string

A

A string is made up of a number of characters. Each character has a specified “index” (box or slot). The index of the first character in the String is 0

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

How should you compare strings? Why?

A

You can’t use == on Strings because they are objects, not a primitive data type, so you would just be checking if the memory addresses are the same. Instead you should use string.equals(String) or string.equalsIgnoreCase(String)

17
Q

How can you find out the length of a String?

A

string.length();

18
Q

How can you find a character at a specific index in a String?

A

string.charAt(int)

19
Q

What is the char type?

A

used to hold one single character; a primitive type (traditionally 1 byte, but occupies 2 bytes in Processing)

20
Q

Is there a difference between how you must write char literals and String literals?

A

‘x’ is a char, “x” is a String

21
Q

What must you write to get ‘ as a char?

A

’'’

22
Q

Why can you convert back and forth between a char and an int?

A

A char is simply an integer underneath (ASCII or UNICODE), and it is smaller than any other numeric type, so you can do an implicit cast from char to int.

23
Q

What are the built-in functions to check for specific kinds of characters?

A

boolean Character.isLetter(char c);
boolean Character.isUpperCase(char c);
boolean Character.isLowerCase(char c);
boolean Character.isDigit(char c);

24
Q

How can you convert from a character to a string?

A

the str() command

25
Q

What is the built-in key variable?

A

a char that tells us which key is being pressed