What is a constructor in Java?
A constructor initializes the instance variables of an object
What is it called when a class has multiple constructors?
Constructor overloading
What is the return type of a constructor?
Constructors don't have a return type (not even void)
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
True or False: Constructors can call each other.
Yes! (you don't even need to make a new object if you do this())
How do you call a constructor in Java?
Using the new keyword
What allows Java to choose the correct constructor?
The number and type of arguments provided when creating an object
If an int instance variable isn't set in the constructor, what is it's value?
0
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
True or False: Destructors, the opposite of constructors, are a real thing.
Not in Java, but yes in languages like C++
If no constructor is provided, what does Java do?
The compiler generates a no-argument constructor that initializes instance variables to default values
Given BankAccount() and BankAccount(double initialBalance), which constructor is used in new BankAccount(100.0)?
BankAccount(double initialBalance) constructor is used
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
What is wrong with the following:
Dog dog1 = new Dog();
Nothing
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)
What must be the same between a constructor and its class?
The constructor name must be identical to the class name
What is an advantage of having multiple constructors?
It allows objects to be initialized in different ways, providing flexibility
If a String instance variable isn't set in the constructor, what is it's value?
null
What is wrong with new MyClass.MyClass();
Will the following code compile?
class TylerNinjaBlevins {
public boolean hasLowTaperFade;
private TylerNinjaBlevins(boolean lowTaperFade) {
hasLowTaperFade = lowTaperFade;
}
}
Yes (private constructors exist!)
What is one key difference between a constructor and a method?
A constructor has no return type, not even void
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
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
What is wrong with calling register1.CashRegister();
You cannot invoke a constructor on an existing object
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