Miscellaneous Flashcards

1
Q

What is the ‘instanceof’ keyword for?

A

You can use it to check the type of objects, like this:
if (object instanceof String)
System.out.print(“object is a string”);

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

How can you read the input: “10 2 4 5 10” ?

Can you use input.nextInt(); ?

A

You can’t use .nextInt(), it will only get the first integer.
You CAN use input.nextLine(), which will read everything into a single string. You can later convert into integers if you wish

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

How do you convert from String to int?

A

Integer.parseInt(str). For example:

int number = Integer.parseInt(“333”);

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

What is the syntax of a ‘for-each’ loop?

A

for (double element: myList) {
System.out.println(element);
}
Iterating through a list like this is easier if all you need is the elements in the list.

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