Term
What are the 4 types of Access Modifiers (AMs)? |
|
Definition
1. Public 2. Protected 3. pkg-private (default) 4. private |
|
|
Term
What are the optional specifiers? |
|
Definition
1. Static 2. Abstract 3. Final 4. Synchronized 5. Native 6. Strictfp |
|
|
Term
"final" is an optional modifier used when a method is not allowed to be _____ by sa _____. |
|
Definition
|
|
Term
"static" is an optional modifier used for accessing _____ methods and variables. |
|
Definition
|
|
Term
"abstract" is an optional modifier used when not providing a _____. |
|
Definition
|
|
Term
True or false - The order in a method declaration for optional specifiers static and final does not matter. |
|
Definition
True - can be "static final" OR "final static". |
|
|
Term
True or false - optional specifiers can come before the access modifiers. |
|
Definition
True - you are allowed to do things like "final public", but it is not recommended. |
|
|
Term
True or false - an optional specifier can come after the return type, such as "void final methodName". |
|
Definition
False - the return type must come directly before the method name. |
|
|
Term
True or false - you can omit the return type in a method decalaration if it is not required. |
|
Definition
False - return type is required! |
|
|
Term
True or false - a method that has a return type, can have a return statement in the body with no value, "return;". |
|
Definition
False - you must return a value indicated by the return type in the method declaration. |
|
|
Term
True or false - you are not required to insert a return statement with a value in the body of a method if the return type in the method declaration is not void. |
|
Definition
False - you must provide a return statement in the body of a method if you do not have void as the return type in the method declaration. |
|
|
Term
If the return type in the method declaration is void, what can you do in the method body to reflect this? |
|
Definition
1. It is legal to not provide a return statement. 2. You can provide a return statement that does not provide a value, "return;". |
|
|
Term
True or false - the first character of a method name is allowed to be a number. |
|
Definition
false - first character of method name cannot be a reserved word, and can only be $, _, or letter. |
|
|
Term
Input params in a method must be separated by a _____. |
|
Definition
|
|
Term
True or false - In the throws clause, you can only list 1 Exception. |
|
Definition
False - you can list as many as you want as long as they are separated by a comma. |
|
|
Term
A Vararg parameter in a method is just like a ____. |
|
Definition
|
|
Term
True or false - a Vararg param in a method call must be the first parameter in the list. |
|
Definition
False - a vararg param must the the LAST param in the list. |
|
|
Term
True or false - you are only allowed to have 1 vararg parameter in a method's parameter list. |
|
Definition
True - and it must be the last one. |
|
|
Term
True or false - The 3 dots in a vararg param must come before the type, such as "...int Nums". |
|
Definition
False - the correct syntax for a vararg param is "int... Nums". |
|
|
Term
What are the valid ways in which you can pass vararg parameters to a method? |
|
Definition
1. Can list the params separately - test(1, 2, 3); 2. Can pass in an array - test(new int[]{1, 2, 3}); 3. Can call with an empty method parameter list - test() , this will pass in an array of length zero 0. 4 Can pass in null, but will probably cause NullPointerException - test(null);
if the method containing the vararg param required multiple input params, say "public void test(int num, int... Nums)", the following method calls would be valid; 1. test(1); - where the num is 1, and Nums is an empty array of length zero 2. test(1, 2) - where num is 1, and Nums is an array of length 1 3. test(1, 2, 3) - where num is 1, and Nums is an array of length 2 4. test(1, new int[]{1,2,3}); - where num is 1, and Nums is an array of length 3 5 test(1, null); - where num is 1, and Nums is null |
|
|
Term
Accessing a vararg s param in a method is the same accessing a ____. |
|
Definition
Array - for instance if the param is int... Nums, then the method will access the vararg with "Nums[0]", the same as an array. |
|
|
Term
What does the access modifier "private" mean? |
|
Definition
Only accessible within the same class. In other words, only code that exists in the same class can call private methods or access private fields. |
|
|
Term
What does the access modifier "default (private-pkg)" mean? |
|
Definition
private and other classes in the same package. Private Package means members are "private" to classes in the same package, or rather, only classes in the same package may access it. |
|
|
Term
What does the access modifier "protected" mean? |
|
Definition
In a nutshell protected means -
- Protected: Protected members are accessible from its derived classes or any classes in the package where it is declared.
so if there is a subclass in a different package, you the subclass can access protected members within its own class.
AND
protected members can be access by any classes in the package where it is declared.
default access and child classes (subclasses). Protected access means that parent members are accessible to child subclasses.
NOTE: Keep in mind - although subclasses in different packages can access protected members of the parent, other classes in the same package are the ONLY ones allowed to access protected members due to protected access which includes default access. Classes in other packages, that are NOT SUBCLASSES, CANNOT access protected members, even when accessing the protected member from the class or its subclasses!
EXAMPLE1: Say you have 4 classes, in 2 different packages;
Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.
[image]
Classes and Packages of the Example Used to Illustrate Access Levels
The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Visibility
Modifier |
Alpha |
Beta |
Alphasub |
Gamma |
public |
Y |
Y |
Y |
Y |
protected |
Y |
Y |
Y |
N |
no modifier |
Y |
Y |
N |
N |
private |
Y |
N |
N |
N |
EXAMPLE2: Say you have a Bird class, and a Goose class, and they are in different packages.
Bird is in animal.bird package.
Goose is in animal.goose package. Goose imports animal.Bird and extends from Bird see below;
public Goose extends Bird{ }
Bird has a protected method - "protected void fly();"
Within the goose class you can access the fly() method directly by just calling "fly();"
public void gooseFly(){
fly(); // CAN CALL FLY DIRECTLY, DUE TO INHERITANCE
}
Because Goose is a subclass, and fly is inherited, this is ok. Also,
You can create a Goose instance with a Goose reference, and you can call "fly():" on it because you are going through a goose reference and it is a subclass of Bird, although in a different package;
public void gooseFlyHigher(){
Goose goose = new Goose();
goose.fly(); // THIS IS OK BECAUSE THE REFERENCE IS GOOSE BECAUSE ITS STILL USING INHERITANCE
}
HOWEVER, if you are in the Goose class, even though you imported the Bird class, and the object you are referencing is a Goose, you still CANNOT use a Bird reference to call the "fly();" method;
public void gooseFlyEvenHigher(){
Bird bird = new Goose();
bird.fly(); // THIS DOES NOT COMPILE
}
THIS DOES NOT COMPILE SINCE YOU ARE ACCESSING THE PROTECTED fly() METHOD WITH A BIRD REFERENCE, IT DOESN'T MATTER THAT THE OBJECT BEING REFERENCE IS A GOOSE, THIS WILL STILL FAIL
Also, what if you were in another class that was not a subclass of the class having the protected method, and not in the same package, but you imported the subclass and tried to call a method on it?
Package animal.goosewatcher;
import animal.goose;
public GooseWatcher{
Goose goose = new Goose();
goose.fly(); // DOES NOT COMPILE - YOU CAN ONLY CALL THIS FROM WITHIN THE ACTUAL SUBCLASS OR FROM ANOTHER CLASS IN THE SAME PACKAGE, NOT FROM CALLERS OF THE SUBCLASS
} |
|
|
Term
What does the access modifier "public" mean? |
|
Definition
protected and classes in the other packages. |
|
|
Term
Can you call a static member from an instance variable?
public Test{
public static String str = "";
P public static void main(String[] args){
Test t = new Test();
System.out.println(t.str); // WILL THIS COMPILE?
}
}
How about when the instance is set to null?
public Test{
public static String str = "";
P public static void main(String[] args){
Test t = null;
System.out.println(t.str); // WILL THIS COMPILE?
}
} |
|
Definition
1. Yes
2. Yes - Java knows via the static optional modifier that this member is part of the class, and will reference it that way even when it's being called by an reference variable. |
|
|
Term
True or false - an instance member (for instance a method) can call other instance members (for instance fields or methods) and static members. |
|
Definition
True - instance members can call static members because the compiler will know what kind of member it is |
|
|
Term
True or false - static members can access instance members and static members. |
|
Definition
False - static members can access other static members, but they cannot access instance members, as they are not aware of instance information, only class information. |
|
|
Term
What do we call "static final" variables?
How are they typically named in Java? |
|
Definition
1. Constants
2. Using all caps and underscores to separate words;
public static String THIS_IS_A_CONSTANT = "constant"; |
|
|
Term
True or false - you cannot update static final reference variables, i.e.;
public static final List list = new ArrayList();
public static void main(String[] args){
list.add("one"); // WILL THIS COMPILE?
} |
|
Definition
False - you CAN update static final reference variables, by calling methods on them! When marking them as final, all the compiler can do is make sure you are not reassigning them to another object, i.e.;
public static final List list = new ArrayList();
public static void main(String[] args){
list.add("one"); // THIS WILL COMPILE
list = new ArrayList(); // THIS WILL NOT COMPILE!
}
|
|
|
Term
How do you declare a static block? |
|
Definition
|
|
Term
When are static variables/blocks initialized in class construction? |
|
Definition
First, and in the order they appear. |
|
|
Term
True or false - you cannot initialize static final variables after they have been first declared, they must be initialized on declaration. |
|
Definition
False - you can initialize them later in a static initialization block, first thing during object construction, and prior to the class and variable being used in the program. |
|
|
Term
What is the format for static import statements, and can you use them to import classes or members? |
|
Definition
import static java.util.Arrays.asList;
OR
import static java.util.Arrays.*;
They are only used for the static members of a class, not the class itself.
Also in your code it is not ok to preface the static reference call with the class name when you use a static import;
Arrays.asList("one"); // WILL NOT COMPILE
asList("one"); // THIS WILL COMPILE |
|
|
Term
True or false - statically importing two static members with the same name from different classes will compile;
static import com.text.B.Type;
static import com.text.A.Type; // WILL THIS COMPILE? |
|
Definition
False - this will not compile, its best to just import one of them, and then provide the full package name reference in the code when you need to use the other to make sure the compiler knows we are accessing one different than the one that has been imported. |
|
|
Term
True or false - when passing a primitive parameter in a method, variable reassignment will affect the original value.
i.e.
int nine = 9; // WHAT WILL THE VALUE OF THIS BE AFTER THE METHOD CALL
change(nine);
void change(int nine){
nine = 8;
} |
|
Definition
False - Java uses pass-by-value which creates a copy of the primitive in the method call, the method is only reassigning the input parameter, and does not affect the original value. The value in the example would after the method call would be 9. |
|
|
Term
Assigning a new primitive or reference to to a parameter inside a method body DOES/DOESN'T change the caller (or the original value of the parameter).
If you call methods on a parameter that is referencing an object inside of a method call, this DOES/DOESN'T affect the caller (or the original value of the parameter). |
|
Definition
|
|
Term
True or false - When a method returns a value, but it is not used or captured by the caller, the returned value is ignored. |
|
Definition
|
|
Term
What is method overloading? |
|
Definition
Method overloading is when you create a method that has the same name as another method, but a different method signature (different type/and or number of parameters). |
|
|
Term
True or false - everything else about a method can change when you overload a method, even the method name. |
|
Definition
False - when overloading, everything else in a method declaration can change EXCEPT the method name, that must be the same as the original method name. |
|
|
Term
True or false - the throws clause and exceptions list as well as the access modifiers are important when creating an overloaded method. |
|
Definition
False - when overloading, the exceptions and the access modifiers are irrelevant. |
|
|
Term
True or false - method overloading occurs when a method has the same method name, the same type and number of parameters, but the input parameters are just in a different order. |
|
Definition
|
|
Term
Will the following code compile, and why or why not?
public void fly(int[] intses){}
public void fly(int... intses){}
|
|
Definition
No. Java treats a vararg input parameter and an array as the same, and you cannot have two methods with the same signature. |
|
|
Term
True or false - varargs arguments give you the flexibility to pass in an array OR standalone parameters;
public void addNums(int... Nums){}
// can call with either?
addNums(new int[]{1,2,3});
addNums(1,2,3); |
|
Definition
True. Vararg s gives you this flexibility, but if the method signature contained an array input parameter instead, standalone values would not compile. |
|
|
Term
True or false - If there are two similar methods, but one with a primitive input param and one with Wrapper Object input param, Java will always use the most specific parameter list it finds, in this case the primitive version;
public void test(int num){
}
public void test(Integer number){
}
short s = 1;
test(s); |
|
Definition
|
|
Term
True or false - Autoboxing is when Java draws a box around a primitive value. |
|
Definition
False. Autoboxing is when Java will automatically promote primitive values to their matching wrapper object types. |
|
|
Term
What is the order that Java chooses the right overloaded method? |
|
Definition
1. Exact match by type.
2. Larger primitive type.
3. Autoboxed type.
4. Varargs |
|
|
Term
True or false - When method overloading, Java is ok with converting an input parameter two or more times to find the most specific type for the method call. |
|
Definition
False. Java cannot handle converting in two steps, only one. |
|
|
Term
_____ are used when creating new Objects. This process is called _____ because it creates a _____ _____ of the class. A constructor is called when we write _____ followed by the name of the class we want to _____. |
|
Definition
1. Constructors
2.instantiation
3.new instance
4. New
5. Instantiate |
|
|
Term
True or false - when Java sees the new keyword, it allocates memory for the new object. |
|
Definition
|
|
Term
True or false - Java does not need the this keyword when it needs to determine an instance variable or a parameter when they have the same name. |
|
Definition
False. Java does need to use this when trying to determine wether to use an instance variable or an input parameter. If they have the same name, Java will refer to the one with the most granular scope which would be the input parameter. So, using this.instanceVarName tells the compiler that it is referring to the instance var and not the input param. |
|
|
Term
If you don't include any constructors in a class, Java will insert one for you. This is called the _____ _____. |
|
Definition
default constructor OR default no-arg constructor.
Remember the compiler will only ever insert one if there are no other constructors in the class. |
|
|
Term
When does java insert the default constructor? |
|
Definition
|
|
Term
If the exam asks you "which of these classes has a default constructor?" And it shows you something like;
class Bear{
public Bear(){}
}
Does this class have a default constructor? |
|
Definition
No. You would not say this class has a default constructor, because the coder already provided one, and even if it is the same as the default the constructor would insert. If you saw;
class Bear{
}
This would imply that the coder did not supply any constructor at all, and the compiler will be inserting a default constructor at compile time.
|
|
|
Term
True or false - even if a class already has a private no-arg constructor, the compiler will still insert a default constructor. |
|
Definition
False. The compiler will only insert a default constructor if no constructors exist in a class. This class has a constructor, and even though it is private, that does not matter, a constructor still exists, and the compiler will not insert a default no-arg constructor. |
|
|
Term
Classes can have 1/many consuctor(s) in a class, and each one must have the same/different name as the class itself, and each must have same/different signature, and must/must not have a return type. |
|
Definition
1. Many
2. Same
3. Different
4. Must not |
|
|
Term
If we need one constructor to call another one, why can't we just call new Object(T types) inside the constructor, when referring to another constructor? |
|
Definition
What happens in a constructor when you use new, is that it just creates another, different, object in memory, and has no effect on the instance and ignores the instance.
You must use either this() when referring to another constructor in the same class. |
|
|
Term
True or false - The this() call in a constructor that refers to another class constructor can be placed anywhere in the constructor. |
|
Definition
False. the this() call must be the first statement in the constructor, and this() only calls constructors in the current class. To call another class constructor, you must use super(). |
|
|
Term
When you have each constructor in a class add one parameter and call the next constructor until you get to the one does all the work and assigns the values to the variables (among other things) this is called _____ _____. |
|
Definition
|
|
Term
Final instance variables must be assigned _____. Assignment can be in the _____ or a _____. |
|
Definition
1. Once
2. Variable declaration
3. Constructor |
|
|
Term
True or false - by the time object construction is completed, the values for all final instance variables must have been set. |
|
Definition
|
|
Term
What is Java's order of initialization? |
|
Definition
1. Superclass
2. Static variable declarations and static blocks (initializers) in the order they appear.
3. Instance variable declarations and instance blocks (initializers) in the order they appear.
static blocks look like this;
static {
}
instance initialized blocks look like this;
{
}
|
|
|
Term
True or false - If the exam presents you with a class that has a mixture of instance and static variables and blocks, but only refers to static members in the question, and does not instantiate the object, you must still initialize the static variables and blocks. |
|
Definition
|
|
Term
In the main method lets say the first line is a System.out.println("main"); and after that there is a line in main that has a new Object() constructor, where the first line in the constructor is a System.out.println("constructor"); and where the new Object has a static variable public static String sVar = "sVar"; and a static block that prints out sVar System.out.println(sVar); and then a static block that initializes a new Object(); and then also has an instance variable public String iVar = "iVar"; and an instance block that prints out iVar System.out.println(iVar); - in what order will Java's order of initialization print out the statements and variables when the program is run? |
|
Definition
It will initialize and print the static variable(s) first, then will call the static initiliazer block with the new Object() line which will first call the initializer var(s) and blocks then the print statement for the "constructor" in the Objects contructor, and then print statement in the main method "main", and it will look like this;
sVar
iVar
constructor
main |
|
|
Term
What happens when a class gets loaded in the JVM? |
|
Definition
Static variables and blocks are initialized right away, ONLY once, and then the main method is run. |
|
|
Term
What are the rules for Encapsulation? |
|
Definition
All properties (variables) are private. All getters and setters of the properties are public. Boolean getter method names begin with is. All other property type getter method names start with get. All setter method names begin with set. Method names begin with is/get/set and then the name of the property with the first letter of the property name capitalized;
public String strVar= "";
public Boolean safe;
public String getStrVar(){
return this.strVar;
}
public void setStrVar(String strVar){
this.strVar = strVar;
}
public Boolean isSafe(){
return this.safe;
}
public void setSafe(Boolean safe){
this.safe = safe;
} |
|
|
Term
How do you create an Immutable class? |
|
Definition
Create an encapsulated class, but remove the setters. |
|
|
Term
How do we set the initial values in an immutable class? |
|
Definition
Using a constructor that has input parameters that set the initial values. |
|
|
Term
If you have an immutable class, and a caller initializes the values on a variable through the constructor, how do we prevent a caller from modifying the variable via the get method by call methods on it by chaining them to the get method?
i.e.
ImmutableClass imute = new ImmutableClass(new StringBuilder("new"));
imute.getSb().append("modify the stringbuilder"); // THIS WILL CHANGE THE INTERNAL SB - how do we stop this? |
|
Definition
Change the get method in the immutable class to not return a reference to the object, but a copy via a new object;
public StringBuilder getSb(){
return new StringBuilder(this.sb);
}
This way the caller can get and make changes to the copy, but the original immutable class, stays the same. |
|
|
Term
Provide single statement legal lambdas with a method called "print", with 1 and 2 String parameters, and calling the .startsWith() method on the string;
1 param
2 params |
|
Definition
print(s -> s.startsWith("a"));
print((s,t) -> s.startsWith("a"));
print((String s) -> s.startsWith("a"));
print((String s, String t) -> startsWith("a")); |
|
|
Term
Provide lambdas with statements for String parameters, one with .startsWith, one with .endsWith for
2 parameters;
|
|
Definition
print((s,t) - > {
if(s.startsWith("a"))
System.out.println("ok");
return (s.startsWith("a") && t.endsWith("s"));
});
print((String s, String t) -> {
Return (s.startsWith("a") && t.startsWith("s"));
}); |
|
|
Term
True or false - If you have a lambda with multiple parameters parenthesis are not required.
i.e.
print(s,t -> s.startsWith("a"));
OR
print(String s, String t - > s.startsWith("a"));
Both of these are legal? |
|
Definition
False. Any time a lambda has multiple parameters, you need to wrap them in parenthesis. |
|
|
Term
True or false - you are allowed to redeclare variables in a lambda expression.
i.e.
print(s -> {
String s = "d";
return s.startsWith("d");
}); |
|
Definition
False. You are not allowed to redeclare or name a variable the same in a lambda expression is the same as an input parameter name.
in the example above you would just need to rename String s to something else;
String f = "d"; |
|
|
Term
What package is the Predicate interface in? |
|
Definition
|
|
Term
Which Java class has a method with the Predicate interface built-in, and what is the method called? |
|
Definition
ArrayList has the removeIf method with the Predicate interface built-in as a parameter. |
|
|
Term
If you are declaring a static print method passing in params List<String> and Predicate<String>, what would the signature and the method look like, if you wanted to see if a List of Strings started with the "a" string?
What would the method call look like? |
|
Definition
The signature would look like;
public static void print(List<String> l, Predicate<String> checker){
for(String s: l){
if(checker.test(s)){
System.out.println("s ="+ s);
}
}
}
The method call would look like;
List strings...
print(strings, s -> s.startsWith("a")); |
|
|
Term
True or false - including public setters is required for encapsulation, And for immutability they must exist. |
|
Definition
False - it is true that for immutability, the public setters must be removed, but including public setters is not required for encapsulation. |
|
|
Term
True or false - when using Predicates the following is required;
i.e.
you have a print method that uses a Predicate<String> to check the values in a list, and you pass in the code for the predicate to run the test method on;
print (list, s - > s.equals("s"));
1) The Predicate is only expecting one parameter so if you see (s1, s2) ->..., this will not compile.
2) When sending the code, you must refer to the variable name in the code s -> s.equals("s"), or it will not compile.
3) Any time you use multiple statements, in sending the code, you need braces, and a return.
|
|
Definition
|
|
Term
True or false - 1) data encapsulation and immutability both require private variables.
2) Immutability does not allow setters.
3) Encapsulation and Immutability do not require that all instance variables be exposed. |
|
Definition
|
|
Term
True or false - the predicate method definition does not care about using interfaces as parameters or the objects implementing them.
public static Boolean checkList(List list, Predicate<List> p){
}
so you can call it using
checkList( new ArrayList(), (ArrayList al) -> al.isEmpty()); |
|
Definition
False - you must use this exact type in the method which in this case would be list, not arrayList |
|
|
Term
True or false - if a variable name is already used above a call to a predicate, in the same scope, you can still use that variable name in a call to the predicate method.
Data d = new Data(1);
filterData(al, d -> d.value%2 == 0); |
|
Definition
False - you cannot use the same variable name in a call to a predicate that has the same name as a variable in the same scope. |
|
|
Term
True or false - when the return type in a parent method that is overridden is primitive, you can substitute a higher level primitive so it can be promoted in the overridding method. |
|
Definition
False - if overridden method has primitive return, then the subclass method that overrides it must be an exact match. |
|
|
Term
True or false - a class cannot override the superclass's constructor. |
|
Definition
True - because constructors are not inherited. |
|
|