0
0
Javaprogramming~20 mins

Procedural vs OOP approach in Java - Practice Questions

Choose your learning style9 modes available
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.