Methods

Here's a mix of basic to advanced problem statements covering a wide range of scenarios for testing your knowledge on methods in Java:

  1. Basic Method Invocation: Create a method named printMessage that prints "Hello, World!" to the console. Call this method from the main method.

  2. Method with Parameters: Write a method named add that takes two integers as parameters and returns their sum. Call this method from the main method and print the result.

  3. Method with Return Value: Create a method named isEven that takes an integer as parameter and returns true if it's even, otherwise false. Call this method from the main method and print the result.

  4. Void Method: Define a method named printSquare that takes an integer as parameter and prints its square. Call this method from the main method.

  5. Method Overloading: Create multiple versions of the add method that accept different types of parameters (integers, doubles, etc.). Call each version from the main method and print the results.

  6. Recursive Method: Write a recursive method named factorial that calculates the factorial of a given number. Call this method from the main method and print the result.

  7. Passing Arrays to Methods: Define a method named findMax that takes an array of integers as parameter and returns the maximum element. Call this method from the main method and print the result.

  8. Returning Arrays from Methods: Create a method named generateArray that generates an array of random integers of given size and returns it. Call this method from the main method and print the array.

  9. Method with Variable Arguments: Define a method named average that takes a variable number of integers as arguments and returns their average. Call this method from the main method and print the result.

  10. Static Method: Create a static method named isPrime that checks if a given number is prime. Call this method from the main method and print the result.

  11. Passing Objects to Methods: Define a method named swap that takes two integer objects as parameters and swaps their values. Call this method from the main method and print the values before and after swapping.

  12. Method with Default Parameter: Write a method named calculateInterest that calculates simple interest with default rate (5%) and returns it. Call this method from the main method and print the result.

  13. Method with Exception Handling: Create a method named divide that takes two integers as parameters and handles division by zero exception. Call this method from the main method and print the result.

  14. Method with Object Return Type: Define a method named createPerson that takes a name and age as parameters and returns a Person object. Call this method from the main method and print the details of the person.

  15. Method Chaining: Implement a class with multiple methods where each method returns the object itself (this). Chain these methods in the main method.

  16. Method with Recursive Object Return Type: Write a method named getFibonacciSequence that takes a number n as parameter and returns an array containing the first n Fibonacci numbers. Call this method from the main method and print the array.

  17. Anonymous Method: Create an anonymous method that calculates the area of a rectangle with given length and width. Call this method from the main method and print the result.

Static vs Non-Static

Absolutely! Here are 20 problem statements covering static and non-static variables, methods, and constructors in Java, ranging from basic to advanced scenarios:

  1. Static Variable: Create a class Counter with a static variable count initialized to 0. Increment the value of count in a method increment().

  2. Non-Static Variable: Define a class Person with a non-static variable name and initialize it in the constructor. Create multiple instances of Person with different names.

  3. Static Method: Implement a class MathUtils with a static method add(int a, int b) that returns the sum of two integers passed as parameters.

  4. Non-Static Method: Create a class Circle with a non-static method calculateArea(double radius) that calculates the area of a circle.

  5. Static Constructor: Demonstrate that Java does not allow static constructors by attempting to define one in a class StaticConstructor.

  6. Non-Static Constructor: Define a class Employee with non-static variables like name, age, and salary, and initialize them in a non-static constructor.

  7. Static Variable Access: Access a static variable count from the Counter class in another class Main and print its value.

  8. Non-Static Variable Access: Access a non-static variable name from the Person class in another class Main and print its value.

  9. Static Method Invocation: Call the static method add() from the MathUtils class in the main() method of another class and print the result.

  10. Non-Static Method Invocation: Create an instance of the Circle class and call the calculateArea() method with a radius value, then print the calculated area.

  11. Static Method Overloading: Define multiple versions of the add() method in the MathUtils class that accept different parameter types (e.g., int, double).

  12. Non-Static Method Overloading: Implement a class Calculator with overloaded methods add() and add(int a, int b) to perform addition.

  13. Static Variable Modification: Modify the value of a static variable count in the Counter class from another class Main and print its updated value.

These problem statements cover a wide range of scenarios, from basic method invocation to more advanced concepts like recursion, lambda expressions, and static/non-static method elements in Java.

Last updated