Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a parameterized constructor in Java?
A parameterized constructor is a special method in a class that takes parameters to initialize an object with specific values when it is created.
Click to reveal answer
beginner
How does a parameterized constructor differ from a default constructor?
A default constructor has no parameters and initializes objects with default values, while a parameterized constructor requires arguments to set custom initial values.
Click to reveal answer
beginner
Why use a parameterized constructor?
It allows creating objects with different initial states easily by passing different values when creating each object.
Click to reveal answer
beginner
Example of a parameterized constructor in Java:
class Car {
String model;
int year;
Car(String m, int y) {
model = m;
year = y;
}
}
// Creates a Car object with model and year set
Click to reveal answer
intermediate
What happens if you define a parameterized constructor but no default constructor?
Java does not create a default constructor automatically, so you must define one if you want to create objects without parameters.
Click to reveal answer
What is the main purpose of a parameterized constructor?
ATo create multiple objects at once
BTo initialize an object with specific values
CTo delete an object
DTo convert an object to a string
✗ Incorrect
A parameterized constructor initializes an object with values passed as parameters.
What happens if you do not define any constructor in a Java class?
AThe program will not compile
BThe class cannot be instantiated
CYou must always define a constructor
DJava provides a default constructor automatically
✗ Incorrect
If no constructor is defined, Java automatically provides a default constructor with no parameters.
Which of these is a valid parameterized constructor signature?
ACar(String model, int year)
Bvoid Car()
CCar()
Dint Car(String model)
✗ Incorrect
A constructor has the same name as the class and no return type.
If a class has only a parameterized constructor, what happens when you try to create an object without arguments?
ACompilation error
BObject is created with default values
CObject is created with null values
DRuntime exception
✗ Incorrect
Without a default constructor, creating an object without arguments causes a compilation error.
How do you call a parameterized constructor when creating an object?
AClassName()
BClassName.new()
Cnew ClassName(arguments)
Dnew ClassName()
✗ Incorrect
You call a parameterized constructor by passing arguments inside parentheses after new.
Explain what a parameterized constructor is and why it is useful.
Think about how you give specific details when creating something new.
You got /3 concepts.
Write a simple Java class with a parameterized constructor and explain how to create an object using it.
Start with a class name and add a constructor that takes inputs.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of a parameterized constructor in Java?
easy
A. To initialize an object with specific values when it is created
B. To create multiple objects without any initial values
C. To delete an object from memory
D. To define a method that returns a value
Solution
Step 1: Understand constructor purpose
A constructor is used to initialize objects when they are created.
Step 2: Identify parameterized constructor role
A parameterized constructor takes arguments to set initial values for the object's fields.
Final Answer:
To initialize an object with specific values when it is created -> Option A
Quick Check:
Parameterized constructor = initialize with values [OK]
Hint: Constructors with parameters set initial object values [OK]
Common Mistakes:
Confusing constructors with methods
Thinking constructors delete objects
Believing constructors don't take parameters
2.
Which of the following is the correct syntax for a parameterized constructor in Java?
public class Car {
String model;
int year;
// Constructor here
}
easy
A. void Car(String model, int year) { this.model = model; this.year = year; }
B. public Car(String model, int year) { this.model = model; this.year = year; }
C. public Car() { model = ""; year = 0; }
D. public void Car(String model, int year) { model = model; year = year; }
Solution
Step 1: Check constructor syntax
A constructor has no return type and matches the class name exactly.
Step 2: Verify parameter usage
public Car(String model, int year) { this.model = model; this.year = year; } correctly uses parameters and assigns them to fields with this.
Final Answer:
public Car(String model, int year) { this.model = model; this.year = year; } -> Option B
Quick Check:
Constructor syntax = no return type + class name [OK]
Hint: Constructor name = class name, no return type [OK]
Common Mistakes:
Adding return type to constructor
Not using 'this' to assign fields
Using void or other return types
3.
What will be the output of the following Java code?
class Book {
String title;
int pages;
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
void display() {
System.out.println(title + ": " + pages + " pages");
}
}
public class Main {
public static void main(String[] args) {
Book b = new Book("Java Basics", 250);
b.display();
}
}
medium
A. Java Basics: 250 pages
B. Java Basics pages
C. 250 pages
D. Compilation error
Solution
Step 1: Understand constructor initialization
The constructor sets title to "Java Basics" and pages to 250.
Step 2: Analyze display method output
The display() method prints the title, colon, pages, and "pages" text.
Final Answer:
Java Basics: 250 pages -> Option A
Quick Check:
Constructor sets fields, display prints them [OK]
Hint: Constructor sets fields, display prints combined string [OK]
Common Mistakes:
Ignoring 'this' keyword effect
Expecting only title or pages printed
Assuming syntax error without reason
4.
Identify the error in this Java class with a parameterized constructor:
public class Student {
String name;
int age;
public Student(String n, int a) {
name = n;
age = a;
}
public Student() {
name = "Unknown";
age = 0;
}
public void Student(String name, int age) {
this.name = name;
this.age = age;
}
}
medium
A. No default constructor defined
B. Missing return type in parameterized constructor
C. The method named Student with void return type is not a constructor
D. Fields name and age are not declared
Solution
Step 1: Check constructor definitions
Constructors have no return type and match class name exactly.
Step 2: Identify method named Student with void
The method public void Student(String name, int age) is not a constructor but a method, which is confusing.
Final Answer:
The method named Student with void return type is not a constructor -> Option C
Quick Check:
Constructors have no return type [OK]
Hint: Constructors never have a return type, not even void [OK]
Common Mistakes:
Writing constructors with void return type
Confusing methods with constructors
Missing default constructor when needed
5.
Given the class below, what will be the output when creating two Employee objects with different parameters and printing their details?
class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void printInfo() {
System.out.println(name + " earns $" + salary);
}
}
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("Alice", 50000);
Employee e2 = new Employee("Bob", 60000);
e1.printInfo();
e2.printInfo();
}
}
hard
A. Runtime error due to uninitialized fields
B. Alice earns $60000.0
Bob earns $50000.0
C. Compilation error due to missing default constructor
D. Alice earns $50000.0
Bob earns $60000.0
Solution
Step 1: Analyze object creation with parameters
Two Employee objects are created with names and salaries passed to the parameterized constructor.
Step 2: Check printInfo output
Each object's printInfo() prints the name and salary correctly.
Final Answer:
Alice earns $50000.0
Bob earns $60000.0 -> Option D
Quick Check:
Parameterized constructor sets fields, printInfo shows them [OK]
Hint: Each object holds its own values set by constructor [OK]