User-Defined Methods
A. Tick (√ ) the correct option.
1. Once a function is defined, it can be invoked repeatedly. What is this feature called?
a. Interface b. Reusability
c. Restructuring d. All of these
Ans. b. Reusability
2. If a function does not return any value, its return type should be:
a. int b. no-return
c. void d. empty
Ans. c. void
3. A function that computes a value and returned is called:
a. Computational function b. Manipulative function
c. Procedural function d. None of these
Ans. a. Computational function
4. A type of parameter that are used to identify what data is to be passed to a function is called:
a. Formal parameter b. Actual parameter
c. Both a and b d. None of these
Ans. a. Formal parameter
5. The parameter list in a function definition is called:
a. Function prototype b. Function signature
c. Both a and b d. None of these
Ans. b. Function signature
6. The first line of the function definition is called:
a. Function prototype b. Function signature
c. Both a and b d. None of these
Ans. a. Function prototype
7. The number of values that a function can return is:
a. 1 b. 2 c. 3 d. 4
Ans. a. 1
8. A local variable in a function has its scope:
a. Limited within the function
b. Can be accessed anywhere within the same class
c. No limitation at all
d. None of these
Ans. a. Limited within the function
9. Which among the following is a valid name for a function?
a. function b. 2function
c. fun in action d. fun#
Ans. a. function
10. Which among the following is not a valid access specifier?
a. public b. private
c. partially d. protected
Ans. c. partially
11. A method that does not return a value has a ........... return type.
- double
- class
- float
- void ✓
12. A method can return ...........
- any number of values
- 2 values
- only 1 value ✓
- 3 values
13. If a method returns a value, then it must be ...........
- of the same data type as defined in its prototype ✓
- void type
- double type
- boolean type
14. Parameters in the method definition are called ...........
- actual parameters
- formal parameters ✓
- informal parameters
- void parameters
15. The parameters that are passed to the method when it is invoked are called ...........
- formal parameters
- actual parameters ✓
- informal parameters
- void parameters
16. The method that changes the state of an object is known as ...........
- pure method
- impure method ✓
- perfect method
- imperfect method
17. The scope of a local variable is limited to the ...........
- Windows
- Multiple programs
- Class
- Method or block it is declared in ✓
18. The technique in which the change in the formal parameter gets reflected in the actual parameter is known as ...........
- call by reference ✓
- call by value
- call by argument
- call by method
19. In which technique are the values of actual parameters copied to the formal parameters?
- call by reference
- call by value ✓
- call by argument
- call by method
20. A method with many definitions is known as ...........
- many method
- multiple method
- void method
- overloaded method ✓
- pure function
- impure function ✓
- replace function
- none of the above
- defined parameter
- passed parameter
- actual parameter ✓
- formal parameter
- forward parameter
- actual parameter
- formal parameter ✓
- none of the above
- call by reference ✓
- call by value
- call by method
- none
- multiple function
- function overloading ✓
- floating function
- none
- return ✓
- call
- promote
- none
- int
- float
- double
- void ✓
- { } ✓
- [ ]
- ( )
- under a rectangular box
- object ✓
- system
- parameter
- none
B. State whether the given statements are True or False
Question 1 : A method may contain any number of return statements.
Ans: True
Question 2: The non-static methods need an instance to be called.
Ans: True
Question 3: A method can return more than one value.
Ans: False
Question 4: Methods defined as void must return a value.
Ans: False
Question 5: The static methods need an instance to be called.
Ans: False
Question 6: In Java, all primitive types are passed by value and all reference types are passed by reference.
Ans: True
Question 7: You can place the return statement in a void method without any expression.
Ans: True
Question 8: If a method returns a value, then it must be of the same data type as defined in the method prototype.
Ans: True
Question 9: Parameters in the method definition are called dummy parameters.
Ans: True
Question 10: Methods reside in a class in Java.
Ans: True
Question 11: Method overloading is one of the ways by which Java implements polymorphism.
Ans: True
Question 12: The scope of a local variable is limited to the method or the block it is declared in.
Ans: True
Question 13: The keyword static makes a method a class method.
Ans: True
Question 14: An impure method always returns the same value when the same arguments are given.
Ans: False
C. Fill in the blanks.
1. In Java functions are known as _________________.
Ans: methods
2. Methods are contained in __________.
Ans: class
3. The function name and the parameter list together is known as ___________.
Ans: function signature
4. The access specifier, return type and the function signature together is known as __________.
Ans: prototype
5. The arguments of the function given in function prototype are called ___________.
Ans: formal arguments
6. The arguments of the function given in statement that calls the function are called _________.
Ans: actual arguments
7. If a function does not return any value, the returning type in the function prototype will be ____.
Ans: void
8. When a function is called by _____, the values of actual parameters are copied into separate
memory locations as allocated by the formal parameters.
Ans: value
9. Impure functions uses call by _______.
Ans: reference
10. One of the practical implementation of polymorphism is __________.
Ans: overloading
11. A function can return only ___________value to its caller program.
Ans: one
12. If a function does not return any value its return type is _____.
Ans: void
13. A function indicating the function name, return type along with function arguments is known as _______.
Ans: function header/prototype.
14.The variables used to receive the values in function header are known as _______.
Ans. formal parameters
15. Method in a Java program resides in _____.
Ans. package
16. The entire function body is enclosed under ________brackets.
Ans. curly
17. The ____________ function performs some actions without returning any output.
Ans. procedural
18. A function contains _____and _______.
Ans. header and body
19. Functions used with same name but different types of arguments are known as ___________.
Ans. function overloading.
20. A function that is called by itself in its body is known as __________.
Ans. recursive function
D. Answer the following questions.
1. What is a method?
Ans. A Java method is a collection of statements that are grouped together to perform an operation.
2. Write two advantages of using functions in a program.
Ans. Reduces complexity and Reusability.
3. Explain the function of the ‘return’ statement.
Ans. The ‘return’ statement is used to return a value from a method, before exiting from the function.
4. If a function contains several return statements, how many of them will be executed?
Ans. The first one.
5. Name the keyword that causes the control to transfer back to the method call.
Ans. return
6. What is the role of the keyword ‘void’ in declaring functions?
Ans. The void keyword ensures that a method do not return any value.
7. Classify functions depending upon the value it returns.
Ans. Computational function, Procedural functions and Manipulative functions
9. State the difference between function prototype and function signature.
Ans. Function prototype is the first line of the function definition that consist of return type function
name and the parameter list. Function signature on the other hand only specifies the parameter
list.
10. How are functions called? How does it return a value?
Ans. Functions are called using the name of the function, they may be called using either:
• Call by Value • Call by reference
The return statement is used to return a value from a function.
11. How are the following passed?
(i) Primitive types (ii) Reference types
Ans. (i) Call by value
(ii) Call by reference
12. Which OOP principle implements function overloading?
Ans. Polymorphism
13. When there are multiple definitions with the same function name, what makes them different
from each other?
Ans. Function signature
14. What are the different access specifiers available in Java?
Ans. default, public, private and protected
15. What is the use of main() method?
Ans. The main() method, in general, is from where a program execution begins in Core Java.
16. How are static methods of one class called by methods in other classes?
Ans. The function invocation should be preceded with the name of the class followed by the dot
operator.
17. Define a function. What is meant by function prototype?
Ans: A function or a method is a sequence of statements grouped together and given a name. This group of statements can be called at any point in the program using its name to perform a specific task.
First line of function definition that tells about the type of value returned by the function and the number and type of arguments is called function prototype.
18. What are the two ways of invoking functions?
Ans : Two ways of invoking functions are:
Pass by value.
Pass by reference.
19. When a function returns the value, the entire function call can be assigned to a variable. Do you agree with the statement?
Ans: Yes, when a function returns a value, we can assign the entire function call to a variable. Below example illustrates the same:
public class Example
{
public int sum(int a, int b)
{
int c = a + b;
return c;
}
public static void main(String args[])
{
Example obj = new Example();
int x = 2, y = 3;
int z = obj.sum(x, y);
System.out.println(z);
}
}
20. When a function is invoked how many values can be returned from the function?
Ans: A function can only return a single value.
21. Debug the errors and rewrite the following function prototypes:
(a) int sum(x,y);
Ans: int sum(int x, int y);
(b) float product(a,int y);
Ans: float product(float a, int y);
(c) float operate(int x, float=3.4);
Ans: float operate(int x, float y);
(d) float sum(int x,y);
Ans: float sum(int x, float y);
22. In what situation does a function return a value?
Ans: For a function to return a value, it should have a return type other than void in its function prototype and it should return a value of the corresponding type using the return statement in the function body.
23. Differentiate between pure and impure functions.
Ans: Pure functions
- Pure functions take objects and/or primitive data types as arguments but does not modify the objects.
- Pure functions doesn't have side effects.
Impure functions
- Impure functions change the state of received objects.
- Impure functions have side effects.
24. Differentiate between call by value and call by reference.
Ans: Call by value
- Actual parameters are copied to formal parameters. Any changes to formal parameters are not reflected onto the actual parameters.
- All primitive data types are passed using Call by value
Call by reference
- Formal parameters refer to actual parameters. The changes to formal parameters are reflected onto the actual parameters.
- All reference data types like arrays and objects of classes are passed using Call by reference.
25. What are the advantages of defining a method in a program?
Ans: Advantages of defining methods in a program are:
- Methods help to manage the complexity of the program by dividing a bigger complex task into smaller, easily understood tasks.
- Methods are useful in hiding the implementation details.
- Methods help with code reusability.
26. What is meant by function overloading? In what way it is advantageous?
Ans: Function overloading is the process of defining functions/methods within a class, that have the same name but differ in the number and/or the data types of their arguments. Advantages of function overloading are:
Function overloading is one of the ways in which Java implements the object oriented concept of Polymorphism.
With Function overloading, programmers don't have to create and remember different names for functions doing the same thing for different data types.
27. Define the following:
(a) Return data type
Return data type specifies the type of value that the method should return. It is mentioned before the method name in the method prototype. It can be any valid primitive or composite data type of Java. If no value is being returned, it should be void.
(b) Access specifier
Access specifiers determine the type of access to the method. It can be either public, private or protected.
(c) Parameter list
Parameter list is a comma-separated list of variables of a method along with their respective data types. The list is enclosed within a pair of parentheses. Parameter list can be empty if the method doesn't accept any parameters when it is called.
(d) Recursive function
A function that calls itself inside its body is called a Recursive function.
(e) Method signature
Method signature comprises of the method name and the data types of the parameters. For example, consider the below method:
int sum(int a, int b) {
int c = a + b;
return c;
}
Its method signature is:
sum(int, int)
28. Explain the function of a return statement in Java programming.
Ans: A function returns a value through the return statement. Once a return statement is executed, the program control moves back to the caller function skipping the remaining statements of the current function if any. A function can have multiple return statements but only one of them will be executed. For example, consider the below method:
int sum(int a, int b)
{
int c = a + b;
return c;
}
It uses a return statement to return a value of int type back to its caller.
29. What is the role of the keyword void in declaring functions?
Ans: The keyword 'void' signifies that the function doesn't return a value to the calling function.
30. If a function contains several return statements, how many of them will be executed?
Ans: A function can have multiple return statements but only one of them will be executed because once a return statement is executed, the program control moves back to the caller function skipping the remaining statements of the current function.
31. Which OOP principle implements function overloading?
Ans: Polymorphism implements function overloading.
32. How are the following data passed to a function?
Ans:
- Primitive types
By value
- Reference types
By reference
E. Write function prototypes for the following:
1. Private access method sum which accepts three int type variables as parameters and return a
float type.
Ans. private float sum(int a,int b,int c)
2. Default access method “length” which accepts a String type variable as parameter and returns
an int type.
Ans. int length(String s)
3. Public access method “increment” which accepts an object of Myclass type as parameter and
does not return any value.
Ans. public void increment(Myclass ob)
4. Protected access method largest which accepts a float type, int type and double type data as
parameters and have a byte type as return type.
Ans. protected byte largest(float a, int b, double c)
5. Public access static method calculate which accepts a byte and int type data type as parameters
and return float type.
Ans. public static float calculate(byte b, int d)
6. Write the function prototype for the function “sum” that takes an integer variable (x) as its
argument and returns a value of float data type.
Ans. float sum(int x)
7. Write the prototype of a function which takes in 2 integers and 1 String arguments and returns
a value which is either ‘true’ or false’.
Ans. boolean function(int a,int b,String c)
0 Comments