Class Design

Practice Questions

Computer Science › Class Design

Page 1 of 5
10 of 44
1

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail?
  • Is the dog purebred?
  • A list of strings of definitive features for the dog
2

Design a shell for a class named Dog that has 3 attributes:

  • Does the dog have a tail?
  • Is the dog purebred?
  • A list of strings of definitive features for the dog
3

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

4

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

5

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

6

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

7

What line of code is needed to import in order to use Color in the following format Color.Blue, Color.RED, Color.White?

8

What line of code is needed to import in order to use Color in the following format Color.Blue, Color.RED, Color.White?

9

USING STATIC VARIABLES

CONSIDER THE FOLLOWING JAVA CODE:

**public class Employee**

**{**

static int nextID = 1;

String name;

int employeeID;

``

Employee(String n){

name = n;

employeeID = nextID;

nextID++;

}

public String getName(){

return name;

}

public int getID(){

return employeeID;

}

public static void main(String[] args)
{

Employee emp3 = new Employee("Kate");

Employee emp1 = new Employee("John");

Employee emp2 = new Employee("Sandy");

System.out.println(emp1.getName() + " ID: " + emp1.getID());

System.out.println(emp3.getName() + " ID: " + emp3.getID());

}

**}**

WHAT WOULD BE THE CONSOLE OUTPUT?

10

USING STATIC VARIABLES

CONSIDER THE FOLLOWING JAVA CODE:

**public class Employee**

**{**

static int nextID = 1;

String name;

int employeeID;

``

Employee(String n){

name = n;

employeeID = nextID;

nextID++;

}

public String getName(){

return name;

}

public int getID(){

return employeeID;

}

public static void main(String[] args)
{

Employee emp3 = new Employee("Kate");

Employee emp1 = new Employee("John");

Employee emp2 = new Employee("Sandy");

System.out.println(emp1.getName() + " ID: " + emp1.getID());

System.out.println(emp3.getName() + " ID: " + emp3.getID());

}

**}**

WHAT WOULD BE THE CONSOLE OUTPUT?

Page 1 of 5
Return to subject