Programming Fundamentals Flashcards

(31 cards)

1
Q

What is the difference between compiled and interpreted languages?

A

Compiled languages (like C++, Java) are translated into machine code before execution, resulting in faster runtime performance. The entire source code is processed by a compiler to create executable files. Interpreted languages (like Python, JavaScript) are executed line-by-line at runtime by an interpreter, offering more flexibility and easier debugging but generally slower execution. Some languages like Java use a hybrid approach, compiling to bytecode then interpreting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain Object-Oriented Programming principles.

A

The four main OOP principles are: Encapsulation: Bundling data and methods together, hiding internal implementation details. Inheritance: Creating new classes based on existing ones, promoting code reuse. Polymorphism: Objects of different types responding to the same interface in different ways. Abstraction: Hiding complex implementation details while exposing only necessary functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a stack and a queue?

A

A stack follows Last-In-First-Out (LIFO) principle, where elements are added and removed from the same end (top). Common operations are push (add) and pop (remove). A queue follows First-In-First-Out (FIFO) principle, where elements are added at the rear and removed from the front. Common operations are enqueue (add) and dequeue (remove).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain recursion and provide an example.

A

Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It requires a base case to prevent infinite loops. Example: Factorial calculation - factorial(n) = n × factorial(n-1), with base case factorial(0) = 1. Recursion is useful for problems with self-similar subproblems like tree traversal, divide-and-conquer algorithms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the differences between static and dynamic typing?

A

Static typing requires variable types to be declared at compile time (C++, Java), enabling early error detection and better performance. Dynamic typing determines types at runtime (Python, JavaScript), offering more flexibility but potential runtime errors. Static typing provides better IDE support and optimization opportunities, while dynamic typing allows for more rapid prototyping and flexible code structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain memory management in programming languages.

A

Memory management involves allocating and deallocating memory for program execution. Manual management (C/C++) gives programmers control but risks memory leaks and segmentation faults. Automatic management uses garbage collection (Java, Python) or reference counting (Swift) to automatically free unused memory. Stack memory stores local variables and function calls, while heap memory stores dynamically allocated objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between pass-by-value and pass-by-reference?

A

Pass-by-value creates a copy of the argument’s value, so modifications inside the function don’t affect the original variable. Pass-by-reference passes the memory address, allowing functions to modify the original variable. Some languages like Java use pass-by-value for primitives and pass-by-reference for objects. Understanding this is crucial for predicting function behavior and memory usage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the concept of immutability in programming.

A

Immutable objects cannot be modified after creation; any ‘change’ creates a new object. Benefits include thread safety, easier debugging, and prevention of unexpected side effects. Languages like Haskell enforce immutability, while others offer immutable data structures (Java’s String, Python’s tuple). Functional programming heavily relies on immutability for predictable code behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are lambda functions and closures?

A

Lambda functions are anonymous functions defined inline, useful for short operations and functional programming. Closures are functions that capture and remember variables from their surrounding scope, even after the outer function returns. They enable powerful patterns like callbacks, event handlers, and maintaining state in functional programming paradigms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe different programming paradigms.

A

Major paradigms include:
- Imperative: Step-by-step instructions (C, Pascal)
- Object-Oriented: Code organized around objects and classes (Java, C++)
- Functional: Computation through function evaluation (Haskell, Lisp)
- Declarative: Describing what should be accomplished (SQL, HTML)
- Logic: Using logical statements and rules (Prolog)

Many modern languages support multiple paradigms.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is exception handling and why is it important?

A

Exception handling manages runtime errors gracefully using try-catch blocks, preventing program crashes. It separates error-handling code from normal logic, improving readability. Key concepts include throwing exceptions, catching specific exception types, finally blocks for cleanup, and creating custom exception classes. Proper exception handling improves program robustness and user experience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the concept of concurrency vs parallelism.

A

Concurrency is about dealing with multiple tasks at once through time-slicing, while parallelism involves actually executing multiple tasks simultaneously on multiple cores. Concurrency can exist on single-core systems through context switching, but parallelism requires multiple processing units. Both require careful synchronization to avoid race conditions and deadlocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are design principles like SOLID?

A

SOLID principles guide object-oriented design:
- Single Responsibility: Class should have one reason to change
- Open-Closed: Open for extension, closed for modification
- Liskov Substitution: Subtypes must be substitutable for base types
- Interface Segregation: Clients shouldn’t depend on unused interfaces
- Dependency Inversion: Depend on abstractions, not concretions

These principles promote maintainable, flexible, and testable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain different types of errors in programming.

A

Programming errors include:
- Syntax Errors: Incorrect language grammar, caught at compile time
- Runtime Errors: Occur during execution (division by zero, null pointer)
- Logic Errors: Program runs but produces wrong results
- Semantic Errors: Code doesn’t match intended meaning
- Off-by-one Errors: Boundary condition mistakes in loops/arrays

Understanding error types helps in debugging and writing robust code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are primitive data types vs. composite/reference types?

A

Primitive Types:
- The most basic data types provided by a language.
- They store actual values directly in memory.
- Examples: integer (int), character (char), boolean (bool), float.

Composite/Reference Types:
- Constructed from primitive types and other composite types.
- They store a memory address (a reference) that points to where the actual data is located.
- Examples: arrays, strings, objects, classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is type coercion (or type casting)?

A

The automatic or explicit conversion of a value from one data type to another.
Implicit Coercion: The language automatically converts types (e.g., adding an integer to a float results in a float).
Explicit Casting: The programmer manually converts types (e.g., converting a float 9.7 to an integer 9).

