Dayrvia
Jv

Java · Practice questions

Try 16 questions on Java, then read the explanations and review the related concepts.

Practice questions

Choose an answer and move to the next question. Grade the last one to see explanations and review links.

16 questions
Q1 What is a after b is incremented?
int a = 3;
int b = a;
b++;
Q2 What is the value of a[1]?
int[] a = {1, 2};
int[] b = a;
b[1] = 7;
Q3 Which expression tests character content for equality?
String a = new String("OK");
String b = new String("OK");
Q4 Memo overrides the instance method text() from Note. Which implementation does n.text() call?
Note n = new Memo();
Q5 What is v[0] after the call?
static void edit(int[] a) {
    a[0] += 2;
    a = new int[]{50};
}
// inside main
int[] v = {3};
edit(v);
Q6 Which value does the caller’s n retain?
static void add(int n) { n += 10; }
// inside main
int n = 4;
add(n);
Q7 What is the content of name afterwards?
String name = "day";
name.concat("light");
Q8 What does pick(v) return?
static String pick(Object v) { return "O"; }
static String pick(String v) { return "S"; }
// inside main
Object v = "book";
Q9 What does a.id() return?
class A { String id() { return "A"; } }
class B extends A { String id() { return "B"; } }
// inside main
A a = new B();
Q10 What does c.total(6) return?
interface Cost { int total(int n); }
class Pair implements Cost {
    public int total(int n) { return 2 * n; }
}
// inside main
Cost c = new Pair();
Q11 What are a.size() and b.size()?
List<String> a = List.of("x", "x", "y");
Set<String> b = new HashSet<>(a);
Q12 What are m.size() and m.get("A")?
Map<String, Integer> m = new HashMap<>();
m.put("A", 2);
m.put("A", 5);
Q13 For custom HashMap keys, what must follow when a.equals(b) is true?
Q14 Which conversion occurs in the last line?
List<Integer> xs = new ArrayList<>();
xs.add(4);
int x = xs.get(0);
Q15 Why can head accept a nonempty List<Double>?
static Number head(List<? extends Number> xs) {
    return xs.get(0);
}
Q16 What is s after the blocks finish?
String s = "";
try {
    s += "1";
    Integer.parseInt("no");
    s += "2";
} catch (NumberFormatException e) {
    s += "3";
} finally {
    s += "4";
}
Original practice questions written for Dayrvia.