← all problems

Unit 1 Review: Methods, Objects, and null 🌶️🌶️

A review for the end of Unit 1: how a method call is chosen and what it returns, how == and .equals differ for objects, and what null is. Questions about the two classes below. Where a question asks what is printed, type exactly what println would show; where a call throws an exception, type the exception's full name.

public class Calc {    public static int twice(int n) { return n * 2; }    public static double twice(double x) { return x * 2; }    public static int addOne(int n) { n = n + 1; return n; }    public static void shout(String s) { System.out.println(s.toUpperCase() + "!"); }    public static String join(String a, String b) { return a + "-" + b; }}public class Dog {    private String name;    public Dog(String n) { name = n; }    public String getName() { return name; }    public String speak() { return name + " says woof"; }}

Part 1: signatures and which method runs.

1. Which of these is the method signature of the join method in Calc?

2. Calc has two methods named twice, one taking an int and one taking a double. What is that called, and why does it compile?

3. What does System.out.println(Calc.twice(3)); print?

4. What does System.out.println(Calc.twice(3.0)); print?

5. What does System.out.println(Calc.twice(Calc.twice(3))); print?

6. What does this print?

double d = Calc.twice(5); System.out.println(d);

7. What does System.out.println(Calc.twice(2) + Calc.twice(2.5)); print?

8. What does Calc.shout("hey"); print?

Part 2: return values and parameters.

9. What does this print?

int n = 5; Calc.addOne(n); System.out.println(n);

10. What does this print?

int n = 5; int y = Calc.addOne(n); System.out.println(n + " " + y);

11. What does System.out.println(Calc.join("a", "b".toUpperCase())); print?

12. What does System.out.println(Calc.join(Calc.join("x", "y"), "z")); print?

13. What does System.out.println(Calc.join("b", "a").length()); print?

14. What does this print?

String t = "x"; Calc.join(t, "y"); System.out.println(t);

15. This program does not compile. The compiler's message is incompatible types: void cannot be converted to int. Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

16. This program does not compile. The compiler's message is no suitable method found for twice(int,int). Click the line the compiler points at.

Click a line to select it (click it again to unselect), then check.

Part 3: == and .equals. For two object variables, == asks whether they refer to the same object. A method a.equals(b) asks whatever the class's equals decides. String writes its equals to compare the characters.

17. Given String a = new String("hi"); and String b = new String("hi");, which statement is true?

18. What does this print?

String a = new String("hi"); String b = new String("hi"); System.out.println(a == b);

19. What does this print?

String a = new String("hi"); String b = new String("hi"); System.out.println(a.equals(b));

20. What does this print?

String a = new String("hi"); String c = a; System.out.println(c == a);

21. What does == compare when both sides are object variables?

22. Dog p = new Dog("Rex"); and Dog q = new Dog("Rex"); The Dog class above does not write its own equals method. What does p.equals(q) return?

23. What does this print?

Dog p = new Dog("Rex"); Dog q = p; System.out.println(p == q);

24. What does this print?

Dog p = new Dog("Rex"); Dog q = p; q = new Dog("Rex"); System.out.println(p == q);

Part 4: null. A variable of an object type holds a reference. The special value null means "refers to no object".

25. Which statement about null is true?

26. What does this print?

String s = null; System.out.println("Name: " + s);

27. What happens with String s = null; System.out.println(s.length());? Type the exception's name, or what is printed.

28. What does this print?

Dog p = new Dog("Rex"); Dog q = p; p = null; System.out.println(q.getName());

29. What happens with Dog p = new Dog("Rex"); p = null; System.out.println(p.speak());? Type the exception's name, or what is printed.

30. What does Dog d = null; System.out.println(d == null); print?

31. What does String s = null; System.out.println("x".equals(s)); print?

32. What happens with String s = null; System.out.println(s.equals("x"));? Type the exception's name, or what is printed.

33. This program compiles, but when it runs it stops with this error: java.lang.NullPointerException: Cannot invoke "String.length()" because "t" is null. Click the line that throws it.

Click a line to select it (click it again to unselect), then check.

Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.