Page 280 - ComputerScience_Class_11
P. 280
7. ………………… are methods that have the same name as that of a class.
8. The ………………… keyword is required to use the object of the class from the member method of the same class.
Answers
1. method name 2. Method prototype 3. Methods 4. Private
5. nested loop 6. Member Methods 7. constructors 8. this
C. Answer the following questions:
1. What is the use of a method?
Ans. The methods are useful as they occupy less space in memory and the execution becomes faster.
2. What is the syntax of declaring a method?
Ans. <access specifier> <return-type> <name-of-method> <parameter-list>
{Job done by the method;}
3. What is a Method Header? Give an example.
Ans. The first line of any method definition is known as the Method Header or Method Prototype. The different parts it contains are
the Access Specifier, Return Data Type, Method Name and the Parameter List.
For example: public int twice (int n)
4. What is an Access Specifier? Name the different Access Specifiers.
Ans. An Access Specifier defines how the data members and methods of a class can be accessed. The three different Access Specifiers
are public, private and protected.
5. What is a user-defined method in Java?
Ans. Sometimes, we may come across certain problems which require executing some tasks repeatedly, but with different input values.
This is exactly the situation where we use user-defined methods.
6. What is meant by a method signature?
Ans. The name of the method along with its parameter list in the method header is called the method signature.
7. Describe Method Header with an example.
Ans. The first line of any method definition is known as the method header or method prototype. It consists of the access specifier,
return data type, method name and the parameter list. Let us consider the following example:
public int twice (int n)
where:
public: Access Specifier
int: Return Type
twice: Method Name
int n: Parameter List
8. What are access specifiers in Java? Explain public, private and protected access specifiers with examples.
Ans. There has to be some restriction on the accessibility of the methods as they can also be accessed by other methods, same or
different class. Hence, access specifiers help a method to be accessed by the objects of the class in different ways. So, access
specifiers are segregated into three different types which are as follows:
• Public: The keyword “Public” is used to give access to the methods of any class.
Example: public int twice (int i)
• Private: The keyword “Private” allows the methods to be accessed only by the same class.
Example: private int twice (int i)
• Protected: The keyword “Protected” allows other methods of the same class and the inherited class to access the method.
Example: protected int twice (int i)
9. Explain return type and return statement in Java methods. Give example.
Ans. A function can be used only when it is called from another function. So, after completing the assigned job, the control returns
back to the part from where it is called. While doing this, it may or may not return a value along with it. Thus, we may need the
return type and return statement.
278 Touchpad Computer Science (Ver. 3.0)-XI

