Bird
Raised Fist0
Javaprogramming~20 mins

Procedural vs OOP approach in Java - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
πŸŽ–οΈ
Master of Procedural and OOP Approaches
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of procedural style code
What is the output of this procedural Java code?
Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int sum = add(a, b);
        System.out.println(sum);
    }

    public static int add(int x, int y) {
        return x + y;
    }
}
A53
B8
CError: method add not found
D0
Attempts:
2 left
πŸ’‘ Hint
Look at how the add method works and what it returns.
❓ Predict Output
intermediate
2:00remaining
Output of OOP style code
What is the output of this Java code using classes and objects?
Java
class Calculator {
    int a, b;
    Calculator(int a, int b) {
        this.a = a;
        this.b = b;
    }
    int add() {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator(5, 3);
        System.out.println(calc.add());
    }
}
A53
BNullPointerException
CCompilation error: missing return type
D8
Attempts:
2 left
πŸ’‘ Hint
Check how the add method uses the object's fields.
🧠 Conceptual
advanced
2:00remaining
Difference in data handling between procedural and OOP
Which statement best describes how data is handled differently in procedural vs OOP approaches?
ABoth procedural and OOP always keep data and functions separate.
BProcedural code bundles data and functions in objects; OOP separates them completely.
CProcedural code separates data and functions; OOP bundles data and functions together in objects.
DOOP does not use functions, only data.
Attempts:
2 left
πŸ’‘ Hint
Think about how classes group data and behavior.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in this OOP code
What error will this Java code produce?
Java
class Calculator {
    int a, b;
    Calculator(int a, int b) {
        a = a;
        b = b;
    }
    int add() {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator(5, 3);
        System.out.println(calc.add());
    }
}
AOutput is 0
BCompilation error: variable a not initialized
CNullPointerException at runtime
DOutput is 8
Attempts:
2 left
πŸ’‘ Hint
Look at the constructor assignments carefully.
πŸš€ Application
expert
3:00remaining
Choosing approach for a banking system
You need to design a banking system that manages accounts, transactions, and customers. Which approach is best and why?
AOOP, because it models real-world entities like accounts and customers as objects with data and behavior.
BOOP, because it does not allow data to be changed once created.
CProcedural, because it avoids the overhead of creating classes and objects.
DProcedural, because it is simpler and faster for managing many data types separately.
Attempts:
2 left
πŸ’‘ Hint
Think about how real-world things map to programming concepts.

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

  1. Step 1: Understand procedural programming basics

    Procedural programming focuses on writing instructions in order to perform tasks.
  2. Step 2: Compare with other approaches

    OOP models real-world things as objects, which is different from procedural step-by-step instructions.
  3. Final Answer:

    It writes step-by-step instructions to perform tasks. -> Option D
  4. 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

  1. Step 1: Identify Java class syntax

    Java classes are defined using the keyword 'class' followed by the class name and curly braces.
  2. Step 2: Check options for correct Java syntax

    class Car { int speed; void drive() { } } uses 'class' keyword and proper Java method and variable syntax.
  3. Final Answer:

    class Car { int speed; void drive() { } } -> Option A
  4. 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

  1. Step 1: Trace variable assignment

    Initially, speed = 0. Then speed = speed + 10 sets speed to 10.
  2. Step 2: Print the value of speed

    System.out.println(speed) prints the current value, which is 10.
  3. Final Answer:

    10 -> Option C
  4. 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

  1. Step 1: Check object initialization

    Dog object 'd' is created but 'name' is never set, so it is null.
  2. 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.
  3. Final Answer:

    Missing constructor to set name -> Option B
  4. 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

  1. Step 1: Analyze program needs

    A library system has entities like books and members with data and behaviors.
  2. Step 2: Choose approach based on modeling

    OOP models real-world entities as objects, making it easier to manage complex data and actions.
  3. Final Answer:

    OOP, because it models books and members as objects with properties and actions -> Option A
  4. Quick Check:

    Complex systems benefit from OOP modeling [OK]
Hint: Use OOP for real-world entities with data and actions [OK]
Common Mistakes:
  • Choosing procedural for complex object management
  • Thinking OOP avoids classes (it uses them)
  • Assuming procedural always uses less memory