Date: 02/01/2023
Score: 15/21 (71%)
| Question # | Correct |
|---|---|
| 1 | ✅ |
| 2 | ✅ |
| 3 | ✅ |
| 4 | ✅ |
| 5 | ✅ |
| 6 | ✅ |
| 7 | ✅ |
| 8 | ❌ |
| 9 | ✅ |
| 10 | ✅ |
| 11 | ✅ |
| 12 | ❌ |
| 13 | ✅ |
| 14 | ❌ |
| 15 | ❌ |
| 16 | ✅ |
| 17 | ❌ |
| 18 | ✅ |
| 19 | ✅ |
| 20 | ❌ |
| 21 | ✅ |
❓What is the result of the following code❓
public class Employee {
public int employeeId;
public String firstName, lastName;
public int yearStarted;
@Override public int hashCode() {
return employeeId;
}
public boolean equals(Employee e) {
return this.employeeId == e.employeeId;
}
public static void main() {
Employee one = new Employee();
one.employeeId = 101;
Employee two = new Employee();
two.employeeId = 101;
if (one.equals(two)) System.out.println("Success")
else System.out.println("Failure");
}
}A. Success🎃
B. Failure🎃
C. The hashCode() method fails to compile🎃
D. The equals() method fails to compiler🎃
E. Another line fails to compile🎃
F. A runtime exception is thrown🎃
- While equals method has not override the correct method in the Object class, it still works fine
- It will print success since the ids are equivalent
- A✅✅✅✅
❓What is the result of the following code❓
public class Book {
private int ISBN;
private String title, author;
private int pageCount;
public int hashCode() {
return ISBN;
}
@Override public boolean equals(Object obj) {
if (!(obj instanceof Book)){
return false;
}
Book other = (Book) obj;
return this.ISBN == obj.ISBN;
}
// imagine setters and getters
}A. The code compiles🎃
B. The code does not compile because hashCode() is incorrect🎃
C. The code does not compile because equals() does not override the parent method correctly🎃
D. The code does not compile because equals() tries to refer to a private field🎃
E. The code does not compile because the ClassCastException is not handled or declared🎃
F. The code does not compile for another reason🎃
- A - true
- B - false, it does compile
- A✅✅✅✅
❓What is the result of the following code❓
String s1 = "Canada";
String s2 = new String(s1);
if (s1 == s2) System.out.println("s1 == s2");
if (s1.equals(s2)) System.out.println("s1.equals(s2)");A. There is no output 🎃
B. s1 == s2 🎃
C. s1.equals(s2) 🎃
D. Both B and C🎃
E. The code does not compile🎃
F. The code throws a runtime exception🎃
- s2 is a completely different string so it will not print the first sysout
- s1 and s2 are equal in content
- C✅✅✅✅
❓What is true about the following code❓
public class BaseballTeam {
private String city, mascot;
private int numberOfPlayers;
public boolean equals(Object obj) {
if (!(obj instanceof BaseballTeam))
return false;
BaseballTeam other = (BaseballTeam) obj;
return (city.equals(other.city) && mascot.equals(other.mascot));
}
public int hashCode() {
return numberOfPlayers;
}
// imagine setters and getters
}A. The class does not compile🎃
B. The class compiles but has an improper equals() method🎃
C. The class compiles but has an improper hashCode() method🎃
D. The class compiles but has proper equals() and hashCode() methods🎃
-
The code does compile
-
C - true
-
C✅✅✅✅
-
The equals method can be anything the business demands!
-
It is the hashcode which needs to comply!!!!
❓Which of the following statements are true, assuming a and b are String objects?❓
A. if a.equals(b) is true, a.hashCode() == b.hashCode() is always true 🎃
B. if a.equals(b) is true, a.hashCode() == b.hashCode() is sometimes but not always true🎃
C. if a.equals(b) is false, a.hashCode() == b.hashCode() can never be true🎃
D. if a.equals(b) is false, a.hashCode() == b.hashCode() can sometimes be true🎃
- A - true
- B - false
- C - false, it is not required that two instances with the same hash are different!
- D - true
- A,D✅✅✅✅
❓What is the result of the following code❓
public class FlavorsEnum {
enum Flavors {
VANILLA, CHOCOLATE, STRAWBERRY
}
public static void main() {
System.out.println(Flavors.CHOCOLATE.ordinal());
}
}A. 0 🎃
B. 1 🎃
C. 9 🎃
D. CHOCOLATE 🎃
E. The code does not compile due to a missing semi-colon🎃
F. The code does not compile for a different reason 🎃
- An enum does not require a semi-colon
- B✅✅✅✅
❓What is the result of the following code❓
public class IceCream {
enum Flavors {
VANILLA, CHOCOLATE, STRAWBERRY
}
public static void main() {
Flavors f = Flavors.STRAWBERRY;
switch (f) {
case 0: System.out.println("vanilla");
case 1: System.out.println("chocolate");
case 2: System.out.println("strawberry");
break;
default: System.out.println("missing flavor");
}
}
}A. vanilla 🎃
B. chocolate 🎃
C. strawberry 🎃
D. missing flavor 🎃
E. The code does not compile 🎃
F. An exception is thrown 🎃
- The case statements are wrong, so will not compile
- E✅✅✅✅
❓What is the result of the following code?❓
1: public class Outer {
2: private int x =5;
3: protected class Inner {
4: public static int x = 10;
5: public void go() { System.out.println(x); }
6: }
7: public static void main(String[] args) {
8: Outer out = new Outer();
9: Outer.Inner in = out.new Inner();
10: in.go();
11: }
12: }A. The output is 5. 🎃
B. The output is 10. 🎃
C. Line 4 generates a compiler error. 🎃
D. Line 8 generates a compiler error. 🎃
E. Line 9 generates a compiler error. 🎃
F. An exception is thrown. 🎃
-
I think the output is 5
-
A❌❌❌❌
-
CORRECT ANSWER: C
-
Inner classes can NOT have static variables!!!
❓What is the result of the following code?❓
1: public class Outer {
2: private int x = 24;
3: public int getX() {
4: String message = "x is ";
5: class Inner {
6: private int x = Outer.this.x;
7: public void printX() {
8: System.out.println(message + x);
9: }
10: }
11: Inner in = new Inner();
12: in.printX();
13: return x;
14: }
15: public static void main(String[] args) {
16: new Outer().getX();
17: }}A. x is 10 🎃
B. x is 24 🎃
C. Line 6 generates a compiler error🎃
D. Line 8 generates a compiler error🎃
E. Line 11 generates a compiler error🎃
F. An exception is thrown🎃
- Since the
xin the inner class is referenced usingOuter.this.xit gets assigened to the Outer class's value - So prints
x is 24 - B✅✅✅✅
❓The following code appears in the file named Book.java. What is the result of compiling the source file❓
1: public class Book {
2: private int pageNumber;
3: private class BookReader {
4: public int getPage() {
5: return pageNumber;
6: } } }A. The code compiles successfully, and one bytecode file is generated: `Book.class` 🎃
B. The code compiles successfully, and two bytecode files are generated: `Book.class` and `BookReader.class` 🎃
C. The code compiles successfully, and two bytecode files are generated: `Book.class` and `Book$BookReader.class` 🎃
D. A compiler error occurs on line 3 🎃
E. A compiler error occurs on line 5 🎃
- No compiler error
- Two bytecode is
Book.classandBook$BookReader.class - C✅✅✅✅
❓Which of the following statements can be inserted to make FootballGame compile?❓
package my.sports;
public class Football {
public static final int TEAM_SIZE = 11;
}
package my.apps;
// INSERT CODE HERE
public class FootballGame {
public int getTeamSize() { return TEAM_SIZE; }
}A. import my.sports.Football; 🎃
B. import static my.sports.*; 🎃
C. import static my.sports.Football; 🎃
D. import static my.sports.Football.*; 🎃
E. static import my.sports.*; 🎃
F. static import my.sports.Football; 🎃
G. static import my.sports.Football;
- It needs to statically import the
TEAM_SIZEvariable - A - false
- B - false, this would not even compile
- C - false, as above
- D - true
- E,G - false, wrong syntax
- D✅✅✅✅
❓What is the result of the following code❓
public class Browsers {
static class Browser {
public void go() {
System.out.println("Inside Browser");
}
}
static class Firefox extends Browser {
public void go() {
System.out.println("Inside Firefox");
}
}
static class IE extends Browser {
public void go() {
System.out.println("Inside IE");
}
}
public static void main() {
Browser b = new Firefox();
IE e = (IE) b;
e.go();
}
}A. Inside Browser 🎃
B. Inside Firefox 🎃
C. Inside IE 🎃
D. The code does not compile 🎃
E. A runtime exception is thrown 🎃
-
Does not compile. The firefox instance is not related to the IE class
-
D❌❌❌❌
-
CORRECT ANSWER: E
-
A runtime exception is thrown
-
It does actually compile but the casting throws exception at runtime
❓Which is a true statement about the following code❓
public class IsItFurry {
static interface Mammal { }
static class Furry implements Mammal { }
static class Chipmunk extends Furry { }
public static void main() {
Chipmunk c = new Chipmunk();
Mammal m = c;
Furry f = c;
int result = 0;
if (c instanceof Mammal) result += 1;
if (c instanceof Furry) result += 2;
if (null instanceof Chipmunk) result += 4;
System.out.println(result);
}
}A. The output is 0 🎃
B. The output is 3 🎃
C. The output is 7 🎃
D. c instanceof Mammal does not compile 🎃
E. c instanceof Furry does not compile🎃
F. null instanceof Chipmunk does not compile🎃
cis an instance of Mammal and Furry- The output is 3
- B✅✅✅✅
❓What is a true statement of the following code (choose all that apply)❓
import java.util.*;
public class IsItFurry {
static class Chipmunk { }
public static void main() {
Chipmunk c = new Chipmunk();
ArrayList<Chipmunk> l = new ArrayList<>();
Runnable r = new Thread();
int result = 0;
if (c instanceof Chipmunk) result += 1;
if (l instanceof Chipmunk) result += 2;
if (r instanceof Chipmunk) result += 4;
System.out.println(result);
}
}A. The code compiles, and the output is 0 🎃
B. The code compiles, and the output is 3 🎃
C. The code compiles, and the output is 7 🎃
D. c instanceof Chipmunk does not compile 🎃
E. l instanceof Chipmunk does not compile 🎃
F. r instanceof Chipmunk does not compile 🎃
-
The code will not compile
-
F is true
-
F❌❌❌❌
-
CORRECT ANSWER: E
-
newArrayList<Chipmunk> instance of Chipmunkwill not compile -
ArrayList is a concrete class, the compiler knows that this will never be compatible!!!
-
If it were to be referenced via an interface (like with
r) then it will compile fine!!!!
❓Which statements are true about the equals() method (choose all that apply)❓
A. if equals(null) is called, the method should throw an exception 🎃
B. if equals(null) is called, the method should return false 🎃
C. if equals(null) is called, the method should return true 🎃
D. If equals() is passed the wrong type, the method should throw an exception 🎃
E. If equals() is passed the wrong type, the method should return false 🎃
F. If equals() is passed the wrong type, the method should return true 🎃
-
A - false
-
B - true
-
C - false
-
D - false
-
E - false
-
F - false
-
B❌❌❌❌
-
CORRECT ANSWER: B,E
-
null AND INCORRECT types should return false
❓Which of the following can be inserted in main❓
public class Outer {
class Inner { }
public static void main(String[] args) {
// INSERT CODE HERE
}
}A. Inner in = new Inner(); 🎃
B. Inner in = Outer.new Inner(); 🎃
C. Outer.Inner in = new Outer.Inner(); 🎃
D. Outer.Inner in = new Outer().Inner(); 🎃
E. Outer.Inner in = new Outer().new Inner(); 🎃
F. Outer.Inner in = Outer.new Inner(); 🎃
- A - false, you cannot instantiate inner directly
- B - false as above
- C - false wrong syntax
- D - false wrong syntax
- E - true
- F - false, wrong syntax
- E✅✅✅✅
❓What is the result of the following code (choose all that apply)❓
1: public enum AnimalClasses {
2: MAMMAL(true), FISH(Boolean.FALSE), BIRD(false),
3: REPTILE(false), AMPHIBIAN(false), INVERTEBRATE(false)
4: boolean hasHair;
5: pubic AnimalClasses(boolean hasHair) {
6: this.hasHair = hasHair;
7: }
8: public boolean hasHair() {
9: return hasHair;
10: }
11: public void giveWig() {
12: hasHair = true;
13: }}A. Compiler error on line 2 🎃
B. Compiler error on line 3 🎃
C. Compiler error on line 5 🎃
D. Compiler error on line 8 🎃
E. Compiler error on line 12 🎃
F. Compiler error on another line 🎃
G. The code compiles successfully 🎃
-
There needs to be seperation between the enums and the fields of the class
-
B❌❌❌❌
-
CORRECT ANSWER: B,C
-
Both lines 3 and 5 do NOT compile
-
A semi-colon is needed after the enum values
-
The constructor can NOT have a
publicmodifier, it can either have no modifier or be private only
❓What is the result of the following code (choose all that apply)❓
public class Swimmer {
enum AnimalClasses {
MAMMAL, FISH {
public boolean hasFins() { return true; }},
BIRD, REPTILE, AMPHIBIAN, INVERTEBRATE;
public abstract boolean hasFins();
}
public static void main() {
System.out.println(AnimalClasses.FISH);
System.out.println(AnimalClasses.FISH.ordinal());
System.out.println(AnimalClasses.FISH.hasFins());
System.out.println(AnimalClasses.BIRD.hasFins());
}
}A. fish 🎃
B. FISH 🎃
C. 0 🎃
D. 1 🎃
E. false 🎃
F. true 🎃
G. The code does not compile
- The code does not compile as not all the enums implement the abstract class
- G✅✅✅✅
❓Which of the following can be inserted to override the superclass method (choose all that apply)❓
public class LearnToWalk {
public void toddle() {}
class BabyRhino extends LearnToWalk {
// INSERT CODE HERE
}
}A. `public void toddle() {}` 🎃
B. `public void Toddle() {}` 🎃
C. `public final void toddle() {}` 🎃
D. `public static void toddle() {}` 🎃
E. `public void toddle() throws Exception {}` 🎃
F. `public void toddle(boolean fall)` 🎃
- A - true
- B - false typo
- C - true
- D - false
- E - false, an implementation cannot throw checked exception
- F - false
- A,C✅✅✅✅
❓What is the result of the following code❓
public class FourLegged {
String walk = "walk,";
static class BabyRhino extends FourLegged {
String walk = "toddle,";
}
public static void main() {
FourLegged f = new BabyRhino();
BabyRhino b = new BabyRhino();
System.out.print(f.walk);
System.out.print(b.walk);
}}A. toddle,toddle, 🎃
B. toddle,walk, 🎃
C. walk,toddle, 🎃
D. walk,walk, 🎃
E. The code does not compile. 🎃
F. A runtime exception is thrown 🎃
- Both instances are babyRhinos
- A❌❌❌❌❌
- CORRECT ANSWER: C
- We get
walk,toddle,printed - Instance variables DO NOT get overrided by subclass
- As we are referencing the
FoureLeggedfor the first object, we getwalk,printed - As we are referencing
BabyRhinofor the second object, we get,toddle
❓Which of the following could be inserted to fill in the blank (Choose all that apply)❓
public interface Otter {
default void play() { }
}
class RiverOtter implements Otter {
____________________________
}A. @Override public boolean equals(Object o) { return false; }
B. @Override public boolean equals(Otter o) { return false; }
C. @Override public int hashCode() { return 42; }
D. @Override public long hashCode() { return 42; }
E. @Override public void play() { }
F. @Override void play() { }
- A - true, it correctly overrides the
equals(Object)method in the Object class - B - false
- C - true
- D - false hashCode returns int
- E - true
- F - false, it restricts visibility
- A,C,E✅✅✅✅