JavaRush /Java Blog /Random EN /Analysis of questions and answers from interviews for a J...

Analysis of questions and answers from interviews for a Java developer. Part 1

Published in the Random EN group
Hello! Various people gathered at CodeGym. Some of us just want to become Java developers, investing a lot of time and effort into development, while others are already Java developers. In one case or another, it is necessary to be prepared for tests - technical interviews. This test is not easy, and in addition to moral preparation, technical preparation is also needed. Analysis of questions and answers at the interview.  Part 1 - 1I recently came across a large list of interview questions.in Java developer on dou. These questions are divided into different levels - Junior, Middle and Senior. Don't be alarmed: not all questions are easy, but those with an asterisk are rarely asked. Questions are good, but I would like to try to answer most of them. It is clear that I will not invest in the framework of one article, after all, there are extremely many questions there. Therefore, it will be a whole cycle of articles-answers to such questions. Let me just emphasize a few things:
  1. There is a great article with top questions and answers for them. Some questions overlap with the list above (250+), so these questions will be skipped so as not to duplicate information once again.

  2. Questions are presented in Ukrainian, but since most of the CodeGym participants are Russian speakers (and I am, to a greater extent, too), the answers will be in Russian.

  3. The answers will be short, because if you paint in great detail, the answers to some questions can pull on a separate article. Yes, and interviews do not need such detailed and voluminous answers, because your interviewer has only an hour to interview you on the necessary topics (and, as you remember, that is enough). For those who like to dig deeper, I will leave links.

So, let's begin.

Junior Q&A

Analysis of questions and answers at the interview.  Part 1 - 2

General issues

1. What design patterns do you know? Tell us about two patterns that you used in your work.

There are a lot of templates: you can start getting acquainted with them from this and this article. Well, for those of you who want to get acquainted with them in detail, I recommend reading the book “Head First. Design Patterns” . With its help, you can learn the most basic design patterns in detail and in an easy way. When talking about design patterns that you can cite as examples in an interview, the following come to mind:
  • Builder - a frequently used template, an alternative to the classic creation of objects;
  • pattern Strategy , which in its essence represents polymorphism. That is, we have one interface, but the behavior of the program will change depending on which specific implementation of this interface was passed to the functional (now the strategy is practically used everywhere in java applications).
If this is not enough for you, pay attention to Spring (if you are already familiar with it), because it is a whole platform of frameworks, which in turn are riddled with patterns inside and out. Here are a couple of examples of what I'm talking about:
  • Factory - in ApplicationContext (well, or in BeanFactory);
  • Singleton - all beans are singletons by default;
  • Proxy - in fact, everything in spring uses this pattern in one way or another, for example, AOP;
  • Chain of responsibility is the pattern on which Spring Security works;
  • Template - used in Spring Jdbc.

Java Core

Analysis of questions and answers at the interview.  Part 1 - 3

2. What data types are there in Java?

Java has primitive data types:
  • byte - integers within -128 to 127, weighs 1 byte;
  • short - integers in the range -32768 to 32767, weighs 2 bytes;
  • int - integers -2147483648 to 2147483647, weighs 4 bytes;
  • long - integers within 9223372036854775808 to 9223372036854775807, weighs 8 bytes;
  • float — floating point numbers within -3.4E+38 to 3.4E+38, weighs 4 bytes;
  • double — floating point numbers within -1.7E+308 to 1.7E+308, weighs 8 bytes;
  • char - single characters in UTF-16, weighs 2 bytes;
  • boolean values ​​true/false , weighs 1 byte.
And reference data types that point to objects on the heap.

3. How is an object different from primitive data types?

The first difference is the amount of memory used: primitives take up very little, because they only contain their own value, while objects can contain very, very many different values: both primitives and references to other objects. The second difference is that Java is an object-oriented language, so everything in it works through the interaction between objects, and primitives do not fit here much (in fact, this is why Java is not a 100% object-oriented language). Third, following from the second, since Java is focused on interaction between objects, these objects have many different mechanisms for managing. For example, constructors, methods, exceptions (which work primarily with objects), etc. Actually, so that primitives could somehow get involved (work) in this object-oriented environment and were inventedwrappers for primitive types ( Integer , Character , Double , Boolean ...)

4. What is the difference between passing parameters by reference and by value?

Primitive fields store their value: for example, if we set int i = 9; the i field stores the value 9 . When we have a reference to an object, this means that we have a field with a reference to the object, or in other words, with the value of the address of the object in memory.
Cat cat = new Cat();
It turns out that the fields with a reference to the object also store values , the values ​​of the memory address. That is, cat stores the value of the address of the new Cat() object in memory. When we pass a parameter to a method, its value is copied. In the case of a primitive, the value of the primitive will be copied. Accordingly, the method will work with a copy, changing which will not affect the original. In the case of a reference type, the value of the memory address will be copied, respectively, the address will be the same as the object it points to. And if we change the object by this new reference, it will be changed for the old one (after all, they both point to the same object).

5. What is JVM, JDK, JRE?

JVM - Java Virtual Machine is a virtual machine that runs Java bytecode previously generated by the compiler. JRE - Java Runtime Environment - in fact, this is an environment for running java applications, which contains the JVM , standard libraries and other components for running applets and applications written in the Java programming language. In other words , JRE is a package of everything you need to run a compiled Java program, but it does not contain tools and utilities such as compilers or debuggers for application development. JDK - Java Development Kit - an extended set of JREs, that is, the environment is not only for launching, but also for developing java applications. The JDK contains everything that is in the JRE, plus various additional tools - compilers and debuggers - that are needed to create Java applications (it also contains java docs).Analysis of questions and answers at the interview.  Part 1 - 4

