Intermediate Flashcards

1
Q

What are arrays in java?

A

An array is an object containing a fixed number of values of the same type

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

What are varargs?

A
  • Varargs are used to pass an arbitrary number of arguments to a method but we can also pass an array directly as the argument
  • Each method can only have one varargs parameter and the varargs parameter needs to be the last argument
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the disadvantage of varargs?

A

They can lead to Heap Pollution

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

What does the static keyword mean?

A

The static keyword means that the particular member belongs to a type itself, rather than to an instance of that type

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

What is important about static fields (class variables)?

A

A SINGLE copy of that field is created and shared among all instances of that class, this static variable is stored in the heap memory

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

When to use static fields?

A
  • When the value of the variable is independent of objects
  • When the value is supposed to be shared across all objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is important about static methods?

A
  • Static methods belong to a class not an object and cannot be overwritten
  • Static methods cannot use this or super keywords
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When to use static methods?

A

Static methods are used to access or manipulate static variables and other static methods that do not depend upon objects; static methods are widely used in utility and helper classes

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

What is the benefit of static classes?

A

A static class allows to create a class within a class and provides a way of grouping elements that are only used in one place -> keeps the code more organized and readable

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

In what two types can nested class architecture be divided?

A
  1. Nested classes that are declared as static are called static nested classes
  2. Nested classes that are non-static are called inner classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between inner classes and static nested classes?

A

Inner classes have access to all members of the enclosing class, whereas static nested classes only have access to static members of the outer class

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

Why to use static nested classes?

A
  • Grouping classes that will be used only in one place increases encapsulation
  • Code is closer to the place where it will be used -> Increases readability, and maintainability
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are enums?

A
  • The keyword enum denotes a special type of class that always extends the java.lang.Enum class
  • Enum is short for enumerations
  • Basic example for enum is days of the week
  • Enum declaration looks similar to a class declaration
  • Enum entries are typically written in uppercase
  • Enums can have fields and a constructor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are implementation design patterns using enums?

A
  • Singleton Pattern
  • Strategy Pattern
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the final keyword mean?

A
  • The final keyword allows to set limitations on extensibility -> classes marked as final cannot be extended
  • The final keyword in a class declaration does not mean that the objects of this class are immutable, we just cannot extend it
  • Methods marked as final cannot be overwritten
  • Variables marked as final cannot be reassigned -> once a final variable is initialized, it cannot be changed
  • A final reference variable cannot be reassigned but that does not mean that the object it refers to is immutable -> the properties of this object can be changed freely
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the aim of generics?

A

The aim of generics is to reduce bugs and add an extra layer of abstraction over types

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

When are generics useful?

A

Generics are useful if you would run into a code duplication like e.g. an IntegerPrinter class or a StringPrinter class where all classes would hold the exact same code only the types of the variables would be different -> What we want is one class that is flexible for many different types

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

What are generic wildcards?

A

Todo is to print a list containing any type of thing -> Use a ? as a type parameter in the angle brackets

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

What is one restriction of generics in java?

A

One restriction is that the type parameter cannot be a primitive type, because generics are a compile-time feature, meaning the type parameter is erased and all generic types are implemented as type object

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

How to read and write user input in java?

A
  • Reading from system.in
  • Writing to system.out
  • Using the console class for input and output
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are checked exceptions?

A
  • Checked exceptions represent errors outside the control of the program
  • They are verified at compile-time
  • The throws keyword is used to declare a checked exception
22
Q

What are unchecked exceptions?

A
  • Unchecked exceptions reflect some error inside the program logic
  • They are not verified at compile-time
23
Q

What are common unchecked exceptions?

A

NullpointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException

24
Q

How to remember when to use checked and unchecked exceptions?

A

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception

25
Q

What are the main reasons for introducing custom exceptions?

A
  • Business logic exceptions -> exceptions that are specific to the business logic and workflow. These help the application users or developers to understand what the exact problem is
  • To catch and provide specific treatment to a subset of existing java exceptions
26
Q

How to create custom checked and unchecked exceptions?

A
  • Custom checked exceptions have to extend the java.lang.Exception class
  • Custom unchecked exceptions have to extend the java.lang.RuntimeException
