Java Basics Flashcards
Is the following syntax valid?
int x=10, int y;
No.
It must be written either:
int x=10; int y;
or
int x=10, y;
Must a java file have a public class?
No.
If a class (or enum) is public then where must it be defined?
In a file by the same name.
Can there be more than one public class (or enum) per file?
No.
Is final
a valid modifier for a main method?
public final static void main (String[ ] args) { }
Yes.
Final means this cannot be overridden. Making this static method final prevents subclasses from implementing the same (hidden) static method.
What does Object class’s toString method do?
It prints the name of the class + the @
sign + the object’s hash code.
If you pass the name of an array to a print/println method, what happens?
It will print the symbol ` [ ` + the Element Type encoding + the ` @ ` sign + the hash code.
E.g.: [LmyArray;@
If you do not import a given package, how can you still access its public methods?
Use the fully qualified class name.
T/F: Every class belongs to a package.
True.
Even if you omit the package statement, the class belongs to the unnamed package.
The following are the complete contents of TestClass.java file. Which packages are automatically imported?
class TestClass{ public static void main(String[] args){ System.out.println("hello"); } }
java.lang
and
“The package with no name”
Does a main method necessarily need to take an array of strings as an argument if it is to run from the command line?
Yes.
A class or interface can be referred to by using a fully qualified name or a simple name. What must happen to be able to use the simple name?
The class must be in the same package or imported.
Does Java support multiple inheritance of types?
Yes.
Interfaces, classes and enums are all “types”. Since a class can implement multiple interfaces, Java does support multiple inheritance of types.
Does Java support multiple inheritance of state?
No.
State is represented by instance fields, which only a class can have.
What is encapsulation?
The technique used to package the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.
It helps make sure that clients have no accidental dependence on the choice of representation