Baidu Frequently Asked Core Java Interview Questions

Baidu Most Frequently Asked Latest Core Java Interview Questions Answers

What Is The Static Field Modifier?

Class member variables are often called instance variables because each instance of the class, each object, gets its own copy of each variables. Depending on your program's purpose, there may be times when the class objects need to share information. In such cases, you can specify one or more class variables as shared among the objects. To share a variable among class objects, you declare the variable as static, as shown

public static int ShareThisValue;

In this case, if object changes the value of the ShareThisValue variables, each object will see the updated value.

What Is The Final Field Modifier?

When you declare variable in a class final, you tell the compiler that the -variable has a constant value that program should not change. To define a final variable, you must also include an initializer that assigns a value to the constant. For example, the following statement creates a constant value named MAXJCEYS, to which the program assigns the value 256

protected static final int MAX_KEYS = 256;

Explain The Transient Field Modifier?

When you declare a variable in a class transient, you tell Java that the variable is not a part of the object's persistent state. Currently, Java does not use the transient keyword. However, future (persistent) versions of Java may use the transient keyword to tell the compiler that the program is using the variable for "scratch pad" purposes and that the compiler does not need to save the variable to disk. The following statement illustrates the use of the transient keyword

transient float temp__swap__value;

Explain The Use Of Volatile Field Modifier?

When you compile a program, the compiler will examine your code and perform some "tricks" behind the scenes that optimize your program's performance. Under certain circumstances, you may want to force the compiler not to optimize a variable. Optimization can make certain assumptions about where and how memory is handled. If, for example, you are building an interface to memory-mapped device, you may need to suppress optimization. To protect a variable from being optimized, you should use the volatile keyword, as shown

volatile double system_bit_flags;
Baidu Most Frequently Asked Latest Core Java Interview Questions Answers
Baidu Most Frequently Asked Latest Core Java Interview Questions Answers

What Is Default Constructors?

When you create your classes, you should always provide one or more constructor methods. However, if you do not specify constructor, Java will provide a default constructor for you. Java's default constructor will allocate memory to hold the object and will then initialize all instance variables to their defaults. Java will not provide a default constructor, however, if your class specifies one or more constructor for the class.

What Is The Public Method Modifier?

Using the public, private and protected keywords, you can control a variable's scope. In a similar way, Java lets you use these attributes with class methods as well. A public method is visible everywhere that the class is visible. To declare a method as public, simply precede the method header with the public keyword, as shown

public float myPublicMethod();

What Is The Private Method Modifier?

Using the public, private and protected keywords, you can control a variable's scope. In a similar way, Java lets you use these attributes with class methods as well. A private method is only visible within its class. Subclasses cannot access private methods. To declare a method as private, simply precede the method header with the private keyword, as shown

private int myPrivateMethod();

What Is The Protected Method Modifier?

Using the public, private and protected keywords, you can control a variable's scope. In a similar way, Java lets you use these attributes with class methods as well. A protected method is only visible within its class, within subclasses or within the class package. To declare a method as protected, simply precede the method header with the protected keyword, as shown

protected int myProtectedMethod();

Explain The Private Protected Method Modifier?

Using the public, private and protected keywords, you can control a variable's scope. In a similar way, Java lets you use these attributes with class methods as well. A private protected method is only visible within its class and within subclasses of the class. The difference between a protected method and a private protected method is that a private protected method is not accessible throughout the class package. To declare method as private protected, simply precede the method header with the private protected keyword, as shown

private protected int myPrivateProtectedMethod();

Can You Explain The Final Method Modifier?

Java lets you extend one class (the superclass) with another (the subclass). When a subclass extends a class, the subclass can override the superclass methods. In some cases depending on a method's , purpose, you may want to prevent a subclass from overriding a specific method. When you declare a class method as final, another class cannot override the methods. Methods which you declare static or private are implicitly final. To declare a method as final, simply precede the method header with the final keyword, as shown

public final CannotOverrideThisMethod();

What Is The Abstract Method Modifier?

When the abstract keyword precedes a class method, you cannot create an instance of the class containing the method. Abstract methods do not provide an implementation. Instead, your abstract method definition only indicates the arguments return type. When a subclass extends the class containing the abstract method, the subclass is required to supply the implementation for the abstract method. To declare a method abstract, simply provides the abstract keyword , as follows
public abstract void implementMeLater(int x) ;

What Is The Synchronized Method Modifier?

Java supports multiple threads of execution which appear to execute simultaneously within your program. Depending on your program's processing, there may be times when you must guarantee that two or more threads cannot access method at the same time. To control the number of threads that can access a method at any one time, you use the synchronized keyword. When the Java compiler encounters the synchronized keyword, the compiler will include special code that locks the method as one thread starts executing the method's instruction and later unlocks the method as the thread exits. Normally, programs synchronize methods that access shared data. The following statement illustrates the use of the synchronized keyword

synchronized void refreshData( )

Explain The Init Method?

When a Web browser (or an appletviewer) runs a Java applet, the applet's execution starts with the init method. Think of the Java init method as similar to the main function in C/C++, at which the program's execution starts. However, unlike the C/C++ main function, when the Java init method ends, the applet does not end. Most applets use init to initialize key variables (hence, the name init). If you do not supply an init method within your applet, Java will run its own default init method which is defined in the Applet class library. The following statements illustrate the format of an init method

public void init()
{
//statements
}
The public keyword tells the Java compiler that another object (in this case, the browser) can call the init method from outside of the Applet class. The void keyword tells the Java compiler that the init method does not return a value to the browser. As you can see from the empty parentheses following the method name, init does not use any parameters.

What Is The Destroy Method?