27
Q

What is try with resources?

A

Try with resources allows us to declare resources to be used in a try block with the assurance that the resources will be closed after the execution of that block

28
Q

What is an effectively final variable?

A

Effectively final means the variable does not change after the first assignment

29
Q

When are the Comparator and Comparable interfaces useful?

A

When working with custom types, or trying to compare objects that are not directly comparable, we need to make use of a comparison strategy -> Make use of Comparator and Comparable interfaces

30
Q

What is Comparable?

A

Comparable is an interface defining a strategy of comparing an object with other objects of the same type. This is called the classes “natural ordering”

31
Q

What is Comparator?

A

The comparator interface defines a compare(arg1, arg2) method with two arguments that represent compared objects, and works similarly to the Comparable.compareTo() method

32
Q

When to use Comparator or Comparable?

A
  • Comparable interface is a good for defining the default ordering, it is the main way of comparing objects
  • Sometimes we cannot modify the source code of the class whose objects we want to sort, thus making the use of Comparable impossible -> Comparator allows us to avoid additional code to our domain classes
33
Q

What is a list?

A

A list represents an ordered sequence of values where some value may occur more than one time

34
Q

What is an arraylist?

A

An arraylist is one of the list implementations built atop an array, which is able to dynamically grow and shrink as you add and remove elements

35
Q

What is a linkedlist?

A

A linkedlist is a doubly-linked list implementation of the list and dequeue interfaces which implements all optional list operations and permits all optional list operations and permits all elements including null

36
Q

What is the difference between arraylist and linkedlist?

A
  • Arraylist is faster in random access than linkedlist
  • Insertion, addition and removal operations are faster in linkedlist
  • linkedlist consumes more memory
37
Q

What are features of a hashset?

A
  • It stores unique elements and permits null
  • It is backed by a hashmap
  • It does not maintain insertion order
  • It is not thread-safe
38
Q

When is a hashset useful?

A

Useful whenever you want a collection of your elements without any duplicates and the particular order is not important

39
Q

What is a typical use case for hashset?

A

Removing duplicate items from a list

40
Q

What are other possible set implementations?

A
  • TreeSet
  • LinkedHashSet
41
Q

How does a hashset maintains uniqueness?

A
  • When we put an object into a hashset, it uses the object’s hashcode value to determine if an element is not in the set already
  • Each hashcode value corresponds to a certain bucket location which can contain various elements, for which the calculated hash value is the same. But two objects with the same hashcode might not be equal. So, objects within the same bucket will be compared using the equals() method
42
Q

What is the rule of thumb for the performance of a hashset?

A
  • A high initial capacity is good for a large number of entries coupled with little to no iteration
  • A low initial capacity is good for few entries with a lot of iteration
43
Q

What is a hashmap?

A

Hashmap is a map with key-value mapping, which means that every key is mapped to exactly one value that we can use the key to retrieve the corresponding value from a map

44
Q

What is the advantage of a hashmap?

A

The advantage of a hashmap is that the time complexity to insert and retrieve a value is O(1) on average

45
Q

What is an iterator and what are its pros and cons?

A
  • An iterator is one of many ways we can traverse a collection
  • Iterators made it possible to remove elements from a collection we are iterating over
  • Iterators do not guarantee the iteration order
46
Q

What is an anonymous inner class?

A
  • Is like a nested class without a name; it provides a class definition and instantiates it at the same time
  • Implements functional interfaces and abstract classes without creating additional sub-classes
47
Q

What is a lambda expression?

A
  • Implements functional interface
  • Is essentially a method definition without a name
  • Makes code more concise and readable
  • Provides a way to pass a function as a method argument
48
Q

Anonymous class vs. lambda expression?

A
  • Anonymous classes are used for interfaces and abstract classes
  • Lambda expressions are only used for functional interfaces
49
Q

What are functional interfaces?

A

Functional interfaces are interfaces that have exactly one method

50
Q

How is the performance of lambda expressions compared to anonymous classes?

A

Lambda expression is better compared to the anonymous class in terms of performance because an anonymous class lead to an extra class file on the compilation, this takes additional time during class loading and verification during runtime