method Flashcards

1
Q

java predefined method for finding the absolute value

A

Math.abs( any number )

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

java predefined method for finding the bigger (max) value

A

Math.max(big num, small num)

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

java predefined method for finding a num to the power of another num

A

Math.pow

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

java predefined method for finding the smaller (min) max

A

Math.min

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

java predefined method for rounding a num

A

long x = Math.round

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

java predefined method for finding the square root

A

Math.sqrt

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

in Math.ceil, you round to the

A

right
ex:
double num = Math.ceil (2.4); ///3.0
double num = Math.ceil(-2.4); //-2.0
double num = Math.ceil(2.0); //2.0

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

in Math.floor, you round to the

A

left
ex:
double num = Math.ceil (2.4); ///2.0
double num = Math.ceil(-2.4); //-3.0
double num = Math.ceil(2.0); //2.0

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

java predefined method for giving you random numbers within a certain range

A

Math.random();
ex:
print number between 1000 and 1113 inclusive
System.out.println(int) ( Math.random() * 114 + 1000 );
[we take the difference between the two numbers and add 1 to it then multiply it to Math.random(), the add the smaller value between the two]

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

what can you write at the beginning of a code that uses a lot of Math. predefined methods?

A

import static java.lang.Math.*;

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

T/F: in a public class, there can exist public static void main only.

A

F, we can also have a method public static void.

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

T/F: in a public class, there can exist public static void main only.

A

F, we can also have a method public static void.

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

T/F: You can call out a method more than once in main.

A

T,

public class testM2
3 {
4 public static void main(String args[])
5 {
6 System.out.println(“welcom”) ;
7
8 mona() ;
9
10 System.out.println(“end my program”) ;
11 mona() ;
12 }
13 public static void mona()
14 {
15 System.out.println(“inside mona “) ;
16 }

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

T/F: You can name a method anything even if it defies the naming laws.

A

F, you can name it anything so long as it abides by the naming laws

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

the variables in a method header are called _________

A

formal parameter

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

the variables that are called out in main not metho are the ________

A

actual parameter

17
Q

T/F: the variable intialized in main must be the same in the method.

A

F, in main the variable could be called out as (a , b) and in method they would be (x, y)

18
Q

is the following correct:
public static void sum (int x, y)

A

incorrect, we have to identify the variables each time since this area isn’t an intializing space…
public static void sum (int x, int y)

19
Q

T/F: the method must be intialized inside of the main to be called out.

A

true

20
Q

a reserved word in method that destroys everything before it and returns only the thing you next to it to main.

A

return
ex:
public static int sum (int x, int y){
int s = x + y
return s ;
}

21
Q

correct the following:
public static void sum (int x, int y){
int s = x + y
return s ;
}

A

it must be int not void since the value you are going to return is int.
public static int sum (int x, int y){
int s = x + y
return s ;
}

22
Q

correct the following:
import java.util.*;
public class testMethod{
scanner console = new Scanner(System.in);
public static void main (String[] args){

A

if the scanner is written before/outside the main, we must add “static” to it.

import java.util.*;
public class testMethod{
static scanner console = new Scanner(System.in);
public static void main (String[] args){

23
Q

correct the following:
public static void main (String[] args){
System.out.println( squar(10) );
System.out.println( cube(10) );
}

public static int square(int x){
return x*x;
}

public static void cube(int x){
System.out.println( Math.pow(x, 3));
}

A

incorrection: calling out the method in print.
public static void main (String[] args){
System.out.println( squar(10) );
cube(10);
}

public static int square(int x){
return x*x;
}

public static void cube(int x){
System.out.println( Math.pow(x, 3));
}

24
Q

correct the following:
public static int methodsx(int x){
System.out.println(“**”);
return x + 5;
System.out.println(“end.”);
}

A

incorrection: adding anything after return is useless and a “unreachable statement” message would appear for you as an error.

public static int methodsx(int x){
System.out.println(“**”);
return x + 5;
}

25
Q

correct the following:
public static int (int x){
if (x > 0)
return x+ 5;
return x;
}

A

incorrecting: missing method name.

public static int meow(int x){
if (x > 0)
return x+ 5;
return x;
}

26
Q

correct the following:
public static void methodx(int x){
if ( x > 0 )
return x + 5;
}

A

incorrection (2): incorrect method type, and missing return

public static int methodx(int x){
if ( x > 0 )
return x + 5;
return x;
}

27
Q

correct the following:
public static void area (int x, y)
{
System.out.println(“area=” + (x*y));
}

A

incorrection: not adding a variable type to both variables in a parameter.

public static void area (int x, int y)
{
System.out.println(“area=” + (x*y));
}

28
Q

correct the following:
public static void main (String[] args){
area(“10”, “30”);
}

public static void area (int x, int y){
System.out.println(“area =” + (x* y);
}

A

incorrection: the variable type defined in method is int, and in main it is string in the parameter.

public static void main (String[] args){
area( 10, 30 );
}

public static void area (int x, int y){
System.out.println(“area =” + (x* y);
}

29
Q

correct the following:
public static void main(String[] args){

sum (10.6, 3);
sum (50 , 100);
sum (5 , 10.8);
}

public static void sum(double x, int y) {
System.out.println(x + y);
}

A

incorrection: the variables in the parameter in main don’t ALL match the variable type in method.

public static void main(String[] args){

sum (10.6, 3);
sum (50 , 100);
sum (5 , 10.8);
}

public static void sum(double x, double y) {
System.out.println(x + y);
}

30
Q

correct the following:
public static double power(double x, int y){
int result = Math.pow(x, y);
return result;

System.out.println(“completed’);
}

A

public static double power(double x, int y){
double result = Math.pow(x, y);
return result;
}

31
Q

correct the following:
public static void main(String args[]){
int a = 4;
int b = 2;
System.out.println( power(a,b) ));
}

}

A
32
Q

this. method is used when…

A

intializing and the name of the object is the same as the attribute (ex: this.name = name;)

33
Q

if there is a line under an attribute in a UML then that means that we need to intialize it as….
(ex: + numOfFemale: int is one of the attributes of class trainee. )

A

public static int numOfFemale;

34
Q

why do we use getters and setters?

A

we create a getter so that we can get a private attribute.