Unit 3: Methods/Functions Flashcards
Define: Method
A collection of statements that are grouped together to perform an operation (e.g. main())
True or False: In Java, a method is always defined inside a class
True
Define: Pre-defined method
Methods already written and defined inside classes and provided by Java. Organized and stored in the standard packages (e.g. java.lang.Math)
Define: Programmer-defined method
Programmer writes the method definition inside a class based on their requirement, if no pre-defined method is available. The method definition includes modifier(s), return type, method name, and types and number of parameters
Why are methods useful?
A complex program can be broken down into sub tasks, each of which is easy to manage and implement (this is known as a modular programming approach)
True or False: Generally, the driver class in Java contains static methods
True
True or False: A method with a static modifier is not a static method
False
True or False: A static method can only call a non-static method after creating an object of that class it belongs to
True
True or False: In Java, a method can be defined inside another method and does not have to be defined as a separate entity
False
Define: Void method
A method that does not return any value
Define: Value-returning method
A method that returns a value using a return statement
If a static method is called from a different class, it is done by using the ___________ followed by ___________
Name of the class, the dot (.) operator
Define: Method signature
The combination of the method name (a valid identifier which can not be a keyword) and the data/object-types in the parameter list that contains formal parameters
Define: Formal parameters
The variables declared in the method header
Define: Actual parameters
Parameters involved in the method call line, there is no need to specify the datatype or method type in these parameters
Define: Return-value type
The datatype of the value the method returns via a return-statement after it is done with the calculation
True or False: A method can not return more than one object
True
If a method does not return a value, the keyword ______ is used in place of return-value type
void
When a method is called, the arguments must agree with the formal parameters in ____, _____________, and _______
Order, number of parameters, data types
True or False: The variable names in the method header and in the arguments of the method-call can be different
True
Define: Pass by value
When using a variable inside a method, any change to the value of the variable inside the called method does not change the original value in the calling method
Define: Pass by reference value
When passing a reference-value of an object to a called method, the original value in the calling method can be changed from the called method
Define: Stack (in RAM)
Stores local primitive variables (variables inside a program block), formal parameters, and local reference variables. Managed by the compiler in LIFO (Last in First out) basis
Define: Heap (in RAM)
Stores dynamic/runtime variables; in Java, whenever we create any object, it is created in the heap space
Define: Program code (in RAM)
Stores other program data not stored in heap or stack
True or False: The parameter list contains all variables used by the method
False
True or False: The following code is a valid definition for a method called “cube”:
public static double cube(double x) { //some code }
True
Suppose your method does not return any value, could void be used as a return-value type?
Yes
The signature of a method consists of _____ and ____
Method name, parameter list
When you call a method with a parameter, the value in the argument is passed to the parameter. This is referred to as ________
Pass by value
What will be the output of the method-call nPrint(‘a’, 4) for the method printChar() defined below:
public static void printChar(char ch, int n) {
System.out.print(ch);
n–;
}
a
Breaking a program in to manageable-sized methods/procedures is called _______
Modular programming
True or False: When a method is called, flow of control moves to the method’s header
True
Define: Method overloading
If two or more method signatures have the same name but different parameter lists (in terms of datatypes, number of datatypes, or order of the datatypes), these methods are called overloaded methods, and this concept is known as method overloading
Define: Ambiguous invocation
When there are two or more possible matches for a call of an overloaded function. This results in a compilation error
What are some common mistakes in code writing with methods?
Misspelled method name in the method call or method header, calling a void method from the println() statement, no return statement for a non void method, returning a value from a void method, using datatypes in the method call, ambiguous invocation
Define: Scope
The region of the program where an identifier can be used. It also provides the information about the lifetime of an identifier (the time during which an identifier actually has memory allocated to it)
Define: Local scope (block scope)
Extends from the point of the variable/reference-variable declaration to the end of the block
True or False: The variables/reference variables in Java can be accessed only within the block that declares it, and these variables are often called local variables
True
True or False: If we want to use a modifier for a declared local variable or local reference-variable inside any method, other modifiers besides the “final” modifier can be used
False
True or False: You can not declare a variable with the same name twice in the same code block or in a nested code block
True (compilation error will be generated)
A __________ variable is defined inside the body of a method and is not accessible outside that method
Local
Methods are ideal for use in menu-driven programs. When a user selects a menu item, the program can ______ the appropriate method to carry out the user’s choice
Call
A method is executed when it is ______
Called
The group of java statements in which an object/variable can be used is its __________
Scope