Java Programming Exam Questions 2023-24
Java Programming Exam Questions 2023-24
Late binding, or dynamic method dispatch, in Java is the mechanism by which a call to an overridden method is resolved at runtime rather than compile-time. It supports polymorphism by allowing a program to decide which method version to execute based on the runtime object. This enables flexibility and enhances code adaptability to future changes without breaking existing functionalities as new subclasses can be added .
Abstraction in Java involves hiding complex implementation details and exposing only essential features of an object. It can be achieved primarily through abstract classes and interfaces. An abstract class provides a base for subclasses to fill in the implementation details, and contains both abstract methods (without body) and concrete methods. Below is a sample abstract class: ```java abstract class Shape { abstract void area(); abstract void volume(); } class Cuboid extends Shape { void area() { /* implementation */ } void volume() { /* implementation */ } } ``` This sample defines an abstract class 'Shape' with abstract methods 'area' and 'volume', allowing subclasses such as 'Cuboid' to implement these methods .
Method overloading in Java occurs when two or more methods in the same class have the same name but different parameters (either in number, types, or order). Method overriding happens when a subclass redefines a method inherited from its superclass with the same signature. For example, in method overloading, you might have methods 'add(int a)' and 'add(int a, int b)'. In method overriding, a superclass method 'display()' might be overridden in the subclass as 'display()' to change its behavior .
Java Swing differs from AWT in that it is built on top of the AWT API but provides a richer set of lightweight components that are platform-independent. Swing components are all written in Java and don't rely on the native system's GUI, making them more versatile and flexible. Java Swing is significant for GUI applications because it provides a consistent look-and-feel across platforms, extensive features such as advanced components (tables, lists, and text areas), and a more sophisticated event handling mechanism .
The 'final' keyword in Java serves various purposes: it prevents a class from being subclassed, stops a method from being overridden, and ensures a variable remains constant (assigned once). The three properties of 'final' are: 1) It can make variables immutable (constant), 2) It can make methods non-overridable, and 3) It can make classes non-inheritable, adding security and predictability to code .
To connect a Java application with a database using JDBC, the following steps are followed: 1) Load the JDBC driver, 2) Establish a connection using the DriverManager, 3) Create a statement using the Connection object, 4) Execute a query using the Statement object to retrieve results, and 5) Close the connections, statements, and result sets to free up resources .
Multithreading in Java allows multiple threads to run concurrently within a program, enhancing performance and efficiency by utilizing CPU resources effectively. It is important for Java applications to perform multiple operations simultaneously, such as user interface responsiveness and background processing. The lifecycle of a thread includes several states: New (thread is created), Runnable (thread is ready to run), Running (currently executing), Blocked/Waiting (waiting for resources or synchronization), and Terminated (completes execution).
String immutability in Java is crucial because it ensures security and efficient memory management by allowing string literals to be reused from the String pool. Immutability prevents malicious alterations and simplifies synchronization in multithreaded environments. When a new String is created, it checks the String pool first, allowing for reduced memory usage as identical strings share the same memory location, leading to memory optimization .
The keyword used to invoke a parent class constructor in Java is 'super'. It is necessary to use 'super' in object-oriented programming to ensure that the parent's constructor is called, which initializes any inherited properties from the parent class before the child class constructor is executed. This is crucial in maintaining the integrity and expected behavior of the base class components within the child class instances .
String in Java is immutable, meaning once a String object is created, its value cannot be changed. On the other hand, StringBuffer and StringBuilder are mutable, allowing modifications such as append, insert, or reverse to occur without creating new objects. StringBuffer is synchronized and thread-safe, whereas StringBuilder is faster but not synchronized. Inbuilt methods of StringBuffer include append(), insert(), delete(), reverse(), and replace().