18.6k views
5 votes
Given the piece of code shown below, in which line(s) is (are) an object(s) created?

1 class Vehicle {
2 int passengers;
3 int fuelcap;
4 int mpg;
5 }
6 class VehicleDemo {
7 public static void main(String args[]) {
8 Vehicle minivan = new Vehicle();
9 Vehicle truck = new Vehicle();
10 int range;"

a) Line 8
b) Line 9
c) Line 10
d) Objects are created in lines 8 and 9

User Typhon
by
7.8k points

1 Answer

4 votes

Final answer:

Objects are created in lines 8 and 9 of the given code, where the 'new' keyword is used to initialize new instances of the 'Vehicle' class. The correct multiple-choice option is (D).

Step-by-step explanation:

The object creation in Java happens when the new keyword is used. This keyword is followed by a call to the constructor of the class, which initializes the new object. Looking at the given code:

  • Line 8: Vehicle minivan = new Vehicle();
  • Line 9: Vehicle truck = new Vehicle();

Both lines 8 and 9 use the new keyword followed by the class name and parentheses, indicating the use of the constructor. There are no objects being created in line 10, as it merely declares a variable of primitive type int without using new. The correct multiple-choice option is (D).

User Marcshilling
by
8.7k points

No related questions found