Java Base Flashcards

1
Q

What is SOLID?

A
SOLID is development principle in Java
S - Single Responsibility
O - Open-closed
L - Liskov Substitution 
I - Interface Segregation 
D -  Dependency Inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Single Responsibility principle

A

One class should have only one and only responsibility

It should only have one reason to change.

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

Single Responsibility principle benefit

A

Testing – A class with one responsibility will have far fewer test cases

Lower coupling – Less functionality in a single class will have fewer dependencies

Organization – Smaller, well-organized classes are easier to search than monolithic ones

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

Open-closed principle

A

Open for Extension, Closed for Modification

Extend classes for modification

Benefit: In doing so, we stop ourselves from modifying existing code and causing potential new bugs

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

Liskov Substitution

A

If class A is a subtype of class B, then we should be able to replace B with A without disrupting the behavior of our program.

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

Interface Segregation

A

Larger interfaces should be split into smaller ones.

By doing so, we can ensure that implementing classes only need to be concerned about the methods that are of interest to them.

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

Dependency Inversion

A

The principle of Dependency Inversion refers to the decoupling of software modules.

This way, instead of high-level modules depending on low-level modules, both will depend on abstractions.

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

Difference between String str = “String” and String str = new String(“String”);

what is difference between String literal and String object in Java?

A

Both expression gives you String object, but there is subtle difference between them.

When you create String object using new() operator, it always create a new object in heap memory.

On the other hand, if you create object using String literal syntax e.g. “Java”, it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it’s already exists. Otherwise it will create a new string object and put in string pool for future re-use. In rest of this article, why it is one of the most important thing you should remember about String in Java.

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

String a = “Java”;
String b = “Java”;

System.out.println(a == b);

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
String c = new String("Java"); 
String d = new String("Java"); 

System.out.println(c == d);

A

False

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

Checked and unchecked exceptions

A

The main difference between checked and unchecked exception is that the checked exceptions are checked at compile-time while unchecked exceptions are checked at runtime.

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

What are checked exceptions?

A

Checked exceptions are checked at compile-time.

It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.

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

What are Unchecked exceptions?

A

Unchecked exceptions are not checked at compile time.

It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error.

Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class.

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

Difference between exception and error.

A

Error mostly is thrown by JVM

Exception is thrown by Application

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

Error

A

An Error “indicates serious problems that a reasonable application should not try to catch.”

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

Exception

A

An Exception “indicates conditions that a reasonable application might want to catch.”

17
Q

HashMap

A

A hash code table (array) of a map key and value

18
Q

Hashmap vs Hashtable

A
  1. HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
  2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
  3. HashMap is generally preferred over HashTable if thread synchronization is not needed
19
Q

Why HashTable doesn’t allow null and HashMap does?

A

To successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method.

Since null is not an object, it can’t implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later.