AppDynamics Most Frequently Asked 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();
AppDynamics Most Frequently Asked Core Java Interview Questions Answers
AppDynamics Most Frequently Asked Core Java Interview Questions Answers

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.

Post a Comment

Previous Post Next Post