Exam Flashcards
How many public classes in a .java file, and what are conditions on it and the file name.
1, and name must match with file name
When can compilation of Java code be omitted
When entire program is in one file
What is the output from compiling a java type definition?
Each type definition in a .java file is compiled to its own .class file whether public or not
What are the conditions for single file program
- All definitions in one file,
- First class much define main method
- No compiled .class file for contents
- Can’t access other compiled classes
Statement, Expression which returns a value?
Expression
What are all the syntax’s for comments
// Text
/* Text */
“””Text”””
What is the order of primitive widening conversions? How does char fit into this?
byte, short, int, long, float, double
char can be widened to int but not byte, short
What is the range of byte type
-128, 127
How is long 2L printed
2
How is float 2F printed?
2.0
In operator precedence where does equality come?
After relative >=, but before bitwise and &. Mid table
What is XOR ^ truth table
T^T = F
T^F = T
F^T = T
F^F = F
Name all 9 cases where var type is not permitted
Instance variable
Static variable
Method Parameters
Without initialisation or initialised to null
Compound declaration
Array initialisation like this a = { 1, 2, 3 }
As array element type
No self reference in initialisation
Return type
Give example of compound assignment
String s, t = “def”
(FYI, s is not initialised)
List all the valid array initialisations
int[][] a = new int[10][10]
int[] a [] = new int[3][3]
int a[][] = new int[3][3]
int[] a = new int[]{1,2,3}
int[] a = {1,2,3}
What are the 6 +/- methods on AtomicInteger
.addAndGet
.getAndAdd
.decrementAndGet
.getAndDecrement
.incrementAndGet
.getAndIncrement
What are the characteristics of the Runnable interface and Callable interface
Runnable takes no inputs and doesn’t return anything and throws no Exceptions. Callable takes no inputs but can return a value and can throw Exception
Both can throw unchecked exceptions
Are private static methods allowed in Interfaces?
Yes
What are the integer primitives and is this valid
integerPrimitiveType t = 0;
byte, short, char, int and yes the assignment is valid as they are integer primitives.
What is the result of:
Path p1 = Paths.get(“c:\a\b\c”);
p1.getName(1);
b
Drive letter is not included in path.
What is getRoot(path) on both windows and linux file systems
Windows -> c:\ (drive letter)
Linux -> /
Is this valid
Integer i = 1;
double a = i
Yes
Unboxing followed by widening conversion is allowed
https://docs.oracle.com/javase/specs/jls/se11/html/jls-5.html
Does Object class implement Comparable?
No
Collections.sort(array, null)
Does this compile and if so how does it sort
Yes and natural sorting
What are the types in the below
Object[] o = { 100, 100.0, “100” }
What is module-info.java compiled into
module-info.class
What is the result of and why?
int[] ia1 = { 0, 1, 2, 6};
int[] ia2 = { 0, 1 };
System.out.println(Arrays.compare(ia1, ia2));
2
If one array is a prefix of the other than the result will not just be -1, 0, 1
It will take into account the difference in the number of elements
Non null value is considered lexicographically larger than null value.
Record instance variables are implicitly what?
private and final
What runtime exception can be thrown with
Console c = System.console();
It can throw NullPointerException if console is not support and null is return, the next access of c will throw.
Which is time based and which date based
Period
Duration
Duration - Time
Period - Date
What are the interfaces for
Car::getModel()
car::getModel()
where Car is the object type, car is an instance of Car
Function and Supplier respectively
Can boolean be used in switch statement/expression?
What are the allowed types for switch?
No
byte, short, char, int and their wrapper classes. Then enum and string
What is the StringBuilder method to set capacity?
.ensureCapacity(int)
What is the lambda for stream.mapToInt()
X->x
Choosing from modules and packages, which are required and which are exported?
Modules are required and packages exported
int a, b;
a = b = 2;
Is this valid?
Yes
What format is LocalDate printed in?
2023-12-30
ISO_DATE format
What are the rules for functional interfaces
One abstract method
Default methods are allowed
Static Methods are allowed
Private Methods are allowed
With an SQL query that returns columns ID, NAME, AGE as ResultsSet. What are the ways of getting AGE value for row as an int?
rs.getInt(2)
rs.getInt(“AGE”)
What is the condition for a variable to be used in a lambda
It needs to be effectively final
What type of variables are initialised to default values and which are not? What are their default values?
Static and instance variables are initialised to default values
Boolean - False
Char - ‘\u 0000’
Numerical Primitive - 0 or 0.0
Reference - Null
Local variables are not initialised to default values
What can be put in to
Public static void main() and won’t change behavior.
What will change it?
Final
Changing access type, removing static and return type.
Is this valid?
abstract interface Interface {}
Yes, interfaces are implicitly abstract but there is no issue adding keyword explicitly
What does Paths.get(“c:\..\temp\test.txt”).normalize().toUri() evaluate to?
file:///c:/temp/test.txt
.. on root is just root
What do these String methods to:
.isBlank()
.isEmpty()
.strip()
.isBlank() - True if empty or only comprised of whitespace
.isEmpty() - True only if length is zero
.strip() - Strip removes leading and trailing whitespace
What happens when .remove(index) is called on an empty list?
IndexOutOfBoundsException
What is the method determination for
- Hidden methods
- Overridden methods
- Overloaded methods
- Hidden: Reference
- Overridden: Actual object type at runtime
- Overloaded: Actual object type at runtime
What is the method determination order for inputs?
Promotion, Cast, Boxing, Varargs
What does \s regex stand for?
A single whitespace
Can abstract classes have constructors?
Yes
What is the return type of Stream.count()
long
How is the Duration 12H 30M 12.45S printed?
What are the units for durations always?
PT12H30M12.45S
Hours, Minutes and Seconds with decimal
What exception is thrown when a file does not exist, give the fully qualified name.
java.nio.file.NoSuchFileException
When a sub class defined instance variable the same as super class is it overridden, overloaded or hidden?
How is the instance variable determined?
Hidden by the type of reference variable not actual type of object.
Will this compile and why?
switch(day){
case MONDAY:
TUESDAY:
WEDNESDAY:
THURSDAY:
FRIDAY: System.out.println(“working”);
case SATURDAY:
SUNDAY:
System.out.println(“off”);
}
Yes, the lines without case are just labels on the printing of “working”
What class and generic types does Properties class extend?
HashMap<Object, Object>
What is wrong with this definition?
Consumer x = (String msg)->{ System.out.println(msg); };
Consumer<> Is not typed and therefore implicitly typed to Object. This is not compatible with String msg and therefore won’t compile.
Does Stream.findFirst() take any parameters, and if so what?
No
How do you describe a module with java cmd
java -p/–module-path moduleFolder -d/–describe-module moduleName
How do you describe a module with jar cmd
jar -f/–file jarFile.jar -d/–describe-module moduleName
How do you list the java system modules
java –list-modules
How do you list the java system modules and the modules in a particular directory
java -p . –list-modules
Is this a valid record definition?
record Student(int id, String… subjects)
Yes
Are Records allowed static fields?
Yes
Can Records implement interfaces?
Yes
Can Records define other instance methods?
Yes
Which one of these can Records define?
- Extra instance fields
- Static fields
- Static methods
- Private method
No
Yes
Yes
Yes
What does \ mean in regex terms
Negate carriage return
What is the result of 10.0/0.0
Double.POSITIVE_INFINITY
How do you load a service?
ServiceLoader.load(service.class)
What are the elements of ServiceLoader.stream() and how are they accessed
<ServiceLoader.Provider<S>></S>
Provider.get()
Is this valid?
public int method() {
throw Exception();
}
Yes not need to return value if throwing exception
Which cmd line tool is –jdk-internals valid for and what does it do?
jdeps - It finds class level deps on JDK internal APIs which are inaccessible.
How is determination of an instance field named the same in both parent and child class?
How can the instance field in parent be accessed?
Based on reference type
By casting the reference type to the type of parent class.
How is determination of static field named the same in both parent and child class?
Based on reference type