Navigate
JavaScript Tutorial for Beginners
The Ultimate CSS Tutorial for Beginners
Data Structure Tutorial for Beginners
ReactJS Tutorial for Beginners
Java Tutorial for Beginners
Node.js Tutorial for Beginners
SQL Tutorial for Beginners
Functions and Methods
Content
- Functions (Methods) in Java?
- Example
- Parameters-and-Return-Types
- Overloading-Methods
- Activity
- Updated on 10/09/2024
- 450 Views
Functions (Methods) in Java
Functions (or methods) are blocks of code that perform a specific task. Syntax: returnType methodName(parameters) { // body }
Example
Example of using methods in Java.
Parameters and Return Types
Methods can accept parameters and return values. Void methods do not return a value.
Overloading Methods
Methods can have the same name but different parameters. This is called method overloading.
Activity
Create a Java program with multiple methods to perform arithmetic operations (addition, subtraction, multiplication, division).
Quiz
1. What is the correct syntax for defining a method in Java?
- a) returnType methodName(parameters) { // body }
- b) methodName returnType(parameters) { // body }
- c) parameters methodName(returnType) { // body }
- d) methodName(parameters returnType) { // body }
2. Which keyword is used to call a method?
- a) void
- b) return
- c) call
- d) static
3. What will be the output of the following code?
- a) 6
- b) 5
- c) 2
- d) 3
4. Can a method be overloaded with different return types but the same parameters?
- a) Yes
- b) No
5. How do you call a method in Java?
- a) methodName;
- b) methodName[];
- c) methodName();
- d) methodName{}