603 Advance Java | 2 Marks Questions with Answer

ADVANCED JAVA

Answer the following questions 

 

1.          What is the use of SetAutoCommit()?

We can use the setAutoCommit() method. This method belongs to the Connection interface and, it accepts a boolean value. If you pass true to this method it turns on the auto-commit feature of the database and, if you pass false to this method it turns off the auto-commit feature of the database.

 

2.          Write short note on: JDBC API Packages.
The Java Database Connectivity (JDBC) API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which tools and alternate interfaces can be built.
The JDBC API is comprised of two packages:
1. java.sql
2. javax.sql
To use the JDBC API with a particular database management system, you need a JDBC technology-based driver to mediate between JDBC technology and the database.

 

3.          What is database?
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a specification from Sun microsystems that provides a standard abstraction(API or Protocol) for java applications to communicate with various databases.

 

4.          What is meant by JDBC driver?
JDBC Driver is a software component that enables Java application to interact with the database. JDBC Driver translates the standard JDBC API calls to the DBMS specific API calls. JDBC drivers implement the defined interfaces in the JDBC API, for interacting with the database server.

 

5.          What is the use of JDBC API?
The Java Database Connectivity (JDBC) API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which tools and alternate interfaces can be built.
The JDBC API is comprised of two packages:
1. java.sql
2. javax.sql
To use the JDBC API with a particular database management system, you need a JDBC technology-based driver to mediate between JDBC technology and the database.

 

6.          What is ResultSetMetaData?
The ResultSetMetaData provides information about the obtained ResultSet object like, the number of columns, names of the columns, datatypes of the columns, name of the table etc… Following are some methods of ResultSetMetaData class.

7.          What is DatabaseMetaData?
database metadata refers to data about database data or, more elaborately, the information about tables, views, column types, column names, result sets, stored procedures, and databases. Java's JDBC metadata API provides the means to retrieve that information through Java code.

 

8.          What are two different ways used to implement threading in java programs?
1. void. start() It is used to start the execution of the thread.
2. void. run() It is used to do an action for a thread.
3. static void. sleep() It sleeps a thread for the specified amount of time.
4. static Thread. currentThread()
5. void. join()
6. int. getPriority()
7. void. setPriority()
8. String. getName()

 

9.          Explain the concept of thread synchronization?
Synchronization Multithreading introduces asynchronous behavior to the programs. If a thread is writing some data another thread may be reading the same data at that time. This may bring inconsistency. When two or more threads wants to access a shared resource, then it must ensure that the resource will be used by only one thread at an instant of time. The mechanism of this process is called synchronization.

 

10.        What is a thread?
A task is completed by a program with the sequence of steps, called as process. Each specific task in a process is called thread. When there are multiple threads execute simultaneously, it is called multithreading. A thread is a program's path of execution. A thread is a line of execution. lightweight sub process, a smallest unit of processing. Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

 

11.        Explain the following methods.

a.          Sleep()
The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.

b.          resume()
The resume() method of thread class is only used with suspend() method. This method is used to resume a thread which was suspended using suspend() method. This method allows the suspended thread to start again.

c.           getPriority()
The getPriority() method of thread class is used to check the priority of the thread. When we create a thread, it has some priority assigned to it. Priority of thread can either be assigned by the JVM or by the programmer explicitly while creating the thread. The thread's priority is in the range of 1 to 10.

d.          getName()
The getName() method is a part of File class. This function returns the Name of the given file object. The function returns a string object which contains the Name of the given file object. If the abstract path does not contain any name then a null string is returned.

e.          wait()
When wait() method is called, the calling thread stops its execution until notify() or notifyAll() method is invoked by some other Thread.

f.            notify()
The notify() method is defined in the Object class, which is Java's top-level class. It's used to wake up only one thread that's waiting for an object, and that thread then begins execution. The thread class notify() method is used to wake up a single thread.

 

12.        What is Networking?
Java Networking is a concept of connecting two or more computing devices together so that we can share resources. Java socket programming provides facility to share data between different computing devices.

 

13.        What are the advantages of networking?

1.          Flexible & powerful

2.          Very sufficient

3.          Updated Information can be used to send only between devices

4.          Low network traffic if efficient use

 

