Test 1 Flashcards
What types of objects can be used with an enhanced for loop in Java?
An enhanced for loop can be used with either an array or an object of a class that implements the java.lang.Iterable interface.
How do you import all static members from a class using static imports?
To import all static members from a class using static imports, you can use the syntax:
import static packageName.className.*;
Is the java.lang.System class final?
Yes, the java.lang.System class is final, which means it cannot be extended. It provides access to system-wide resources and operations.
What are covariant return types in Java?
Covariant return types allow an overriding method to change the return type to a subclass of the return type declared in the overridden method, introduced since Java 1.5.
What does the ArrayList.subList() method in Java return?
The ArrayList.subList() method returns a view of a portion of the original list between specified indices, inclusive of the fromIndex and exclusive of the toIndex.
What happens when multiple static blocks are defined in a Java class?
Multiple static blocks, known as static initialization blocks, execute sequentially from top to bottom as the class is loaded.
They help with initializing static variables and performing other actions during class loading.
What happens if the overridden method returns a primitive type in Java?
Covariant return types do not apply to primitives. If the overridden method returns a primitive type, the overriding method’s return type must also be the same primitive type.
Is the java.lang.Number class final?
No, the java.lang.Number class is not final. It serves as a base class for numeric wrapper classes like Integer, Long, and Double.
will the following compile if not then why
boolean b1 = false; boolean b2 = false; if (b2 != b1 = !b2)
This statement will not compile the reason is that = has least precedence of all operators so the order of evaluation goes
b2 != b1
»_space;> false != false
»_space;> false
// firtsFalse = !b2
»_space;> compile time error because we are trying to assign to the value false // then
What are the characteristics of Java string literals?
String literals begin and end with double quotes (“ “), can contain one or more characters, and may also be empty.
How does the String substring(int beginIndex, int endIndex)
method of the String class work?
The substring(int beginIndex, int endIndex) method returns a new string that is a substring of the original string, starting from beginIndex and ending just before endIndex.
How do you import all classes within a package using standard imports?
To import all classes within a package using standard imports, you can use the syntax:
import packageName.*;
This allows access to all members of any class within the specified package, based on their access modifiers.
How are instance initializer statements/blocks executed in a class
Instance initializer statements/blocks are executed in the order they are defined
execution of these is after the static blocks and before the constructor. They are called when an object of the class is created.
What information is printed when you use System.out.println(exception)
?
The name of the exception class (fully qualified name) along with the message encapsulated in the exception.
Example:
exceptions.MyException: Exception from foo
describe the following method from the String class
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
How can you make an object eligible for garbage collection in Java?
An object can be made eligible for garbage collection by ensuring there are no references pointing to that object. Once no references exist, the object becomes a candidate for automatic memory reclamation.
Is it allowed to put underscores in a type cast?
No, underscores are not allowed in a type cast.
Example:
double d = (double) 1_000;
- This is invalid.
What should a lambda expression return to satisfy the Predicate interface in Java?
To satisfy the Predicate interface, a lambda expression must return a boolean value. The test method of the Predicate interface expects a boolean result.
describe the following method from the String class
int indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
Are changes made to the returned sublist from ArrayList.subList() reflected in the original list, and vice versa?
Yes, non-structural changes made to the returned sublist are reflected in the original list, and vice versa.
What does the ArrayList.add(E e) method in Java return?
The ArrayList.add(E e) method returns true if the collection was changed as a result of the call, indicating that the element was successfully added.
What is the purpose of standard imports in Java?
Standard imports are used to bring an entire package or a specific class into scope, allowing access to all members of the imported class or package using their simple names.
What are the default access modifiers for fields defined in an interface in Java?
Fields defined in an interface are always considered as public, static, and final.
in the following code on line 2 what is the order of evaluation and would this code compile, if not, why
Object t = new Integer(107); int k = (Integer) t.intValue()/9; // line 2 System.out.println(k);
evaluation begins with t.intValue()
and then the cast, because the (.) takes precedence over a cast
however the code will not compile because intValue() is not a method of Object
the following code would recify it:
Object t = new Integer(107); int k = ((Integer) t).intValue()/9; // line 2 System.out.println(k);
What is the significance of the final keyword for variables in Java?
Declaring a variable as final (final variableType variableName = “value”) means that the value of the variable cannot be changed once it has been instantiated with a value.
Can you declare a field as private or protected in an interface in Java?
No, you cannot declare a field as private or protected in an interface. They are always implicitly public, static, and final
Can you directly invoke the garbage collector in Java?
No, you cannot directly invoke the garbage collector. However, you can suggest the JVM to perform garbage collection by calling System.gc();.