Frenquently asked interview questions Java

Source: Internet
Author: User

 

Java Questions & Answers

1. What is the difference between an applet and an application?

A Java application is made up of a main () method declared as public static void that accepts a string array argument, along with any other classes that main () CILS. it lives in the environment that the Host OS provides.

A Java applet is made up of at least one public class that has to be subclassed from Java. AWT. applet. the applet is confined to living in the user's web browser, and the browser's security rules, (or Sun's appletviewer, which has fewer restrictions ).

The differences between an applet and an application are as follows:

1. Applets can be embedded in HTML pages and downloaded over the Internet whereas applications have no special support in HTML for embedding or downloading.

2. Applets can only be executed inside a Java compatible container, such as a browser or appletviewer whereas applications are executed at command line by java.exe or jview.exe.

3. Applets execute under strict security limitations that disallow certain operations (Sandbox Model Security) whereas applications have no inherent security restrictions.

4. applets don't have the main () method as in applications. instead they operate on an entirely different mechanic where they are initialized by Init (), started by START (), stopped by stop () or destroyed by destroy ().

2. What are Java Beans?

JavaBeans is a portable, platform-independent component model written in the Java programming language, developed in collaboration with industry leaders. it enables developers to write reusable components once and run them anywhere -- benefiting from the platform-independent power of Java technology. javaBeans acts as a bridge between proprietary component models and provides a seamless and powerful means for developers to build components that run in ActiveX container applications.

Java Beans is very powerful tool you can use in your Servlet/jsp bridge. you can use the Servlets to build the bean and can be passed over to the JSP for reading. this provides tight encapsulation of the data while preserving the sanctity of servlets and JSP.

3. What is RMI?

RMI stands for remote method invocation. traditional approaches to executing code on other machines processing ss a network have been confusing as well as tedious and error-prone to implement. the nicest way to think about this problem is that some object happens to live on another machine, and that you can send a message to the remote object and get a result as if the object lived on your local machine. this simplification is exactly what Java Remote Method Invocation (RMI) allows you to do.

4. What gives Java it's "write once and run anywhere" nature?

Java is compiled to be a byte code which is the intermediate language between source code and machine code. this byte code is not platorm specific and hence can be fed to any platform. after being fed to the JVM, which is specific to a special operating system, the Code platform specific machine code is generated thus making Java platform independent.

5. How does Java inheritance work?

A class can only directly extend one class at a time. multiple inheritance is only allowed with regard to interfaces. A class can implement implements interfaces. but a class can only extend one non-interface class.

6. What are native methods? How do you use them?

Native methods are used when the implementation of a particle method is present in language other than Java say C, C ++.
To use the native methods in Java we use the keyword native
Public native method_a ()
This native keyword is signal to the Java compiler that the implementation of this method is in a language other than Java.
Native methods are used when we realize that it wocould take up a lot of rework to write that piece of already existing code in other language to Java.

7. Class A subclass B subclass C. All override Foo (). I cast C to A and call Foo (). What happens? Can C call a-> Foo ()?

An instance of class C is of type Class B and A (both). So you can cast C to A. You cannot cast an instance of A to C.

8. What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?

-- Static variables: these are class level variable whose value remain same irrespective of the number of instances of the class.

-- Static Methods:
These are those methods that can be called without the need for creating the objects of the Class I. e. they are class level methods. they can call only static methods. they cannot refer to "this" as they are not associated with any participant instance.

-- Static block: these are called before the main is called and are called only once. subsequent invocation of the Java program containing static block wocould not call it again. hence, they can be used to load libraries say in native function call.

-- Only inner class cocould be declared as a "static". This DeclarationSuppressThe generation of the reference to the outer class object. this means: 1) To create a static internal class object, we do not need an external class object; 2) We cannot access an external class object from the static internal class object.

9. How many different types of JDBC drivers are present? Discuss them.

There are four JDBC driver types.

