A data-processing application uses this Java class. The ? in a generic type is a wildcard representing an unknown type argument.
final class Cell< T > { private T value; Cell(T value) { this.value = value; } T get() { return value; } void set(T value) { this.value = value; } }
Inside a method, the following initialized parameters are available:
Cell< ? extends Number > source Cell< ? super Integer > target
Consider each candidate statement independently. Classify it using its compile-time type, without changing the parameters or adding casts. The cast already shown in one candidate is part of that candidate.
Read candidates:
Number first = source.get(); Integer second = source.get(); Object third = source.get(); Double fourth = source.get();
Write candidates:
target.set(Integer.valueOf(37)); target.set(Double.valueOf(6.5)); target.set((Number) Integer.valueOf(37)); target.set(null);
Give the number of read candidates that compile and the number of write candidates that compile, each as an exact integer count of statements. Also give the most specific type guaranteed safe for assigning target.get() without a cast, choosing from Integer, Number, and Object.
Unlock full course notes by signing up
From template call to machine code
A C++ source file defines template < typename T > T scale(T x, T k) { return x * k; } and later calls scale(2.5, 4.0). Put the events that turn this call into running code in the order in which they happen during compilation and linking.
Tracing a generic method and counting compiled copies
Consider this Java generic method and the calls that follow it.
Find the value stored in x, the value stored in y, the number of compiled method bodies for countMatches in the Java class file, and the number of instantiations a C++ compiler would generate if the method were rewritten as a function template and called once with an int array and once with a std::string array. Give each answer as a whole number.
Checking bounds and generic assignments
A sensor application uses the following Java class. Integer and Double extend Number, whereas String does not.
Consider each declaration independently inside a method in the same package. Enter yes if the declaration compiles or no if it does not. No casts or omitted type arguments are allowed.
Counting instantiations from literal types
A C++ program defines the function template below and then makes seven calls to it in main.
How many distinct instantiations of mid does the compiler generate? Give your answer as a whole number.