JavaRush /Java Blog /Random EN /java core. Questions for the interview, part 1
Andrey
Level 26

java core. Questions for the interview, part 1

Published in the Random EN group
For those who hear the word Java Core for the first time, these are the fundamental foundations of the language. With this knowledge, you can already safely go on an internship / internship. java core.  Questions for the interview, part 1 - 1These questions will help you refresh your knowledge before the interview, or learn something new for yourself. For practical skills, study on CodeGym .
  1. How to create an immutable object in Java? List all the benefits

    An immutable class is a class whose state cannot be changed after it has been created. Here, the state of an object is essentially the values ​​stored in an instance of the class, whether they be primitive types or reference types.

    To make a class immutable, the following conditions must be met:

    1. Do not provide setters or methods that modify fields or objects that refer to fields. Setters imply changing the state of an object, which is what we want to avoid here.
    2. Make all fields finaland private. Fields marked with privatewill be inaccessible from outside the class, and designating them finalensures that you don't change them even by accident.
    3. Do not allow subclasses to override methods. The easiest way to do this is to declare the class as final. Finalized classes in Java cannot be overridden.
    4. Always remember that your variable instances can be either mutable or immutable. Define them and return new objects with copied content for all mutable objects (reference types). Immutable variables (primitive types) can be safely returned with no additional effort.

    Also, you need to keep in mind the subsequent benefits of immutable classes. You may need them for an interview. Immutable classes:

    • easy to design, test and use
    • are automatically thread safe and have no synchronization issues
    • do not require a copy constructor
    • allow you to "lazy initialize" the hashcode and cache the return value
    • do not require secure copy when used as a field
    • make good Mapkeys and Setelements (these objects should not change state when they are in the collection)
    • make their class permanent by creating it once, and it does not need to be re-checked
    • always have “failure atomicity,” a term coined by Joshua Bloch: if an immutable object throws an exception, it will never be left in an unwanted or undefined state.

    Look at the example written in this post .

  2. In Java, pass by value or by reference?

    The Java specification states that everything in Java is passed by value. There is no such thing as "pass by reference" in Java. These conditions are associated with calling methods and passing variables as method parameters. Well, primitive types are always passed by value without any confusion. But, the concept should be understood in the context of a method parameter of complex types.

    In Java, when we pass a complex type reference as any method parameter, the memory address is always copied into the new reference variable step by step. Look at the image:

    java core.  Questions for the interview, part 1 - 2

    In the example above, the address bits of the first instance are copied to another reference variable, causing both references to point to the same memory location where the object is stored. Remember that by assigning null to the second reference, you will not assign null to the first reference. But a change in the state of an object with one referenced variable will be reflected in the other reference as well.

    See details here .

  3. What is the use of the block finally? Does this block guarantee the execution of its code? When is finallya block not called?

    The block finallyis always called if the block tryexists. This ensures that the block finallyis called even if an unexpected exception occurs. But finallyit is more useful than just exception handling - this block allows you to clean up code that accidentally bypassed through return, continueor break. Placing cleanup code in a block finallyis always good practice, even when no exceptions are expected.

    If the virtual machine exits while the tryor block is executing catch, then the block finallywill not be executed. Similarly, if a thread is interrupted or killed while executing a tryor block catch, the block finallywill not be executed even though the application is still running.

  4. Why are there two classes Date, one in java.util packageand the other in java.sql?

    java.util.Daterepresents a date and time, but java.sql.Dateonly a date. The complement to java.sql.Dateis the class java.sql.Time, which represents only time.

    The class java.sql.Dateis a subclass (extension) of the class java.util.Date. So what has changed in java.sql.Date:

    • toString()generates a different string representation: yyyy-mm-dd
    • static method valueOf(String)creates date from string with above representation
    • excluded getters and setters for hours, minutes and seconds

    The class java.sql.Dateis used in JDBC and is intended to not have a time component, i.e. hours, minutes, seconds and milliseconds must be zero...but this is not required for the class.

  5. Explain marker interfaces.

    The marker interface pattern is a computer science design pattern used by programming languages ​​that provide information about objects at run time . It provides a way to associate class metadata where the language has no explicit support for such metadata . Java uses interfaces for this without specifying methods.

    A good example of using a marker interface in Java is the Serializable. A class implements this interface to indicate that its non- transientdata can be written to a byte stream or file system.

    The main problem with a marker interface is that an interface defines a convention for the classes that implement it, and that convention is inherited by all subclasses. This means that you cannot "de-realize" the token. In the example above, if you create a subclass that you don't want to serialize (perhaps because it's in a transient state), you must resort to explicit casting NotSerializableException.

  6. Why is the method main()declared as public static void?

    Why public? The method mainhas an access modifier publicso it can be accessed everywhere and by any object that wants to use this method to launch the application. Here I'm not saying that JDK/JRE have a similar reason, because java.exe or javaw.exe (for windows) use Java Native Interface (JNI) call to run the method, so they can call it anyway, regardless of the access modifier .

    Why static? Let's assume that we have a mainnon-static method. Now, to call any method, you need an instance of the class. Right? Java allows you to have overloaded constructors, we all know that. Then which one should be used, and where will the parameters for the overloaded constructor come from?

    Why void? There is no use for the return value in the virtual machine that actually calls this method. The only thing the application wants to tell the calling process is a normal or abnormal termination. This is already possible using System.exit(int). A non-zero value implies abnormal termination, otherwise everything is fine.

  7. What is the difference between constructing a string as new()a literal (using double quotes)?

    When we create a string using new(), it is created in the heap and also added to the string pool, while the string created with the literal is only created in the string pool.

    You need to know more about the string pool concept to answer this or similar questions. My advice is to learn the String class and string pool well .

    We already have a good article in translations about strings and string pool: Part 1 , Part 2 .
  8. How does a substring()class method work String?

    Like other programming languages, strings in Java are sequences of characters. This class is more like a utility class for working with this sequence. The character sequence is provided by the following variable:

    /** The value is used for character storage. */
    /** Значение используется для хранения символов */
    private final char value[];
    Для доступа к этому массиву в различных сценариях используются следующие переменные/** The offset is the first index of the storage that is used. */
    /** Смещение – это первый индекс используемого хранorща. */
    private final int offset;
    
    /** The count is the number of characters in the String. */
    /** Счет – это количество символов в строке. */
    private final int count;

    Each time we create a substring from an existing string instance, the method substring()only sets the new values ​​of the variables offsetand count. The internal character array is not modified. This is a possible source of a memory leak if the method substring()is used carelessly:

    The original value value[]is not changed. So if you create a string of 10000 characters and create 100 substrings with 5-10 characters each, all 101 objects will contain the same character array of 10000 characters. This is no doubt a waste of memory.

    This can be avoided by changing the code like this:

    replace original.substring(beginIndex)with new String(original.substring(beginIndex)), where originalis the original string.

    Translator's note: I find it difficult to say which version of Java this applies to, but at the moment in Java 7 this paragraph of the article is not relevant. The method substring()calls the class constructor new String(value, beginIndex, subLen), which in turn calls the method Arrays.copyOfRange(value, offset, offset+count). This means that each time we will have a new value for the variable value[]containing our new number of characters.
  9. Explain work HashMap. How is the problem of duplicates solved?

    Most of you will surely agree that HashMapthe most favorite topic for discussion in an interview at the present time. If someone asks me to tell you “How does it work HashMap?”, I will simply answer: “Based on the principle of hashing.” As simple as it is.

    So, hashing is essentially a way to assign a unique code to any variable/object after applying any formula/algorithm to its properties.

    The definition of a map ( Map) is: "An object that binds keys to values." Very simple, right? So, HashMapit contains its own inner class Entry, which looks like:

    static class Entry implements Map.Entry
    {
    final K key;
    V value;
    Entry next;
    final int hash;//More code goes here
    }

    When someone tries to put a key-value pair into HashMap, the following happens:

    • First of all, the key object is checked against null. If the key is null, the value is stored in position table[0]. Because the hashcode for nullis always 0.
    • Then, the next step is to calculate the hash value by calling its method on the key variable hashCode(). This hash is used to calculate the index in the array to store the object Entry. The JDK developers were well aware that a method hashCode()could be poorly written and could return a very large or very small value. To solve this problem, they introduced another hash()method, and pass the hashcode of the object to this method to cast this value to the size range of the array index.
    • The method is now called indexFor(hash, table.length)to calculate the exact position to store the Entry.
    • Now the main part. As we know two dissimilar objects can have the same hashcode value, how can two different objects be stored in the same location in an archive [called a trash can]?

    The answer is LinkedList. If you remember, the class Entryhas a “next” property. This property always points to the next object in the chain. This behavior is very similar to LinkedList.

    So, in case of hashcode matches, Entry objects are stored in the form LinkedList. When an object Entryneeds to be placed at a particular index, HashMapchecks if another object exists at that location Entry? If there is no record there, our object will be saved in this location.

    If there is already another object at our index, its field is checked next. If it is equal to null, our object becomes the next node in LinkedList. If next is not equal to , this procedure is repeated until a field equal nullto is found .nextnull

    What happens if we add another key value equal to the one we added earlier? It is logical that it should replace the old value. How does this happen? After determining the position index for the Entry, running through the LinkedList, located at our index, HashMapcalls a method equals()on the key value for each Entry. All of these objects Entryin LinkedListhave the same hashcode value, but the method equals()will check for true equality. If key. equals(k)will be true , then both will be treated as the same object. This will cause only the value object inside the Entry.

    This HashMapensures the uniqueness of the keys.

  10. Differences between interfaces and abstract classes?

    This is a very common question if you are interviewing for a junior level programmer. The most significant differences are listed below:

    • In Java interfaces, variables are a priori final. Abstract classes may contain non- finalvariables.
    • An interface in Java definitely cannot have an implementation. An abstract class can have instances of methods that implement the base behavior.
    • Interface components must be public. An abstract class can have access modifiers of any kind.
    • The interface must be implemented with the keyword implements. An abstract class must be extended using the extends keyword .
    • In Java, a class can implement many interfaces, but can only inherit from one abstract class.
    • An interface is completely abstract and cannot be instantiated. An abstract class also cannot have instances of the class, but can be called if the main().
    • An abstract class is slightly faster than an interface because an interface expects a lookup before calling any overridden method in Java. In most cases, this is a minor difference, but if you are writing a time-critical application, you need to consider this fact as well.
  11. When do you override methods hashCode()and equals()?

    hashCode()The and methods equals()are defined on the class Object, which is the parent class of all Java objects. For this reason, all Java objects inherit the base implementation of these methods.

    The method hashCode()is used to get a unique integer value for a given object. This value is used to determine the location of the bin when the object needs to be stored in a data structure like HashTable. By default, the method hashCode()returns an integer representation of the memory address where the object is stored.

    The method equals(), as the name suggests, is used for simple object equivalence. The basic implementation of the method is to check the references of the two objects to see if they are equivalent.

    Note that it is usually necessary to override a method hashCode()whenever a method is overridden equals(). This is necessary to support the general method convention hashCodethat equal objects must have equal hashcodes.

    The equals() method must determine the equality of the relationship (it must be recursive, symmetric, and transitive). In addition, it must be persistent (if the object has not changed, the method must return the same value). Also, o.equals(null)it should always return false .

    hashCode()must also be persistent (if the object has not changed under the conditions of the method equals(), it must continue to return the same value.

    The relationship between the two methods is: always, if a.equals(b), then a.hashCode()must be the same as b.hashCode().

Good luck with your studies!! Article by Lokesh Gupta Original article Links to other parts: Java Core. Interview questions, part 2 Java Core. Questions for the interview, part 3
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION