Constructor Basics
Multiple Constructors
Constructor Rules
Common Errors
:)
100

What is a constructor in Java?

A constructor initializes the instance variables of an object

100

What is it called when a class has multiple constructors?

Constructor overloading

100

What is the return type of a constructor?

Constructors don't have a return type (not even void)

100

What is wrong with the following constructor for a class named Coins:

    public CoinsStartValue(int initialAmount)

    {

      amount = initialAmount;

    }

The name of the constructor does not match the class

100

True or False: Constructors can call each other.

Yes! (you don't even need to make a new object if you do this())

200

How do you call a constructor in Java?

Using the new keyword

200

What allows Java to choose the correct constructor?

The number and type of arguments provided when creating an object

200

If an int instance variable isn't set in the constructor, what is it's value?

0

200

What is wrong with the following constructor for a class named Coins:

    public void Coins(int initialAmount)

    {

      amount = initialAmount;

    }

Void should not be used to declare a constructor

200

True or False: Destructors, the opposite of constructors, are a real thing.

Not in Java, but yes in languages like C++

300

If no constructor is provided, what does Java do?

The compiler generates a no-argument constructor that initializes instance variables to default values

300

Given BankAccount() and BankAccount(double initialBalance), which constructor is used in new BankAccount(100.0)?

BankAccount(double initialBalance) constructor is used

300

What happens if you try to invoke a constructor on an existing object?

It results in an error; constructors are only called when an object is created

300

What is wrong with the following:

Dog dog1 = new Dog();

Nothing 

300

Consider the following code:

class RedNote {

     public RedNote() {}

}

class TikTok extends RedNote {

     public TikTok() {}

}

If TikTok spyware = new TikTok() is run, which class's constructor is called?

Both (first RedNote then TikTok)

400

What must be the same between a constructor and its class?

The constructor name must be identical to the class name

400

What is an advantage of having multiple constructors?

It allows objects to be initialized in different ways, providing flexibility

400

If a String instance variable isn't set in the constructor, what is it's value?

null

400

What is wrong with new MyClass.MyClass();  

The syntax is wrong for a constructor. It should be new MyClass();
400

Will the following code compile?

class TylerNinjaBlevins {

     public boolean hasLowTaperFade;

     private TylerNinjaBlevins(boolean lowTaperFade) {

          hasLowTaperFade = lowTaperFade;

     }

}

Yes (private constructors exist!)

500

What is one key difference between a constructor and a method?

A constructor has no return type, not even void

500

Which of the following constructors will this line of code use: Car car1 = new Car();

public Car(int speed);

public Car(String model);

public void Car();

None of them 

500

What happens if you define a constructor with parameters in a class but do not define a no-argument constructor?

The class will still compile, but you will be unable to instantiate it without arguments

500

What is wrong with calling register1.CashRegister();

You cannot invoke a constructor on an existing object

500

Consider the following code:

class CollegeBoard extends NonProfit {

     public double profitInMillions;

     public Item(double profitInMillions) {

          profitInMillions = profitInMillions;

     }

}

What is the value of profitInMillions after running CollegeBoard cb = new CollegeBoard(5.00 + 1 / 2.00)?

0.0

M
e
n
u