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
printMessage
that prints "Hello, World!" to the console. Call this method from themain
method.Method with Parameters: Write a method named
add
that takes two integers as parameters and returns their sum. Call this method from themain
method and print the result.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 themain
method and print the result.Void Method: Define a method named
printSquare
that takes an integer as parameter and prints its square. Call this method from themain
method.Method Overloading: Create multiple versions of the
add
method that accept different types of parameters (integers, doubles, etc.). Call each version from themain
method and print the results.Recursive Method: Write a recursive method named
factorial
that calculates the factorial of a given number. Call this method from themain
method and print the result.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 themain
method and print the result.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 themain
method and print the array.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 themain
method and print the result.Static Method: Create a static method named
isPrime
that checks if a given number is prime. Call this method from themain
method and print the result.Passing Objects to Methods: Define a method named
swap
that takes two integer objects as parameters and swaps their values. Call this method from themain
method and print the values before and after swapping.Method with Default Parameter: Write a method named
calculateInterest
that calculates simple interest with default rate (5%) and returns it. Call this method from themain
method and print the result.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 themain
method and print the result.Method with Object Return Type: Define a method named
createPerson
that takes a name and age as parameters and returns aPerson
object. Call this method from themain
method 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 themain
method.Method with Recursive Object Return Type: Write a method named
getFibonacciSequence
that takes a numbern
as parameter and returns an array containing the firstn
Fibonacci numbers. Call this method from themain
method 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
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:
Static Variable: Create a class
Counter
with a static variablecount
initialized to 0. Increment the value ofcount
in a methodincrement()
.Non-Static Variable: Define a class
Person
with a non-static variablename
and initialize it in the constructor. Create multiple instances ofPerson
with different names.Static Method: Implement a class
MathUtils
with a static methodadd(int a, int b)
that returns the sum of two integers passed as parameters.Non-Static Method: Create a class
Circle
with 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
Employee
with non-static variables likename
,age
, andsalary
, and initialize them in a non-static constructor.Static Variable Access: Access a static variable
count
from theCounter
class in another classMain
and print its value.Non-Static Variable Access: Access a non-static variable
name
from thePerson
class in another classMain
and print its value.Static Method Invocation: Call the static method
add()
from theMathUtils
class in themain()
method of another class and print the result.Non-Static Method Invocation: Create an instance of the
Circle
class and call thecalculateArea()
method with a radius value, then print the calculated area.Static Method Overloading: Define multiple versions of the
add()
method in theMathUtils
class that accept different parameter types (e.g., int, double).Non-Static Method Overloading: Implement a class
Calculator
with overloaded methodsadd()
andadd(int a, int b)
to perform addition.Static Variable Modification: Modify the value of a static variable
count
in theCounter
class from another classMain
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