14.        What is Protocol? List out at least two protocols.
A protocol is a set of rules to govern the data transfer between the devices. The rules are used for the following purposes.
1. For compressing the data.
2. For sending device to indicate that it has finished sending a message.
3. For receiving device to indicate that it has received a message.
List out 2 Protocals
1. Transmission control Protocol (TCP)
This is a communication protocol that computers uses to communicate over a network. TCP divides message into stream of packets which are sent and then reassembled at the destination.
2. Internet Protocol (IP)
Internet protocol is addressing protocol. It is always used together with TCP. IP addresses of packet, routes them through different nodes and networks until it reaches its final destination. TCP/IP is perhaps the most used standard protocol for connecting computer networks.

 

15.        What is Internet?
Internet is a global network that connects billions of computers across the world with each other and to the World Wide Web. It uses standard internet protocol suite (TCP/IP) to connect billions of computer users worldwide. It is set up by using cables such as optical fibers and other wireless and networking technologies. At present, internet is the fastest mean of sending or exchanging information and data between computers across the world.

 

16.        Explain use of Socket class.
The java.net package in the Java platform provides a class, Socket , that implements one side of a two-way connection between your Java program and another program on the network. The Socket class sits on top of a platform-dependent implementation, hiding the details of any particular system from your Java program.

 



 

18.        Explain the use of ServerSocket class.
ServerSocket Class is used for providing system-independent implementation of the server-side of a client/server Socket Connection. The constructor for ServerSocket throws an exception if it can't listen on the specified port (for example, the port is already being used). It is widely used so the applications of java.

 

19.        Explain the use of DatagramSocket class.
Class DatagramSocket. This class represents a socket for sending and receiving datagram packets. A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed.

 

20.         Define protocol list TCP and UDP.
1. TCP
2. UDP
3. IP

 

21.        What is Internet Addressing? Give the purpose of InetAddress class?
The java. net. InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses
Purpose : 
This class is used to encapsulate both the numerical IP address and the domain name for that address. A host on the Internet can be represented either in dotted decimal format as an IP address or as a hostname such as yahoo.com. In Java, such addresses are represented by the java.net.InetAddress class. This class can fill a variety of tasks, from resolving an IP address to looking up the hostname.

 

22.        Describe the use of following:

