Page 281 - ComputerScience_Class_11
P. 281
• Return Statement: This statement is used when the function is returning some value. The returned value may be of any data
type, even non-primitive data types.
• Return Type: If the function is returning a value, then the data type of the value is mentioned here, in this part of the
method definition. The different data types written here are byte, short, int, long, float, double, char or Boolean. We can
also write a string if we want to return a word or a sentence. Other than these, all non-primitive data types such as array,
class, etc. can be used. In case, the function does not return any value, the keyword “void” is used in the method definition.
Example: public int twice (int i)
{
int tw=i*2;
return tw;
}
10. What is the difference between Actual Parameter and Formal Parameter?
Ans. The difference between actual parameters and formal parameters are the following:
Actual Parameter Formal Parameter
The actual values that are passed directly or through variables The values send by the calling program are received in the
to the respective method at the time of calling the method parameters described in the method. These parameters that
are called Actual Parameters. receives the values from the caller program are known as
Formal parameters.
D. Higher Order Thinking Skills (HOTS)
1. A method returns an integer value using a return statement placed inside an if–else block. Explain why only one return statement
is executed even though multiple return statements exist in the method.
Ans. Although a method may contain more than one return statement, only one of them is executed during a single method call.
The return statement that satisfies the condition gets executed first and immediately terminates the method. Once the method
terminates, control returns to the calling method and no other return statement in that method can be executed.
2. A programmer writes a Java program where the same calculation logic is written multiple times for different inputs. Later, the
programmer replaces this repeated code with a single user-defined method and calls it whenever required. Analyse how this
change affects program size, debugging effort and execution efficiency.
Ans. Using a user-defined method removes redundant code by writing the logic only once and reusing it through method calls. This
reduces the overall size of the program and makes it easier to debug because any error needs to be corrected in only one place.
It also improves execution efficiency and memory usage as the method is invoked only when required rather than repeating the
same statements multiple times.
E. Assertion and reasoning questions.
The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
appropriate option given below:
a. Both A and R are true and R is the correct explanation of A.
b. Both A and R are true but R is not the correct explanation of A.
c. A is true but R is false.
d. A is false but R is true.
1. Assertion (A): User-defined methods help in reducing redundancy in a Java program.
Reason (R): A method allows a block of statements to be written once and executed multiple times by calling it.
Ans. a. Both A and R are true and R is the correct explanation of A.
2. Assertion (A): The method signature consists of the method name and parameter list.
Reason (R): The method signature uniquely identifies a method for the compiler during method calls.
Ans. a. Both A and R are true and R is the correct explanation of A.
3. Assertion (A): In pass-by-value, changes made to formal parameters affect the actual parameters.
Reason (R): In pass-by-value, a copy of the actual parameter is passed to the called method.
Ans. d. A is false but R is true.
Methods and Constructors 279

