Basics Flashcards
When is a prefix operator evaluated?
Before its FIRST use in the expression
When is a postfix operate evaluated?
After it is FIRST used in the expression
Which packages are automatically inherited into every Java source file?
java.lang. This does not mean that you can’t import it explicitly, its just not required.
How many public classes can appear at the top level of a source file?
Only 1. There can be public nested classes, but not more than 1 top-level public class. You can have more than 1 class at the top level so long as only 1 is public
Does an abstract class need abstract methods to be abstract?
No. An abstract class simply means it is not a concrete class and cannot be instantiated.
Can you import a class statically?
No, only static methods can be imported. “import static a.b.C” where C is a class is not permitted
Can you import a method statically?
Yes. To import all static methods of a class use the .* notation or just reference the method name to import only that method
Does importing a class give you direct access to its members?
No, you must explicitely import them.
Is it valid to use () in front of a method name when importing that static method
No, this is not allowed.
What is the order for declaration of class/interface definitions, import statements and package statements?
(1) Package statement (2) import statements (3) class/interface declarations
How many package statements can occur in a source file?
1 and only 1. If there is no package statement then the classes/interfaces defined in the class can NEVER be referenced by a class in another package
What java statements can be annotated with the abstract keyword and what does it mean in each case?
(1) abstract class - it cannot be instantiated (2) abstract method - needs to be implemented by child classes and the class must be declared abstract. You CANNOT have abstract variables.
Where can the final keyword appear and what does it mean?
(1) final class - you cannot extend the class (2) final variable - you cannot change the variable once its value is set either in the declaraiton or the constructor (3) final method - you cannot override the method
Where can the keyword static appear and what does it mean?
(1) nested classes NOT top-level classes (2) variables (3) methods - in each case, only 1 entity appears per class in the JVM
Define what class cohesiveness means?
It is ensuring that each of your classes only does what it is intended for with clear boundaries and responsibilities
Descrbe each of the rules used by the Java compiler to determine if an identifier is legal?
(1) The identifier can start with loweror upper case alphabet, (2) can’t start with numbers, (3) can start with underscore or connecting symbols, (4) can start with any currency-based symbol. (5) Can be any length in theory. (6) cannot be a java keyword, and is case-sensitive (7) the characters used can be any letter, currency characters, connecting characters and numbers
Are interfaces objects?
No.
What are the 8 rules of source file declarations?
(1) There can only be 1 public class in a source code file (2) Comments can appear anywhere
(3) Package statements, if included must be the first line of the file
(4) If there is a public class, it must match the name of the source code file
(5) If there are import statements they must appear between the package statement and class/interface declarations
(6) import and package statementa apply to all class in the file - you cannot declare classes in the same source code file in different packages or with different import statements
(7) A file can have more than one nonpublic class
(8) Files with no public class do not have to have a class whose name matches the source code file
What are the 5 requirements for a method to be executable in a class?
(1) call main
(2) return type is void
(3) accepts an array of strings
(4) be static
(5) be public
Note this means the main method can be final
Can the main() method be overloaded?
Yes, but any other main method will not be executable. It will just be treated as a normal main method.
Can a main method accept a varargs?
Yes. The following is perfectly legitimate -
public static void main (String .. args)
What is a classes fully qualified name? Give an example
The fully qualified name of a class is its class name plus the package path to get to it e.g. java.util.ArrayList and not just ArrayList.
Can you use a static import statement to import non-static entities?
No - you can only use static imports to import static members of a class
What is the order of the static and import keywords in a static import statement?
always, always “import static”. You cannot have static import instead