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

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

Published in the Random EN group
Hello! Various people gathered at JavaRush. Some of us just want to become Java developers, investing a lot of time and effort in development, while others are already Java developers. In one case or another, you need 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 interview questions and answers.  Part 1 - 1I recently came across a large list of Java Developer Interview Questions 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'd like to try to answer most of them. It’s clear that I won’t go into the scope of one article; after all, there are a lot of questions there. Therefore, this will be a whole series of articles answering such questions. Let me immediately emphasize a few points:
  1. There is an excellent article with top questions and answers for them. Some questions overlap with the list presented above (250+), so these questions will be skipped so as not to duplicate information once again.

  2. The questions are presented in Ukrainian, but since the majority of JavaRush participants are Russian-speaking (and to a greater extent me too), the answers will be in Russian.

  3. The answers will be brief, since if you write in great detail, the answers to some questions may require a separate article. And during interviews, such detailed and voluminous answers are not needed, because your interviewer only has an hour to interview you on the necessary topics (and, as you remember, that’s enough). For those who like to dig deeper, I will leave links.

So, let's begin.

Junior Level Questions and Answers

Analysis of interview questions and answers.  Part 1 - 2

General issues

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

There are a huge variety of templates: you can start getting acquainted with them from this and this article. Well, for those of you who want to familiarize yourself with them in detail, I recommend reading the book “Head First. Design Patterns" . With its help, you can study the most basic design patterns in detail and in an easy way. When it comes to design patterns that you can use as examples in an interview, some that come to mind are:
  • Builder is a frequently used template, an alternative to classic object creation;
  • Strategy pattern , which inherently represents polymorphism. That is, we have one interface, but the behavior of the program will change depending on what specific implementation of this interface was transferred to the functionality (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 up and down. Here are a couple of examples of what I'm talking about:
  • Factory - in the ApplicationContext (or in the BeanFactory);
  • Singleton - all beans are singletons by default;
  • Proxy - essentially everything in Spring uses this pattern in one way or another, for example, AOP;
  • Chain of responsibility is a pattern based on the concept of which Spring Security works;
  • Template - used in Spring Jdbc.

Java Core

Analysis of interview questions and answers.  Part 1 - 3

2. What data types are there in Java?

Java has primitive data types:
  • byte — integers in the range -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 in the range 9223372036854775808 to 9223372036854775807, weighs 8 bytes;
  • float — floating point numbers in the range -3.4E+38 to 3.4E+38, weighs 4 bytes;
  • double — floating point numbers in the range -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 , which point to objects on the heap.

3. How does an object differ from primitive data types?

The first difference: the amount of memory occupied: primitives take up very little, because they contain only their own value, while objects can contain very, very many different values: both primitives and references to other objects. The second difference: Java is an object-oriented language, so everything in it works through the interaction between objects, and primitives do not fit in very well (in fact, this is why Java is not a 100% object-oriented language). Third, following on from the second: since Java is focused on interaction between objects, these objects have many different mechanisms for managing them. For example, constructors, methods, exceptions (which operate primarily on objects), etc. Actually, so that primitives could somehow get involved (work) in this object-oriented environment, wrappers were invented 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; field i stores the value 9 . When we have a reference to an object, it 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 fields with a reference to an object also store values , memory address values. That is, cat stores the address value 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 to which it points. And if we change the object using this new link, 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 pre-generated by the compiler. JRE - Java Runtime Environment - is essentially an environment for running java applications, which contains JVM , standard libraries and other components for running applets and applications written in the Java programming language. In other words , the JRE is a package of everything needed to run a compiled Java program, but does not contain tools and utilities such as compilers or debuggers for application development. JDK - Java Development Kit - an extended set of JRE , that is, an environment 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 applications in Java (also contains java docs).Analysis of interview questions and answers.  Part 1 - 4

6. Why use JVM?

As mentioned above, Java Virtual Machine is a virtual machine that runs Java bytecode pre-generated by the compiler. That is, the JVM does not understand the Java source code. Therefore, first, .java files are compiled , which after compilation already have a .class extension and which are presented in the form of the same byte code that the JVM understands. Each OS has its own JVM, so having received the bytecode files, the JVM executes it, adapting it to the OS on which it happens. Actually, due to different JVMs, JDK (or JRE) versions differ for different OSes (each of them requires 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 (compilation and processing of JVM code bytes), you can enjoy the benefits of cross-platform. We once created the code, recompiled it into bytecode, transferred it to any OS, and the local JVM runs the code. This is the legendary property of Java - write once, run anywhere . Analysis of interview questions and answers.  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 intermediate bytecode (files with the .java extension into files with the .class extension). Bytecode is in many ways similar to machine code, only it uses a set of instructions not from a real processor, but from a virtual one. Moreover, it may include sections focused on the use of a JIT compiler, which optimizes the execution of commands for the real processor on which the program is running. JIT compilation, also called on-the-fly compilation, is a technology that increases the performance of a program using bytecode by compiling the bytecode into a machine or other format while the program is running. As you might have guessed, the JVM uses a JIT compiler when it runs bytecode. Let's take a look at a bytecode example: Analysis of interview questions and answers.  Part 1 - 6Not too readable, is it? Well, this is not an instruction for us, but for the JVM. Here is an article that will help you understand this issue better.

8. What are the characteristics of a JavaBean?

JavaBeans is a Java class with certain rules. Here are some rules for writing a JavaBean :
  1. The class must contain an empty (no parameters) public access 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 a class are accessed through 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 contents of 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 a marker interface - Serializable or implement the Externalizable interface . This is necessary so that the state of the bean can be reliably saved, stored and restored.

Analysis of interview questions and answers.  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 available for it and the garbage collector cannot allocate more memory. Some types of OutOfMemoryError :
  • OutOfMemoryError: Java heap space - the object cannot be allocated on the Java heap due to insufficient memory. The error could be caused by a memory leak or because 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 in the heap, the garbage collector runs all the time, and the Java program runs very slowly, and as a result, the overhead limit of the garbage collector 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 that is larger than the heap size, which again may be due to insufficient default memory allocation.

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

  • OutOfMemoryError: request size bytes for reason. Out of swap space - some failure occurred when trying to allocate memory from the heap and, as a result, there was 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 a stack trace at a specific point in the application like this:
StackTraceElement[] stackTraceElements =Thread.currentThread().getStackTrace();
This way we will get an array of stack trace elements arranged in LIFO order - Last In First Out . Analysis of interview questions and answers.  Part 1 - 8In Java, as a rule, when they talk about a stack trace, they mean the stack trace that 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 outputting the exception stack trace in the console:
try{
                ...
} catch (Exception e) {
  e.printStackTrace();
}
Also, if we have an error, an unchecked exception or a checked exception , which we will not process, but will only forward, then when the application crashes we will automatically receive a stack trace of exceptions in the console. A small example of a stack trace exception in the console: Analysis of interview questions and answers.  Part 1 - 9You can read more about Stack Trace here . We will focus on this issue today...Analysis of interview questions and answers.  Part 1 - 10
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION