java_1 Flashcards

(10 cards)

1
Q

What is a Garbage Collector

A

The garbage collector in Java automatically periodically scans the heap memory to find objects that aren’t being used. The process of garbage collection involves several steps, including marking, sweeping, and compacting.

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

Given the following code, what will be the result

Map<String, Car> cars = Stream.of(
                        new Car.CarBuilder().mark("BMV").mileage(100).build(),
                        new Car.CarBuilder().mark("Dacia").mileage(106660).build(),
                        new Car.CarBuilder().mark("Honda").mileage(0).build(),
                        new Car.CarBuilder().mark("Honda").mileage(100).build()).
                        collect(Collectors
                        .toMap(Car::getMark, Function.identity()));
A

Runtime exception

as toMap() calls uniqKeysMapMerger() which throws exception if it encounters on already existing key

            V u = m1.putIfAbsent(k, v);
            if (u != null) throw duplicateKeyException(k, u, v);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will be the output?

        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        System.out.println(map.get("C"));
A

null

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

What is deadlock?

A

Deadlock is situation when multiple threads are waiting for the mutually locked resources.

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

Which of the given Stream API methods is not terminal?
reduce
collect
map
forEach

A

map

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

Select the right option to transform Stream of countries into the Map<Continent, List<Country>>
Stream<Country> countries = getCountries();
Map<Continent, List<Country>> countriesByContinent = countries.???</Country></Country></Country>

A

Map<Continent, List<Country>> countriesByContinent =
countries.collect(Collectors.groupingBy(Country::getContinent));</Country>

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

What are correct statements about a database index?
- index makes inserts and updates faster
- index makes searches faster
- index makes searches slower
- index makes inserts and updates slower

A

index makes searches faster
index makes inserts and updates slower

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

Which of the following collections in Java preserve the insertion order
ArrayList
HashMap
TreeSet
LinkedList
LinkedHashMap

A

ArrayList

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

Select all the statements that return true

   System.out.println("String" == new String("String"));
    System.out.println("String".equals("String"));
    System.out.println("String" == "String");
    System.out.println("String".equals(new String("String")));
A

System.out.println(“String”.equals(“String”));
System.out.println(“String” == “String”);
System.out.println(“String”.equals(new String(“String”)));

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

You are running an web application on production. Usually it responds to web requests in 200 ms. However, you are getting a complaint that once in every 2 minutes its responds for 1 second. What might be the reason for it?

A

In a Java-based web application, a 1-second pause every ~2 minutes is highly indicative of a full garbage collection (Full GC).

Other (less likely) possibilities:
Background thread spikes (e.g., scheduled jobs, health checks, batch tasks)

Database connection pool saturation

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