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 class in Java?
A class is a blueprint or template that defines the structure and behavior (fields and methods) that objects created from the class will have.
Click to reveal answer
beginner
What is an object in Java?
An object is an instance of a class. It represents a real-world entity with state (data) and behavior (methods) defined by its class.
Click to reveal answer
beginner
How do you create an object from a class in Java?
You use the 'new' keyword followed by the class constructor. For example: <br> <code>ClassName obj = new ClassName();</code>
Click to reveal answer
intermediate
What is the purpose of a constructor in a class?
A constructor is a special method used to initialize new objects. It has the same name as the class and no return type.
Click to reveal answer
beginner
Explain the difference between fields and methods in a class.
Fields are variables that hold data about the object. Methods are functions that define the behavior or actions the object can perform.
Click to reveal answer
Which keyword is used to create an object in Java?
Anew
Bclass
Cobject
Dcreate
✗ Incorrect
The 'new' keyword is used to create a new object instance from a class.
What does a constructor do in a Java class?
ADeletes objects
BDefines the class name
CInitializes new objects
DCalls methods
✗ Incorrect
Constructors initialize new objects when they are created.
Which of the following is NOT a part of a class?
AObjects
BMethods
CFields
DConstructors
✗ Incorrect
Objects are instances created from classes, not part of the class itself.
How do you call a method named 'run' on an object 'car'?
Arun.car();
Bcar.run();
Ccar->run();
Drun(car);
✗ Incorrect
You call a method on an object using dot notation: objectName.methodName();
What is the correct way to declare a class named 'Dog'?
Aclass = Dog {}
BDog class {}
Cnew class Dog {}
Dclass Dog {}
✗ Incorrect
The correct syntax is: class ClassName {}
Describe what a class and an object are in Java and how they relate to each other.
Think about how a cookie cutter (class) relates to cookies (objects).
You got /3 concepts.
Explain the role of constructors in creating objects and how they differ from regular methods.
Constructors prepare new objects when they are born.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a class in Java?
easy
A. To serve as a blueprint for creating objects
B. To execute the program's main method
C. To store data permanently on disk
D. To perform input and output operations
Solution
Step 1: Understand the role of a class
A class defines the structure and behavior that objects created from it will have.
Step 2: Identify the correct purpose
Classes are not for running programs or storing data on disk; they are blueprints for objects.
Final Answer:
To serve as a blueprint for creating objects -> Option A
Quick Check:
Class = blueprint for objects [OK]
Hint: Classes define objects' structure and behavior [OK]
Common Mistakes:
Confusing classes with methods
Thinking classes store data permanently
Believing classes execute the program
2. Which of the following is the correct way to create an object of class Car in Java?
easy
A. Car myCar = new Car;
B. Car myCar = Car();
C. Car myCar = new Car();
D. new Car myCar = Car();
Solution
Step 1: Recall object creation syntax
In Java, objects are created using the new keyword followed by the class constructor with parentheses.
Step 2: Check each option
Car myCar = new Car(); uses correct syntax: Car myCar = new Car();. Others miss parentheses or have wrong order.
Final Answer:
Car myCar = new Car(); -> Option C
Quick Check:
Use new ClassName() to create objects [OK]
Hint: Use 'new ClassName()' to create objects [OK]
Common Mistakes:
Omitting parentheses after class name
Placing 'new' keyword incorrectly
Missing semicolon at the end
3. What will be the output of the following code?
class Dog {
String name;
void bark() {
System.out.println(name + " says Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.name = "Buddy";
dog1.bark();
}
}
medium
A. null says Woof!
B. Woof!
C. Compilation error
D. Buddy says Woof!
Solution
Step 1: Understand object property assignment
The object dog1 has its name set to "Buddy" before calling bark().
Step 2: Analyze the bark method output
The method prints the name followed by " says Woof!" so it prints "Buddy says Woof!".
Final Answer:
Buddy says Woof! -> Option D
Quick Check:
Object property used in method = Buddy says Woof! [OK]
Hint: Check object fields before method calls for output [OK]
Common Mistakes:
Assuming default null value prints
Forgetting to assign the name
Thinking method prints only 'Woof!'
4. Identify the error in the following code snippet:
class Person {
String name;
void setName(String name) {
name = name;
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alice");
System.out.println(p.name);
}
}
medium
A. Missing semicolon after setName method
B. The method setName does not assign the parameter to the instance variable
C. Cannot print p.name directly
D. Class Person should be public
Solution
Step 1: Analyze the setName method
The assignment name = name; assigns the parameter to itself, not to the instance variable.
Step 2: Understand instance variable shadowing
To assign correctly, use this.name = name; to refer to the instance variable.
Final Answer:
The method setName does not assign the parameter to the instance variable -> Option B
Quick Check:
Use 'this' to assign instance variables [OK]
Hint: Use 'this.variable' to refer to instance variables [OK]
Common Mistakes:
Confusing parameter and instance variable names
Forgetting 'this' keyword
Assuming assignment works without 'this'
5. Given the class below, how can you create a method that returns a new Rectangle object with double the width and height of the current one?
class Rectangle {
int width;
int height;
Rectangle(int w, int h) {
width = w;
height = h;
}
// Your method here
}
hard
A. Rectangle doubleSize() { return new Rectangle(width * 2, height * 2); }