Functions and Modules Flashcards

1
Q

What is a method in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are parameters in a Java method?

A

data that are inputted or passed into the method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Identify the name, parameters in the Java method below:

public int calculateAge ( int m, int n) {
statements…}

A

calculateAge is the name of the method
m,n are the parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the first method executed in Java?

A

the main method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is the main method in Java declared as static?

A

because it allows the main method to be called as the first method without having to create an instance of the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is the return value declared in a method?

A

By the data type alone before the method name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can Java methods return more than one value?

A

No Java methods can only return one or no values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The number of strings passed into the main method’s local variable named args can be obtained via _____.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

_____ are passed to the main method as an array of String objects.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A __________ in Java is like a project: it is a group of similar class types all contained within the same set of code.

A

package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The _________ keyword provides the highest level of protection for members of classes.

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

It is recommended to create most methods and variables as _____.

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which type of variable or method is protected but can be used with classes in the package (and also subclasses)?

A

Protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

A

final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When used to create a class, the final keyword will prevent the creation of _____

A

A subclass from the class

18
Q

Trying to create a subclass from a final class, or re-initialize a final variable, will result in what?

A

Compiler errors: program will not run

19
Q

Final can be used to protect the following:

A

Variables, methods, and classes

20
Q

What is a static method?

A

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.

21
Q

What is the difference between an instance method and a static method?

A

Static methods should be accessed through class and not instance

22
Q

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 ?

A

cannot use the keyword this as there is no instance

23
Q

To make a method a static method we need to:

A

Add the keyword ‘static’ to the method signature

24
Q

What does a static method belong to?

A

In Java a static method belongs to the class

25
What does a non-static method belong to?
In Java a non-static method belongs to the object of a class.
26
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
27
Characteristics of Non-Static Methods:
can access any static method or variable without creating an instance of the class
28
What is modular programming?
instead of writing a program as one large block of code, we divide it into small and independent modules,
29
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
30
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
31
Class characteristics and data we want to store related to that class are called _________________________.
class attributes
32
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
33
java.util
classes for lists, maps, queues, sets, arrays, date, scanner for reading data, etc.
34
java.lang
fundamental classes for stdin and stdout (System), data types (Boolean, Character, Integer, etc.), math methods (Math), and more
35
java.awt
classes for creating user interfaces, graphics, and images
36
java.swing
classes and support for GUI components
37
When passing an array to the function where is the reference to the array stored?
The parameter variable.
38
Arrays allow the storing of multiple values of the ______ data type.
same
39
In programming, a __________ method is like this: it calls itself over and over until something triggers its exit.
recursive
40
When a Java function calls itself, it's known as _________. When we have a repeating loop, we're using ___________.
recursion, iteration
41
What are measures of performance?
Execution time and memory usage
42
What can help prevent infinite loops?
An exit point