Java Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful JavaScript knowledge. If you are an Anki or Quizlet fan, consider adding some of these questions into a deck. 😀


Who created Java and in what year?James Gosling and others at Sun, 1995
Java programs are made up entirely of ________________ grouped into ________________.Classes, packages
What are the classic, if somewhat annoying, six words all beginners learn when writing their first command line app in Java?public, static, void, main, string, args
What is the line of code that writes "Hello, world" to standard output?
System.out.println("Hello, world");
Suppose Hello.java contained a program intended to be run from the command line. How do you compile and run the program from a typical operating system terminal application?
java Hello.java
Python, Swift, and Haskell require explcit import directives to use entities from a different module (or package). Does Java require one as well? Why or why not?No, if you simply mention a class in a Java source file, the compiler will look for the corresponding class file.
What does the import directive actually do?It simply allows code in this file to mention classes without using their fully-qualified name (provided there is no ambiguity).
What is the difference between plain import and import static?The former imports a class, and the latter imports a static member of a class.
What is special about the package java.lang?Classes from this package never need to be qualified.
What is the difference between InputStream and Reader on the one hand and OutputStream and Writer on the other?InputStream and OutputStream read and write bytes, while Reader and Writer read and write characters.
Are InputStream and OutputStream actually the same kind of stream objects as those that the package java.util.Stream is concerned with?No, stop being silly.
How do you generally compile and run a multi-file Java command-line application?
javac *.java && java Main.java
What kinds of members do classes have?Constructors, fields, methods, initializers, and (inner) classes.
What is the significance of fields and methods marked with static?These fields and methods are associated with the class itself, and not with any particular instance of the class.
What is the difference between a variable and an object in Java?Variables are those things you declare in your source code. Objects are the the entities created at run time.
A variable is _________________-constrained, while an object is an instance of a single ________________.Type, class.
What kind of names can Java infer the types of?Local variables and lambda expression parameters.
Java’s REPL is called ________________.JShell
What are the eight primitive types?boolean, byte, char, short, int, long, float, double.
What is the difference between a primitive and a reference type?Values of entites having a primitive type are actual values, while values of reference types are references to an actual entity. Alternatively, primitives behave as if they are always copied while references behave as if they are always shared.
Are Java strings mutable or immutable?Immutable
Java char values are not actual Unicode characters, but are rather 16-bit code units. How do you get the actual Unicode characters from a string?You can get a stream of the code points of a string s via s.codepoints().
Are Java arrays fixed-size or variable size? Mutable or immutable?Fixed-size, mutable.
How do you write the JavaScript array literal [13, 5, 8, 2] in Java?
new int[]{13, 5, 8, 2}
How do you define a class C that has a superclass S and that implements two interfaces A and B?
class C extends S implements A, B
What is an abstract method and why does Java have them?An abstract method is a method without a body. It can only appear in an abstract class, and is used to model behavior that concrete subclasses must implement in their own way, but for which no actual generalized implementation exists.
What are the two main differences between an interface and a class?(1) Interfaces cannot be instantiated but classes can. (2) Interfaces can only contain constants, abstract methods, static methods, and default methods, but classes can contain tons more things.
Sometimes you might hear people say an interface is a class. What might they mean?Interfaces are compiled into class files. So it’s like every type that isn’t primitive (i.e., arrays, interfaces, enums, and regular classes) is just, like, you know, classes.
How does Java handle the multiple inheritance problem when conflicting constants are inherited from two different interfaces?Both are inherited so you have to mention the interface type explicitly (e.g., via a cast) to access the proper value.
How does Java handle the multiple inheritance problem when conflicting static methods are inherited from two different interfaces?
How does Java handle the multiple inheritance problem when conflicting default methods are inherited from two different interfaces?
Is flatMap a method of arrays? What about filter? What about map?No, no, and no. These are methods of the Stream interface.
In Java, is List a type? Is List<Integer> a type?They are both types. The first one is left over from the early days of the language before generics existed. You should not use it.
What is the deal with generics and primitive types in Java?Java generics cannot be parameterized with primitive types.
What are collections, sets, and lists in Java?Interfaces. Collections are things that have their size queried,can be added to and removed from, and support membership tests. Sets are unordered collections in which all elements are unique. Lists are collections that are ordered and support adding and removing at arbitrary positions.
Why do most of the queue methods come in “pairs” (add/offer, remove/poll, element/peek)? The methods add, remove, and element throw an exception if they fail, while offer, poll, and peek return a value like null or false to indicate failure.
What is the main characteristic of lists, sets, and maps created with the static of methods (List.of, Set.of, Map.of)?The created objects are unmodifiable. (They also disallow null and are value-based.)
Java lambda functions impelement interfaces annotated with which annotation?@FunctionalInterface
What are suppliers, consumers, and predicates, and what are their characteristic operations?
SupplierFunction with no args, returning an objectget: () → T
ConsumerFunction with one arg, returning nothingaccept: (T) → void
PredicateFunction with one args, returning a booleantest: (T) → bool
What is the method reference for s -> s.toUpperCase()?
String::toUpperCase
Is null a member of every reference type in Java? If so, what were the designers of the language thinking when they included this “feature”?Maybe they just didn’t know this was the billion dollar mistake. Maybe they were lazy and thought the billion-dollar mistake was no big deal. Maybe they thought it was too complex to introduce optionals in 1995, since technology wasn’t so advanced back then.
What is the decision to include null as a member of every reference type referred to as? Who called it that? Why?Tony Hoare called it the billion-dollar mistake because he felt bad about inventing it. He estimated many years ago that this decision caused a billion dollars of wasted productivity from programmers making mistakes surrounding that darn null masquerading as an actual object.
Suppose variable s is declared to have type String, but at runtime, the value of s is null. What happens when executing s.toLowerCase()? A NullPointerException is thrown.
What is the equivalent of Swift’s nil in Java?
Optional.empty()
What is the equivalent of Swift’s x ?. y in Java?
x.map(y)
What is the equivalent of Swift’s x ?? y in Java?
x.orElse(y)
Suppose that o is an optional that wraps an integer, and f is a function that takes in an integer. How do you safely invoke f on the wrapped integer, given that o might be empty?
How do you create a stream producing the integers 1 through 100?
IntStream.range(1, 101)
How do you create an infinite stream of the even numbers starting at 20?
Stream.iterate(20, x -> x + 2)
How do you create an empty stream?
Stream.of()
How do you create a stream of 100 zeros followed by 1000000 ones? (Note: the entire stream is to have exactly those 1000100 elements. The stream should, as streams do, generate elements on demand.)
How do you create a stream from an array?
How do you create a stream from a LinkedList?
Given an array a, what does Arrays.stream(a).distinct().toArray() do?
Does Stream.of(2,3,5,7,11).map(x -> x * x).toArray() produce an integer array? If not, what does it produce, and how do you make it produce the expected integer array?
For array a, what does Arrays.stream(a).min() return, exactly?
Suppose r was a Reader object. Does r.lines().count() return the number of lines?
Suppose r was a Reader object. How do you get the lines of the reader into an array?
Suppose r was a Reader object. How do you get the lowercased lines of the reader into an array?
Suppose r was a Reader object. How do you get the lowercased lines of the reader into an array, sorted by line?
Suppose r was a Reader object. How do you get the lowercased lines of the reader into an array, eliminating all duplicates?
Given a list a, what does a.stream().anyMatch(x -> x < 5) produce? (Make sure to give the type of the result.)
Write an expression that produces whether or not all elements of a list (not an array, but a list) of integers, are even.
Does the expression a.stream().reduce(0, (x, y) -> x + y) produce the sum of the elements of list a? Why or why not?
The expression IntStream.range(1, n).reduce((x, y) -> x * y) is a good attempt to produce the factorial of n, but it doesn’t produce an integer. What does it produce? How can you modify it (slightly) to produce the integer factorial?
Java streams have many terminal operations, such as findFirst, findAny, anyMatch, allMatch, noneMatch, min, max, count, toArray, and reduce. There is also forEach. It is tempting to use forEach to post-process the stream by, say, adding the elements to a map. But this is a misuse of forEach. What kind of terminal operation should be used instead?
The collectors Collectors.toList and Collectors.toSet take no arguments. But Collectors.toCollection does take an argument. What kind of argument is this, and what is it for?
If you wanted to average some integers, you could write Stream.of(1,2,3).collect(Collectors.averagingInt(f)) for some particular function f. What should f be here?
The Collectors class has static methods toList, toSet, toMap, and toCollection, but there is no toArray. Why not?
What does Stream.of(1,2,3,4,5,6,7).collect(Collectors.groupingBy(x->x%3)) produce?
What is the difference between List<Object> and List<?>?
Suppose Animal is a superclass of Dog. In Java, is List<Dog> a subclass of List<Animal>?NO WAY! Java lists are invariant.