Programming Concepts
Variables, loops, functions, recursion
Programming concepts are the reusable ideas behind most languages: data, variables, control flow, functions, scope, recursion and object-oriented design. Technical aptitude questions usually test whether you can trace these ideas accurately, choose the right abstraction and predict behavior at the boundaries. Syntax varies by language, but the underlying reasoning patterns remain useful across languages.
Key formulas and rules
Key concepts
Data types and variables
A data type describes the kind of value a program can store and the operations that are meaningful for it. Common primitive types include integers, floating-point numbers, characters and booleans. Arrays, strings, objects and records are composite or reference-like structures in many languages. A variable binds a name to a value or object, and assignment rules depend on the language.
Sequence, selection and iteration
Sequence runs statements in order. Selection chooses a path using if-else or a similar construct. Iteration repeats a block with a for, while or equivalent loop. When tracing code, record the condition, the changing variable and the number of times the loop body executes. A loop must make progress toward its stopping condition.
Functions, parameters and return values
A function packages a task behind a name. Parameters receive inputs, local variables hold temporary state, and a return statement sends a result to the caller. Separate parameters from arguments: parameters appear in the definition, while arguments are the values supplied at a call. Small functions with clear inputs and outputs are easier to test and reuse.
Scope, lifetime and mutability
Scope is the region where a name can be accessed. Lifetime is how long the associated value or object exists. A local name may hide a name with the same spelling in an outer scope. Mutable objects can change without replacing the object, while immutable values require a new value for a change. These distinctions explain many aliasing and state bugs.
Recursion
A recursive function calls itself on a smaller or simpler subproblem. Every correct recursive design needs a base case and a step that moves toward it. Calls use stack frames, so deep recursion can consume significant memory or exceed a runtime limit. Iteration and recursion can express the same logic, but their clarity and resource use may differ.
Object-oriented programming
An object combines state and behavior. A class is a blueprint from which objects can be created. Encapsulation keeps data and the operations that protect it together. Abstraction exposes essential behavior while hiding implementation detail. Inheritance derives a type from another type, and polymorphism lets the same interface select different implementations. Use inheritance only when the relationship is genuinely an is-a relationship.
References, copying and complexity
Two variables may contain independent copies or refer to the same object, depending on the operation and language. Mutating a shared object can affect every alias. For performance, estimate how work grows with input size: direct indexing is commonly constant time, one full pass is linear, and two nested full passes are commonly quadratic. Confirm the actual data structure and operation before assigning a complexity.
Worked examples
Example 1
Consider this value-based function:
```
function addOne(value) {
value = value + 1;
return value;
}
let score = 4;
let updated = addOne(score);
```
What are the values of score and updated after the call?
Step 1: The argument score has the value 4 when the function is called.
Step 2: The parameter value receives that value for the call.
Step 3: Changing value to 5 changes the function's local binding, not score.
Step 4: The function returns 5, which is assigned to updated.
Answer: score remains 4 and updated becomes 5. The exact copy/reference behavior for objects is language-dependent, so always check the type being passed.
Example 2
What is the output of this recursive function for factorial(4)?
```
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
```
Step 1: The base case is factorial(0) = 1.
Step 2: factorial(4) calls 4 × factorial(3).
Step 3: The calls continue as 4 × 3 × 2 × 1 × factorial(0).
Step 4: The base case returns 1, so the result is 4 × 3 × 2 × 1 × 1 = 24.
Answer: 24. The call argument decreases by one each time, so the recursion reaches its base case.
Example 3
What is printed, and what is the time complexity?
```
for i from 1 to n:
print(i)
```
Step 1: The loop starts at 1 and visits each integer through n.
Step 2: It prints 1, 2, 3 and so on up to n.
Step 3: The body executes n times, with constant work per iteration.
Answer: It prints the integers from 1 to n, and its time complexity is O(n).
Example 4
A class keeps a private balance field and exposes deposit(amount) and getBalance() methods. Which OOP principle is most directly illustrated?
A) Encapsulation
B) Inheritance
C) Recursion
D) Iteration
Step 1: The balance field is kept behind the class boundary.
Step 2: Other code uses controlled methods instead of changing the field directly.
Step 3: Bundling state with the operations that manage it is encapsulation.
Answer: A) Encapsulation.
Example 5
What does this function return, and what happens to the outer total?
```
let total = 10;
function addLocal() {
let total = 3;
return total + 1;
}
```
The function is called once.
Step 1: The total declared inside addLocal has local scope.
Step 2: It shadows the outer total only while the function runs.
Step 3: The function returns the local value 3 plus 1, which is 4.
Step 4: The outer total is never assigned a new value.
Answer: The function returns 4 and the outer total remains 10.
Representative solved questions
See the kind of question in this topic before opening the full practice set.
Question 1
What are the four pillars of Object-Oriented Programming?
Variables, Loops, Conditions, Arrays
Encapsulation, Inheritance, Polymorphism, Abstraction
Input, Output, Processing, Storage
Classes, Objects, Methods, Functions
Answer: B. Encapsulation, Inheritance, Polymorphism, Abstraction
ExplanationStep 1: Recall the core OOP principles
Step 2: Encapsulation bundles data and methods
Step 3: Inheritance allows class hierarchies
Step 4: Polymorphism enables multiple forms
Step 5: Abstraction hides implementation details
Answer: Encapsulation, Inheritance, Polymorphism, Abstraction
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 2
What is encapsulation in OOP?
Creating multiple instances of a class
Breaking a program into smaller functions
Bundling data and methods that operate on that data within a single unit
Converting one data type to another
Answer: C. Bundling data and methods that operate on that data within a single unit
ExplanationStep 1: Encapsulation is about data hiding and bundling
Step 2: It restricts direct access to some components
Step 3: Data is protected within the class
Step 4: Access is provided through public methods
Answer: Bundling data and methods that operate on that data within a single unit
Sources and review notes
This is an AISEA-authored practice question.
Review status: accepted · Reviewed 2026-08-13 · structure and answer-key checks, editorial quality checks, duplicate screening
Question 3
What is inheritance in OOP?
Common mistakes and useful habits
- Trace variables in a small table when a question contains several assignments or nested calls.
- For every loop, identify the initial value, stopping condition, update step and number of iterations.
- For recursion, write the base case first and verify that every recursive call moves closer to it.
- Distinguish scope from lifetime: a name can be inaccessible even while its associated object remains alive elsewhere.
- When comparing pass-by-value and references, ask whether the operation reassigns a binding or mutates a shared object.
- Use encapsulation to protect invariants, not just to hide code. Public methods should preserve valid object state.
- Estimate complexity from the operation and data structure actually used, not from the topic label alone.
Sources and review notes
Reviewed by AISEA Editorial Desk on 1 Sep 2026.
- The page presents language-neutral fundamentals and labels behavior that varies by language or data type.
- Worked examples were traced for scope, recursion termination, value semantics, OOP principles and basic complexity.
- Object-oriented definitions were checked against the cited platform documentation without turning Java-specific syntax into a universal rule.
- JavaScript GuideMDN Web Docs · Checked 1 Sep 2026Supports: variables, control flow, loops, functions, scope, objects and classes as concrete programming examples
- Object-Oriented Programming ConceptsOracle Java Tutorials · Checked 1 Sep 2026Supports: objects, classes, encapsulation, inheritance, interfaces and polymorphism
- More Control Flow ToolsPython Documentation · Checked 1 Sep 2026Supports: function definitions, parameters, recursion examples and control-flow fundamentals
Ready to test your understanding?
Work through 501 questions with explanations after each answer.