powerpoint 5 methods.txt

The flashcards below were created by user dimeng on FreezingBlue Flashcards.

  1. Methods help you ___________ your code.
  2. Methods help you ____________ reusable code segments and avoid redundant code.
  3. What are the four components of a method?
    • 1) name
    • 2) parameters (optional)
    • 3) return type (can be void)
    • 4) method body
  4. How are methods executed?
    Methods reside outside of the main program (main()). Other methods are called by name and passed any needed parameters.
  5. If the method returns a value, a variable to hold the return value can be ___________ and ______________ at the same time.
    • declared
    • instantiated
  6. What is another word for calling a method?
  7. Must methods return a value?
  8. What is the syntax for declaring a method?
    • modifier (public, private, static, final, etc)
    • return type (String, double, int, etc.)
    • name (method name in camel case)
    • parentheses (whether or not parameters are accepted)
    • parameter list (list of parameters passed to the method when invoked - syntax: parameter type parameter name)
    • body (between curly braces)
  9. Review method example slides (there are a lot).
    (pages 6-15, 16-19 of powerpoint)
  10. Why are methods used?
    • encapsulate repetitive code
    • make programs neater and easier to read
    • minimize errors
  11. What does it mean if a method is overloaded?
    • A method is overloaded if more than one method exists with the same name but each has distinct variable lists.
    • An example of a good time to do this is max (int num1, int num2) and max (double num1, double num2).
  12. What type of error should you anticipate if you have an overloaded method?
    The ambiguous invocation error occurs when Java cannot determine which version of an overloaded method should be used. For example, if there are two versions of a method, (one accepts one int and one double, one accepts one double and one int), and two ints are passed when the method is invoked, Java will throw an exception.
  13. Can you overload based on return type?
  14. Can you overload a method with the same name, parameter number and parameter types?
    Not in the same class.
  15. What is variable scope?
    It is the part of the program where the variable is known and can be referenced.
  16. Variables are local to the _____________.
    block they are declared in
  17. A variable can be declared multiple times in ________________, but only once in _______________.4
    • non-nested blocks
    • nested blocks
  18. Parameter names are ______________ to the method.
  19. Review slides p. 22 - 25
Card Set:
powerpoint 5 methods.txt
2015-03-21 15:39:40
diane cs140
Diane cs140 powerpoint 5 methods
Show Answers: