0
0
Javaprogramming~5 mins

Default constructor in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a default constructor in Java?
A default constructor is a constructor that Java provides automatically if no other constructors are defined. It has no parameters and initializes objects with default values.
Click to reveal answer
intermediate
What happens if you define a constructor with parameters but no default constructor in your class?
Java will NOT provide a default constructor automatically. You must define the default constructor yourself if you want one.
Click to reveal answer
beginner
How does Java initialize fields when using the default constructor?
Java sets numeric fields to 0, boolean fields to false, and object references to null by default.
Click to reveal answer
beginner
Code snippet: <br>public class Car {<br>  String model;<br>}<br>What constructor does this class have by default?
This class has a default constructor with no parameters that Java provides automatically. It sets model to null by default.
Click to reveal answer
intermediate
Why might you want to explicitly write a default constructor?
To add custom initialization code or to ensure the constructor exists when other constructors are defined.
Click to reveal answer
What does Java provide if you do NOT write any constructor in your class?
AAn error during compilation
BNo constructor at all
CA constructor with parameters
DA default constructor with no parameters
If you write a constructor with parameters, does Java still provide a default constructor?
AYes, always
BNo, you must write it yourself
COnly if you use the 'default' keyword
DOnly if the class is abstract
What value does a boolean field have when initialized by the default constructor?
Afalse
B1
Cnull
Dtrue
Which of these is true about a default constructor?
AIt is provided only if no constructors exist
BIt must have parameters
CIt throws an exception by default
DIt cannot be overridden
Why might you write your own default constructor?
ATo prevent object creation
BTo make the class abstract
CTo add custom setup code during object creation
DTo delete all fields
Explain what a default constructor is and when Java provides it.
Think about what happens if you don't write any constructor.
You got /3 concepts.
    Describe why you might need to write your own default constructor in a Java class.
    Consider what happens if you add a constructor with parameters.
    You got /3 concepts.