ITSC 1213 KNOWLEDGE CHECKPOINT 2 REVIEW
QUESTIONS
An online store is working on an online ordering system for Books and Movies. For each
type of Published Material (books and movies) they need to track the id, title, date
published, and price. Which of the following would be the best design? - Answers -
Create the class PublishedMaterial and have Book and Movie inherit from it all the listed
attributes.
Given the following class declarations and code, what is the result when the code is
run?
***
public class Car
{
private int fuel;
public Car() { fuel = 0; }
public Car(int g) { fuel = g; }
public void addFuel() { fuel++; }
public void display() { System.out.print(fuel + " ");
}
}
public class RaceCar extends Car
{
public RaceCar(int g)
{ super(2*g);
}
}
Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display() car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display(); - Answers - The code compiles and runs with no errors, the output is:
5 6 10 11
If you don't specify the parent class in a class declaration which of the following is true?
- Answers - It inherits from the Object class.
Given the following class declarations and initializations in a client program, which of the
following is a correct call to method1?
***
public class Test1
, {
public void method1(Test2 v1, Test3 v2)
{
// rest of method not shown
}
}
public class Test2 extends Test1
{
}
public class Test3 extends Test2
{
}
The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3(); - Answers - t3.method1(t3,t3);
Given the following class definitions which of the following would not compile if it was
used in place of the missing code in the main method?
***
Parent Class
class Item
{
private int x;
public void setX(int theX)
{
x = theX;
}
// ... other methods not shown
}
Child Class
public class EnhancedItem extends Item
{
private int y;
public void setY(int theY)
{
y = theY;
}
// ... other methods not shown
public static void main(String[] args)
{
EnhancedItem currItem = new EnhancedItem();
// missing code
QUESTIONS
An online store is working on an online ordering system for Books and Movies. For each
type of Published Material (books and movies) they need to track the id, title, date
published, and price. Which of the following would be the best design? - Answers -
Create the class PublishedMaterial and have Book and Movie inherit from it all the listed
attributes.
Given the following class declarations and code, what is the result when the code is
run?
***
public class Car
{
private int fuel;
public Car() { fuel = 0; }
public Car(int g) { fuel = g; }
public void addFuel() { fuel++; }
public void display() { System.out.print(fuel + " ");
}
}
public class RaceCar extends Car
{
public RaceCar(int g)
{ super(2*g);
}
}
Car car = new Car(5);
Car fastCar = new RaceCar(5);
car.display() car.addFuel();
car.display();
fastCar.display();
fastCar.addFuel();
fastCar.display(); - Answers - The code compiles and runs with no errors, the output is:
5 6 10 11
If you don't specify the parent class in a class declaration which of the following is true?
- Answers - It inherits from the Object class.
Given the following class declarations and initializations in a client program, which of the
following is a correct call to method1?
***
public class Test1
, {
public void method1(Test2 v1, Test3 v2)
{
// rest of method not shown
}
}
public class Test2 extends Test1
{
}
public class Test3 extends Test2
{
}
The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3(); - Answers - t3.method1(t3,t3);
Given the following class definitions which of the following would not compile if it was
used in place of the missing code in the main method?
***
Parent Class
class Item
{
private int x;
public void setX(int theX)
{
x = theX;
}
// ... other methods not shown
}
Child Class
public class EnhancedItem extends Item
{
private int y;
public void setY(int theY)
{
y = theY;
}
// ... other methods not shown
public static void main(String[] args)
{
EnhancedItem currItem = new EnhancedItem();
// missing code