java_1 Flashcards
(10 cards)
What is a Garbage Collector
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.
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()));
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);
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"));
null
What is deadlock?
Deadlock is situation when multiple threads are waiting for the mutually locked resources.
Which of the given Stream API methods is not terminal?
reduce
collect
map
forEach
map
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>
Map<Continent, List<Country>> countriesByContinent =
countries.collect(Collectors.groupingBy(Country::getContinent));</Country>
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
index makes searches faster
index makes inserts and updates slower
Which of the following collections in Java preserve the insertion order
ArrayList
HashMap
TreeSet
LinkedList
LinkedHashMap
ArrayList
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")));
System.out.println(“String”.equals(“String”));
System.out.println(“String” == “String”);
System.out.println(“String”.equals(new String(“String”)));
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?
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