Chapter 4: Methods and Encapsulation Flashcards
(120 cards)
What does a basic method signature (method declaration) include?
access modifier, optional modifiers, return type, name, parameters, method body public static void main( String [] args){ //method body }
Which of these are required? Access modifier (ie Public) specifier (final) Return type Method name Parameter list exception list method body
no no yes yes yes no yes
What are the four access modifiers?
public private protected and default (which is package private, there is actually no keyword for this)
Which of these compile? public void walk1(){}
default void walk2(){}
void public walk3(){}
void walk4(){}
yes
no default is not a keyword
no access modifier comes before the return type
yes
What are the three optional specifiers you need to know for this exam?
final used when a method is not allowed to be overridden by a subclass static when you want to bind a method to a class abstract used when not providing a method body
Which of these don’t compile and why?
public void walk(){}
public final void walk2(){}
public static final void walk3(){}
public final static void walk4(){}
public modifier void walk5(){}
public void final walk6(){}
final public void walk7(){}
yes
yes
yes
yes
no modifier is not a key word
no modifiers cannot come directly before the return type
yes
Which of these compile?
public void walk1(){}
public void walk2(){return;}
public String walk3(){return “”;}
public String walk4(){}
public walk5(){};
String walk6(int a){ if (a==4 return “”;)}
yes
yes
yes
no there is not a return statement in the body
no
no there is no return type specified
no tricky, but because a string may not always be returned, if a does not equal 4 for example, the compiler will complain
Which of these compiles?
int number(){ int temp = 9; return temp; }
int longMethod(){ int temp =9L return temp ; }
The first. You cannot stuff a long into an int. Also, this shows that a method that is supposed to return an int cannot return a long value.
Which of these compile? public void walk1(){} public void 2walk(){} public walk3 void(){} public void Walk_$(){} public void() {}
yes no you cannot start a method name with a number no, the name cannot come before the return type yes no, there is no method name
Is the parameter list required?
Yes, but it doesn’t have to contain any parameters
Which of these compile? public void walk1(){} public void walk2{} public void walk3(int a){} public void walk4(int a; int b){} public void walk5(int a, int b){}
yes no, no parameter list ie the parenthesis are missing yes no, semi-colon in the place of the comma. Semi-colons are for separating statements, no parameter lists yes
How does Java indicate that something went wrong?
By throwing an exception
Which of these compile? public void example1(){} public void example2() throws IllegalArgumentException{} public void example3() throws IllegalArgumentException, InterruptedException{}
All of these compile. You can have as many exceptions as you want–separated by commas–or none at all.
What is a method body?
Simply a code block { } that contains zero or more Java Statements.
Which of these compile? public void walk1(){} public void walk2(); public void walk3(int a ) { int anem = 5;}
yes no, no method body yet there is a semicolon. yes
What does varargs stand for?
variable argument(s) that are treated like an array
How are varargs different than arrays?
Varargs parameters must be the last element in the method’s parameters list.
How many varargs can you have in a method signature?
Only one. Varargs must be the last element of a methods parameter list, therefore, there can only be one of them.
Which of these compile? public void walk1(int… numbers){} public void walk2(int start, int… numbers){} public void walk3(int…numbers, int start){} public void walk4(int… start, int… numbers){}
yes, varargs at the end yes, varargs at the end and separated by a comma no, varargs are not at the end no, you can only have one varargs element in the parameter list of method signature
What are the three ways you can call a method with a varargs parameter?
- You can pass in an array of elements. 2. Or, you can list the elements of the array and Java will create the array for you. 3. Or, you can omit the varargs values in the method call and Java will create an array of zero
What is the output? public static void main(String[] args) { walk(1); walk(1,2); walk(1,2,3); walk(1, new int[] {4,5}); walk(1, null); } public static void walk(int start, int… numbers){ System.out.println(numbers.length); } }
0 1 2 2 null pointer exception. As null is not an int, Java treats it as an array reference that happens to be null. When walk tries to determine the length of the array it returns a null pointer exception.
What is the output? public static void main(String[] args) { run(11, 22); } public static void run(int… numbers) { System.out.println(numbers[1]); } }
- you can access a varargs parameter just like accessing an array.
How accessible are ‘private’ classes, methods and fields?
Only accessible within the same class. Only code in the same class can call private methods or access private fields. Accessing private members of another class will always stop the program from compiling.
How accessible are ‘default’ (package private) access:
private and other classes in the same package. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.