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 in Java is a blueprint or template that defines the structure and behavior (data and methods) of objects. It groups related variables and functions together.
Click to reveal answer
beginner
How do you define a simple class named <code>Car</code> in Java?
You define it using the <code>class</code> keyword followed by the class name and curly braces:<br><pre>public class Car {
// fields and methods
}</pre>
Click to reveal answer
beginner
What are fields and methods inside a class?
Fields are variables that hold data about the object. Methods are functions that define actions or behaviors the object can perform.
Click to reveal answer
beginner
What does the keyword <code>public</code> mean when used before a class or method?
<code>public</code> means the class or method can be accessed from other classes anywhere in the program.
Click to reveal answer
beginner
Why do we use classes in programming?
Classes help organize code by grouping related data and actions. They make code reusable, easier to understand, and model real-world things.
Click to reveal answer
Which keyword is used to define a class in Java?
Aobject
Bstruct
Cdefine
Dclass
✗ Incorrect
The keyword class is used to define a class in Java.
What is inside a class in Java?
AOnly methods
BFields and methods
COnly variables
DFunctions only
✗ Incorrect
A class contains fields (variables) and methods (functions) that define data and behavior.
What does public before a class mean?
AClass is private
BClass is static
CClass can be accessed anywhere
DClass is abstract
✗ Incorrect
public means the class can be accessed from any other class.
Which of these is a valid class name in Java?
ACar123
B123Car
Ccar-name
Dclass
✗ Incorrect
Class names must start with a letter or underscore and cannot be Java keywords. Car123 is valid.
Why do programmers use classes?
ATo organize data and behavior together
BTo write code faster
CTo avoid using variables
DTo make programs run slower
✗ Incorrect
Classes organize related data and actions, making code easier to manage and reuse.
Explain what a class is and why it is useful in Java programming.
Think about how a class models real-world things.
You got /4 concepts.
Describe the parts inside a class and their roles.
What does a class hold to represent an object?
You got /4 concepts.
Practice
(1/5)
1. What is a class in Java? class Car { }
easy
A. A blueprint to create objects with data and actions
B. A type of variable that stores numbers
C. A method that runs automatically
D. A special kind of loop
Solution
Step 1: Understand the role of a class
A class defines a template or blueprint for creating objects that hold data and actions.
Step 2: Match the definition to options
A blueprint to create objects with data and actions correctly describes a class as a blueprint for objects.
Final Answer:
A blueprint to create objects with data and actions -> Option A
Quick Check:
Class = blueprint for objects [OK]
Hint: Remember: class = blueprint for objects [OK]
Common Mistakes:
Confusing class with variable
Thinking class is a method
Mixing class with loops
2. Which of the following is the correct way to define a class named Person in Java?
easy
A. Person class { }
B. class = Person { }
C. class Person { }
D. define class Person { }
Solution
Step 1: Recall Java class syntax
In Java, a class is defined using the keyword class followed by the class name and braces.
Step 2: Check each option
class Person { } matches the correct syntax: class Person { }. Others have incorrect order or keywords.
Final Answer:
class Person { } -> Option C
Quick Check:
Correct class syntax = class Name { } [OK]
Hint: Use 'class ClassName { }' to define a class [OK]
Common Mistakes:
Swapping 'class' and class name
Using '=' sign in class definition
Using wrong keywords like 'define'
3. What will be the output of this Java code?
class Dog {
String name = "Buddy";
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println(d.name);
}
}
medium
A. Buddy
B. null
C. Dog
D. Compilation error
Solution
Step 1: Understand object creation and field access
The code creates a Dog object and accesses its field 'name' which is set to "Buddy".
Step 2: Predict the printed output
Printing d.name outputs the string "Buddy" stored in the object.
Final Answer:
Buddy -> Option A
Quick Check:
Object field value = Buddy [OK]
Hint: Access object fields with dot notation: object.field [OK]
Common Mistakes:
Expecting class name instead of field value
Thinking uninitialized fields print 'null'
Confusing syntax causing compile errors
4. Identify the error in this class definition:
class Animal {
String type;
void Animal() {
type = "Mammal";
}
}
medium
A. Class name should be lowercase
B. Constructor has void return type
C. Missing semicolon after type declaration
D. Field 'type' must be static
Solution
Step 1: Check constructor syntax
Constructors in Java must not have a return type, not even void.
Step 2: Identify the error
The method void Animal() is treated as a regular method, not a constructor, causing no constructor defined.
Final Answer:
Constructor has void return type -> Option B
Quick Check:
Constructor = no return type [OK]
Hint: Constructors never have a return type, not even void [OK]
Common Mistakes:
Adding void to constructor
Thinking semicolon needed after field
Believing class names must be lowercase
Assuming fields must be static
5. You want to create a class Book with a field title and a method printTitle() that prints the title. Which code correctly implements this?
hard
A. class Book {
String title;
void printTitle() {
System.out.println("title");
}
}
B. class Book {
String title;
void printTitle() {
print(title);
}
}
C. class Book {
String title;
void printTitle() {
System.out.print("title");
}
}
D. class Book {
String title;
void printTitle() {
System.out.println(title);
}
}
Solution
Step 1: Check method to print field value
Method should use System.out.println with the field variable title to print its value.
Step 2: Evaluate each option
Options printing the literal "title" (with or without newline) are incorrect. Calling undefined print(title) causes an error. Only System.out.println(title) correctly prints the field value.
Final Answer:
class Book {
String title;
void printTitle() {
System.out.println(title);
}
} -> Option D
Quick Check:
Print field with System.out.println(field) [OK]
Hint: Use System.out.println(field) to print variable content [OK]