Advanced Java Viva Questions Guide
Advanced Java Viva Questions Guide
Using the Runnable interface offers more design flexibility than extending the Thread class. Implementing Runnable allows a class to extend other classes as Java does not support multiple inheritance, facilitating better code modularity and reuse. It also separates the thread task from the Thread class properties, leading to a cleaner design. This approach is preferred in scenarios requiring multiple threads to perform distinct operations within a larger, cohesive class hierarchy .
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) serve distinct roles in networking. TCP is connection-oriented, ensuring reliable data transmission with error checking, sequencing, and flow control, making it well-suited for applications requiring data integrity like HTTP, FTP. UDP, being connectionless, offers low-latency and reduced overhead for applications like streaming or gaming where speed is prioritized over reliability. Java networking leverages these protocols based on application needs - TCP for guaranteed data delivery and UDP for faster but less reliable communication .
PreparedStatement objects in JDBC enhance security by preventing SQL injection attacks through parameterized queries where input is bound to placeholders, reducing risk of concatenation-based injections. In terms of performance, PreparedStatements are precompiled and cached by the database, allowing for faster execution of repeated queries by avoiding repeated compilation. In contrast, Statement objects do not precompile queries, leading to security vulnerabilities and inefficiencies in repeated query execution .
A JDBC driver acts as an intermediary enabling Java applications to connect to and interact with databases. It translates Java calls into DBMS-specific calls. Understanding the four types of JDBC drivers—Type 1 (JDBC-ODBC bridge), Type 2 (Native-API driver), Type 3 (Network Protocol driver), and Type 4 (Thin driver)—is crucial as each type offers different trade-offs in terms of performance, portability, and ease of use. Selecting the appropriate driver type impacts the efficiency and portability of database connectivity within a Java application .
Implicit objects in JSP are predefined objects provided by the JSP container and available to the developer without explicit declaration. They include request, response, session, application, out, and others, offering direct access to the core components needed in web application development. Unlike servlets, which require these objects to be manually retrieved and managed, JSP implies a more simplified syntax, allowing developers to easily interact with web elements and produce dynamic content efficiently .
In web applications, a session is a server-side storage mechanism that keeps user-specific data across multiple requests. Cookies are client-side, persisting user data in the browser, which helps in session identification across different sessions. Application context, on the other hand, maintains data that is accessible throughout the application lifecycle but is not tied to specific users. Each mechanism serves unique roles in managing state - sessions handle user-specific continuity, cookies enable cross-request data transfer, and application context supports global data availability within the application .
MVC (Model-View-Controller) architecture contributes to modularity and scalability by clearly separating the business logic from UI components. It consists of the Model, which manages the data and business logic; the View, which handles the presentation layer; and the Controller, which acts as an interface between the Model and View, processing user input. This separation facilitates independent development, testing, and maintenance of each component, enhancing application scalability and flexibility in adapting to changes. Through this, Java web applications maintain cleaner codebases and better accommodate growth and complexity .
Core Java refers to the standard version of Java that focuses on basics and essential features of the language such as object-oriented programming, data structures, basic I/O, and exception handling. It serves as the foundation for all Java applications. Advanced Java, on the other hand, encompasses specialized domains like JDBC, Servlets, and JSPs to facilitate web and network application development. It extends Core Java with advanced libraries and APIs necessary for developing enterprise-level applications .
GET and POST are two HTTP methods that serve different purposes. GET appends data to the URL, making it visible and limited in size, useful for retrieving data without causing side effects on the server. POST, in contrast, sends data in the request body, which remains hidden from users and is not limited in size, suitable for sending sensitive or large messages and performing operations that modify server state. Developers choose between them based on the type of data transmission and the actions being performed, ensuring efficiency and security .
A servlet lifecycle consists of loading and instantiation, initialization, request handling, and destruction. First, the servlet is loaded into the memory by the servlet container and instantiated, following which the init() method is called to perform any initialization tasks. During its lifecycle, the servlet handles multiple requests using the service() method which processes requests and generates responses. Finally, the destroy() method allows the servlet to release resources before being removed from memory, ensuring a managed and seamless web application execution .