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:
Basic Method Invocation: Create a method named
printMessagethat prints "Hello, World!" to the console. Call this method from themainmethod.Method with Parameters: Write a method named
addthat takes two integers as parameters and returns their sum. Call this method from themainmethod and print the result.Method with Return Value: Create a method named
isEventhat takes an integer as parameter and returns true if it's even, otherwise false. Call this method from themainmethod and print the result.Void Method: Define a method named
printSquarethat takes an integer as parameter and prints its square. Call this method from themainmethod.Method Overloading: Create multiple versions of the
addmethod that accept different types of parameters (integers, doubles, etc.). Call each version from themainmethod and print the results.Recursive Method: Write a recursive method named
factorialthat calculates the factorial of a given number. Call this method from themainmethod and print the result.Passing Arrays to Methods: Define a method named
findMaxthat takes an array of integers as parameter and returns the maximum element. Call this method from themainmethod and print the result.Returning Arrays from Methods: Create a method named
generateArraythat generates an array of random integers of given size and returns it. Call this method from themainmethod and print the array.Method with Variable Arguments: Define a method named
averagethat takes a variable number of integers as arguments and returns their average. Call this method from themainmethod and print the result.Static Method: Create a static method named
isPrimethat checks if a given number is prime. Call this method from themainmethod and print the result.Passing Objects to Methods: Define a method named
swapthat takes two integer objects as parameters and swaps their values. Call this method from themainmethod and print the values before and after swapping.Method with Default Parameter: Write a method named
calculateInterestthat calculates simple interest with default rate (5%) and returns it. Call this method from themainmethod and print the result.Method with Exception Handling: Create a method named
dividethat takes two integers as parameters and handles division by zero exception. Call this method from themainmethod and print the result.Method with Object Return Type: Define a method named
createPersonthat takes a name and age as parameters and returns aPersonobject. Call this method from themainmethod and print the details of the person.Method Chaining: Implement a class with multiple methods where each method returns the object itself (
this). Chain these methods in themainmethod.Method with Recursive Object Return Type: Write a method named
getFibonacciSequencethat takes a numbernas parameter and returns an array containing the firstnFibonacci numbers. Call this method from themainmethod and print the array.Anonymous Method: Create an anonymous method that calculates the area of a rectangle with given length and width. Call this method from the
mainmethod 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:
Static Variable: Create a class
Counterwith a static variablecountinitialized to 0. Increment the value ofcountin a methodincrement().Non-Static Variable: Define a class
Personwith a non-static variablenameand initialize it in the constructor. Create multiple instances ofPersonwith different names.Static Method: Implement a class
MathUtilswith a static methodadd(int a, int b)that returns the sum of two integers passed as parameters.Non-Static Method: Create a class
Circlewith a non-static methodcalculateArea(double radius)that calculates the area of a circle.Static Constructor: Demonstrate that Java does not allow static constructors by attempting to define one in a class
StaticConstructor.Non-Static Constructor: Define a class
Employeewith non-static variables likename,age, andsalary, and initialize them in a non-static constructor.Static Variable Access: Access a static variable
countfrom theCounterclass in another classMainand print its value.Non-Static Variable Access: Access a non-static variable
namefrom thePersonclass in another classMainand print its value.Static Method Invocation: Call the static method
add()from theMathUtilsclass in themain()method of another class and print the result.Non-Static Method Invocation: Create an instance of the
Circleclass and call thecalculateArea()method with a radius value, then print the calculated area.Static Method Overloading: Define multiple versions of the
add()method in theMathUtilsclass that accept different parameter types (e.g., int, double).Non-Static Method Overloading: Implement a class
Calculatorwith overloaded methodsadd()andadd(int a, int b)to perform addition.Static Variable Modification: Modify the value of a static variable
countin theCounterclass from another classMainand 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
Was this helpful?