OCA Java SE 8 Programmer I Flashcards
If package component is not defined in a class, where does this class resides?
In a default package
Can a class have 2 packages defined?
No, it will fail to compile
Can import statement be placed before package statement?
No, it will fail to compile
How do you structure multi-line comments?
/*
* This
* is
* a comment
*/
Asterisks in every line are not mandatory but they can often be found due to aesthetics reasons.
What is the name of java compiler in Windows and Unix systems?
Windows - javac.exe
Unix - javac
What is the difference between multi-line comments and Javadoc comments?
Java doc comments start with /**
They are processed with Javadoc which is a JDK tool for generating API documentation of Java classes
What are non-access modifiers?
- final
- static
- abstract
- synchronized
- native
- transient
Can you use Class instead of class to define a class? :)
No, it is case sensitive
What is a class?
Class is a blueprint out of which object is created
Can you define multiple classes and/or interfaces in a single Java source file?
Yes, how much you want and in any order you want
How many public classes/interfaces can you define in a single Java source code file?
You can define only one public class/interface and name that follows has to match the name of a Java source code file.
If you have defined multiple classes with a Java source code file is the import statement propagated to all defined classes?
Yes
Can you define more than one executable class?
Yes, you only need to decide which one to call on start-up
Define a main metod
public static void main(String args[]) {…}
public static void main(String… args) {…}
static public void main(String args[]) {…}
public static void main(String[] args) {…}
Can you define a main method with different signature?
You can, but if you want to run the program you need also a main method with the right signature. Having 2 main methods in the same class like:
- public static void main(String args[]) {…} //will be executed
- public static void main(double number) {…}
is okay since you are only overloading the main method.
Write down an example of running a Java class with command line parameters
java MyClass 1 2 3
Are package names case sensitive?
Yes
How to run a Java class from terminal if you are in different folder than that specific class?
You have to add the base directory to the classpath. Most convenient way would be to do it like:
java -classpath /Work/Projects/java8-certification test.MyClass
If you use an import java.lang.* does the size of your class increases?
No
If two packages are located in the same level, how do you import a class for another package?
You need to import it using the entire filepath (all packages from base directory)
When you use a class defined in java.lang package do you need to import java.lang package?
No, all classes automatically have java.lang package imported
Is it possible to use two classes with the same name that are located in different packages (ex. java.util.Data and java.sql.Data) in a particular class?
It is possible, by defining variables like:
java.util.Date date1;
java.sql.Data date2;
Using import statements for both of them is not possible!
When you use an import statement like import net.netconomy.* what classes are actually accessable?
Only classes defined directly in net.netconomy folder, classes that are defined in the sub-folder are not imported.
How to access classes defined in default package?
Default package classes can only be accessible from the classes that are also in a default package and located in the same directory - classes with named packages are not able to access them.
What are static imports?
Static imports are direct imports of public static attributes and methods for another class.
package net.netconomy.animal;
class Cat {
public static int lives = 9;
public void sound() {
System.out.println(“meow!”);
}
}
_________
import static net.netconomy.animal.Cat.*;
class Tiger {
public void roar() {
sound();
}
public int livesCounter() {
return lives;
}
}
Although they are called static imports, you can only use them as “import static”
What is a top-level class?
A top-level class is a class that is not defined within any other class.
On which type of Java entities access modifiers can be used?
They can be used on a class/interface and it’s members (variables and methods). They can’t be used on local variables and method parameters.
What access modifiers can we use on a class?
Only public and default (no modifier set). If public is set, class can be accessed outside of the package.
What are unrelated classes?
Unrelated classes are classes that don’t have inheritance relation.
Can related class access protected attributes of inherited classes?
Yes, but it can’t access them as reference variables.
What is the purpose of non access modifiers?
They are changing the default behavior of a class, interface or method.
Can you use abstract modifier on interface?
You can, but it is redundant.
Can you use final on a class?
You can use it and that means that the corresponding class cannot be extended.
Can you use final on an interface?
No, interfaces are abstract by default, code won’t compile.