Java Spring Boot Interview Questions

author image Hirely
at 05 Jan, 2025

Question: How do you create a class in Java?

Answer:

In Java, a class is a blueprint for creating objects (instances). A class contains fields (variables) and methods (functions) that define the properties and behavior of the objects created from it.

Steps to Create a Class in Java:

  1. Define the Class: A class is defined using the class keyword.
  2. Define Instance Variables (Fields): These variables represent the properties of the class.
  3. Define Methods: Methods define the actions or behavior that the class objects can perform.
  4. Constructor: A constructor is a special method used to initialize objects when they are created.

Syntax:

public class ClassName {
    // Instance variables (fields)
    private int field1;
    private String field2;

    // Constructor
    public ClassName(int field1, String field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    // Methods
    public void display() {
        System.out.println("Field1: " + field1);
        System.out.println("Field2: " + field2);
    }

    // Getter and Setter Methods
    public int getField1() {
        return field1;
    }

    public void setField1(int field1) {
        this.field1 = field1;
    }
}

Explanation:

  1. Class Declaration:

    • The class is defined using the class keyword followed by the class name (e.g., ClassName).
    • By convention, class names in Java start with an uppercase letter.
  2. Instance Variables (Fields):

    • Inside the class, we declare variables to represent the properties of the objects.
    • In the example above, field1 is an integer and field2 is a string.
  3. Constructor:

    • The constructor is a special method that is called when an object of the class is created.
    • It has the same name as the class and does not have a return type.
    • In the example, the constructor initializes the field1 and field2 variables.
  4. Methods:

    • Methods define the behavior of the class.
    • In the example, the display() method prints the values of field1 and field2.
    • Getter and setter methods (getField1() and setField1()) are also provided to access and modify the private variables.

Example of Creating and Using a Class:

public class Car {
    // Instance variables
    private String model;
    private int year;

    // Constructor
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    // Method to display details of the car
    public void displayDetails() {
        System.out.println("Car Model: " + model);
        System.out.println("Car Year: " + year);
    }

    // Getter and Setter methods
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public static void main(String[] args) {
        // Creating an object of the Car class
        Car myCar = new Car("Toyota Camry", 2020);

        // Calling the displayDetails method
        myCar.displayDetails();
    }
}

Explanation of Example:

  1. Class Definition: The Car class has two fields (model and year), a constructor to initialize these fields, and a method (displayDetails) to print them.
  2. Creating an Object: In the main() method, an object of the Car class is created using the new keyword.
    Car myCar = new Car("Toyota Camry", 2020);
  3. Calling Methods: After creating the object, we call the displayDetails() method to print the details of the car.

Example Output:

Car Model: Toyota Camry
Car Year: 2020

Key Points:

  • A class in Java is a template for creating objects.
  • Use the class keyword followed by the class name.
  • Instance variables store the properties of the object.
  • Constructor initializes the object’s properties when it is created.
  • Methods define the behavior of the object.

Question: How to swap two numbers without using a third variable in Java?

Answer:

In Java, you can swap two numbers without using a third (temporary) variable by using mathematical operations or bitwise XOR. Below are two common methods to swap two numbers without an extra variable:


Method 1: Using Arithmetic Operations (Addition and Subtraction)

You can swap two numbers by leveraging addition and subtraction. Here’s how:

  1. Add the two numbers and store the result in one of the variables.
  2. Subtract the second number from the result to get the first number.
  3. Subtract the first number (which now contains the sum of both numbers) from the result to get the second number.

Example:

public class SwapNumbers {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;

        // Swapping without using a third variable
        a = a + b; // a becomes 15
        b = a - b; // b becomes 5
        a = a - b; // a becomes 10

        // Output swapped values
        System.out.println("a: " + a); // 10
        System.out.println("b: " + b); // 5
    }
}

Output:

a: 10
b: 5

Explanation:

  1. Initially, a = 5 and b = 10.
  2. After a = a + b, a becomes 15 (5 + 10).
  3. Then, b = a - b results in b = 5 (15 - 10).
  4. Finally, a = a - b results in a = 10 (15 - 5).

Method 2: Using Bitwise XOR

Bitwise XOR can also be used to swap two numbers without a third variable. This method is faster than arithmetic operations because it works directly at the binary level.

The steps are as follows:

  1. XOR the two numbers and store the result in one of the variables.
  2. XOR the result with the second number to get the original first number.
  3. XOR the result with the first number to get the original second number.

Example:

public class SwapNumbers {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;

        // Swapping using XOR
        a = a ^ b; // a becomes 15 (binary: 0101 ^ 1010 = 1111)
        b = a ^ b; // b becomes 5 (binary: 1111 ^ 1010 = 0101)
        a = a ^ b; // a becomes 10 (binary: 1111 ^ 0101 = 1010)

        // Output swapped values
        System.out.println("a: " + a); // 10
        System.out.println("b: " + b); // 5
    }
}

Output:

a: 10
b: 5

Explanation:

  1. Initially, a = 5 (binary 0101) and b = 10 (binary 1010).
  2. After a = a ^ b, a becomes 1111 (binary XOR of 0101 and 1010).
  3. Then, b = a ^ b results in b = 5 (binary XOR of 1111 and 1010).
  4. Finally, a = a ^ b results in a = 10 (binary XOR of 1111 and 0101).

Key Points:

  • Arithmetic method: It uses addition and subtraction to swap values but may cause overflow if the values are too large (though this is rare with small numbers).
  • Bitwise XOR method: It works efficiently at the binary level and doesn’t have the risk of overflow. It is considered a more elegant solution.

Both methods allow you to swap two numbers without using a third variable, and both are commonly used in coding challenges and interview questions.

Read More

If you can’t get enough from this article, Aihirely has plenty more related information, such as Java interview questions, Java interview experiences, and details about various Java job positions. Click here to check it out.

Related Posts

Trace Job opportunities

Hirely, your exclusive interview companion, empowers your competence and facilitates your interviews.

Get Started Now