Object-Oriented Development

Help Questions

AP Computer Science A › Object-Oriented Development

Questions 1 - 6
1

Consider the following C++ pseudocode:

Class Car {

const int wheels = 4;

float milesPerGallon;

string make;

string model;

}

Car sportscar = new Car;

What is the difference between the class car, and the object sportscar?

sportscar is instantiated from Car.

They are the same.

Car is instantiated from sportscar.

They have no relation.

They are both instantiated from something else.

Explanation

When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.

The class Car is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar is instantiated from Car."

2

Consider the following C++ pseudocode:

Class Car {

const int wheels = 4;

float milesPerGallon;

string make;

string model;

}

Car sportscar = new Car;

What is the difference between the class car, and the object sportscar?

sportscar is instantiated from Car.

They are the same.

Car is instantiated from sportscar.

They have no relation.

They are both instantiated from something else.

Explanation

When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.

The class Car is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar is instantiated from Car."

3

Consider the following C++ pseudocode:

Class Car {

const int wheels = 4;

float milesPerGallon;

string make;

string model;

}

Car sportscar = new Car;

What is the difference between the class car, and the object sportscar?

sportscar is instantiated from Car.

They are the same.

Car is instantiated from sportscar.

They have no relation.

They are both instantiated from something else.

Explanation

When programming within the Object-Oriented paradigm, think of the class as a blueprint, and the object as a house built from that blueprint.

The class Car is a abstract specification that does not refer to any one particular instance. It is merely a protocol that all objects wishing to be cars should follow. The sportscar object is a realization of the car blueprint. It is a specific instance. In programming jargon, "sportscar is instantiated from Car."

4

JAVA EXPLICIT TYPE CASTING

Consider the following JAVA code:

**public static void main(String[] args)**

**{**

**double number1 = 99.05;**

**int number2 = (int) number1;**

**System.out.println("number1 = " + number1);**

****System.out.println("number2 = " + number2);**

**}**

What would be the console output?

number1 = 99.05

number2 = 99

number1 = 99.05

number2 = 99.05

number1 = 99

number2 = 99

number1 = 99.05

number2 = 99.00

Explanation

Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.

5

JAVA EXPLICIT TYPE CASTING

Consider the following JAVA code:

**public static void main(String[] args)**

**{**

**double number1 = 99.05;**

**int number2 = (int) number1;**

**System.out.println("number1 = " + number1);**

****System.out.println("number2 = " + number2);**

**}**

What would be the console output?

number1 = 99.05

number2 = 99

number1 = 99.05

number2 = 99.05

number1 = 99

number2 = 99

number1 = 99.05

number2 = 99.00

Explanation

Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.

6

JAVA EXPLICIT TYPE CASTING

Consider the following JAVA code:

**public static void main(String[] args)**

**{**

**double number1 = 99.05;**

**int number2 = (int) number1;**

**System.out.println("number1 = " + number1);**

****System.out.println("number2 = " + number2);**

**}**

What would be the console output?

number1 = 99.05

number2 = 99

number1 = 99.05

number2 = 99.05

number1 = 99

number2 = 99

number1 = 99.05

number2 = 99.00

Explanation

Type casting deals with assigning the value of a variable to another variable that is of a different type. In this case we have two variables one that is a double, and another that is an integer. number1 is a double that has a value of 99.05. However, when number2 is assigned the value of number1, there is some explicit type casting going on. When an integer is assigned the value of a double, it drops off the decimal places. This means that number2 has a value of 99.

Return to subject