Functions and Modules Flashcards
What is a method in Java?
a method is a section of the program that contains a set of instructions. When the method is called, the set of instructions within the method is executed.
What are parameters in a Java method?
data that are inputted or passed into the method
Identify the name, parameters in the Java method below:
public int calculateAge ( int m, int n) {
statements…}
calculateAge is the name of the method
m,n are the parameters
What is the first method executed in Java?
the main method
Why is the main method in Java declared as static?
because it allows the main method to be called as the first method without having to create an instance of the method.
How is the return value declared in a method?
By the data type alone before the method name
Can Java methods return more than one value?
No Java methods can only return one or no values.
The number of strings passed into the main method’s local variable named args can be obtained via _____.
args.length
The number of elements stored in any Java array is kept in an internal property named length. Therefore args.length will return the number of elements in the String array args.
_____ are passed to the main method as an array of String objects.
The command line options.
The space separated command line options (which occur AFTER the name of the file containing the Java program) are passed to the main method.
If you want to protect members of a class, but still allow other classes in the package, or subclasses to use them, the __________ keyword can be used.
protected
A ____________ is a class that inherits variables and methods from another class. Private members can’t be shared down to children, but protected members can.
subclass
A __________ in Java is like a project: it is a group of similar class types all contained within the same set of code.
package
The _________ keyword provides the highest level of protection for members of classes.
private
It is recommended to create most methods and variables as _____.
private
Which type of variable or method is protected but can be used with classes in the package (and also subclasses)?
Protected
The Java _______ keyword protects code, variables, classes, and methods. It ensures you can’t create subclasses from the main class. It ensures variables stay local to their methods.
final
When used to create a class, the final keyword will prevent the creation of _____
A subclass from the class
Trying to create a subclass from a final class, or re-initialize a final variable, will result in what?
Compiler errors: program will not run
Final can be used to protect the following:
Variables, methods, and classes
What is a static method?
A static method in Java (also called class method) is a method that belongs to the class and not the instance. Therefore, you can invoke the method through the class instead of creating an instance first and calling the method on that instance.
What is the difference between an instance method and a static method?
Static methods should be accessed through class and not instance
Given a class Example with static method called action with no parameters, which of the following is never a correct call for the static method ?
cannot use the keyword this as there is no instance
To make a method a static method we need to:
Add the keyword ‘static’ to the method signature
What does a static method belong to?
In Java a static method belongs to the class
What does a non-static method belong to?
In Java a non-static method belongs to the object of a class.
Characteristics of static methods:
A static method can call only other static methods; it cannot call a non-static method
A static method can be called directly from the class, without having to create an instance of the class
A static method can only access static variables; it cannot access instance variables
Since the static method refers to the class, the syntax to call or refer to a static method is: class name.method name
To access a non-static method from a static method, create an instance of the class
Characteristics of Non-Static Methods:
can access any static method or variable without creating an instance of the class
What is modular programming?
instead of writing a program as one large block of code, we divide it into small and independent modules,
An object is implemented as a Java class and has a name, some characteristics (called _________) and some actions (__________) that can be performed on it or by it.
attributes, methods
Each Java ________ represents a noun (for example, Student, Car, Building, Address, Reservation, etc.). One way to think of the Java class is as a __________.
class, template
Class characteristics and data we want to store related to that class are called _________________________.
class attributes
The _____________, which is where a Java program always starts executing, is a static method because when it starts, there are no _________ of any classes yet.
main method, instances
java.util
classes for lists, maps, queues, sets, arrays, date, scanner for reading data, etc.
java.lang
fundamental classes for stdin and stdout (System), data types (Boolean, Character, Integer, etc.), math methods (Math), and more
java.awt
classes for creating user interfaces, graphics, and images
java.swing
classes and support for GUI components
When passing an array to the function where is the reference to the array stored?
The parameter variable.
Arrays allow the storing of multiple values of the ______ data type.
same
In programming, a __________ method is like this: it calls itself over and over until something triggers its exit.
recursive
When a Java function calls itself, it’s known as _________. When we have a repeating loop, we’re using ___________.
recursion, iteration
What are measures of performance?
Execution time and memory usage
What can help prevent infinite loops?
An exit point