Does a supercalss know anything about the subclasses that inherit from it?
No
Is a subclass free to override instance variables and methods define in a superclass?
Yes
Does an interface method have to be implemented?
Yes
Can an _ be used at the beginning of an identifier?
Yes
Is this a legal identifier:
:b
No, identifiers cannot start with a colon
Is this a legal identifier:
-d
No, identifiers cannot begin with a -
Can identifiers begin with a number?
No
Can # be part of an identifier?
No
Where can a public method be accessed from?
Anywhere
Where can a private method be accessed from?
Only within that class in which it is declared.
How many public classes per source file can there be?
1
What must come first, package statements or import statements?
Package statements
How many non-public classes can a source file have?
Any number
What is the syntax for static imports:
import static…
static import…
import static…
Can you use an import statement to search through the java API such as ‘import java.*’
No, this is legal but it will NOT search across packages
What are the 3 access modifiers?
Public
Private
Protected
What are the three nonaccess modifiers you need to know for the OCA exam
strictfp
final
abstract
What is the default access modifier that is not typed?
default
Can you have private classes?
No
Can you have protected classes?
No
What is the visibility of a class with default access?
Within same package only
If a class is public, yet in a different package, do you still need to have an import statement to gain access to that class.
Yes
Should a class ever be marked as both final and abstract?
No
What is strictfp
a non-access modifier that can modifiy a method or a class
What does making a class final mean?
It can never be subclassed ie String class
What is the purpose of an abstract class?
To be extended (hence why it can’t be both final and abstract)
What is the correct syntax for abstract methods located within an abstract class?
public abstract void goFast();
public abstract void goFast() {};
public abstract void goFast();
nb abstract methods end in a semi-colon
If a method is marked abstract, does the class also need to be marked abstract?
Yes
Can abstract classes have non-abstract methods?
Yes, these usually have implementations that shouldn’t change
Can a class be marked as both abstract and final?
No
What has to happen to classes which implement an interface?
They must implement the methods from the interface.
What are all interface methods implicitly?
public and abstract
Do you have to declare interface methods as public and abstract?
No, they are implicitly so.
What must all class methods that are implemented from interfaces be declared as?
public
Can interface methods be static?
No
Does an interface extend or implement other interfaces?
Extend
Can an interface extend a class?
No
Is this legal?
public abstract interface Rollable { }
Yes, but typing abstract is redundant
Is this legal in an interface? void bounce ();
Yes
Is this legal in an interface?
final void bounce ();
No, final and abstract cannot be used together, and abstract is implicit
Can interface methods be protected or private?
No, always public implicitly
Is this legal in an interface?
static void bounce ();
No, interfaces define instance methods not static methods
Can you put constants in an interface?
Yes
What must constants be (implicitly) if declared in an interface?
public static final
interface Woo {
int BAR = 55;
}
Is this BAR public, static and final?
Yes, it is implicitly done so because it is in an interface.
Can the values from constants from an interface be changed?
No.
What are methods and instance variables collectively known as?
Members
What can you modify memebers with - access or non-access modifiers?
Both
How many access modifiers can you use on members?
All four
Zoo z = new Zoo();
How can one get access to a member from Zoo?
z.memberName();
class Moo extends Zoo {…
How can one get access to a member from Zoo?
memberName();
What does the public access modifier mean is applied to members?
Can be access by all other classes regardless of package in which they belong
private String doRooThings(){}
Where can this method be access from?
Only from within the class in which it was declared
Can a subclass inherit this method from its superclass?
private int giveMeNumbers();
No, it is invisible to the subclass.
What happens if both a superclass method and the subclass method are the same, but the superclass method is private?
The subclass method is not overriding the superclass method because it is invisible to the superclass
What is the difference between these two methods?
void testIt(){} protected void testingIt(){}
void testIt(){} - default access, only accessible if class is in same package
protected void testingIt(){} - protected access, can be accessed by classes outside of same package only through inheritance i.e. extending a class
protected int x = 9;
Can this protected variable be accessed through the instantiation of a new class and using p.x?
No, it can only be accessed through inheritance by extending class.
If a subclass entends superclass and has access to protected members in the superclass, can another class extend child and have access to the first superclass’ members?
No, once they have been extend and viewed by the subclass, they are then essentially private and cannot be viewed any further by any more subclasses.
Can an access modifier be applied to a local variable ie
private int x = 7;
No
What is the only modifier that can be applied to a local variable?
final
final int x = 7;
How can a private member be accessed?
Only within it’s own class
Where can a public member be access from?
From absolutely anywhere
Can final methods be overridden by the subclass?
No
public CD getListing(int trackNumber, final int lenght) {}
Is final int lenght valid here?
Yes, it just can’t be directly edited in the method.
What must a method be to be abstract? (2 options)
- declared as abtract
- have no functional code
Will this compile?
public class abstractClassNot{ public abstract void wee(); }
No, abstract methods can only be declared in classes where class is also declared abstract.
Will this compile?
public abstract class abstractClass{ public void wee() { //code galore } }
Yes, abstract classes can contain non-abstract methods with full implementations.
What must the first concrete subclass of an abstract class do?
Must implement all abstract methods of the superclass and the superclasses above it.
Will this compile?
public abstract class Animal { abstract void foo(); } class Horse extends Animal { void foo(int i) { } }
No, Horse has not implemented abstract method foo, it has attempted to overload it.
Is this legal?
abstract final void doIt();
No, methods cannot be marked as both abstract and final.
Is this legal?
abstract final void doIt();
No, methods cannot be marked as both abstract and final.
Is this legal?
abstract static void printMe();
No, methods cannot be declared as both static and abstract.
Which of the four access modifiers can synchonized methods be modified with?
public
private
default
protected
What 3 things need to be known for the native modifier?
- can only be applied to methods
- reserve word
like abstract methods, method ends in (;) and implementation is omitted.
What can strictfp be used on?
Class and methods only, never on a variable.
Can a constructor ever have a return type?
No.
What’s the different?
protected Foo() {} protected void Foo() {}
The first is a constructor, and the second is a perfectly legal though hard to read method.
Can constructors use access modifiers?
Yes
Can constructors be marked static?
No
Can constructors be marked final or abstract?
No
What are the eight types of primitive data types?
- char
- boolean
- int
- float
- long
- double
- short
- byte
What does a reference variable refer to?
An object.
Can the 6 primitive number types be negative values as well as positive values?
Yes, they can be both.
How many bits are in a byte?
8
How many bits are in a short?
16
How many bits are in an int?
32
How many bits are in a long?
64
How many bits are in a double?
64
How many bits are in a float?
32
What does a char contain?
a single, 16-bit Unicode character
Where are instance variables declared?
In the class, but outside of any method
What access modifiers can be used on instance variables?
All four
Can instance variables be marked final?
Yes
Can instance variables be marked static?
No, because then they would not be instance variables.
Are local variables on the stack or on the heap?
They are on the stack
What happens if the value of a local variable is passed to another method?
The local variable itself still only lives during the lifetime of the method and will cease to exist at the end of the method.
If a local variable is an object reference, will the object be created on the stack or on the heap?
The object will be created on the heap, no such thing as a stack object, only a stack variable.
What is the main difference in use between instance variables and local variables?
Local variables must be initialized before they are used as they don’t get default values.
What is shadowing?
When a local variable is declared with the same name as an instance variable.
What are the two things that arrays store?
- multiple variables of the same type
- multiple variables that are all subclasses of the same type
Can arrays expand dynamically?
No
Are these legal?
int [] key;
int key [];
Thread[] threads;
Thread threads[];
Yes
Is this legal for a multidimensional array?
String [] managerName [];
Yes
Is this legal?
int [5] names;
No, sizes cannot be declared in the array declaration.
What does final mean for primitives?
The value can never be changed
What does final mean for references?
The reference to an object can never be changed, but the actual object could be changed.
What variables can volatile and transient be applied to?
Instance variables
Can initialization blocks be marked as static?
Yes
Can local variables be marked as static?
No
Is this legal?
int $money = 34;
Yes, variables can begin with currency signs
Is this legal?
int m0ney = 55;
Yes, variable names can contain numeric digits as long as they are not the first digit.
What is the length restriction on variable identifiers?
There is no length restriction.
Can main be overloaded?
Yes
What is the import syntax for static imports?
import static
How many public classes can a source file have?
1
Does the public class have to match the name of the source file?
Yes
What is the order of class layout?
package statement
import statements
class ( 1 public, many non-public)
What are the two access restrictions classes can have?
public
default
What are interface methods implicitly?
public and abstract
Can interfaces have constants?
Yes
What are interface constants implicitly?
public, static and final
Can a class implementing an interface itself be abstract?
Yes
Does an abstract implementing class have to implement the interface methods?
No, but the first concrete class does.
Do interfaces extend or implement other interfaces
Extend
What are methods and instance variables known as?
Members
Can a member be inherited from the superclass?
Yes
If members are accessed without the “.” operator, what must be true?
They must belong to the same class.
What does this refer to?
The executing object
Are private members visible to subclasses?
No
Where can default members be accessed from?
Only within the same package?
Where can protected members be accessed from?
Same package plus subclasses (classes that have been extended)
Can local members have access modifiers
No
What is the only modifier a local members can have?
final
What must happen before a local variable is used?
It must be initialised
How do abstract methods end?
semicolon
What are the three ways of determining a non-abstract method?
- not marked abstract
- has curly braces
- MIGHT have code between curly braces
Why can abstract methods not be marked as final or private?
Because they must be implemented by the subclass
Can a local and instance variable have the same name?
Yes, this is called shadowing
Is there such a thing as a final object?
No, the reference to the object may be final and always point to that object, but the object itself can change always
Can an animal array accept horse objects?
Yes, if Horse extends Animal and hence horse is an animal and passes the IS-A test