C, Java & Python Interview Questions
C, Java & Python Interview Questions
Multi-threading in Java allows simultaneous execution of two or more segments of a program, improving performance and resource utilization. It enables more efficient CPU use by running parallel tasks, reducing CPU idle time. In UI applications, it provides a smoother user experience by handling background tasks concurrently. Threads also allow the program to continue executing even when part is blocked, maintaining responsiveness .
Polymorphism in Java allows objects to be treated as instances of their parent class, enabling multiple forms of a single interface. This enhances flexibility by allowing code to be more generic and reusable, as methods can act on superclass types and process any object of a subclass. It aids maintainability by centralizing method implementation and reducing redundancy, making code easier to manage and extend .
Method overloading in Java refers to having multiple methods with the same name but different parameter lists (type, number, or both) within the same class, providing multiple ways to perform similar tasks. Method overriding, however, involves redefining a method in a subclass that was already defined in its superclass, allowing the subclass to provide a specific implementation for the method. Overloading is resolved at compile time, whereas overriding is determined at runtime .
Object-oriented programming (OOP) in Java is strictly class-based, requiring all functions to be part of classes, while Python supports OOP in a more flexible and informal way, mixing procedural and object-oriented styles. Java uses explicit data types and has strict syntax rules, which offer more control over data flow. Python, in contrast, is dynamic and dynamically typed, which can increase productivity with quicker prototyping but might lead to runtime errors if not handled cautiously .
Arrays in C are collections of elements stored at contiguous memory locations, while pointers are variables that store the address of other variables. Arrays provide index-based access and have a fixed size determined at compile time. Pointers, however, can dynamically point to different data types or memory locations and allow more flexible data handling and dynamic memory allocation. Additionally, pointers can be incremented or decremented to traverse memory, while arrays cannot .
Recursion allows a function to call itself, solving complex problems by breaking them down into simpler sub-problems. It simplifies code for problems like tree traversal, factorials, and the Fibonacci sequence, often leading to more elegant and intuitive solutions than iterative approaches. However, recursion may involve higher memory consumption due to stack usage and sometimes is replaced with an iterative solution when performance is crucial .
The declaration of a function in C specifies the function's name, return type, and parameters, serving as a contract or a promise about what can be expected. It's typically found in header files. The definition of a function, on the other hand, includes the actual body of the function, describing what the function does, and is found in the source file .
Static variables in C retain their value across function calls, unlike local variables which get reinitialized. This persistence allows static variables to maintain state information between functions calls, making them useful for counters or accumulators. They help optimize memory by allocating a single memory space, reducing overhead involved by multiple reassignments .
Encapsulation in Java restricts access to certain components of an object, thereby protecting its internal state from unauthorized access and modification. By using private fields and providing public getter and setter methods, encapsulation ensures that data can only be modified in controlled ways, thus securing sensitive data and reducing the possibility of unintended interference from other parts of the program .
Normalization in database management involves organizing data to reduce redundancy and improve data integrity. By structuring tables using normal forms, redundant data storage is minimized, leading to reduced data anomalies and more efficient updates. It enhances data consistency and integrity, ensuring that dependencies make logical sense and improving scalability and maintainability of the database system .