Hello, everyone! Today I'm continuing my review of Java developer interview questions.
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 1]()
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 2]()
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 3]()
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 4]()
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 5]()
Now we can look at the order of the initialization of the class (together with its parent class) in order to better understanding when exactly the initialization blocks are invoked.
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 7]()
![Exploring questions and answers from a job interview for a Java developer position. Part 4 - 8]()

29. Can return be used in a constructor?
Yes, but only without a value to the right of the return keyword. You can use return; as a helper statement in a constructor to urgently terminate (interrupt) execution of further code and finish the initialization of the object. For example, suppose we have a Cat class, and if a Cat is homeless (isHomeless = true, then we want to terminate initialization and not fill in the other fields (after all, they are unknown to us, since the cat is homeless):
public Cat(int age, String name, boolean isHomeless) {
if (isHomeless){
this.isHomeless = isHomeless;
return;
}
this.isHomeless = isHomeless;
this.age = age;
this.name = name;
}
But if we're talking about concrete values, then the return keyword cannot return a specific value because:- when you declare a constructor, you won't have anything like the return type;
- as a rule, the constructor is implicitly called during instantiation;
- the constructor is not a method: it is a separate mechanism whose sole purpose is to initialize instance variables, i.e., we're using the new operator to create an object.

30. Can an exception be thrown from a constructor?
Constructors work with exceptions in the same way that methods do. Methods allow us to throw exceptions by writing throws <ExceptionType> in the method header. And constructors allow us to do the same. When we are inheriting and defining the constructor of a child class, we can widen the exception type — for example, IOException -> Exception (but not vice versa). Let's use the constructor of the Cat class as an example of a constructor throwing an exception. Let's say that when we create an object, we want to enter the name and age from the console:
public Cat() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
this.name = reader.readLine();
this.age = Integer.parseInt(reader.readLine());
}
Since reader.readLine() throws an IOException, we write it in the header as a possible thrown exception.
31. What are the elements of a class header? Write an example
To illustrate the elements that make up a class header, let's look at a small schema:- mandatory elements appear in brackets <>
- optional elements are in {}
public final class Lion extends Cat implements WildAnimal

32. What are the elements of a method header? Write an example
When considering the elements that make up a method header, let's again consider a small schema:- mandatory elements appear in brackets <>
- optional elements are in {}
public static void main(String[] args) throws IOException
33. Create a default constructor in a child class if one is not already defined in the base class (but a different constructor is defined)
I'm not sure I fully understand the question, but maybe it means that we have some constructor like this in the parent class:
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
In that case, in the parent class, we definitely need to define a constructor that will initialize the parent (i.e. call the parent constructor):
public class Lion extends Cat {
public Lion(int age, String name) {
super(age, name);
}
}

34. When is the this keyword used?
In Java, this has two different meanings. 1. It is a reference to the current object, e.g. this.age = 9. That is, this refers to the object in which it is used and to which the code with this refers. The main purpose is to improve code readability and avoid ambiguity. For example, if an instance field and a method argument have the same name:
public void setName(String name) {
this.name = name;
}
That is, this.name is the object's field, while name is the method parameter.
The this reference cannot be used in static methods.
2. In the constructor, this can be called like a method, e.g. this(value). In this case, it will be a call to another constructor of the same class.
Basically, you can call two constructors during the process of creating an object:
public Cat(int age, String name) {
this(name);
this.age = age;
}
public Cat(String name) {
this.name = name;
}
When calling the first constructor to create a Cat object, both instance fields will be successfully initialized.
There are a couple of nuances here:- this() only works in a constructor.
- A reference to another constructor must be in the first line of the constructor block (body). This means that a constructor cannot call more than one (other) constructor of its class.

35. What is an initializer?
As far as I understand, this question is about ordinary and static initialization blocks. Let's first remember what initialization is. Initialization is the creation, activation, preparation, and definition of fields. Preparing a program or component to be ready for use. You will recall that when you create an object, a class variable can be initialized immediately when it is declared:
class Cat {
private int age = 9;
private String name = "Tom";
Or set after the fact through the constructor:
class Cat {
private int age;
private String name;
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
But there is another way: you can set an instance variable using an initialization block, which takes the form of curly braces {} inside a class, without a name (like a nameless method or constructor):
class Cat {
private int age;
private String name;
{
age = 10;
name = "Tom";
}
An initialization block is a piece of code that is loaded when an object is created.
Such blocks are typically used to perform certain complex calculations that are required when a class is loaded. The results of these calculations can be set as the values of variables. In addition to ordinary initialization blocks, there are static ones.
They look the same but have the static keyword in front of the opening curly brace:
class Cat {
private static int age;
private static String name;
static{
age = 10;
name = "Tom";
}
This block is the same as the previous one. But if the ordinary one is executed when each object is initialized, then the static one is executed only once, when the class is loaded.
As a rule, certain complex calculations are performed in a static block, used to initialize static class variables. The same restrictions apply to a static block that applies to static methods: you cannot use non-static data, such as a reference to the current object (this) in a static block.

36. Given a public Child class that extends Parent, write out the initialization order of the object
When loading the Child class, the initialization order will be as follows:- Static class fields of the Parent class.
- Static initialization block of the Parent class.
- Static fields of the Сhild class.
- Static initialization block of the Child class.
- Non-static fields of the Parent class.
- Non-static initialization block of the Parent class.
- Parent class constructor.
- Non-static fields of the Сhild class.
- Non-static initialization block of the Сhild class.
- The constructor of the Сhild class.

37. What sort of relationships between classes (objects) do you know?
There are two kinds of variables in Java: primitive types and references to full-fledged objects.- IS-A relationships
Lion IS-A Cat
(but not every Cat is a Lion)
The same situation exists with interfaces. If the Lion class implements the WildAnimal interface, then they also exist in the relationship:
Lion IS-A WildAnimal
- HAS-A relationship
Car HAS-A Passenger
And vice versa: if Passenger has a reference to Car, then this will be the relationship:
Passenger HAS-A Car
38. What associative object relationships do you know?
Aggregation and composition are nothing more than special cases of association. Aggregation is a relationship where one object is part of another. For example, a passenger might be located in a car. What's more, there may be multiple passengers or none at all (and if we're talking about Tesla, there may be no driver). For example:
public class Car {
private List passengers = new ArrayList<>();
void setPassenger(Passenger passenger) {
passengers.add(passenger);
}
void move() {
for (Passenger passenger : passengers) {
System.out.println("Transporting passenger - " + passenger.toString());
}
passengers.clear();
}
}
In other words, the number of passengers (in any) is not important to us: the Car class's functionality does not depend on this.
Aggregation also implies that when another object uses one object, the first object can be used by other objects.
For example, the same student may be in both a knitting club and a rock band and simultaneously attend a Spanish class. As you can imagine, aggregation is a looser associative relationship between classes.
Composition is an even tighter relationship where an object is not only part of another object, but one object's work is very dependent on another.
For example, a car has an engine. An engine may exist without a car, but it is useless outside of a car.
And a car cannot work without an engine:
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
void startMoving() {
engine.start();
...
}
Composition also implies that when another object uses an object, the first object cannot belong to any other object. Going back to our example, an engine can only belong to one car, not two or more at the same time.
I think that is enough for today, so we'll stop here.
GO TO FULL VERSION