Type 1: JDBC-ODBC Bridge Plus ODBC driver:
The first type of JDBC driver is JDBC-ODBC Bridge . It is a driver that provides JDBC access to databases through ODBC drivers. the ODBC driver must be configured on the client for the bridge to work. this driver type is commonly used for prototyping or when there is no JDBC driver available for a particle DBMS.

Type 2: Native-API partly-Java DRIVER:
The native to API driver converts JDBC commands to DBMS-specific native CILS. this is much like the restriction of type 1 drivers. the client must have some binary code loaded on its machine. these drivers do have an advantage over type 1 drivers because they interface directly with the database.

Type 3: JDBC-net pure Java DRIVER:
The JDBC-net drivers are a three-tier solution. this type of driver translates JDBC callinto a database-independent network protocol that is sent to a middleware server. this server then translates this DBMS-independent protocol into a DBMS-specific protocol, which is sent to a particle database. the results are then routed back through the middleware server and sent back to the client. this type of solution makes it possible to implement a pure Java client. it also makes it possible to swap databases without affecting the client.

Type 4: Native-Protocol pure Java driver
These are pure Java drivers that communicate directly with the vendor's database. they do this by converting JDBC commands directly into the database engine's native protocol. this driver has no additional translation or middleware layer, which improves performance tremendously.

10. Does Java have "Goto "?

Yes and no. there is no "Goto" Operator Used in Java, but it is a reserved keyword, and one can use break statements to Branch to a labeled statement, exactly as one wocould use a Goto.

11. Why "bytecode "? Can you reverse-engineer the code from bytecode?

Yes, with some tools.

12. How does exception handling work in Java?

1. It separates the working/functional code from the error-handling code by way of try-catch clses.

2. It allows a clean path for error propagation. If the called method encounters a situation it can't manage, it can throw an exception and let the calling method deal with it.

3. By enlisting the compiler to ensure that "predictional" situations are anticipated and accounted for, it enforces powerful coding.

4. exceptions are of two types: compiler-enforced exceptions, or checked exceptions and runtime exceptions, or unchecked exceptions. compiler-enforced (checked) exceptions are instances of the exception class or one of its subclasses -- excluding the runtimeexception branch. the compiler expects all checked exceptions to be appropriately handled. checked exceptions must be declared in the throws clause of the method throwing them -- assuming, of course, they're not being caught within that same method. the calling method must take care of these exceptions by either catching or declaring them in its throws clause. thus, making an exception checked forces us to pay heed to the possibility of it being thrown. an example of a checked exception is Java. io. ioexception. as the name suggests, it throws whenever an input/output operation is abnormally terminated.

13. Does Java have Destructors?

Java does not have Destructors. Garbage Collector does this job periodically depending upon the memory requirements of the machine and on the fact that a participant object is no longer needed.

But it has finalizers that does a similar job. The syntax is
Public void finalize (){}

If an object has a finalizer, the method is invoked before the system garbage collects the object, but using finalize () does not guarantee that it wocould be called B4 garbage collector is invoked.

14. What does the "final" keyword mean in front of a variable? A method? A class?

A final variable cannot beReassigned, But it is notConstant. For instance,
Final stringbuffer x = new stringbuffer ();
X. append ("hello ");

Is valid. x cannot have a new value in it, but nothing stops operations on the object that it refers, including destructive operations.

Also, a final method cannot be overridden or hidden by new access specifications. this means that the compiler can choose to In-line the invocation of such a method. (I don't know if any compiler actually does this, but it's true in theory .)

The best example of a final class is string, which defines a class that cannot be derived.

15. access specifiers: "public", "protected", "private", nothing?

Public? Any other class from any package can instantiate and execute the classes and Methods
Protected? Only subclasses and classes inside of the package can access the classes and Methods
Private? The original class is the only class allowed to execute the methods.
And in case if there is no modifier specified, it means, only the classes inside the package can access this class and its methods, it is also called "friendly ".

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.