Java Flashcards

1
Q

List is Child of

A

Collection

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

How do you convert a long to a String

A

Long.toString()

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

how do you make a two-dimensional array

A

int [][] hi=new int[m][n];

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

How do you reverse a list

A

Collections.reverse(list)

it reverses the input for you! it

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

List is an interface, so you cant make a new list, who are the children?

A

ArrayList,

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

how do you compare two strings lexicographically?

A

public int compareTo(String str)

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

How to convert a character to an integer in java?

A

Character.getNumericValue(c);

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

How to convert an integer to a character in java?

A

int REDIX = 10;

char c = Character.forDigit( a, REDIX );

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

How to sort with a comparable?

A
Collections.sort(persons, new Comparator() {
            public int compare(Person person, Person person1) {
                int name = person.getName().compareTo(person1.getName());
                if(name == 0){
                    return name;
                }
                return person.getAge() > person1.getAge() ? 1 : person.getAge() < person1.getAge() ? -1 : 0;
        }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you read input in Java, only tell me 3 methods and their advantages:

A

1.Using Buffered Reader Class: efficient reading
2. Using Scanner Class: Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
Regular expressions can be used to find tokens.
3. Using Console Class, for not echoing password and stuff, and it’s synchronized.

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

How do you read from a Buffered Reader Class?

A
BufferedReader reader =  
                   new BufferedReader(new InputStreamReader(System.in)); 
    String name = reader.readLine();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

String vs StringBuilder vs StringBuffer in Java, when to use which?string,

A
If a string is going to remain constant throughout the program, then use String class object because a String object is immutable.
If a string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how do you concatenate with a String buffer or a String Builder?

A

.append();

.toString() at the end;

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

can you append an integer to string buffer or String Builder?

A

yes you can append, long, double, float, int, boolean,

String and

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

What is the easiest way to reverse a string in java?

A
using string buffer or String Builder!
String s="khello";
StringBuffer sb=new StringBuffer(s);
String rev=sb.reverse.toString();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we check strings equality?

A

equals

17
Q

how do you get a substring of a string?

A
String substring(begIndex, endIndex)
The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.
18
Q

How can we know a character is alphanumeric?

A

Character.isLetterOrDigit

19
Q

get the highest and the lowest element in a treeSet?

A
  1. pollFIrst()

2. pollLast() -> highest

20
Q

how to get the min and max value?

A

Integer.MIN_VALUE

Integer.MAX_VALUE

21
Q

how do you use max or min-heap in Java?

A

We use PriorityQueue class to implement Heaps in Java. By default Min Heap is implemented by this class. To implement Max Heap, we use Collections.reverseOrder()

22
Q

How to convert a set into an Arraylist in java?

A

we make a new ArrayList(set);