• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/46

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

46 Cards in this Set

  • Front
  • Back

Is the following a valid method signature?




public final void nap (int minutes) throws InterruptedException {


// code code code


}

Yes, it is valid.




public and final are optional


the exception is also optional



return type is required (right before method name)


method name and ( ) are required.

Is the following a valid method signature?




void public walk()

No, the return type must be written right before method name.

Is the following a valid method signature?




public final void walk(){




}

yes, it's valid.



there is no return, though one could include "return; " (which is returning nothing)

Is the following a valid method signature?




public void final walk() {




}

No, not valid




return type must be written directly before method name

Is the following valid?




public walk(){


}

No, it's missing a return type

Will the following compile?




private final String walk ( int a ){


if ( a == 4 ) return "";


}

No


The signature is ok, but if a is not 4, the method won't return anything. This method must always return a String to be valid.

Is this valid?




public void 2walk(){


}

No


The method name (identifier) may only contain letters, numbers, $ or _. Reserved words are not allowed (but capitalizing them is fine.)




The identifier may start with a letter $ or _, but not a number.

Is this valid?




public final Int walk_$$(){


return 3;


}

No




The return type is invalid. You could return Integer - which is the wrapper object, or int.




the method name is legal

Is this valid?




public String walk {


return "123";


}

No




Methods must have ( ) after their identifier (method name)



Is this valid?




void walk(int ... nums, int start) { }

No




Varargs must be listed last in a parameter list

Is this valid?




void walk(int ... start, int ... nums) { }

No




Only one varags is allowed and it must be listed last in the parameter list.

Is this valid?




void walk ( int start, int... nums) { }

Yes




one varags is allowed and it is listed last in the parameter list.





What does the following return?




public static void walk (int start, int ... nums) {


System.out.prinln(nums.length); }




walk(1);


walk(1,2);


walk(1,2,3);


walk(1,[2,3,4]);


walk(1, new int[] {4,5,6} );

public static void walk (int start, int ... nums) { System.out.prinln(nums.length); }




walk(1); // 0


walk(1,2); // 1


walk(1,2,3); // 2


walk(1,[2,3,4]); //3


walk(1, new int[] {4,5,6} ); // 3

Is this valid?




walk(int[] start, ...int nums ) { }

No




The ... must be AFTER the type, not before it.

Is the following valid?




walk(1, { 2,3 } );




public static void walk (int start, int ... nums) { System.out.prinln(nums.length); }









No.




The array within the method call is not properly defined

Define the scope of a private class member

Only available within declared class

Define the scope of a package private class member (default access)

Available within declared class AND


classes within the same package

Define the scope of a protected class member

Available within declared class AND


classes within same package AND


within child classes (sub-classes) in a different package (using the reserved work "extends")

Define the scope of a public class member

Available to all classes and all class members

Can a static class member use an instance class member? Explain how it may/may not be possible

No.




Only an instance method can call another instance method on the same class without using a reference variable.




Instance members require an object, static members are linked to a classname, not an object.




page 82-84

Can an instance class member use/call a static class member? Explain how it may/may not be possible

Yes. The compiler checks for the type of the reference and uses that instead of the object.




example:


static count = 0;


// Reference k = new Object()


Koala k = new Koala();


k = null;


// java doesn't care that k is null, it's reference (object) is still Koala.


System.out.println( k.count ); //prints 0






page 82-84

Can a static method call another static method or variable? Explain how it may/may not be possible

Yes




It uses the specific classname to do so if the member is not in the calling class




page 82-84

Can a static method call an instance method or variable? Explain how it may/may not be possible

Not really.




You could use they keyword new to create an object (instantiate) from a static method (such as in the main method of a class).




Only an instance method can call another instance method on the same class without using a reference variable.




page 82-84



Can an instance method call a static method or variable?

Yes




It uses the classname or a reference (object) variable to do so




page 82-84

Can an instance method call another instance method or variable?

Yes




It must use a reference (object) variable to do so.




page 82-84

Define a final variable and it's naming convention

It is a variable that cannot be changed, a constant.




Proper naming uses all uppercase letters with _ between words.




public static final int NUM_BUCKETS = 5;

Is this a valid import statement?




import static java.util.Arrays;

No -- you can't statically import a class.




