Coding Interview Questions Flashcards

1
Q

Copy char[] to String

  1. char[].toString()
  2. new String(char[])
  3. String.valueOf(char[])
A
  1. new String(char[])

3. String.valueOf(char[])

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

Which one of the two is correct?

  1. int[] sequence = new int[]{0, 1};
  2. int[] sequence = {0, 1};
A

Both 1 and 2 are correct.

  1. int[] sequence = new int[]{0, 1};
  2. int[] sequence = {0, 1};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Initialize a List with values:

  1. ArrayList list = new ArrayList(); list.add(item1);
  2. ArrayList list = new ArrayList() { item1 };
A
  1. ArrayList list = new ArrayList();
    list.add(item1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

public class Test {
class A { }
static class B { }
public static void main(String[] args) {
A a = new A();
B b = new B();
}
}
1. A instantiation will compile, no instance of Test is needed to instantiate A
2. B instantiation will compile, no instance of Test is needed to instantiate B
3. B will fail, you need an instance of Test to instantiate B
4. A will fail, you need an instance of Test to instantiate A

A
  1. B instantiation will compile, no instance of Test is needed to instantiate B
  2. A will fail, you need an instance of Test to instantiate A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Get List size given List list;

  1. list.length;
  2. list.size();
A
  1. list.size()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Initialize a List:

  1. List nodes = new ArrayList<>();
  2. List nodes = new List<>();
A
  1. List nodes = new ArrayList<>();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which one is correct?

  1. O(nlog(n))
  2. O(n log n)
A
  1. O(nlog(n))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to sort an ArrayList?

  1. arrayList.sort()
  2. Collections.sort(arrayLIst);
A
  1. Collections.sort(arrayLIst);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Get character counts from map Map

  1. map.merge(chr, 1, Integer::sum);
  2. map.put(chr, map.getOrDefault(chr, 0) + 1);
A

Both 1 and 2 are correct

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

Traverse Character type chars in string

  1. for (int i = 0; i < s.length(); i++) { s.charAt(i); }
  2. s.chars().forEach(chr -> { })
  3. for (Character chr : s) { }
A
  1. for (int i = 0; i < s.length(); i++) { s.charAt(i); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Add key value to map

  1. map[key] = value;
  2. map.put(key, value);
A
  1. map.put(key, value);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The ‘a’ char code is

  1. 79
  2. 97
  3. 48
  4. 84
A
  1. 97
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The ‘0’ (zero) char code is

  1. 48
  2. 84
  3. 56
  4. 65
A
  1. 48
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The ‘A’ char code is

  1. 97
  2. 79
  3. 65
  4. 56
A
  1. 65
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The best conceivable runtime means

  1. it is the best time you can conceive that the problem could possibly be solved in, and it is definitely impossible for the problem to be solved faster than that.
  2. you have an algorithm that solves the problem, and in the best case, that algorithm has a particular time complexity.
  3. you are feeding the worst possible input (of that size) into your algorithm
A
  1. it is the best time you can conceive that the problem could possibly be solved in, and it is definitely impossible for the problem to be solved faster than that.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

String input = …;

  1. char chr = Input.charCodeAt(4);
  2. int chr = Input.charCodeAt(4);
  3. char chr = input.charAt(4);
  4. int chr = input.charAt(4);
A
  1. char chr = input.charAt(4);
17
Q

Select valid statements

  1. Int codePoint = fifthCharl
  2. int codePoint = (int) fifthChar
  3. char opp = (char);
  4. int opp = (char);
A
  1. int codePoint = (int) fifthChar

3. char opp = (char);

18
Q

Are the following expressions valid? (Yes/No)

  1. ‘z’ - ‘a’
  2. ‘a’ + int
A

Yes

19
Q
Why is the following valid?
char c = 'a' + 10;
But, not the following?
int i = 10;
char c = 'a' + i;
A

Because the compiler can check that it (‘a’ + 10) is within the bounds of a char whereas it cannot (in general) check that ‘a’ + is within the bounds.

20
Q

What is the result of evaluating the following?
26 % 26

  1. 26
  2. 0
  3. 1
A
  1. 0
21
Q
What is the result of evaluating the following?
27 % 26
1. 26
2. 0
3. 1
A
  1. 1
22
Q
What is the result of evaluating the following?
25 % 26
1. 25
2. 0
3. 1
A
  1. 25
23
Q
  1. array.length;

2. array.length();

A
  1. array.length;
24
Q

Math.floor returns

  1. integer
  2. double
  3. float
A
  1. double
25
Q
  1. MIN_VAL
  2. Integer.MIN_VALUE
  3. Integer.MIN_VAL
A
  1. Integer.MIN_VALUE
26
Q
  1. new Map<>{};
  2. new Map<>();
  3. new HashMap<>{};
  4. new HashMap<>();
A
  1. new HashMap<>();
27
Q
  1. map.contains(key);
  2. map.has(key);
  3. map.containsKey(key);
A
  1. map.containsKey(key);
28
Q
  1. static method cannot be referenced from a static context
  2. non-static method cannot be referenced from a non-static context
  3. non-static method cannot be referenced from a static context
  4. static method cannot be referenced from a non-static context
A
  1. non-static method cannot be referenced from a static context
29
Q

In a graph

  1. vertices are the nodes and edges are the connections
  2. vertices are the connections and edges are the nodes
A
  1. vertices are the nodes and edges are the connections
30
Q
  1. Trees are graphs that contain cycles.

2. Trees are graphs that do not contain even a single cycle.

A
  1. Trees are graphs that do not contain even a single cycle.
31
Q

In a graph, acyclic refers to:

  1. contains no cycles
  2. contains cycles
A
  1. contains no cycles
32
Q

DFS time complexity in a tree is:

  1. O(V + E)
  2. O(V)
  3. O(V^2)
A
  1. O(V)
33
Q

DFS time complexity in a graph is;

  1. O(V + E)
  2. O(V)
  3. O(V^2)
A
  1. O(V + E)
34
Q

List is interchangeable with ArrayList?

  1. True
  2. False
A
  1. False
35
Q

Object element

  1. element instanceof ArrayList
  2. element typeof ArrayList
A
  1. element instanceof ArrayList
36
Q

Cast Object to List given: Object obj;

  1. Arrays.asList(obj)
  2. (List) obj
A
  1. (List) obj
37
Q

Which one is considered better?

  1. List myList = new ArrayList();
  2. ArrayList myList = new ArrayList();
A
  1. List myList = new ArrayList();
38
Q
Which one is correct to transform a resizable list of chars into a String?
  1. List chars = new ArrayList<>();
  chars.add(chr);
  return new String(chars);
  2. StringBuilder chars = new StringBuilder();
  chars.append(chr)
  return chars.toString();
A
  1. StringBuilder chars = new StringBuilder();
    chars.append(chr)
    return chars.toString();