Each time your applet ends, Java automatically calls the destroy method to free up memory the applet was using. The destroy method is the compliment of the init method. However, you normally do not need to override, the destroy method unless you have specific resources that you need to remove, such as large graphical files or special threads that your applet has created. In short, the destroy method provides a convenient way to group your applet's "clean up" processing into one location, as shown

public void destroy()
{
  // Statements here
}

What Is Multithreading?

Multithreading is the ability to have various parts of a program perform steps seemingly at the same time. For example, many Web browsers let users view various pages and click on hot links while the browser is downloading various pictures, text, sounds, and while drawing objects on the screen, The Java design began with the goal of supporting multiple threads of execution. Java lets programs interleave multiple program steps through the use of threads.

How Java Uses The String And Stringbuffer Classes?

Java strings are immutable, which means after you create a String object, you cannot change that object's contents. The Java designers found that most of the time, programmers do not need to change a string after it is created. By making String objects immutable, Java can provide better error protection and more efficient handling of strings. However, in case you do need to change the String, you can use a class called StringBuffer as a temporary "scratchpad" which you use to make changes. In short, your programs can change the String objects you store within a Stringbuffer and later copy the buffer's contents back to the String object after your alterations are complete.

In short, you should use String objects for "frozen" character strings whose contents you don't expect to change. In contrast, you should use the StringBuffer class for strings you expect to change in content or size. Within your programs, you can take advantage of features from both classes because both provide methods to easily convert between the two.

What Is The Epoch Date?

Within a Java program, you can create a Date object by specifying a year, month, date and optionally, the hour, minute and second as parameters to the constructor function. You can also create a Date object with no arguments to the constructor, in which case the Date object will contain the current date and time. Finally, you can create a Date object by specifying the number of milliseconds since the epoch date, which is midnight GMT, January 1st, 1970. The Date class uses the epoch date as a reference point which lets your programs refer to subsequent dates in terms of a single long integer. You cannot set a year before 1970.

What Is An Arrays?

Sometimes you will need to manipulate a series of related objects within an array object. Arrays let your programs work conveniently with a group of related objects. In short, an array simply lets you store and access a set of objects of the same type within the same variable. For example, you can use an array to keep track of grades for fifty students or to store a series of file names.

Even though Java arrays are similar in syntax to C/C++ arrays, they have subtle differences. In Java, an array is basically an object that points to a set of other objects or primitive data types. The only visible difference between arrays and objects is that arrays have a special syntax to make them behave like the arrays found in other languages. Unlike C and C++, however, Java arrays cannot change in size, nor can a program use an out-of-bound index with a Java array. Also, you declare and create arrays in Java very differently than in C/C++.

What Is Binary Search?

You know that you can find an element in an array by searching each element of the array one by one. Unfortunately, sequential searches are very inefficient for large arrays. If your array is sorted, you can use a binary search instead to locate a value within the array. A binary search repeatedly subdivides the array until it finds your desired element. For example, you have undoubtedly searched for a word in a dictionary. A sequential search is equivalent to searching each word one by one, starting from the very first word. If the word is much past the first page, this type of search is a very inefficient process.

A binary search is equivalent to opening a dictionary in the middle, and then deciding if the word is in the first half or second half of the book. You then open the next section in the middle and if the word is in the first half or second half of that section. You can then repeat this process of dividing the book in half until you find the word for which you are looking. If you have never tried, pick a word and try to find it in a dictionary using a binary search technique. You might be surprised at how few divisions it takes to get very close to the word you are looking for. A binary search is very efficient. However, the array must be sorted for it to work.

Can A Private Method Of A Superclass Be Declared Within A Subclass?

Sure. A private field or method or inner class belongs to its declared class and hides from its subclasses. There is no way for private stuff to have a runtime overloading or overriding (polymorphism) features.

What Is Quick Sort?

For large arrays, you need an efficient sorting technique. One of the most efficient techniques is the quick sort. The quick sort technique takes the middle element of an array and sub-divides the array into two smaller arrays. One array will contain elements greater than the middle value of the original array. Conversely, the other array will contain elements that are less than the middle value. The quick sort will repeat this process for each new array until the final arrays contain only a single element. At this point, the single element arrays are in the proper order, as shown below

What Is The Difference Between Final, Finally And Finalize?

 final—declare constant
 finally—handles exception
 Finalize—helps in garbage collection.

In System.out.println( ), What Is System, Out And Println?

System is a predefined final class, out is a PrintStream object and println is a built-in overloaded method in the out object.

What Is Meant By "abstract Interface"?

First, an interface is abstract. That means you cannot have any implementation in an interface. All the methods declared in an interface are abstract methods or signatures of the methods.

What Is The Difference Between Swing And Awt Components?

AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt. Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

Why Java Does Not Support Pointers?

Because pointers are unsafe. Java uses reference types to hide pointers and programmers feel easier to deal with reference types without pointers.

What Are Parsers? Dom Vs Sax Parser.

Parsers are fundamental xml components, a bridge between XML documents and applications that process that XML. The parser is responsible for handling xml syntax, checking the contents of the document against constraints established in a DTD or Schema.

What Is A Platform?

A platform is the hardware or software environment in which a program runs. Most platforms can be described as a combination of the operating system and hardware, like Windows 2000/XP, Linux, Solaris and MacOS.

What Is The Main Difference Between Java Platform And Other Platforms?

The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other hardware-based platforms, The Java platform has two components

The Java Virtual Machine (Java VM).
The Java Application Programming Interface (Java API).

What Is The Java Virtual Machine?

The Java Virtual Machine is software that can be ported onto various hardware-based platforms.

What Is The Java Api?

The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets.

Post a Comment

Previous Post Next Post