Unit 3: Methods/Functions Flashcards

1
Q

Define: Method

A

A collection of statements that are grouped together to perform an operation (e.g. main())

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

True or False: In Java, a method is always defined inside a class

A

True

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

Define: Pre-defined method

A

Methods already written and defined inside classes and provided by Java. Organized and stored in the standard packages (e.g. java.lang.Math)

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

Define: Programmer-defined method

A

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

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

Why are methods useful?

A

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)

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

True or False: Generally, the driver class in Java contains static methods

A

True

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

True or False: A method with a static modifier is not a static method

A

False

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

True or False: A static method can only call a non-static method after creating an object of that class it belongs to

A

True

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

True or False: In Java, a method can be defined inside another method and does not have to be defined as a separate entity

A

False

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

Define: Void method

A

A method that does not return any value

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

Define: Value-returning method

A

A method that returns a value using a return statement

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

If a static method is called from a different class, it is done by using the ___________ followed by ___________

A

Name of the class, the dot (.) operator

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

Define: Method signature

A

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

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

Define: Formal parameters

A

The variables declared in the method header

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

Define: Actual parameters

A

Parameters involved in the method call line, there is no need to specify the datatype or method type in these parameters

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

Define: Return-value type

A

The datatype of the value the method returns via a return-statement after it is done with the calculation

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

True or False: A method can not return more than one object

A

True

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

If a method does not return a value, the keyword ______ is used in place of return-value type

A

void

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

When a method is called, the arguments must agree with the formal parameters in ____, _____________, and _______

A

Order, number of parameters, data types

20
Q

True or False: The variable names in the method header and in the arguments of the method-call can be different

A

True

21
Q

Define: Pass by value

A

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

22
Q

Define: Pass by reference value

A

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

23
Q

Define: Stack (in RAM)

A

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

24
Q

Define: Heap (in RAM)

A

Stores dynamic/runtime variables; in Java, whenever we create any object, it is created in the heap space

25
Q

Define: Program code (in RAM)

A

Stores other program data not stored in heap or stack

26
Q

True or False: The parameter list contains all variables used by the method

A

False

27
Q

True or False: The following code is a valid definition for a method called “cube”:

public static double cube(double x) { //some code }

A

True

28
Q

Suppose your method does not return any value, could void be used as a return-value type?

A

Yes

29
Q

The signature of a method consists of _____ and ____

A

Method name, parameter list

30
Q

When you call a method with a parameter, the value in the argument is passed to the parameter. This is referred to as ________

A

Pass by value

31
Q

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

a

32
Q

Breaking a program in to manageable-sized methods/procedures is called _______

A

Modular programming

33
Q

True or False: When a method is called, flow of control moves to the method’s header

A

True

34
Q

Define: Method overloading

A

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

35
Q

Define: Ambiguous invocation

A

When there are two or more possible matches for a call of an overloaded function. This results in a compilation error

36
Q

What are some common mistakes in code writing with methods?

A

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

37
Q

Define: Scope

A

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)

38
Q

Define: Local scope (block scope)

A

Extends from the point of the variable/reference-variable declaration to the end of the block

39
Q

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

A

True

40
Q

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

A

False

41
Q

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

A

True (compilation error will be generated)

42
Q

A __________ variable is defined inside the body of a method and is not accessible outside that method

A

Local

43
Q

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

A

Call

44
Q

A method is executed when it is ______

A

Called

45
Q

The group of java statements in which an object/variable can be used is its __________

A

Scope