Java Methods Flashcards

1
Q

What is a method in Java?

A

A method is a block of code which only runs when it is called.

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

Where are methods created in java?

A

Methods are typically created inside classes or in interfaces. A method in an interface doesn’t not include a code block.

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

What is the static modifier for a Java Methods?

A

Static means the method belongs to the class and not to an object that is created

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

What is the void modifier in Java?

A

It means the method does not return a value

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

What are parameters in methods in Java?

A

Information can be passed to methods as parameter. Parameters act as variables inside the method.

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

What is required for a parameter?

A

Need to have the data type and the variable name

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

Describe a Method in Java

A
public class MyClass {
  static void myMethod() {
    // code to be executed
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe a method with a parameter

A
public class MyClass {
  static void myMethod(String fname, int age) {
    System.out.println(fname + " is " + age);
  }
  public static void main(String[] args) {
    myMethod("Liam", 5);
    myMethod("Jenny", 8);
    myMethod("Anja", 31);
  }
}
// Liam is 5
// Jenny is 8
// Anja is 31
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is method overloading?

A

Method overloading is the process of using the same method with using a different number of parameters to designate which version of the method is used.

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

What is the main method?

A

The main method is the method the compiler looks for when it runs a Java program.

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

What are java string methods?

A

In the string class there are several built in methods that enable the modification and manipulation of string.

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

What are Java Math Methods?

A

There are built in method in the java class that can be used for Math functions

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

What are access modifiers in methods in java?

A

There restrict what has access to the methods in Java or define the process in giving access to these methods.

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

What the the access modifiers in java methods?

A

1- Private
2- Default
3- Public
4 - Private

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

What are non-access modifiers in Java?

A

Java provides a number of non-access modifiers to achieve many other functionalities.

The static modifier for creating class methods and variables.

The final modifier for finalizing the implementations of classes, methods, and variables.

The abstract modifier for creating abstract classes and methods.

The synchronized and volatile modifiers, which are used for threads.

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

Where should methods be placed in a class?

A

best practice is to place the methods below the variables and or main method

17
Q

What is a constructor in Java?

A

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:

18
Q

When is a constructor initialized?

A

It is typically initiated within the class

19
Q

What is the Super method?

A

super keyword can also be used to access the parent class constructor. One more important thing is that, ‘’super’ can call both parametric as well as non parametric constructors depending upon the situation. Following is the code snippet to explain the above concept

20
Q

what are constructor parameters?

A

Constructors can also take parameters, which is used to initialize attributes.

The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y). When we call the constructor, we pass a parameter to the constructor (5), which will set the value of x to 5: