Java Questions 21 - 30  «Prev  Next»

Java Keywords and Identifiers

  1. What is the purpose of the keyword strictfp?

    Answer:
    strictfp forces floating points to adhere to the IEEE 754 standard. In Java, the strictfp keyword has one primary purpose:
    ✅ To enforce strict IEEE 754 compliance for floating-point calculations, ensuring predictable, platform-independent results.
    Beyond IEEE 754 Compliance – Does it Have Any Other Purpose?
    While strictfp doesn't serve a *separate* purpose, it indirectly supports these important goals in Java development:
    1. Cross-Platform Portability and Consistency
      • Without strictfp, some CPUs (e.g., x86) can use extended precision (80-bit registers), which may cause slightly different results on different platforms.
      • By using strictfp, you ensure consistency across all JVMs, regardless of underlying hardware.
    2. Debugging and Reproducibility
      • When debugging numerical code, small rounding differences across platforms can make bugs hard to track.
      • strictfp eliminates those subtle differences, making your numeric output reproducible.
    3. Deterministic Behavior in Scientific or Financial Applications
      • In scientific simulations or financial software where precision and determinism are critical, strictfp is used to guarantee repeatable results.


    Where Can strictfp Be Applied?
    Location Can Use strictfp?
    Class/Interface ✅ Yes
    Method ✅ Yes
    Variable ❌ No
    Constructor ❌ No

    Summary:
    🔹 strictfp doesn’t have multiple unrelated purposes—it only enforces strict IEEE 754 compliance—but this enables other important software qualities like platform independence, reproducibility, and easier testing/debugging of floating-point logic.
    The keyword strictfp can only be used with classes, interfaces and non-abstract methods.

  2. What can strictfp modify?

    Answer:
    stictfp can modify a 1) class or 2) non-abstract method declarations. A variable (such as a primitive int) can never be declared strictfp.

  3. What is the purpose of Java var-args syntax?

    Answer:
    The purpose of Java var-args is to allow you to create methods that can take a variable number of arguments.

  4. What is the name for the elements that you specify between the parentheses when you are invoking a method in Java?

    Answer:
    These are known as arguments.
    The "method call" below contains two arguments:
     getSalary("name", age);
     


  5. What are the elements in the method's signature that indicate what the method must receive when the method is called?

    Answer:
    These are known as parameters. The method declaration below contains 2 parameters.
    Employee getSalary (String name, int age) {} 
    

  6. Where does the "var-arg" parameter appear in a method declaration?

    Answer:
    In a method declaration, the var-arg must appear last.
    static int sum (int ... numbers){
     int total = 0;
      for (int i = 0; i &t; numbers.length; i++)
       total += numbers [i];
      return total;
    }
    


  7. What happens every time a new object is created?

    Answer:
    Everytime you make a new object, at least one constructor is invoked.

  8. When does the default "no-args" constructor get created?

    Answer:
    When you do not explicitly provide a constructor, the compiler will create a default "no-args" constructor.


  9.  


  10. Do constructors have a return type in Java?

    Answer:
    No, constructors do not have a return type in Java, not even void.
    Here’s why:
    • A constructor’s job is to initialize a new object of a class.
    • Unlike a method, a constructor doesn’t return a value, instead it returns the new instance implicitly.
    • Specifying a return type, even void, makes it a regular method, not a constructor.

    🔍 Example for clarity:
    public class MyClass {
        // This is a constructor (no return type)
        public MyClass() {
            System.out.println("Constructor called");
        }
    
        // This is a method (has a return type, so it's not a constructor)
        public void MyClass() {
            System.out.println("This is a method, not a constructor!");
        }
    }
    

    Output when instantiated:
    MyClass obj = new MyClass();
    

    Console Output:
    Constructor called
    
    ⚠️ The method void MyClass() is not a constructor, even though it looks similar, it will never be called automatically during object creation.
    ✅ Summary
    • Constructors: no return type
    • Methods: must have a return type
    • Mistaking one for the other can lead to unexpected behavior
    No, constructors do not have a return type and only methods have a return type.

  11. What type of access modifiers can constructors have?

    Answer:
    Constructors can have the standard access modifiers,
    public, protected, default, private and they can take arguments (including var-args), just like methods.

SEMrush Software