a valid import would be :


import static java.util.Arrays.asList;

Is this a valid import statement?




static import java.util.Arrays.asList;

No -- import must be listed first in the statement

Is this proper implementation?




import static java.util.Arrays.asList;


.


.


Arrays.asList("one");

No. The Array class was not imported -- only .asList was imported.




valid code would be simply


" asList("one"); "

Explain what is to be expected when methods pass variables back and forth.


(how is data affected?)

Assignments made in the method do not affect the caller.




References are passed back and forth - the objects those references point to are not passed.




Remember immutability when passing values. If a new object is assigned to the reference, the parameter accepted is changed locally, but not updated outside of that method.




StringBuilder and ArrayList have methods that change the object itself. The reference never changes so the caller IS changed outside of the local method.

Explain the order Java uses when selecting which overloaded method to execute

Specific ---> non- specific




Exact match by type -- int


Larger primitive type -- long


Autoboxed type -- Integer


Varargs -- int ... nums




** will only do one conversion


int --> Integer object is ok


int --> Long object is not ok.

What is the valid syntax for a constructor signature?

A constructor must match the name of the class (case-sensitive) and have no return type.

Is the following a valid constructor?




public Hamster (int weight) {


System.out.println("in constructor");


// ready to call this!


this(weight, "brown");


}




public Hamster (int weight, String color){


this.weight = weight;


this.color = color;


}

No. If the keyword this() is used to call another constructor, it must be the first non-comment in the code block.

Where can final instance variables be initialized?


(check all that apply)




A. Any place is fine, as long as it's done only once


B. In a Static code block


C. In an Instance initializer


D. In a constructor

C. In an Instance initializer


D. In a constructor





What is the order of initialization?

Superclass --> Static --> Instance --> Constructor




All fields are instantiated in the order that they appear in the file. (while still following the type order of initialization of course)

What are the rules for JavaBeans naming conventions?

1. Properties are private


2. Getter methods begin with get (or is if property is boolean)


3. Setter methods begin with set


4. The method name must include the property being access in the following way




void setNumEggs(int num);


int getNumEggs();

What is encapsulation?

Encapsulation prevents callers from making uncontrolled changes to your class.




Only methods in the class with the variables can refer to the instance variables.

What is needed to create an immutable class?

Immutable classes have no setters. They use constructors to create a new object.




Send a copy of the object passed in by using the keyword new (within a getter or a constructor):




public StringBuilder getBuilder(){


return new StringBuilder(builder);


}




OR return an immutable object.




public String get Value(){


return builder.toString;


}

What is the short version of the following?




(Animal a) -> { return a.canHop(); }

a -> a.canHop()

Is the following a valid lambda expression?




print( a -> { a.startsWith("test"); } );

No




It's missing a return statement.


print( a -> { return a.startsWith("test"); } );

Is the following a valid lambda expression?




print ( Animal a -> a.startsWith("test") );

No




The first parameter needs parenthesis because the type is included.




print ( (Animal a) -> a.startsWith("test") );

Assume there are valid interfaces that can consume a lambda with zero, one, or two String parameters.




Which of the following is invalid?




A. print ( ( ) -> true);


B. print ( a -> a.startsWith("test") );


C. print ( (String a, String b) -> a.startsWith(b) );


D. print ( a, b -> a.startsWith(a) );


D is invalid because it neglected parenthesis around the two input values.




the correct syntax is...


print ( ( a, b ) -> a.startsWith(a) );

Which of the following is invalid?




A. ( a, b ) -> { int a = 0; return 5;}


B. ( a, b ) -> {int c = 0; return 5;}

A -- Java will not allow local variables to be redeclared.




It's perfectly legal to declare a new variable and not use the first two parameters.

What import statement is needed to use the interface Predicate?

java.util.function.Predicate




or




java.util.function

Explain the test() method in the Predicate interface

It is a method that takes any one reference type (similar to how ArrayList can accept any one reference type) and returns a boolean.




example


private void print(List animals, Predicate checker);

Explain ArrayList's removeIf() method

It is a method that takes a lambda expression (a predicate) and removes all elements in the ArrayList that match the boolean check defined.




example:


List bunnies = new ArrayList<>( );


bunnies.removeIf( s -> s.charAt(0) != 'h' );