CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium
A developer is writing a program to manage a list of tasks. Each task has a name (e.g., 'Buy groceries'), a due date (e.g., '2024-12-31'), and a completion status (e.g., true/false). The developer wants to group these related pieces of information together into a single unit. Which programming construct is specifically designed to achieve this bundling of different data types into one logical entity?
- AArray
- BLoop
- CFunction
- DObject/Struct
Show answer & explanationAnswer & explanation
Correct answer: D. Object/Struct
An object (or a struct in some languages like C/C++) is a composite data type that allows you to group related data (properties/attributes) and sometimes functions (methods) into a single entity. This perfectly suits the need to bundle a task's name, due date, and status together.
Why the other options are wrong
- A. An array stores a collection of items, usually of the same type, but doesn't bundle different types into a single logical unit with named properties.
- B. A loop is a control flow statement for repetition, not a data structure.
- C. A function is a block of reusable code, not a data structure for grouping related attributes.
Object/Struct
A composite data type that allows you to combine multiple related pieces of data (and sometimes functions/methods) into a single, named entity. It provides a way to model real-world concepts in code.
- Properties (or members) hold the data.
- Methods (or functions) define behaviors associated with the object.
- Allows for creating instances (objects) that share the same structure but have different data.
- Fundamental to Object-Oriented Programming (OOP).
Memory trick: An Object is an 'object' of data, all bundled together.