Given the following code:
Animal a = new Dog();
if (a instanceof Dog) {
System.out.println("Dog");
} else {
System.out.println("Not Dog");
}
What is printed, assuming Dog extends Animal?
Loading test…
12 questions
Given the following code:
Animal a = new Dog();
if (a instanceof Dog) {
System.out.println("Dog");
} else {
System.out.println("Not Dog");
}
What is printed, assuming Dog extends Animal?
A class declares public String toString(). Which statement is true about this method?
Consider the following class hierarchy:
public class Vehicle {
private String brand;
public Vehicle(String brand) { this.brand = brand; }
public String getBrand() { return brand; }
}
public class Car extends Vehicle {
private int doors;
public Car(String brand, int doors) {
super(brand);
this.doors = doors;
}
}
Which statement about Car's constructor is true?
Given the following classes:
public class Animal {
public String speak() { return "..."; }
}
public class Dog extends Animal {
public String speak() { return "Woof"; }
}
What does the following print?
Animal a = new Dog();
System.out.println(a.speak());
A programmer writes:
public class Printer {
public void print(String s) { System.out.println(s); }
public void print(int n) { System.out.println(n * 2); }
}
Printer p = new Printer();
p.print(5);
What is the output, and what concept does this demonstrate?
Which of the following correctly demonstrates the use of equals() from the Object class, and when would you override it?
public class Point {
int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
Point p1 = new Point(2, 3);
Point p2 = new Point(2, 3);
System.out.println(p1.equals(p2));
What is the output of the following code?
public class Shape {
public String describe() { return "Shape"; }
}
public class Circle extends Shape {
public String describe() { return super.describe() + "-Circle"; }
}
// In main:
Circle c = new Circle();
System.out.println(c.describe());
Which of the following best describes the difference between method overriding and method overloading?
Which keyword is used to indicate that one class inherits from another in Java?
An abstract class Figure is defined as:
public abstract class Figure {
public abstract double area();
public void printArea() {
System.out.println("Area: " + area());
}
}
Which of the following statements is true?
Consider the following hierarchy:
public class Vehicle { public void move() { System.out.println("Vehicle moves"); } }
public class Boat extends Vehicle { public void move() { System.out.println("Boat sails"); } }
public class AmphibiousVehicle extends Boat { }
What is printed by:
Vehicle v = new AmphibiousVehicle();
v.move();
What is the result of the following code?
public class A {
public int value() { return 1; }
}
public class B extends A {
public int value() { return 2; }
}
public class C extends B {
// no override
}
// In main:
A obj = new C();
System.out.println(obj.value());