Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
108 Cards in this Set
- Front
- Back
Common Java legacy packages |
java.lang java.util java.io java.net java.awt javax.swing (javax = Java standard Extensions) |
|
Classes in java.util |
Comparator Date Calendar TimeZone Locale Currency Random StringTokenizer java.util.regex.Matcher java.util.regex.Pattern |
|
Classes in java.io |
byte-stream: InputStream OutputStream
character-stream: Reader - BufferedReader - InputStreamReader - FileReader Writer
File FileDescriptor FlenameFilter RandomAccessFile |
|
Classes in java.net |
Classes for network applications |
|
Java Naming Conventions: which elements begin uppercase and continue camel case? |
Class names Interface names Enumeration names |
|
Java Naming Conventions: which elements begin lowercase and continue camel case? |
Method names Instance, static and local variables Parameters |
|
Java Naming Conventions: which elements are uppercase and with compound words separated with underscore? |
Constants
Enumeration constants |
|
Java Naming Conventions: which elements are lowercase and with compound words separated with underscore? |
Package names |
|
Java Naming Conventions: which elements have one single uppercase letter? |
Generic type parameters
|
|
javac options |
-d destination of compiled classes -classpath -cp
|
|
java or javaw options |
-classpath -c -D= properties java -Dprop1="hello world" -Dprop2=pippo ClassToExecute -version after it the interpreter exits if a class is written before it is executed and this parameter ignored |
|
How to read the properties of an application |
Properties props = System.getProperties(); props.getProperty("property name"); props.setProperty("property_name", "property_value"); props.list(System.out); |
|
What are the type of statements in Java? |
expression conditional iteration transfer of control (Exception handling) |
|
What are the expression statements? |
assignment expressions empty statement (;) |
|
What are the transfer of control statements? |
assert break case continue return |
|
What are the exception handling statements? |
throw try-catch-finally |
|
What are the iteration statements? |
while do-while for loop enhanced for loop |
|
What are the conditional statements? |
if if-then if-then-else switch |
|
What is the structure of the if, if-then and if-then-else statements? |
if if [else] if-then if (else if)+ if-then-else if (else if)+ else |
|
What are the admitted types for the result of the expression in a switch statement? |
byte short int char (and their wrappers) enum String (only for Java > Java 7) |
|
What are the parts of a for loop? |
initialization
expression iteration loop block |
|
What is the structure of the enhanced for loop? |
for ( type variable : collection ) loop-block |
|
What types of element can be used to iterate with the enhanced for loop? |
array collection objects that implements the interface Iterable |
|
In what statements is used the break statement? |
switch for loop enhanced for while do-while |
|
In what statements is used the continue statement? |
for loop enhanced for while do-while |
|
What are the usage of the return statement? |
exit a method with a value return ; exit a method without returning a value return void; The return statement is not needed if it is the last statement of a method and if it returns nothing. |
|
How can be used a labeled statement? |
break ; terminates an outer statement The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement continue ; skips the current iteration of an outer loop marked with the given label |
|
What are the compound assignment operator? |
+= -= *= /= %= &= (and) |= (or) ^= (xor) <<= (signed left bit shift) >>= (signed right bit shift) >>>= (unsigned right bit shift) |
|
What are the numeric promotion rules used when an operation is applied to operands of different types? |
1) if one and only one is a double, convert the non-double to double 2) if one and only one is a float, convert the non-float to float 3) if one and only one is a long, convert the non-long to long 4) convert both to int |
|
What are the operator precedence in Java? |
1. [] () . 2. postfix ++ -- 3. prefix ++ --, !, ~ 4. cast, new 5. * / % 6. + - concatenation
7. < <= > >= instanceof 8. == != 9. & 10. ^ 11. | 12. && 13. || 14. ? : 15. assignment, compound assignment |
|
Strings are mutable or immutable objects? |
immutable! |
|
StringBuilder are mutable or immutable objects? |
mutable |
|
What is the difference between StringBuilder and StringBuffer? |
A StringBuffer object is thread-safe |
|
The string concatenation operator (+) is left or right associative? |
left (a + b) + c "abc" + 3 + 4 ==> "abc34" 3 + 4 + "abc" ==> "7abc" |
|
Among the following kind of initialization of a string variable, what is the most inefficient?
String s1 = new String("abc"); String s2 = "abc"; String s3 = "ab" + "c"; |
The most inefficient manner of initialize a string is by calling its constructor. |
|
What classes implement the CharSequence interface? |
String StringBuilder StringBuffer |
|
List common String methods |
charAt(int)
indexOf(int ch) indexOf(int ch, int indexFrom) indexOf(String string) indexOf(String string, int indexFrom)
length()
concat(String string)
replace(char old, char new) replace(CharSequence old, CharSequence new)
startsWith(String prefix) startsWith(String prefix, int offset)
endsWith(String suffix)
substring(int begin) substring(int begin, int end) [begin, end[
trim() toLowerCase() toLowerCase(Locale locale) toUpperCase() toUpperCase(Locale locale)
equals(String string) equalsIngoreCase(String string) |
|
List common StringBuilder methods |
append(...) insert(...) delete(int begin, int end) [begin, end[ deleteCharAt(int index) reverse() |
|
What are the primitives in Java? |
boolean (1 bit) char (16 bit unsigned) byte (8 bit) short (16 bit) int (32 bit) long (64 bit) float (32 bit) double (64 bit) |
|
How are initialized the primitives by default in a method? And as instance variable? |
In a method variables are not initialized by default, and so if they are used without initialization a compile time error will be generated.
In a class all the primitive instance variables are initialized by default with 0 or false. |
|
What are the range of numbers that can be saved in variables of the different primitives? |
boolean: true, false char: 0, 65535 byte: -128, 128 (127?) short: -32768, 32767 int:-2 000 000 000, 2 000 000 000 (circa) long: -2^63, 2^63 -1 float: ??? double: ??? |
|
What is the object to use in order to store a number without loosing precision? |
BigDecimal |
|
What are the wrapper classes for the primitives? |
Boolean Character Byte Short Integer Long Float Double |
|
What are the mechanisms that convert automatically primitives to their wrapper and vice-versa? |
Autoboxing: primitive -> wrapper
Unboxing: wrapper -> primitive |
|
What is the syntax for defining and use an enumeration? |
enum Example{ EXAMPLE1, EXAMPLE2}; Example ex; ex = Example.EXAMPLE1; boolean test = ex == Example.EXAMPLE2; // false |
|
What is a literal? |
A literal is an hard-coded value. It is any value that is not a variable. |
|
Is the following notation correct?
int number = 1_024; |
Yes. |
|
What are the parts that compound a method declaration? |
[access-modifier] [return-type] ([parameter-list]) { [body] } |
|
What does mean "overloading" a method? |
Overloading is when in a class have been defined many methods with the same name but with different parameter list.
The parameter lists can be different in the number of parameters or in their types.
The return type can also be different among overloaded methods. |
|
When a method is called, what kind of variables are passed by value and what by reference? |
Primitives and enums are always passed by value. All the other kind of variables are passed by reference? |
|
What is the scope of local variables, method parameters and instance variables? |
Local variable: the block of code in which has been declared and every nested block of code.
Method parameter: the entire method.
Instance variable: the entire class, it remains available until the object exists. |
|
What is the default constructor? |
The default constructor is added by the compiler only if a class doesn't specify any constructor. The default constructor has no parameters and only calls the super constructor. |
|
For what is used the keyword "this"? |
Refer the current object, for accessing instance variables or for calling instance methods. Referring to overloaded constructors. The "explicit constructor invocation" must occur in the first line of the constructor. |
|
For what is used the keyword "super"? |
Refer to an object's superclass. For accessing to methods or constructors in the super class. In a constructor must be the first line. If in a constructor is not called super, the compiler adds a call to super with no arguments, and so, if the parent class has not such a type of constructor, a compile time error will be generated. Can be used for calling an overridden method. |
|
For what is used the keyword "static"? |
For defining class methods and class variables, also known as static methods and static variables. |
|
What can be accessed inside a static method? |
Only static variables and other static methods. It cannot access any instance variable or instance method, neither use the keywords this and super. Utility methods, perform a single task. |
|
Where can be accessed static variables? |
Any constructor and any other method can access static variables. |
|
How can be defined a constant? |
Using the keyword final. |
|
How are initialized the elements of an array? |
The elements of an array of primitives are initialized with 0, whereas the elements of an array of objects are initialized with null. |
|
Where can be used the initialization with curly brackets { }? |
Only in-line with the declaration of the array. |
|
How to initialize one-dimensional array? |
int [] b = new int [3]; int [] c = {1,2,3}; int [] d = new int [] {1,2,3}; // without size
Interface [] i = new Interface [3];
int [] a = new int []; **NO** Object [] o = new Object [3] (); **NO** |
|
How to initialize a multi-dimensional array? |
int arr[][] = {{1,2,3}, {4,5,6}, {1,2,3,4,5,6}};
int arr[][] = new int[3][3];
int arr[][] = new int[2][]; arr[0] = new int[3]; Object o [] = {new Object[1], new Object[2]}; |
|
How are performed the following common actions with array? access retrieve the size copy |
var[index] var.length System.arraycopy(src, srcPos, dest, destPos, length); |
|
What are common methods of the class ArrayList? |
ArrayList() ArrayList(int capacity) get(int) size() add(E) add(int, E) ensureCapacity(int) remove(int) remove(Object) |
|
Can an object of type ArrayList be used in an enhanced for loop? |
Yes, because the class ArrayList implements the interface Iterable. |
|
When a class extends another class what does it inherits? |
All the public and protected methods and instance variables, and if they are in the same package all the package-private methods and instance variables. |
|
What is an overridden method? |
A class can override a method inherited from one of its parent classes redefining it with the same signature (name, parameters, return type) The new method can call the overridden method by means of the super keyword. A call to an overridden method is resolved at runtime and is based on the type of the object on which a method is called. |
|
An abstract class must have some abstract method? |
No, it may have some abstract method, but is not mandatory. |
|
A class that implements some interface must implements all its methods? |
Yes, if it is a concrete class. No, if it is an abstract class. |
|
Can an interface extend only one other interface as happen with the inheritance of the classes? |
No, an interface can extend as many interfaces as it needs. |
|
What is the definition of Encapsulation? |
Encapsulation is the gathering of similar data and methods in single classes. Note that Encapsulation is a programming language feature, whereas Information Hiding is a design principle. For the context of the exam, encapsulation is defined as information hiding. |
|
What are the access modifiers? |
private default (package-private) protected public |
|
A method or instance variable marked as private by what methods can be accessed? |
Only by methods in the same class. The access is restricted also to its subclasses. |
|
A method or instance variable with the default access level (package-private) by what methods can be accessed? |
Only by methods on the same package. |
|
A method or instance variable marked as protected by what methods can be accessed? |
By methods on the same package and by the methods in the subclasses. |
|
A method or instance variable marked as public by what methods can be accessed? |
By any method. There aren't any kind of restriction. |
|
What is the definition of Information Hiding? |
Information hiding is the technique of hiding the implementation details of a class using the most restrictive access modifiers that can be used. The class must have a set of public methods that expose its functionality to other objects. Any other method should not be public |
|
What is the access modifier that must be used for the methods of an interface implemented in a class? |
public |
|
What is the meaning of Polymorphism? |
Polymorphism is when an object can take the place of an object with different type. It is the capability of an object to be referred as its more general parent. |
|
What are the cases of polymorphism? |
When a class inherits another class or implements an interface.
Polymorphism uses the is-a relationship.
An object with an is-a relationship with another object can be used polymorphically as the second object, without being cast. |
|
In which cases there is the is-a relationship? |
When a class inherits another class, the first class "is-a" the second class. A more specific class is-a more generic class. The vice-versa is not valid. |
|
How to use practically polymorphism? |
Any subclass can be used interchangeably with its superclass.
If B extends A, to a variable of type A can be assigned an object of type B, but can be used only the methods defined in class A.
Any class that implements an interface can be used with its interface.
If A implements I, to a variable of type I can be assigned an object of type A, but can be used only the methods defined in the interface I
Access to static and instance fields and static methods depends on the class of the reference variable and not the actual object to which the variable points to. This is the opposite to what happens in the case of instance methods, the method of the actual class of the object is called. |
|
What is casting? |
Casting means converting an object to its original runtime type or any of its super classes.
Primitives can be cast to other primitives or compatible objects. Casting is needed to convert primitives when the conversion causes data loss. To perform a division on float variable is needed to cast the integer operands to float, e.g. float a = (float) b / (float) An object can be cast to a type only if it was instantiated as this type or as one of its subclasses. |
|
What is the common parent class for exceptions and errors? |
Throwable |
|
What classes are direct children of the class Throwable? |
Exception and Error. A child of Exception is the class RuntimeException |
|
What are checked exceptions? |
Checked exception are all the children of the class Exception. They are checked at compile time. Must be caught by catch statement or the thread will terminate. |
|
What are unchecked exceptions? |
Unchecked exception are all the children of the class RuntimeException.
They are checked at runtime, rather then compile time.
They don't need to be caught. |
|
What are unchecked errors? |
Unchecked errors are all the children of the class Error. They are used in case of extreme conditions that cause the application to fail. They shouldn't be handled. |
|
How to implement a custom exception or error? |
Extending the class Excpection or RuntimeException or Error.
Custom exceptions must terminate with Exception, whereas the custom errors with Error.
Custom exceptions and errors should implement a no-argument constructor and a constructor which takes a String argument. |
|
What are common methods of an exception class? |
These are the methods inherited from the Throwable class.
getMessage returns a detailed message about the exception
toString returns a detailed message about the exception and the class name printStackTrace prints a detailed message about the exception and the stack trace |
|
What type of object can be thrown with the throw statement? |
Every subtype of the Throwable class, otherwise a compile time error will occur. throw new IllegalStateException(); |
|
What is the usage of the keyword throws? |
It indicates that the method can send the specified exception to the caller. |
|
What kind of try statements exist? |
try-catch try-finally try-catch-finally try-with-resources The multi-catch clause can be used as catch clause. |
|
What is the best practise for sorting the catch statements? |
Subclasses first.
It is not a best practise, it is mandatory, otherwise there will be a compile time error telling that the exception has been already caught. error: exception IllegalArgumentException has already been caught |
|
What does "silencing an exception" mean? |
It happens when a catch block is left empty. It is not a good practice. |
|
In which cases the finally clause is executed? |
It is always executed after the try clause, or any catch clause, even if try or catch return. It is not executed only if the application terminates before the termination of the try block, except if an exception has been thrown. |
|
What is the usage of the try-with-resources statement? |
try(Resource r = new Resource()){ // code } catch(Exception e){ // code }
where the class Resource implements the interface AutoCloseable.
After the execution of the try block the resource is closed automatically, the same if an exception occurs.
The catch clause is not mandatory. |
|
What is the usage of the multi-catch clause? |
It permits to catch multiple exception in one catch clause.
} catch (ExceptionType1 | ExceptionType2 ... e ){ |
|
List common checked exceptions. |
CloneNotSupportedException ClassNotFoundException NoSuchMethodException IOException FileNotFoundException SQLException InterruptedIOException |
|
List common unchecked exceptions. |
IllegalArgumentException NumberFormatException ArrayIndexOutOfBoundsException IndexOutOfBoundsException NullPointerException IllegalStateException ClassCastException ArithmeticException |
|
List common errors. |
AssertionError (failed assertion) ExceptionInInitializeError (unexpected exception in a static initializer) VirtualMachineError NoClassDefFoundError |
|
When is an object marked for garbage collection? |
When every reference to an object are put to null then the object is marked for garbage collection. |
|
When is the garbage collector executed? |
It is a low-priority thread and its execution depends on the OS. You can never be sure about which objects have been garbage collected and when this happens. |
|
How many main methods used to start a Java application can be defined in a class? |
Only one. (note that here we are talking about the main method used to start a Java application and not about a generic main method) |
|
In how many classes of an application can the main method be defined? One or many? |
Many. |
|
What type of arguments can accept the main method? |
String array String... arg (varargs) String **NO** |
|
Can the main method instantiate the current object? |
Yes.
There are no limitations on the kind of objects that can be instantiated in the main method. |
|
Can the value null be assigned to a primitive variable? |
NO |