Java-Methods
Interview Questions of methods
Scenario 1: Code Reusability and Method Design
Scenario:
You are developing a student management system. You notice that the logic for calculating the average marks of a student is repeated in multiple places.
Question:
How would you improve this code using Java methods? What are the benefits?
Answer:
I would extract the repeated logic into a single method, such as calculateAverage(int[] marks). This improves code reusability, reduces duplication, and makes maintenance easier. If the calculation needs to change, I only update it in one place.
Scenario:
You are developing a student management system. You notice that the logic for calculating the average marks of a student is repeated in multiple places.
Question:
How would you improve this code using Java methods? What are the benefits?
Answer:
I would extract the repeated logic into a single method, such as calculateAverage(int[] marks). This improves code reusability, reduces duplication, and makes maintenance easier. If the calculation needs to change, I only update it in one place.
Scenario 2: Static vs. Instance Methods
Scenario:
You have a utility class with a method that converts Celsius to Fahrenheit. This method does not depend on any instance variables.
Question:
Should this method be static or instance? Why?
Answer:
It should be a static method because it doesn’t require access to object state. Static methods belong to the class and can be called without creating an object, making them ideal for utility functions.
Java will automatically call the appropriate method based on the argument type.
Scenario:
You have a utility class with a method that converts Celsius to Fahrenheit. This method does not depend on any instance variables.
Question:
Should this method be static or instance? Why?
Answer:
It should be a static method because it doesn’t require access to object state. Static methods belong to the class and can be called without creating an object, making them ideal for utility functions.
Scenario 3: Passing Arguments and Parameters
Scenario:
You have a method printDetails(String name, int age). In your main method, you want to display the details of several people.
Question:
What are the terms for the variables in the method definition and the values you pass when calling the method? How do they relate?
Answer:
The variables in the method definition (name, age) are parameters. The actual values passed in the method call (e.g., "Alice", 25) are arguments. Arguments are assigned to parameters when the method is invoked.
Scenario:
You have a method printDetails(String name, int age). In your main method, you want to display the details of several people.
Question:
What are the terms for the variables in the method definition and the values you pass when calling the method? How do they relate?
Answer:
The variables in the method definition (name, age) are parameters. The actual values passed in the method call (e.g., "Alice", 25) are arguments. Arguments are assigned to parameters when the method is invoked.
Scenario 4: Method Overloading
Scenario:
You want to create a logging utility that can log both string messages and integer codes.
Question:
How would you design your methods to handle both types of input using the same method name?
Answer:
I would use method overloading by defining two methods with the same name but different parameter types:
Scenario:
You want to create a logging utility that can log both string messages and integer codes.
Question:
How would you design your methods to handle both types of input using the same method name?
Answer:
I would use method overloading by defining two methods with the same name but different parameter types:
void log(String message) { ... }void log(int code) { ... }
Java will automatically call the appropriate method based on the argument type.
Scenario 5: Abstract Methods
Scenario:
You are designing a shape hierarchy where each shape must implement its own area calculation, but the formula differs by shape.
Question:
How would you enforce that every subclass provides its own implementation for calculating area?
Answer:
I would declare an abstract method in the base class:
Scenario:
You are designing a shape hierarchy where each shape must implement its own area calculation, but the formula differs by shape.
Question:
How would you enforce that every subclass provides its own implementation for calculating area?
Answer:
I would declare an abstract method in the base class:
abstract class Shape { abstract double area();}
Each subclass (e.g., Circle, Rectangle) must override and implement the area() method.
Scenario 6: main() Method Variations
Scenario:
A candidate tries to declare the main method as private in a Java application.
Question:
What will happen if the main method is not declared as public? Why?
Answer:
The program will compile, but the JVM will not be able to access the main method at runtime, resulting in a runtime error. The main method must be public static void main(String[] args) so the JVM can invoke it as the entry point.
Scenario:
A candidate tries to declare the main method as private in a Java application.
Question:
What will happen if the main method is not declared as public? Why?
Answer:
The program will compile, but the JVM will not be able to access the main method at runtime, resulting in a runtime error. The main method must be public static void main(String[] args) so the JVM can invoke it as the entry point.