a.          Reserved sockets
Reserved Socket are definded as that the particular sockets are reserved for the particular services that is called as reserved socket. Socket number between 0 to 1023 are reserved for popular application protocols (e.g., Port 80 for HTTP, Port 443 for HTTPS, Port 21 for FTP, Port 23 for Telnet, Port 25 for SMTP, Port 110 for POP3, etc.

b.          DNS (Domain Naming Service)
The domain name system (DNS) is a naming database in which internet domain names are located and translated into Internet Protocol (IP) addresses. The domain name system maps the name people use to locate a website to the IP address that a computer uses to locate that website.
For example, if someone types "example.com" into a web browser, a server behind the scenes maps that name to the corresponding IP address. An IP address is similar in structure to 203.0.113.72.

 

23.        Explain the use of following methods of DatagramPacket class with example:

a.          getPort()
The getPort () method of Java DatagramPacket class returns the port number on the remote host to which this datagram is being sent or from which it was received.
Syntax
public int getPort () 
 

b.          getData()
The getData() method of Java DatagramPacket class returns the data buffer. Any data that is to be received or is to be sent, firstly starts from the offset in the buffer and then runs for length long.
Syntax
public byte[] getData()

 

24.        What is proxy server? Give the purpose of proxy server.
A proxy server is a system or router that provides a gateway between users and the internet. Therefore, it helps prevent cyber attackers from entering a private network. It is a server, referred to as an “intermediary” because it goes between end-users and the web pages they visit online.
Purpose
A proxy server is a special type of network node that alters packets that pass through it, masking their IP addresses and making it seem that the packets originated at the proxy system. Proxy servers may be a natural part of your network architecture, or they may be third-party servers that provide anonymity to their users.

 

25.        What is Servlet? Explain the servlet types.
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
Generic Servlet:
Generic Servlet is a direct division of Servlet type. The servlet is written in this form
This Servlet does not follow any protocol or is not bound to any form of protocol rules as to how to write the servlet and in which order. This results in having no support from any HTTP protocol from the system. We can write servlets easily in Generic Servlet making it common for any methods used in the system. Log method is also used in Generic Servlet which is defined in ServletContext Interface so that all the changes made in the servlet can be monitored easily.
Syntax : javax.servlet.GenericServlet
HTTP Servlet:
HTTP Servlet is a subclass of Generic Servlet. The servlet is called as
javax.servlet.HttpServlet.
As the name suggests, they have HTTP support and can work in any HTTP environment with the help of servers. When there are HTTP-specific methods to be used, we can continue with HTTP servlet. HTTP Servlet can be used only with HTTP protocol and for other protocols, we should use Generic Servlet. Service methods should specify the HTTP protocol in the services handled making it most specific only to HTTP methods.

 




27.        Explain session tracking.
Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet. Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of an user to recognize to particular user.

 

28.        Explain types of cookies.
A cookie is a small amount of data stored on your computer, tablet, or phone, created by and read by website servers. “Cookies were developed during a time when most web usage was anonymous. “They became a way for websites to know about you.”
Cookies are kind of like nametags, in that they allow a website to recognize you, without having to store information about you on their own servers—whenever you visit a site, it sees the cookie on your device and knows who you are.

 

30.        Explain URL rewriting.

URL rewriting is the process of modifying Uniform Resource Locators (URLs) for various purposes. The URL as a “web address” is a string that, when entered into the browser bar field, directs the browser to move to a given site and page. Changing the URL can help with user access and site visibility; it can also be used by hackers to redirect users without their knowledge or “trap” them in a certain site.

 

31.        Explain hidden form field.
A hidden field let web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

 

32.        What is scriptlet in JSP?
This tag allow user to insert java code in JSP. The statement which is written will be moved to jspservice() using JSP container while generating servlet from JSP. When client make a request, JSP service method is invoked and after that the content which is written inside the scriptlet tag executes.
<html><body><% out.print("GeeksforGeeks"); %>  <!-- scriptlet tag --></body></html>

 

33.        What is hibernate?
Hibernate is an open source object relational mapping (ORM) tool that provides a framework to map object-oriented domain models to relational databases for web applications.
Object relational mapping is based on the containerization of objects and the abstraction that provides that capacity. Abstraction makes it possible to address, access and manipulate objects without having to consider how they are related to their data sources.

 

34.        What is ORM?
Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".

 

35.        What are the core interfaces of Hibernate?
1. Session Interface : The basic interface for all hibernate applications. The instances are light weighted and can be created and destroyed without expensive process.
2. SessionFactory interface : The delivery of session objects to hibernate applications is done by this interface. For the whole application, there will be generally one SessionFactory and can be shared by all the application threads.
3. Configuration Interface : Hibernate bootstrap action is configured by this interface. The location specification is specified by specific mapping documents, is done by the instance of this interface.
4. Transaction Interface : This is an optional interface. This interface is used to abstract the code from a transaction that is implemented such as a JDBC / JTA transaction.
5. Query and Criteria interface : The queries from the user are allowed by this interface apart from controlling the flow of the query execution.

 

36.        Mention some of the advantages of using ORM over JDBC.
1. It allows business code access the objects rather than Database tables.
2. It hides the details of SQL queries from OO logic.
3. It is based on JDBC “under hood”.
4. Dealing with database implementation is not required.
5. Entities are based on business concepts rather than database structures.

 

38.        Mention two components of Hibernate configuration object.

The Configuration object is the first Hibernate object you create in any Hibernate application. It is usually created only once during application initialization. It represents a configuration or properties file required by the Hibernate.
The Configuration object provides two keys components −
Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
Class Mapping Setup − This component creates the connection between the Java classes and database tables.

 

39.        How is SQL query created in Hibernate?
Use Session. createSQLQuery(String query) to create the SQLQuery object and execute it. Application will create a native SQL query from the session with the createSQLQuery() method on the Session interface −
Syntax : public SQLQuery createSQLQuery(String sqlString) throws HibernateException

 

40.        What does HQL stand for?
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties.

 

41.        How can we add criteria to a SQL query?
The Hibernate Session interface provides createCriteria() method, which can be used to create a Criteria object that returns instances of the persistence object's class when your application executes a criteria query.
Following is the simplest example of a criteria query is one, which will simply return every object that corresponds to the Employee class.
Criteria cr = session.createCriteria(Employee.class);
List results = cr.list();

 

42.        What is SessionFactory?
The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.

 

43.        Is Session a thread-safe object?
A Session is an inexpensive, non-threadsafe object that should be used once, for a single request, a conversation, single unit of work, and then discarded.

 

Post a Comment

0 Comments