Back-end developer interview preparation questions - Java

· Tech· CS
CS

Threading in Java

  • synchronized, java.util.concurrent Provides utilities and classes for concurrency control
  • Thread class, method implementation with Runnable interface

Hibernate

  • ORM, JPA support
  • JPA is a Java ORM standard specification, and Hibernate is one of the implementations of the JPA specification.
  • QueryDSL enables writing type-safe queries.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_db</property>
<property name="hibernate.connection.username">your_username</property>
<property name="hibernate.connection.password">your_password</property>
<!-- JDBC connection pool settings -->
<property name="hibernate.c3p0.idle_test_period">3000</property>
// ...
<!-- Mention annotated class -->
<mapping class="com.example.Student"/>
</session-factory>
</hibernate-configuration>

Java and Python differences, pros and cons

  • Java
  • Java is a compiled language, has a fast execution time, is suitable for large-scale applications, and can run on various platforms
  • It may look complicated or take a long time to write due to the use of boiler plate code. +Python
  • Concise and easy-to-read grammar, fast development speed
  • Provides various libraries and can be used in various fields such as web, data analysis, artificial intelligence, and scientific computing
  • It is an interpreted language, and its execution speed is relatively slow compared to compiled languages.
  • Compiled languages go through a compilation process that converts source code into machine code, which is performed by a compiler.
  • The interpreted language interprets and executes the source code line by line and executes the code directly. An error is raised at runtime and the program is halted at the point where the error occurs.
  • Limited to concurrency handling, multithreading and multiprocessing can be relatively more complex.

class, abstract class, interface

  • All are core components of object-oriented programming
  • class is a framework or blueprint for creating objects. Contains data (fields) and methods
  • abstract class is an abstract class that allows subclasses to implement abstract methods, including incomplete abstract methods.
  • interface is a framework that only defines the signature of the method and does not provide the implementation, making method implementation itself impossible (default and static method implementations can be included since Java 8)
  • In most languages, multiple implementations of interfaces are possible, but abstract classes only allow single inheritance.
  • Abstract classes and interfaces cannot be instantiated.

public, protected, private

  • public Freely accessible from anywhere
  • protected Can be accessed from classes or subclasses within the same package
  • default This applies to cases where no separate access modifier is specified. Only classes within the same package can be accessed. Not accessible from subclasses
  • private Accessible only from the class

this, super

  • this refers to the current class
  • super refers to the parent class

static, final

  • static
  • Static variables are allocated to memory only once when class is loaded.
  • Static methods can be called with the class name
  • Static members are initialized when loading memory by the class loader.
  • Operates at the class level, allowing access without creating a class instance
  • int field = ExampleClass.staticField;
  • final
  • The value of final variables cannot be changed
  • Final methods cannot be overridden by subclasses
  • Final classes cannot be inherited
  • static fianl
  • When used in a field, it is shared by all instances as an immutable constant.
  • When used in methods, when declaring static methods that cannot be overridden
  • final If a field must have the same value for all instances of a class, it is better to use static final. However, if a final field must have a different value for each instance, you should not use static final.
  • Java best practices
  • When setting variables
  • final if the value should not change, static if it should be shared by all instances, static final if it is shared by all instances and the value should not change.
  • When setting method
  • To disable overriding, use fianl, or static in methods that must be called without creating an instance.
  • When setting a class
  • If inheritance is not possible, final

Among data types, primitive type and reference type

  • primitive type basic type
  • byte, short, int, long, float, double, char, boolean
  • reference type reference type
  • A type that stores the memory address of the object (actual data
  • Classes, interfaces, enumerations, arrays, etc.

Difference and purpose between process and thread, and how to create one

  • See [multiprocess and multithread](/study/2023/10/25/Interview.html#multiprocess and-multithread)
  • A process is an instance of a program and has one or more threads.
  • A thread is an independent execution path that runs within a process and shares the memory and resources of the process.
  • You can create a thread by extending the Thread class or implementing the Runnable interface.

What is serialization?

  • Process of converting the state of an object into a byte stream
  • Maintains data persistence and allows it to be used again as an object.
  • Serializable
import java.io.*;
class Person implements Serializable {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class SerializationExample {
public static void main(String[] args) {
Person p1 = new Person("John Doe", 25);
// serialize
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
out.writeObject(p1);
} catch (IOException e) {
e.printStackTrace();
}
// deserialize
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
Person p2 = (Person) in.readObject();
System.out.println(p2.name + ", " + p2.age);  // Output: John Doe, 25
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

@Override

  • By specifying overriding in Java, errors can be caught at compilation time.
  • Overriding is a concept used in object-oriented programming, which means allowing a subclass to redefine a method of a superclass within its own class.
  • Dynamic binding support

Difference between Array and ArrayList

  • Arrays have a fixed size when declared , can store both basic data and objects, index access is fast, but scalability is poor.
  • ArrayList adjusts its size dynamically and stores it as an object type. Various methods are provided by default

Concepts and uses of try, catch, and finally

  • try handles code that may cause exceptions, handles exceptions with catch, and finally always performs the task to be processed after executing try and catch.

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164