0
0
Javaprogramming~15 mins

Method declaration in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is a method declaration in Java?
A method declaration defines a block of code that performs a specific task. It includes the method's name, return type, parameters (if any), and the body with statements to execute.
touch_appClick to reveal answer
beginner
Identify the parts of this method declaration:
public int add(int a, int b) { return a + b; }
Parts:
- public: access modifier
- int: return type
- add: method name
- (int a, int b): parameters
- { return a + b; }: method body
touch_appClick to reveal answer
beginner
What does the return type in a method declaration specify?
It specifies the type of value the method will send back to the caller. If the method does not return a value, the return type is void.
touch_appClick to reveal answer
beginner
True or False: A method declaration must always have parameters.
False. A method can have zero or more parameters. Parameters are optional depending on what the method needs to do.
touch_appClick to reveal answer
beginner
What is the purpose of the method body in a method declaration?
The method body contains the code statements that define what the method does when it is called.
touch_appClick to reveal answer
Which part of a method declaration specifies the method's name?
AThe method body
BThe access modifier
CThe parameter list
DThe identifier after the return type
What keyword is used when a method does not return any value?
Anull
Bvoid
Cnone
Dempty
Which of these is a valid method declaration in Java?
Apublic int calculateSum(int a, int b) { return a + b; }
Bint calculateSum(a, b) { return a + b; }
Cpublic calculateSum(int a, int b) { return a + b; }
Dpublic int calculateSum(int a, int b)
Can a method have no parameters?
AYes, parameters are optional
BNo, parameters are required
COnly if the return type is void
DOnly if the method is static
What does the method body contain?
AThe method's name
BThe return type
CThe code statements to execute
DThe parameter list
Explain the components of a Java method declaration and their roles.
Describe how you would declare a method that returns no value and takes no parameters.