Google Java Questions

Google Java Questions

Everyone knows how important it is to prepare before heading into a job interview, and choosing the right topics that might come up can be a hit and a miss. We’ve compiled a list of ten topics that every candidate must know to prepare for the inevitable interview questions on Java.

1.Transient variables

As the first interview question on Java let’s take a look at Transient variables. When an object is transferred through the Java network, it needs to be converted from the object state into serial bytes. This processes is known as serialization. However, if a particular member variable of this object is not desired, it can be intentionally lost by attaching a ´transient´ marker to the variable. This marker indicates to the Java Virtual Machine not to serialize this variable therefore excluding it as part of the object’s persistent state. As a result, when the object is archived, the state of the data structure value will return to its default state value.

2.How are Observer and Observable used?

The best way to approach this possible interview question on Java is to first identify the difference between an
Observable and an Observer: an Observable is a class and Observer is an interface. The Observable object maintains a list of subclass objects, which in turn monitor any changes made to the Observable. This ecosystem model is useful when you want to notify a list of objects of changes made to another object.

For example, when a user signs up to a Facebook group, their account essentially becomes an Observer of that group – the Observable. When someone posts to the group, it broadcasts to all of the subscribers that a change has been made in the form of a new post.

3.What is synchronization?

Any programmer worth their weight must have a good understanding of synchronization, and not just for correctly answering Java interview questions. Java is a multi-threaded language and without synchronization these various threads will simultaneously try to read or write to their shared resources or objects. This produces conflicting
actions that can corrupt the data. Synchronization prevents this from happening by controlling the threads´ access using a monitor concept.

A monitor is appointed to every Java object, which needs to be unlocked before its information can be accessed. This monitor allows only one thread at a time to open the lock, thereby ensuring that one task is completed before another is implemented.

4.API Collections

An API (Application Programming Interface) is made of a collection of Objects, which are in turn made up of a variety of methods and functions that interact to create a specific data outcome. Collections are useful because they group together the different classes and interfaces in a way that makes them easier to handle by developers. The Java Collections API works as a kind of library of reusable Collections of data structures that developers can use as architecture to more easily build their own programs.

5. List interface

A Java List interface is a sub-interface Collection in the Java Collections Framework (JCF). It is an ordered collection that stores its data in the sequence in which it was introduced. The List interface has some convenient features. Through providing List Iterator, a unique Iterator, the user is allowed considerable control over element insertion, replacement, as well as bidirectional access. The list interface also allows the duplication of elements.

6. Vector class

Creating a vector class is one alternative to building a linked-list structure and is a desirable option due to its user-friendly features. A vector uses the Enumeration interface which allows for easy traversing and processing of elements. It also implements a dynamic array allowing it to increase or decrease with the items inserted, which is convenient if you think the size of the program will change during the course of its life. A vector is very similar to an Array List however it is synchronized and therefore is not so suitable to non-thread collections.

7.Iterator interface

Another interview question on Java Collections that will almost definitely be addressed, regards the Iterator Interface. This interface is useful for when do you want to transverse all the elements of a Collection. The easiest way to do that is to implement an iterator object, which can be either an Iterator or a List Iterator interface. An Iterator is allows the user to obtain or remove elements, whereas a List Iterator has the additional features of allowing bidirectional traversal of a list and modification of the elements.

8. & 9. Yielding and Sleeping thread methods

If you do have to answer any interview questions on Java yield() or sleep() thread methods it´s a good idea to discuss them in comparison to each other.Because the Java Virtual Machine allows an application to run multiple execution threads simultaneously, there needs to be a system in place that will prioritize the thread actions.

The yielding method more or less puts the calling thread in a lower priority to all other executing threads. It communicates to the other threads and processes that it will put its actions on hold if they need to run. However, once the other threads have finished, it will jump back to the front of the queue and complete its process execution.

The sleep method, on the other hand, completely stops the execution, suspending it for a specified number of milliseconds or nanoseconds. It will not re-initiate till that period of time has expired, whether there are other process orders being executed or not.

The best method is subject to debate, and will depend of course on your program´s requirements, however, yielding is not generally considered to be the best method. Because there is no standard definition, a yield() can be implemented in a different way depending on the platform, which can significantly affect when the thread will be able to re-initiate its process making the method somewhat unreliable.This is why the specifically defined sleep() is almost always preferred.

10. Wrapped classes

The last interview question on Java that we recommend you prepare for is Wrapping classes. Because Java is an object-oriented language, it is sometimes necessary to convert data types into objects so they may be allowed to perform object functions. The Wrapper class provides a mechanism by which a primitive values, such as numbers, can be wrapped and thereby converted into an object. Each of the eight primitive Java data types have a its own wrapping class dedicated to it.