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
Procedural vs OOP approach
π Scenario: You are creating a simple program to manage information about books in a library. First, you will write the program using a procedural approach. Then, you will rewrite it using an object-oriented approach to see the difference.
π― Goal: Build two versions of a program that stores book titles and authors. The first version uses simple variables and arrays (procedural). The second version uses a class to represent each book (OOP). You will see how OOP organizes data and behavior better.
π What You'll Learn
Create an array of book titles and an array of authors using procedural style
Create a variable to count the number of books
Use a for loop to print each book title with its author in procedural style
Create a Book class with title and author fields
Create an array of Book objects
Use a for loop to print each Book object's title and author
π‘ Why This Matters
π Real World
Managing collections of items like books, movies, or products is common in software. Procedural code works for small tasks, but OOP helps organize complex data better.
πΌ Career
Understanding the difference between procedural and object-oriented programming is essential for software development jobs. OOP is widely used in Java and many other languages.
Progress0 / 4 steps
1
Create book data using procedural style
Create a String[] titles array with these exact values: "The Hobbit", "1984", "Pride and Prejudice". Create a String[] authors array with these exact values: "J.R.R. Tolkien", "George Orwell", "Jane Austen".
Java
Hint
Use String[] to create arrays and list the exact book titles and authors inside curly braces.
2
Add a variable to count books
Create an int variable called count and set it to the length of the titles array.
Java
Hint
Use titles.length to get the number of books.
3
Print books using procedural style
Use a for loop with int i = 0; i < count; i++ to print each book title and author in the format: Title: [title], Author: [author].
Java
Hint
Use a for loop to go through each index and print the title and author together.
4
Create Book class and print books using OOP
Create a Book class with String title and String author fields. Create a Book[] books array with three Book objects using the same titles and authors. Use a for loop with int i = 0; i < books.length; i++ to print each book's title and author in the format: Title: [title], Author: [author].
Java
Hint
Define a class with fields and a constructor. Then create an array of objects and loop through it to print details.
Practice
(1/5)
1. Which statement best describes the procedural programming approach in Java?
easy
A. It focuses on graphical user interfaces.
B. It models real-world things as objects with data and actions.
C. It uses inheritance and polymorphism only.
D. It writes step-by-step instructions to perform tasks.
Solution
Step 1: Understand procedural programming basics
Procedural programming focuses on writing instructions in order to perform tasks.
Step 2: Compare with other approaches
OOP models real-world things as objects, which is different from procedural step-by-step instructions.
Final Answer:
It writes step-by-step instructions to perform tasks. -> Option D
Quick Check:
Procedural = step-by-step instructions [OK]
Hint: Procedural = step-by-step instructions, not objects [OK]
Common Mistakes:
Confusing procedural with object-oriented concepts
Thinking procedural uses objects
Assuming procedural focuses on GUIs
2. Which of the following is the correct way to define a class in Java using OOP?
easy
A. class Car { int speed; void drive() { } }
B. procedure Car { speed = 0; drive() }
C. function Car() { speed = 0; drive() }
D. object Car = { speed: 0, drive: function() {} }
Solution
Step 1: Identify Java class syntax
Java classes are defined using the keyword 'class' followed by the class name and curly braces.
Step 2: Check options for correct Java syntax
class Car { int speed; void drive() { } } uses 'class' keyword and proper Java method and variable syntax.
Final Answer:
class Car { int speed; void drive() { } } -> Option A
Quick Check:
Java class syntax uses 'class' keyword [OK]
Hint: Java classes start with 'class' keyword and curly braces [OK]
Common Mistakes:
Using 'procedure' or 'function' keywords which are not Java syntax
Using object literal syntax like JavaScript
Missing curly braces or semicolons
3. What will be the output of this Java code using procedural style?
int speed = 0;
speed = speed + 10;
System.out.println(speed);
medium
A. speed
B. 0
C. 10
D. Compilation error
Solution
Step 1: Trace variable assignment
Initially, speed = 0. Then speed = speed + 10 sets speed to 10.
Step 2: Print the value of speed
System.out.println(speed) prints the current value, which is 10.
Final Answer:
10 -> Option C
Quick Check:
speed updated to 10, printed 10 [OK]
Hint: Follow variable changes step-by-step to find output [OK]
Common Mistakes:
Thinking output is variable name instead of value
Assuming initial value prints without update
Confusing syntax causing errors
4. Identify the error in this OOP Java code snippet:
class Dog {
String name;
void bark() {
System.out.println(name + " barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.bark();
}
}
medium
A. Cannot call method bark() without static keyword
B. Missing constructor to set name
C. Variable name is not declared
D. No error, code runs fine
Solution
Step 1: Check object initialization
Dog object 'd' is created but 'name' is never set, so it is null.
Step 2: Understand effect of missing constructor
Without setting 'name', bark() prints 'null barks', which may be unintended. Adding a constructor to set 'name' fixes this.
Final Answer:
Missing constructor to set name -> Option B
Quick Check:
Object fields need initialization [OK]
Hint: Uninitialized fields cause null or default values [OK]
Common Mistakes:
Thinking bark() must be static
Assuming variable 'name' is undeclared
Ignoring that code compiles but may print null
5. You want to create a program to manage a library system with books and members. Which approach is best and why?
hard
A. OOP, because it models books and members as objects with properties and actions
B. Procedural, because it uses less memory
C. Procedural, because it is simpler for large systems
D. OOP, because it avoids using classes
Solution
Step 1: Analyze program needs
A library system has entities like books and members with data and behaviors.
Step 2: Choose approach based on modeling
OOP models real-world entities as objects, making it easier to manage complex data and actions.
Final Answer:
OOP, because it models books and members as objects with properties and actions -> Option A
Quick Check:
Complex systems benefit from OOP modeling [OK]
Hint: Use OOP for real-world entities with data and actions [OK]