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
intermediateOutput 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; } }
Attempts:
2 left
π‘ Hint
Look at how the add method works and what it returns.
β Incorrect
The add method takes two integers and returns their sum. So 5 + 3 = 8.
β Predict Output
intermediateOutput 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()); } }
Attempts:
2 left
π‘ Hint
Check how the add method uses the object's fields.
β Incorrect
The Calculator object stores 5 and 3, and add() returns their sum, 8.
π§ Conceptual
advancedDifference in data handling between procedural and OOP
Which statement best describes how data is handled differently in procedural vs OOP approaches?
Attempts:
2 left
π‘ Hint
Think about how classes group data and behavior.
β Incorrect
Procedural programming treats data and functions separately, while OOP groups them inside objects.
π§ Debug
advancedIdentify 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()); } }
Attempts:
2 left
π‘ Hint
Look at the constructor assignments carefully.
β Incorrect
The constructor assigns parameters to themselves, not to the fields. So fields a and b remain 0, sum is 0.
π Application
expertChoosing approach for a banking system
You need to design a banking system that manages accounts, transactions, and customers. Which approach is best and why?
Attempts:
2 left
π‘ Hint
Think about how real-world things map to programming concepts.
β Incorrect
OOP is best for complex systems with entities that have data and behaviors, like accounts and customers.
