Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-request.php on line 952

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-request.php on line 972

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-request.php on line 984

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-request.php on line 995

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home1/smartva9/public_html/smartvania/wp-includes/class-wp-block-list.php on line 199

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/smartva9/public_html/smartvania/wp-includes/class-wp.php on line 173

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/smartva9/public_html/smartvania/wp-includes/wp-db.php on line 3031

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637

Warning: Cannot modify header information - headers already sent by (output started at /home1/smartva9/public_html/smartvania/wp-includes/class-wp-theme.php:9) in /home1/smartva9/public_html/smartvania/wp-includes/rest-api/class-wp-rest-server.php on line 1637
{"id":590,"date":"2015-11-27T21:38:41","date_gmt":"2015-11-27T21:38:41","guid":{"rendered":"http:\/\/smartvania.com\/?p=590"},"modified":"2015-11-27T23:38:10","modified_gmt":"2015-11-27T23:38:10","slug":"java-interview-questions","status":"publish","type":"post","link":"https:\/\/smartvania.com\/java-interview-questions\/","title":{"rendered":"Java interview questions"},"content":{"rendered":"
\n
\n
\n\t\tThe Toptal Engineering Blog<\/a> has published a great post called
\n\t\t“
8 Essential Java Interview Questions<\/a>” on there you can submit your Java interview questions, answers and hire freelancers,
\n\t\tand they were happy to share with us these questions. If you would like to like them on Facebook click on the link
Toptal on Facebook<\/a>\n\t<\/div>\n
8 Essential Java Interview Questions<\/div>\n
Click on the question to see the answer<\/div>\n
\n
\n

The main distinction between fail-fast<\/strong> and fail-safe<\/strong>
\n\t\titerators is whether or not the collection can be modified while<\/em> it is being iterated.
\n\t\tFail-safe iterators allow this; fail-fast iterators do not.<\/p>\n<\/p><\/div>\n<\/div>\n

\n
\n

Of the three, LinkedList<\/code><\/a> is generally going to give you the best performance. Here\u2019s why:<\/p>\n

ArrayList<\/code><\/a> and
\n\t\t
Vector<\/code><\/a> each use an array to store the elements of the list.
\n\t\t As a result, when an element is inserted into (or removed from) the middle of the list, the elements that follow must all be shifted accordingly.
\n\t\t Vector<\/code> is synchronized, so if a thread-safe implementation is
\n\t\tnot<\/em> needed, it is recommended to use ArrayList<\/code> rather than Vector.<\/p>\n<\/p><\/div>\n<\/div>\n

\n
\n

In Java, Strings are immutable<\/a>
\n\t\tand are stored in the String pool. What this means is that, once a String is created, it stays in the pool in memory until being garbage collected.
\n\t\tTherefore, even after you\u2019re done processing the string value (e.g., the password), it remains available in memory for an indeterminate period of time thereafter
\n\t\t(again, until being garbage collected) which you have no real control over. Therefore, anyone having access to a memory dump can potentially extract the sensitive data and exploit it.<\/p>\n<\/p><\/div>\n<\/div>\n

\n
\n

A single ThreadLocal<\/code><\/a>
\n\t\tinstance can store different values for each thread independently. Each thread that accesses the get()<\/code> or set()<\/code> method of a
\n\t\tThreadLocal<\/code> instance is accessing its own, independently initialized copy of the variable. ThreadLocal<\/code> i
\n\t\tnstances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or transaction ID).
\n\t\tThe example below, from the
ThreadLocal<\/code> Javadoc<\/a>,
\n\t\tgenerates unique identifiers local to each thread. A thread\u2019s id is assigned the first time it invokes ThreadId.get()<\/code> and remains unchanged on subsequent calls.<\/p>\n

public class ThreadId {\r\n\t\t\t\/\/ Next thread ID to be assigned\r\n\t\t\tprivate static final AtomicInteger nextId = new AtomicInteger(0);\r\n\t\t\t\t\/\/ Thread local variable containing each thread's ID\r\n\t\t\tprivate static final ThreadLocal<Integer> threadId =\r\n\t\t\t\tnew ThreadLocal<Integer>() {\r\n\t\t\t\t\t@Override protected Integer initialValue() {\r\n\t\t\t\t\t\treturn nextId.getAndIncrement();\r\n\t\t\t\t}\r\n\t\t\t};\r\n\r\n\t\t\t\/\/ Returns the current thread's unique ID, assigning it if necessary\r\n\t\t\tpublic static int get() {\r\n\t\t\t\treturn threadId.get();\r\n\t\t\t}\r\n\t\t}\r\n\t\t<\/code><\/pre>\n<\/p><\/div>\n<\/div>\n
\n
\n

In Java, each thread has its own stack, including its own copy of variables it can access.
\n\tWhen the thread is created, it copies the value of all accessible variables into its own stack. The volatile<\/code>
\n\tkeyword basically says to the JVM \u201cWarning, this variable may be modified in another Thread\u201d.<\/p>\n

In all versions of Java, the volatile<\/code> keyword guarantees global ordering on reads and writes
\n\tto a variable. This implies that every thread accessing a volatile field will read the variable\u2019s current value instead of (potentially) using a cached value.<\/p>\n

In Java 5 or later, volatile<\/code> reads and writes establish a happens-before<\/a>
\n\trelationship, much like acquiring and releasing a mutex.<\/p>\n

Using volatile<\/code> may be faster than a lock, but
\n\tit will not work in some situations. The range of situations in which volatile is
\n\teffective was expanded in Java 5; in particular,
double-checked locking<\/a> now works correctly.<\/p>\n

One common example for using volatile<\/code> is for a flag to terminate a thread. If you\u2019ve started a thread, and you want to be able to safely interrupt it from a different thread, you can have the thread periodically check a flag (i.e., to stop it, set the flag to true<\/code>). By making the flag volatile, you can ensure that the thread that is checking its value will see that it has been set to true<\/code> without even having to use a synchronized block. For example:<\/p>\n

\n

public class Foo extends Thread {\r\n\t\tprivate volatile boolean close = false;\r\n\t\tpublic void run() {\r\n\t\t\twhile(!close) {\r\n\t\t\t\t\/\/ do work\r\n\t\t\t}\r\n\t\t}\r\n\t\tpublic void close() {\r\n\t\t\tclose = true;\r\n\t\t\t\/\/ interrupt here if needed\r\n\t\t}\r\n\t}\r\n\t<\/code><\/pre>\n<\/p><\/div>\n<\/div>\n
\n
\n

sleep()<\/code> is a blocking operation that keeps a hold on the monitor \/ lock of the shared object for the specified number of milliseconds.<\/p>\n

wait()<\/code>, on the other hand, simply pauses<\/em> the thread until either<\/em> (a) the specified number of milliseconds have elapsed or<\/em>
\n\t\t(b) it receives a desired notification from another thread (whichever is first), without<\/em> keeping a hold on the monitor\/lock of the shared object.<\/p>\n

sleep()<\/code> is most commonly used for polling, or to check for certain results, at a regular interval. wait()<\/code> is generally used in
\n\t\tmultithreaded applications, in conjunction with notify()<\/code> \/ notifyAll()<\/code>, to achieve synchronization and avoid race conditions.<\/p>\n<\/p><\/div>\n<\/div>\n

\n
\n

Here is an example of a typical recursive function, computing the arithmetic series 1, 2, 3\u2026N. Notice how the addition is performed after
\n\tthe function call. For each recursive step, we add another frame to the stack.<\/p>\n

public int sumFromOneToN(int n) {\r\n\t  if (n < 1) {\r\n\t\treturn n;\r\n\t  }\r\n\r\n\t  return n + sumFromOneToN(n - 1);\r\n\t}\r\n\t<\/code><\/pre>\n

Tail recursion occurs when the recursive call is in the tail position within its enclosing context –
\n\tafter the function calls itself, it performs no additional work. That is, once the base case is complete,
\n\tthe solution is apparent. For example:<\/p>\n

public int sumFromOneToN(int n, int a) {\r\n\t  if (n < 1) {\r\n\t\treturn a;\r\n\t  }\r\n\r\n\t  return sumFromOneToN(n - 1, a + n);\r\n\t}\r\n\t<\/code><\/pre>\n

Here you can see that a<\/code> plays the role of the accumulator – instead of computing the sum on
\n\tthe way down the stack, we compute it on the way up, effectively making the return trip unnecessary,
\n\tsince it stores no additional state and performs no further computation. Once we hit the base case, the work is done – below is that same function, \u201cunrolled\u201d.<\/p>\n

public int sumFromOneToN(int n) {\r\n\t  int a = 0;\r\n\r\n\t  while(n > 0) {\r\n\t\ta += n--;\r\n\t  }\r\n\t  \r\n\t  return a;\r\n\t}\r\n\t<\/code><\/pre>\n

Many functional languages natively support tail call optimization, however the JVM does not. In order
\n\tto implement recursive functions in Java, we need to be aware of this limitation to avoid
\n\tStackOverflowError<\/code>s. In Java, iteration is almost universally preferred to recursion.<\/p>\n<\/p><\/div>\n<\/div>\n

\n