17
Q

Explain the difference between while, do-while, and for loops.

A

while: A pre-test loop. The condition is checked before the loop body is executed. The body may never run.
do-while: A post-test loop. The loop body is executed at least once, and the condition is checked after.
for: Ideal for iterating a known number of times. It combines initialization, condition checking, and an increment/decrement step in one line.

18
Q

What is the difference between a function and a method?

A

Function: A standalone block of code that performs a specific task.
Method: A function that is associated with an object or a class. It is called on an object and can access and manipulate the object’s data (its properties).

19
Q

Explain function overloading vs. overriding.

A

Overloading (Compile-time Polymorphism):
- Defining multiple functions with the same name but with different parameters (different number of arguments or different types).
- The correct function is chosen at compile time.

Overriding (Runtime Polymorphism):
- A subclass provides a specific implementation of a method that is already defined in its superclass.
- The method signature (name and parameters) must be the same.

20
Q

What is the difference between local and global scope?

A

Scope determines the accessibility of variables.
Local Scope: A variable declared inside a function is only accessible within that function.
Global Scope: A variable declared outside of all functions is accessible from any function in the program. Overuse of global variables is generally discouraged as it can lead to hard-to-debug code.

21
Q

Briefly describe the main stages of program compilation.

A
  1. Lexical Analysis: The source code is scanned and broken down into individual units called ‘tokens’.
  2. Parsing (Syntax Analysis): The tokens are organized into a tree structure (like an Abstract Syntax Tree) to check if they follow the grammar rules of the language.
  3. Code Generation: The syntax tree is converted into low-level code (e.g., assembly or machine code).
  4. Linking: The generated code is combined with code from libraries to create the final executable file.
22
Q

What is the role of an interpreter or a virtual machine (like JVM)?

A

Interpreter: Reads and executes source code line-by-line without first compiling it into a machine-code file. Slower execution but faster development cycle.
Virtual Machine (VM): An abstraction of a computer that allows code (like Java bytecode) to run in the same way on any hardware/OS. The VM translates the intermediate bytecode into native machine code for the specific platform.

23
Q

What are the key differences between an Array and a Linked List?

A

Array:
- Stores elements in a contiguous block of memory.
- Fast access to elements by index (O(1)).
- Slow insertion/deletion in the middle, as it requires shifting elements (O(n)).

Linked List:
- Stores elements in nodes scattered in memory, with each node pointing to the next.
- Slow access to elements, requires traversing the list from the head (O(n)).
- Fast insertion/deletion once the position is found, as it only requires updating pointers (O(1)).

24
Q

What is Big O notation?

A

A mathematical notation used to describe the limiting behavior of a function when the argument tends towards infinity. In computer science, it describes the performance (time or space complexity) of an algorithm as the input size grows. It focuses on the worst-case scenario and the overall growth rate (e.g., O(1) is constant, O(n) is linear, O(n^2) is quadratic).

25
What is a hash table, and how are collisions handled?
Hash Table: A data structure that maps keys to values for highly efficient lookups. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. Collision: Occurs when two different keys hash to the same index. Handling Collisions: 1. Separate Chaining: Each bucket index stores a linked list of all key-value pairs that hash to it. 2. Open Addressing: If a collision occurs, the algorithm probes for the next empty slot in the array and places the value there.
26
What is garbage collection?
An automatic memory management feature in many modern programming languages (like Java, Python, C#). How it works: The garbage collector periodically identifies memory that is no longer in use by the program (i.e., there are no references pointing to it) and reclaims it, making it available for new objects. This prevents memory leaks.
27
Why is modularity important in programming?
Modularity is the practice of breaking down a large software program into separate, independent modules, where each module contains everything necessary to execute one aspect of the desired functionality. Benefits: - Maintainability: Easier to fix and update isolated modules. - Reusability: Modules can be reused in other parts of the program or in other projects. - Readability & Collaboration: Code is easier to understand and teams can work on different modules simultaneously.
28
Explain the difference between Compilation, Runtime, and Logical errors.
Compilation (Syntax) Error: An error that violates the rules of the programming language. The code will not compile. (e.g., missing a semicolon). Runtime Error: An error that occurs during the execution of the program. The program compiles but crashes when it runs. (e.g., dividing by zero, accessing a null pointer). Logical Error: An error in the program's logic. The program compiles and runs without crashing, but it produces an incorrect result. (e.g., using `>` instead of `<` in a condition).
29
What is version control (e.g., Git), and what is a 'commit'?
Version Control System (VCS): A tool that tracks and manages changes to source code over time. It allows multiple developers to collaborate and enables reverting to previous versions if needed. Commit: The fundamental operation in Git. It is like taking a snapshot of your entire project at a specific point in time. Each commit has a unique ID and a message describing the changes made.
30
What is the basic idea behind Test-Driven Development (TDD)?
A software development process that relies on the repetition of a very short development cycle: 1. Red: Write an automated test case for a new feature. The test will fail because the feature doesn't exist yet. 2. Green: Write the minimum amount of code required to make the test pass. 3. Refactor: Clean up the code you just wrote while ensuring all tests still pass. This process encourages simple design and inspires confidence in the code.
31
What is the difference between text and binary file handling?
Text Files: Store data as human-readable characters (e.g., ASCII, UTF-8). Lines are often terminated by newline characters. Data may need to be converted to/from text when reading/writing. Binary Files: Store data in the same format as it is stored in memory (raw bytes). They are not human-readable but are more compact and faster to process as no data conversion is needed.