Fiveable
Fiveable

Method

Definition

A method is a named sequence of instructions that can be called or invoked to perform a specific task or action. Methods are used for code reusability, organization, and abstraction.

Related terms

Parameter: A parameter is an input variable passed into a method when it is called. It allows data to be passed from one part of the program to another.

Return Type: The return type specifies the type of value that will be returned by a method after it completes its execution.

Void Method: A void method does not return any value after execution; it performs certain actions but does not produce an output.

"Method" appears in:

Subjects (4)

  • AP European History

  • AP Human Geography

  • AP Psychology

  • Intro to Business

Study guides (3)

  • AP Computer Science A - 2.3 Calling a Void Method

  • AP Computer Science A - 3.2 If Statements and Control Flow

  • AP Computer Science A - 5.8 Scope and Access

Additional resources (1)

  • AP Computer Science A - Unit 1 Overview: Primitive Types

Practice Questions (20+)

  • Which access level makes a variable, class, or method available only to its own class?

  • What access level makes a variable, method, or class only accessible by any subclass or class within the same package?

  • Consider the following method: public void printPowersOfTwo(int n) { for (int i = 0; i <= n; i++) { int power = (int) Math.pow(2, i); System.out.print(power + " "); } } Assume that the method printPowersOfTwo(5) appears in a method in the same class. What is printed as a result of the method call?

  • Consider the following method: public void printVowels(String str) { str = str.toLowerCase(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { System.out.print(ch + " "); } } } Assume that the method printVowels("OpenAI") appears in a method in the same class. What is printed as a result of the method call?

  • Consider the following method: public static boolean isPalindrome(String word) { int length = word.length(); for (int i = 0; i < length / 2; i++) { if (word.charAt(i) != word.charAt(length - 1 - i)) { return false; } } return true; } Assume that the following code segment appears in a class: boolean result = isPalindrome("racecar"); System.out.println("Is 'racecar' a palindrome? " + result); What is printed as a result of executing the code segment?

  • Which method is used to convert a Double object to its primitive double value?

  • Which method of the Object class is used to compare two objects for equality?

  • "Given the block of code below, answer the following question: ```java class Shape { // constructor not shown public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { // constructor not shown public void draw() { System.out.println("Drawing a circle"); super.draw(); } } ``` Question: Consider the code above. If we create an object of the Circle class and call the `draw()` method on that object, what will be the output?

  • If we don’t want our subclass method to do anything different from the superclass's implementation of that method, what should we do?

  • Which of the following correctly calls a superclass method in a subclass?

  • If a method of a superclass is overridden in the subclass, which method is called by the line "methodName();" in the subclass?

  • Consider the code below. If we create an object of the Child class and call the `prinMessaget()` method on that object, what will be the output? ```java class Parent { void printMessage() { System.out.println("Hello from Parent"); } } class Child extends Parent { void printMessage() { super.printMessage(); System.out.println("Hello from Child"); } } ```

  • When a subclass defines a method with the same name, return type, and parameters as a method in its superclass, it is an example of:

  • Given the block of code below, answer the following question: ```java class Animal { // constructor not shown } class Dog extends Animal { // constructor not shown public void makeSound() { System.out.println("Woof! Woof!"); } } ``` Question: Consider the code above. If we create an object of the `Dog` class and call the `makeSound()` method on that object, what will be outputted?

  • What happens when an object is passed as a parameter to a method in Java?

  • What happens if a method is declared with a return type but does not contain a return statement?

  • Which of the following names for a method that modifies a boolean attribute named "isFlag" follows the common naming convention for mutator methods?

  • How many parameters does the method with header "public void doSomething (double a, double b, int c, boolean flag, String text)" have?

  • What happens when a primitive value is passed as a parameter to a method in Java?

  • If a local variable and a global variable have the same name in a method, which one takes precedence?

collegeable - rocket pep

Are you a college student?

  • Study guides for the entire semester

  • 200k practice questions

  • Glossary of 50k key terms - memorize important vocab


Fiveable
About Us

About Fiveable

Blog

Careers

Code of Conduct

Terms of Use

Privacy Policy

CCPA Privacy Policy

Resources

Cram Mode

AP Score Calculators

Study Guides

Practice Quizzes

Glossary

Cram Events

Merch Shop

Crisis Text Line

Help Center

Stay Connected


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

About Us

About Fiveable

Blog

Careers

Code of Conduct

Terms of Use

Privacy Policy

CCPA Privacy Policy

Resources

Cram Mode

AP Score Calculators

Study Guides

Practice Quizzes

Glossary

Cram Events

Merch Shop

Crisis Text Line

Help Center

© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.