Methods Flashcards

1
Q

public static void main (String []args)

Identify the parts of the method.

A

void - return type
main - method name
(String []args) - parameter

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

What is the format of a method?

A

public static return_type methodName ([parameters])

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

It is a variable that will receive the value being passed in calling the method.

A

Parameters

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

This return type will not return any value.

A

void

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

a collection of statements that are grouped together to perform an operation.

A

Method

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

a collection of statements that are grouped together to perform an operation.

A

Method

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

Examples of defined methods in the Java library

A
  • System.out.println
  • Integer.parseInt,
  • Double.parseDouble
  • System.exit
  • Math.pow
  • Math.random.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Syntax in declaring method

A

[accesskeyword] static returntype methodname (parameter) {
}

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

can be public, private, or protected.

A

Access keyword

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

What will be the default form of access if no access modifier is declared?

A

Package

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

indicates the type of value returned or reported back to the calling method, and it is most often used in conjunction with the assignment of a value to a variable in the calling method.

A

Return type

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

What can the return type be?

A
  • any Java primitive data type
  • an object
  • void
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

This return type denotes hat the method has no return value

A

Void

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

What naming rules does the method name follow?

A

Follows the same naming rules as variables and will generally begin with a lowercase letter.

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

What are methods and variables caused because they are both contained inside classes?

A

They are called class members

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

receives the value being passed when a function is called

A

Parameter

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

What is used to separate parameters in a parameter list?

A

Comma

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

states the data type of the parameter, which can be either a primitive data type or an object.

A

First entry in a parameter

19
Q

declares the name to be used when referencing the parameter within the method.

A

Second entry in a parameter

20
Q

a variable inside the method

A

Parameter

21
Q

Name the parts of the method
public static int max(int num1, int num2)

A

public static int max(int num1, int num2) - method header
public - access keyword
static - modifier
int - returntype
max - method name
(int num1, int num2) - parameter list
num1 & num2 - parameter

22
Q

What would the output be?
public static void main(String[] args) {
System.out.println(“Main Methods”);
DisplayMessage();
System.out.println(“Back to Main Methods”);
}

static void DisplayMessage(){
System.out.println(“Programming is great fun!”);
}

A

Main Methods
Programming is great fun!
Back to Main Methods

23
Q

What would the output be?
public static void main(String[] args) {
DisplayNumbers();
DisplayMessage();
}

static void DisplayMessage(){ 
	System.out.println("Programming is great fun!");
}

static void DisplayNumbers(){ 
	System.out.println("One");
	System.out.println("Two");
	System.out.println("Three");
}
A

One
Two
Three
Programming is great fun!

24
Q

TRUE OR FALSE: Methods can also be called in a hierarchical, or layered fashion.

A

TRUE

25
Q

What would the output be?
public static void main(String[] args) {
DisplayMessage();
DisplayNumbers();
}

static void DisplayMessage(){
System.out.println(“Programming is great fun!”);
Looping();
}

static void DisplayNumbers(){
System.out.println(“One”);
System.out.println(“Two”);
System.out.println(“Three”);
}

static void Looping(){
for (int x=1; x<=5; x++)
System.out.println(x + “ “);
}

A

Programming is great fun!
1 2 3 4 5
One
Two
Three

26
Q

TRUE OR FALSE: A method may be written so it accepts arguments. Data can then be passed into the method when it is called.

A

TRUE

27
Q

Values that are sent into a method

A

Arguments

28
Q

a special variable that holds a value being passed into a method.

A

Parameter variable

29
Q

What would the output be?
public static void main(String[] args) {
DisplayNumber(5);
}

static void DisplayNumber(int num){ 
	System.out.println("The value is " + num);
}
A

The value is 5

30
Q

What would the output be?
public static void main(String[] args) {
int a=3, b = 4;
DisplayNumber(5);
DisplayNumber(4 * 3-1);
DisplayNumber(a * 2);
DisplayNumber(a+b);
}

static void DisplayNumber(int num){
System.out.println(“The value is “ + num);
}

A

The value is 5
The value is 11
The value is 6
The value is 7

31
Q

What would the output be?
public static void main(String[] args) {
ShowNum(23.4, 45.7);
}

static void ShowNum(double num1, double num2){
double sum= num1 + num2;
System.out.println(“Num1 : “ + num1);
System.out.println(“Num2 : “ + num2);
System.out.println(“The sum is “ + sum);
}

A

Num1 : 23.4
Num2 : 45.7
The sum is 69.1

32
Q

TRUE OR FALSE: Each parameter variable in a parameter list doesn’t need to have a data type listed before its name.

A

FALSE, a compiler error would occur if not all of the parameter variables doesn’t have a data type listed before its name

33
Q

declared inside a method and is not accessible to statements outside the method.

A

Local Variables

34
Q

Can different methods can have local variables with the same names?

A

Yes, because the methods cannot see each other’s local variables

35
Q

Identify the local variables and method parameters.
public static void main(String[] args) {
int x, y;
}

static void ShowNum(double num1, double num2){
int sum;
}

static void FindProduct(double number){
int product, square;
}

A

main
local variables - x, y
method parameter - args
ShowNum
local variables - sum
method parameter - num1, num2
FindProduct
local variables - number
method parameter - Product, square

36
Q

are not automatically initialized with a default value. They must be given a value before they can be used.

A

Local variables

37
Q

What happens if you use a local variable before it has been given a value?

A

A compilor error will result

38
Q

What would the output be?
public static void main(String[] args) {
int x=5;
System.out.println(“Before Calling ChangeValue Method: “ + x);
ChangeValue(x);
System.out.println(“Before Calling ChangeValue Method: “ + x);
}

static void ChangeValue(int x){
x=50;
System.out.println(“Inside ChangeValue Method: “ + x);
}

A

The value of the local value is not affected when the value of variable is changed in other method.
Before Calling ChangeValue Method: 5
Inside ChangeValue Method: 50
Before Calling ChangeValue Method: 5

39
Q

A method may send a value back to the statement that called the method. Data may be passed into a method by way of parameter variables. Data may also be returned from a method, back to the statement that called it.

A

Returning a value from a method

40
Q

These are methods that return a value

A

Value-returning methods

41
Q

Explain what happens to the variables and the output.
public static void main(String[] args) {
int total = GetSum(10,20);
System.out.println(“Total: “ + total);
}

static int GetSum(int num1, int num2){
int sum = num1+num2;
return sum ;
}

A

VARIABLE TRACING
num1=10
num2=20
sum=num1+num2 | sum=30
total=sum | total=30
OUTPUT
Total:30

42
Q

TRUE OR FALSE: The non void value must return any value otherwise it would be a compilation error

A

TRUE

43
Q

Why does an error occur if you put a statement after the return statement?

A

Because the statement after the return statement is a dead code or unreachable statement