B.S.W.E. Bachelor of Software Engineering Programming Languages & Paradigms 2 — Questions and Answers
Question 1: What is polymorphism in object-oriented programming?
- The ability to inherit from multiple parent classes simultaneously
- Hiding implementation details inside a class using access modifiers
- The ability of different objects to respond to the same interface in different ways (Correct answer)
- Converting one primitive data type into another automatically
Correct answer: The ability of different objects to respond to the same interface in different ways
Polymorphism allows objects of different types to be treated through a common interface, with each type providing its own implementation of the expected behavior.
Question 2: What is tail call optimization (TCO)?
- Caching the return values of frequently called functions
- An optimization that reuses the current stack frame for a call in tail position (Correct answer)
- Automatically converting recursive functions into iterative loops
- Inlining all small functions at compile time to reduce call overhead
Correct answer: An optimization that reuses the current stack frame for a call in tail position
TCO allows the runtime to reuse the current stack frame when a function call is in tail position, preventing stack overflow in deeply recursive programs.
Question 3: Which language feature allows algorithms and data structures to be written in a type-independent manner?
- Inheritance hierarchies
- Encapsulation via access modifiers
- Generics (parametric polymorphism) (Correct answer)
- Abstract base classes
Correct answer: Generics (parametric polymorphism)
Generics let programmers write type-independent code that can operate on any specified type, as seen in Java's ArrayList<T> or C++ templates.
Question 4: What is a higher-order function?
- A function defined in a parent class and overridden in a subclass
- A function that takes other functions as arguments or returns a function as a result (Correct answer)
- A function with more than four parameters
- A function that executes concurrently on separate threads
Correct answer: A function that takes other functions as arguments or returns a function as a result
Higher-order functions treat functions as first-class values, enabling them to accept functions as parameters or return functions as output.
Question 5: What is the key distinction between pass-by-value and pass-by-reference parameter passing?
- Pass-by-value copies the memory address; pass-by-reference copies the actual value
- Pass-by-value passes a copy of the data; pass-by-reference passes the memory address of the data (Correct answer)
- Pass-by-value is used for objects; pass-by-reference is used for primitives
- They are semantically identical but differ only in syntax across languages
Correct answer: Pass-by-value passes a copy of the data; pass-by-reference passes the memory address of the data
Pass-by-value gives the function a copy of the data so the original is unaffected, while pass-by-reference passes the address so the function can modify the original variable.
Question 6: In logic programming languages such as Prolog, what is the primary computational mechanism?
- Iteration through imperative loops
- Execution of sequential mathematical expressions
- Unification and backtracking over logical facts and rules (Correct answer)
- Message passing between concurrent objects
Correct answer: Unification and backtracking over logical facts and rules
Logic programming computes by unifying terms against a database of facts and rules, using backtracking to explore alternative solution paths when one path fails.
Question 7: What is 'duck typing' as used in dynamically typed languages?
- A type system where compatibility is determined strictly by class inheritance
- A compile-time type inference system used in functional languages
- An approach where an object's suitability is determined by its methods and properties rather than its declared type (Correct answer)
- A runtime error triggered when incompatible types are combined
Correct answer: An approach where an object's suitability is determined by its methods and properties rather than its declared type
Duck typing determines an object's compatibility based on whether it possesses the required methods or attributes, not based on its explicit type or class hierarchy.
What is polymorphism in object-oriented programming?