Categories
Java

Java Interview Questions for Freshers: Part 2

Java Interview Questions for Freshers: Part - 2

Java Interview Questions for Freshers: Part - 2

11. Can you tell the difference between equals() method and equality operator (==) in Java?

The equals() method in Java is used to compare the contents or values of objects to check if they are equivalent. It’s a method that can be overridden by classes to define their own notion of equality.

The == operator, on the other hand, checks for reference equality. For primitive types, it checks for value equality. For objects, == checks if two references point to the same memory location, not necessarily if the actual content of the objects is the same.

12. How can an infinite loop be created in Java?

An infinite loop in Java can be created using constructs like while(true), for(;;), or by having a condition that always evaluates to true within the loop.

java

while (true) {

    // Code that runs indefinitely

}{

13. Can you provide a concise explanation of constructor overloading in Java?

Constructor overloading in Java means having multiple constructors within a class, each with a different parameter list. This allows creating objects in different ways without necessarily having the same set of parameters for object initialization.

14. Define Copy constructor in Java.

In Java, a copy constructor is a constructor that takes an object of the same class as a parameter and creates a new object by copying the values of the fields from the passed object. It’s used to create a copy of an existing object.

java

public class MyClass {

    private int value;

 

    // Copy constructor

    public MyClass(MyClass another) {

        this.value = another.value;

    }

}

15. Can the main method be Overloaded?

Yes, the main method in Java can be overloaded. Java allows multiple main methods in the same class with different parameter lists. However, only the public static void main(String[] args) method is the entry point for JVM execution.

16. Compare method overloading and method overriding using relevant examples in Java.

Method overloading occurs within the same class when multiple methods share the same name but have different parameter lists. Method overriding happens in subclasses, where a method in the subclass has the same signature (name and parameter list) as a method in its superclass.

java

class Parent {

    void display() {

        System.out.println(“Parent’s display”);

    }

}

 

class Child extends Parent {

    void display() {

        System.out.println(“Child’s display”);

    }

}

 

// Method overriding

Parent obj = new Child();

obj.display(); // Output: “Child’s display”

 

// Method overloading

class Example {

    void show(int a) {

        System.out.println(“show with int parameter”);

    }

 

    void show(double a) {

        System.out.println(“show with double parameter”);

    }

}

17. Elaborate on the possibility of having a single try block with multiple catch blocks coexisting in a Java Program.

In Java, a single try block can have multiple catch blocks, allowing you to handle different types of exceptions separately. When an exception occurs within the try block, the JVM compares the type of the thrown exception against each catch block’s parameter types until it finds a match, and the appropriate catch block executes.

java

try {

    // Code that may throw exceptions

} catch (IOException e) {

    // Handle IOException

} catch (SQLException e) {

    // Handle SQLException

} catch (Exception e) {

    // Catch-all for other exceptions

}

18. Describe how the final keyword is utilized in variables, methods, and classes.

  • final variables: When applied to a variable, it makes the variable a constant whose value cannot be changed.
  • final methods: Prevents methods from being overridden in subclasses.
  • final classes: Prevents inheritance, meaning the class cannot be subclassed.

19. Do final, finalize and finalise keywords have the same function?

No, they have different functions:

  • final: Used with variables, methods, and classes for different purposes as explained earlier.
  • finalize(): It’s a method called by the garbage collector on an object when it determines that there are no more references to the object, allowing the object to do some cleanup before it’s garbage collected.
  • finalise: It seems like a spelling variation of finalize. In Java, the method is spelled finalize.

20. Is there a scenario where the 'finally' block might not execute in Java? If so, illustrate the case.

In Java, the finally block is designed to execute even in exceptional situations, unless the program is terminated abnormally (like System.exit(0) or a crash). If the JVM is shut down forcibly or there’s a hardware failure, the finally block may not execute.

In conclusion,

mastering Java is a crucial step for freshers looking to excel in the competitive IT landscape. Our compilation of Java interview questions for freshers is designed to be a comprehensive guide, helping you prepare with confidence. Whether you’re just starting your career or aiming for new opportunities, a solid understanding of Java is a valuable asset.

Ready to take your Java skills to the next level? Explore our top-notch Java Training in Chennai. Our expert instructors and hands-on approach ensure that you not only ace interviews but also thrive in real-world scenarios. To kickstart your journey to Java excellence, contact us at +91 9159-333-334. Secure your future today with the best Java Training in Chennai. Don’t miss out on the chance to propel your career forward!

Java Interview Questions for Freshers: Part 1

Java Interview Questions for Freshers: Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *