Java Fundamentals Flashcards

1
Q

How does garbage collection work?

A

Unreferenced objects are identified and marked as ready for garbage collection.
Marked objects are deleted.
Memory may be compacted after the GC deletes objects so that the remaining objects are in a contiguous block at the start of the heap.

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

How is the heap divided in the HotSpot JVM?

A
  • Young gen (eden, survivor0, survivor1)
  • Old gen (tenured)
  • Perm gen
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is CMS GC?

A
  • Concurrent Mark Sweep
  • Concurrent and parallel
  • Designed to minimize pauses (good choice for responsive apps - e.g. GUIs)
  • G1 is designed to replace CMS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Parallel GC?

A
  • Efficient but can result in long pauses due to compaction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Options for GC performance tuning

A

Use compiler flags

  • Change GC (CMS or G1 instead of parallel)
  • Change the initial and maximum size of the heap
  • Change the size of heap sections
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Static variable

A

A variable shared by every instance of that class

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

Static methods

A

Methods that manipulate only static variables

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

Static class

A

Inner class not tied to enclosing class

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

Static keyword

A

Used around a block of code to specify code that runs when the JVM is first started before instances of the class are created. Often used to initialize static variables.

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

Java void vs null

A

Void means a method does not return a result. Null means a variable doesn’t reference a value or an object.

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

Difference between interface and abstract class.

A

An interface is a contact. It specifies method signatures only. Prior to Java 8, it was not possible to include method implementations in an interface. In Java 8, it’s possible to add a method implementation to an interface using the default keyword.

Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).

When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an abstract class can extend another abstract class and abstract methods from the parent class don’t have to be defined.

Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.

A single class can only inherit from one parent class. A single class can implement several interfaces.

A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.

A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

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

Java interface default methods

A

Beginning with Java 8, interface methods can include an implementation using the default keyword. The ability to add a default implementation makes it much easier to add a new method to an interface without having to update each implementing class separately.

If a class extends multiple interfaces that contain default methods with the same name, then that class must override the default methods. Otherwise, it will generate a compile-time error.

Eliminates the need for base classes that contain a default implementation.

If any the method of any class in the class hierarchy has the same name as an interface default method, the interface method is ignored. Default interface methods cannot be used to override class behavior.

Default interface methods were added in part to support adding lambda expressions to Java collections.

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

Java interface static methods

A

Implementation cannot be overridden by class methods. Good choice for implementing utility classes (e.g sorts).

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

Java 8 Functional Interfaces

A

In java.util.function package (e.g. Consumer, Supplier, Function, Predicate).

Previously, you needed to create an anonymous class in order to override a single method

Runnable r = new Runnable(){
			@Override
			public void run() {
				System.out.println("My Runnable");
			}};

Using lambda expression
Runnable r1 = () -> System.out.println(“My Runnable”);

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

Polymorphism

A

Means “many forms”. Declare a variable using a superclass class/interface type and a subclass/concrete class implementation.

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

Difference between HashMap and HashSet

A

HashSet
HashSet class implements the Set interface
In HashSet, we store objects(elements or values) e.g. If we have a HashSet of string elements then it could depict a set of HashSet elements: {“Hello”, “Hi”, “Bye”, “Run”}
HashSet does not allow duplicate elements that mean you can not store duplicate values in HashSet.
HashSet permits to have a single null value.
HashSet is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.[similarity]

                  add      contains next     notes HashSet               O(1)     O(1)     O(h/n)   h is the table  HashMap HashMap class implements the Map interface HashMap is used for storing key & value pairs. In short, it maintains the mapping of key & value (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This is how you could represent HashMap elements if it has integer key and value of String type: e.g. {1->”Hello”, 2->”Hi”, 3->”Bye”, 4->”Run”} HashMap does not allow duplicate keys however it allows having duplicate values. HashMap permits single null key and any number of null values. HashMap is not synchronized which means they are not suitable for thread-safe operations until unless synchronized explicitly.[similarity]
17
Q

Difference between HashTable and HashMap

A
  • HashTable is synchronized. HashMap is not. However, if you want a thread-safe, high concurrent implementation of a Map, use ConcurrentHashMap.
  • HashTable shipped with Java 1. It is a precursor to the the Map interface in Java collections and was later retrofitted to implement the Map interface.