6. Why use JVM?

As mentioned above, the Java Virtual Machine is a virtual machine that runs Java bytecode previously generated by the compiler. That is, the JVM does not understand Java source code. Therefore, .java files are compiled first , which, after compilation, have an extension of .classand which and which are presented in the form of the same byte of code that the JVM understands. Each OC has its own JVM, therefore, having received files in byte code, the JVM executes it, adapting it to the OS on which it happens. Actually, due to different JVMs, the versions of the JDK (or JRE) differ for different operating systems (each of them needs its own JVM). Let's remember how development works in other programming languages. You develop a program, then its code is compiled into machine code for a specific OS, and then you can run it. In other words, you need to write different versions of the program for each system. Whereas in Java, thanks to dual code processing (compiling and processing JVM code bytes), you can enjoy the benefits of cross-platform. We once created a code, recompiled it into bytecode, ported it to any OS, and already local JVM and runs the code. This is the legendary Java property -write once, run anywhere . Analysis of questions and answers at the interview.  Part 1 - 5Read more about this in the article “ Compiling and executing Java applications under the hood ”.

7. What is bytecode?

As I said above, the compiler converts Java code into an intermediate one - bytecode (files with the .java extension into files with the .class extension). Bytecode is in many ways similar to machine code, only it uses the instruction set of a virtual processor rather than a real one. At the same time, it may include sections focused on the use of a JIT compiler that optimizes the execution of instructions for the real processor on which the program is running. JIT compilation, also called on-the-fly compilation, is a technique that improves the performance of a bytecode-using program by compiling the bytecode to native or another format while the program is running. As you might have guessed, the JVM uses the JIT compiler when it runs bytecode. Let's take a look at a bytecode example:Analysis of questions and answers at the interview.  Part 1 - 6Not very readable, is it? Well, this is not an instruction for us, but for the JVM. Here is an article that will help you better understand this issue.

8. What are the features of a JavaBean?

JavaBeans - A Java class that follows certain rules. Here are some rules for writing JavaBean :
  1. The class must contain an empty (parameterless) public constructor with the public access modifier . This constructor makes it possible to create an object of this class without unnecessary problems (so that there is no unnecessary fuss with parameters).

  2. The internal fields of the class are accessed via the get and set methods , which should be standard. For example, if the field is name , then getName and setName , etc. This, in turn, allows various tools (frameworks) to automatically determine and update the content of the beans without complications.

  3. The class must contain overridden versions of the equals() methods - hashCode() and toString() .

  4. The class must be serializable, that is, it must have an interface marker - Serializable or implement the Externalizable interface . This is necessary so that the state of the bean object can be reliably saved, stored and restored.

Analysis of questions and answers at the interview.  Part 1 - 7You can read about the types of JavaBeans in this material .

9. What is OutOfMemoryError?

OutOfMemoryError is one of the critical runtime errors associated with the operation of the Java Virtual Machine (JVM). Called when the JVM cannot allocate an object because there is not enough memory for it and the garbage collector cannot allocate more memory. Some kinds of OutOfMemoryError :
  • OutOfMemoryError: Java heap space - An object cannot be allocated on the Java heap due to lack of memory. The error can be caused by a memory leak, or if the default heap size is not large enough for the current application.

  • OutOfMemoryError: GC Overhead limit exceeded - due to the fact that the amount of data barely fits on the heap, the garbage collector runs all the time, and the Java program runs very slowly, and as a result, the garbage collector overhead limit is exceeded and the application crashes with this error.

  • OutOfMemoryError: Requested array size exceeds VM limit - Indicates that the application attempted to allocate memory for an array larger than the heap size, which again may be due to insufficient default allocated memory.

  • OutOfMemoryError: Metaspace - the heap has run out of space allocated for metadata (metadata are instructions for classes, methods).

  • OutOfMemoryError: request size bytes for reason. Out of swap space - there was some failure when trying to allocate memory from the heap and as a result - a lack of memory in the heap.

10. What is a stack trace? How to get it?

A stack trace is a list of classes and methods that have been called up to this point in the application. You can call the stack trace at a specific point in the application like this:
StackTraceElement[] stackTraceElements =Thread.currentThread().getStackTrace();
Thus, we will get an array of stack trace elements arranged in LIFO - Last In First Out order . Analysis of questions and answers at the interview.  Part 1 - 8In Java, as a rule, when people talk about a stack trace, they mean the stack trace, which is displayed in the console when an error (or exception) occurs. You can get the stack trace of exceptions like this:
StackTraceElement[] stackTraceElements;
try{
                ...
} catch (Exception e) {
   stackTraceElements = e.getStackTrace();
}
Well, if we are talking about displaying the stack trace of the exception in the console:
try{
                ...
} catch (Exception e) {
  e.printStackTrace();
}
Also, if we have an error, an unchecked exception or a checked , which we will not process, but only forward, then when the application crashes, we will automatically receive an exception trace stack in the console. A small example of a stacktrace exception in the console: Analysis of questions and answers at the interview.  Part 1 - 9You can read more about Stack Trace here . We will focus on this issue today...Analysis of questions and answers at the interview.  Part 1 - 10Analysis of questions and answers at the interview.  Part 1 - 11
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION