IT1908 Method Naming Conventions
13 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the access modifier 'protected' imply about method visibility?

  • It can only be accessed within its own package and by subclasses. (correct)
  • It is accessible by all classes regardless of package.
  • It is only accessible within the class that defines it.
  • It allows access to only subclasses in the same package.

Which optional specifier indicates that a method cannot be overridden by a subclass?

  • final (correct)
  • public
  • static
  • abstract

What is indicated by a method's return type being declared as 'void'?

  • The method does not return any value. (correct)
  • The method can return an object.
  • The method may throw an exception.
  • The method returns an integer value.

Which of the following is true about the 'package-private' access modifier?

<p>It provides access only within the package but not to subclasses. (C), It is the default modifier when no other is specified. (D)</p> Signup and view all the answers

What must be included when calling the nap() method?

<p>An integer value in parentheses. (A)</p> Signup and view all the answers

Which access modifier allows a method to be available to all classes and packages?

<p>public (B)</p> Signup and view all the answers

Which of the following is NOT a common optional specifier for methods in Java?

<p>void (B)</p> Signup and view all the answers

What can the 'static' specifier indicate about a method?

<p>The method belongs to the class rather than instances of the class. (B)</p> Signup and view all the answers

What should a method name always start with?

<p>A verb in lowercase (D)</p> Signup and view all the answers

Which of the following is a correct example of a method declaration?

<p>public void nap(int minutes) (B)</p> Signup and view all the answers

What is the purpose of the optional exception list in a method declaration?

<p>To indicate potential exceptions that the method may throw (B)</p> Signup and view all the answers

Which of the following is NOT a part of a method declaration?

<p>Local variable declaration (A)</p> Signup and view all the answers

What is the purpose of the method body in a method declaration?

<p>To execute the method's code (B)</p> Signup and view all the answers

Study Notes

Method Declaration

  • A method declaration provides necessary information to call a method, including its name, return type, parameters, and body.
  • Naming convention: method names should be a verb in lowercase (e.g., run), or a multi-word name starting with a lowercase verb (e.g., runFast).
  • Example method declaration: public final void nap(int minutes) throws Exception { //statements }

Components of a Method Declaration

  • Access Modifier: Defines visibility. public, private, protected, and package-private (default with no modifier).
  • Optional Specifier: Can include keywords like final (prevents method overriding) and static (indicates a class method).
  • Return Type: Indicates the data type of the return value. Use void if no return value is present.
  • Method Name: The identifier for the method (e.g., nap), mandatory.
  • Parameter List: A comma-separated list of input parameters with data types, enclosed in parentheses. Can be empty if no parameters exist.
  • Optional Exception List: Indicates the exceptions that may be thrown by the method (e.g., throws Exception), optional.
  • Method Body: Enclosed in braces {}, contains the executable code and local variable declarations. Can be empty if no code is necessary.

Access Modifiers

  • Public: Accessible by any class.
  • Private: Accessible only within its own class.
  • Protected: Accessible within its own package and by subclasses in other packages.
  • Package-private: Accessible only within its own package (default when no modifier is specified).

Optional Specifiers

  • Static: Defines a method that belongs to the class rather than any instance.
  • Abstract: Used when a method does not have an implementation (must be overridden in subclasses).
  • Final: Ensures a method cannot be overridden by subclasses.

Method Usage

  • To call a method, type the method name followed by an argument in parentheses (e.g., nap(15);).

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Designing Methods PDF

Description

Explore the essential guidelines for naming methods in programming as outlined in IT1908. This quiz focuses on the proper declaration of methods, including the use of verbs and naming conventions. Test your understanding of creating effective and compliant method names.

More Like This

method of glucose measurment
32 questions
Ruby Class Methods and Naming Conventions
5 questions
Python List and Variable Naming Quiz
6 questions
Use Quizgecko on...
Browser
Browser