Procedural vs Object-Oriented Programming - Quiz
Total: 6 questions
1. What is procedural programming?
What is procedural programming?
Procedural programming is a paradigm in which a program is described as a sequence of instructions executed from top to bottom, with the order changed by branching (if, switch) and loops (for, while). Repeated fragments are extracted into subprograms — called procedures, functions or, in Java, methods. Data lives separately from the code that uses it: variables on one side, methods that take them as parameters and return results on the other. Combine that with the rule "sequence, selection and iteration only, no goto" and you get structured programming.
2. What is object-oriented programming and how does it differ from procedural programming?
What is object-oriented programming and how does it differ from procedural programming?
Object-oriented programming is a paradigm built around two ideas: the object and the class. Before writing instructions you identify the entities involved in the problem and describe them with classes: a class defines the characteristics of an object (its fields) and its behaviour (its methods). An object is a concrete instance created from a class with new.
The key difference from procedural code: in OOP the data and the code that operates on it end up in the same place, and access to the data is hidden by encapsulation (private fields plus a public API). In procedural code data and methods are separate, so any change to the shape of the data forces you to edit every method that touches it, whereas in OOP such a change usually stays local to one class.
3. Is Java procedural or object-oriented?
Is Java procedural or object-oriented?
Java is an object-oriented programming language, not a procedural one: every method must be declared inside a class, and the standard library is built from classes and interfaces. Java still lets you write procedural code — statements inside a static void main method run top to bottom just like a C program, with the class acting as a mere container.
Java is not fully object-oriented either: primitive types (int, double, boolean) are not objects and static members are called without an instance. Which paradigm you actually use is decided by your design, not by the compiler.
4. Which languages are called procedural, and why is the procedural/object-oriented split a simplification?
Which languages are called procedural, and why is the procedural/object-oriented split a simplification?
A procedural language is one whose basic building block is the procedure (function) rather than the object. The classic examples are C (systems programming, the Linux kernel), Pascal (designed to teach structured programming), Fortran (scientific computation), COBOL (banking and accounting systems still in production), plus BASIC, Ada and Go — Go attaches methods to types but has no classes and no inheritance.
The split is a simplification because most modern languages are multi-paradigm: C++, Python, JavaScript and PHP support both styles. Java sits closest to the object-oriented end of the scale, yet writing procedural code in it takes no effort at all.
5. When is procedural code enough, and what are the signals that it is time to introduce objects?
When is procedural code enough, and what are the signals that it is time to introduce objects?
If the whole task fits into a dozen lines — compute a factorial, sort an array, parse a string — procedural code is the right answer. A class with no state and a single method is usually just an extra layer that buys you nothing. Procedural style suits scripts, calculations and learning exercises; OOP suits large applications maintained for years.
The signals that it is time to introduce objects: several variables are always passed around together; the same set of data is processed in different parts of the program; behaviour starts to vary by the type of entity. To refactor procedural code into object-oriented code, group those variables into a class, move the methods that took them into the same class, mark the fields private and expose only the methods the caller genuinely needs.
6. How does the same task look in procedural and in object-oriented style in Java?
How does the same task look in procedural and in object-oriented style in Java?
The task: price a line in a shopping cart. Procedural version — the data sits in local variables and the method receives it through parameters:
public class Main {
public static void main(String[] args) {
String name = "Coffee";
double price = 12.0;
int quantity = 3;
System.out.println(name + ": " + total(price, quantity));
}
static double total(double price, int quantity) {
return price * quantity;
}
}
Object-oriented version — data and behaviour move into a Product class and the price is hidden behind the private modifier:
public class Product {
private final String name;
private final double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public double total(int quantity) {
return price * quantity;
}
public String getName() {
return name;
}
}
Product coffee = new Product("Coffee", 12.0);
System.out.println(coffee.getName() + ": " + coffee.total(3));
The output is identical, but in the second version the price cannot be corrupted from the outside, and the rule for calculating the total sits next to the data it uses. Adding a discount or a tax means editing one class instead of every place that performs the calculation.