Programming Languages and Compilers: Programming paradigms · Imperative and Declarative Styles
How imperative code spells out steps that change state while declarative code states the result and leaves the how to the system.
Try 3 questions
1. What is the defining trait of imperative programming?
- Step-by-step commands that change program state
- A description of the result, not the steps
- Code organized only around reusable objects
Show the answer
Answer: Step-by-step commands that change program state
Imperative code tells the machine how to proceed: a sequence of statements, each updating variables or memory.
2. A query reads: SELECT name FROM users WHERE age > 30. Which style does it illustrate?
- Imperative, because it runs in order
- Object-oriented, because it names a table
- Declarative, because it states the wanted result
Show the answer
Answer: Declarative, because it states the wanted result
The query says which rows and columns are wanted but never says how to find them; that is the essence of declarative code.
3. When you run a declarative database query, what usually decides whether to use an index or scan the whole table?
- The operating system's file cache
- The database's query optimizer
- The programmer, through the order of conditions
Show the answer
Answer: The database's query optimizer
Because the query only states the result, the engine's optimizer chooses the execution plan, such as using an index.
The rest of the quiz, a few questions at a time
BrainTok asks a few questions from quizzes like these before the apps you choose open, so your screen time turns into learning. For iPhone.
Join the betaMore Computer Science quizzes
- Artificial Intelligence: Intelligent agents and environments · Agents, Percepts and Rationality
- Artificial Intelligence: Why Chess Was Easier Than Laundry
- Computer Architecture: Data representation and binary arithmetic · Binary and Hexadecimal Numbers
- Computer Architecture: Why Your Processor Guesses the Future
- Computer Networks: Layered models: OSI and TCP/IP · The Seven Layers of the OSI Model
- Computer Networks: Why the Internet Never Promises Delivery