Extrieve Programming Interview Questions
Extrieve Programming Interview Questions
Polymorphism in Java is the ability of a single interface to represent different types of objects and methods. Compile-time polymorphism, also known as method overloading, occurs when multiple methods with the same name exist in a class with different parameters. The method to be executed is determined at the compile time. For example, a class may have methods `add(int a, int b)` and `add(double a, double b)` for addition operations. Runtime polymorphism, or method overriding, involves methods with the same name and parameters in different classes related by inheritance. The decision on which method to execute is made during runtime. For instance, if a superclass `Animal` has a method `makeNoise()`, and subclasses `Dog` and `Cat` override this method, the actual method to be called is determined by the object's actual type during runtime .
In JDBC, a `statement` is a SQL statement that is executed directly, making it suitable for SQL commands run only once. A `prepared statement` is a precompiled SQL statement that can be executed multiple times with different inputs, benefitting from improved performance and safety against SQL injection attacks. `Callable statements` are used to execute stored procedures in a database, allowing complex operations to be executed in a controlled environment. Each type serves unique requirements: `statements` for simple, quick executions; `prepared statements` for batch processing; and `callable statements` for complex procedure calls .
Multiple inheritance is not supported in Java directly through classes to prevent the complexity and ambiguity it introduces. However, it can be achieved using interfaces. In Java, a class can implement multiple interfaces, allowing it to inherit the behavior from multiple sources. Each interface can define methods that the implementing class will provide, which effectively simulates multiple inheritance. For instance, if there are interfaces `Interface1` and `Interface2`, a class `MyClass` can implement both interfaces, thereby inheriting behaviors from both .
Wrapper classes in Java encapsulate primitive data types within an object, allowing those primitives to be used where objects are required, such as in Collections and Generics. Java provides a wrapper class for each of the primitive types, like `Integer` for `int`, `Double` for `double`, and so on. These classes provide utility methods for parsing, converting, and manipulating the values of primitive data types. Wrapper classes are essential for situations where an object representation of a primitive is necessary, enabling interoperability with Java's object-oriented features .
Binary search is an efficient algorithm for finding an element's position within a sorted array. It works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the search continues in the lower half; otherwise, it continues in the upper half. This process is repeated until the value is found or the interval is empty. Binary search is most efficient when used on a sorted dataset, reducing the time complexity to O(log n) as it eliminates half of the remaining elements during each comparison .
In Java, static elements belong to the class itself rather than to any specific instance of the class. This includes static variables and static methods, which means they can be accessed without creating an instance of the class. Conversely, non-static elements are tied to the particular instance of the class, which means you must create an instance to access them. Static methods cannot access non-static variables or methods directly due to their lack of association with an instance. Non-static methods, however, can access static variables and methods .
The `throw` and `throws` keywords in Java serve different purposes related to exception handling. The `throw` keyword is used within a method to explicitly throw an exception, either a newly instantiated one or a re-thrown caught instance. It is followed by an instance of the `Throwable` class or its subclasses like `Exception`. `Throws`, on the other hand, is a keyword included in a method's declaration indicating that the method might throw exceptions of the specified types, which must be handled by the calling method. This is used for checked exceptions, allowing them to propagate upwards in the stack .
The `public static void main(String[] args)` method serves as the entry point of any standalone Java application. The method must be `public` to be accessible by the JVM, `static` so it can be invoked without instantiating the class, `void` as it does not return any value, and accepts a `String` array to handle command-line arguments. This signature allows the JVM to locate and call the method to begin executing an application, thus following a standardized entry mechanism for Java programs .
In Java, an array is a fixed-size, homogeneous data structure to store elements indexed directly and occupies contiguous memory locations. An ArrayList is part of the Java Collections Framework and offers dynamic resizing, meaning it can adjust its size as elements are added or removed. While arrays offer better performance due to fixed memory allocation, an ArrayList provides more flexibility and convenience with automatic resizing and rich collection methods. Arrays are preferable when the size of the list is known and constant, whereas ArrayLists are better suited for applications where data size might vary .
Exception handling in Java is a means to manage runtime errors, ensuring the normal flow of application execution is maintained. It uses the `try`, `catch`, `finally`, `throw`, and `throws` keywords to handle exceptions gracefully without stopping the application's execution. The importance of exception handling lies in increasing the robustness and reliability of the software by catching unexpected conditions and errors, handling them appropriately, and allowing programmers to provide custom error messages or remedial actions. It aids developers in debugging and maintaining Java applications effectively .