Showing posts with label Java Basics. Show all posts
Showing posts with label Java Basics. Show all posts

Wednesday, November 5, 2014

Advanced Java

1. Which containers use a FlowLayout as their default layout?
The Panel and Applet classes use the FlowLayout as their default layout.

2. What state does a thread enter when it terminates its processing?
When a thread terminates its processing, it enters the dead state.

3. What is the Collections API?
The Collections API is a set of classes and interfaces that support operations on collections of objects.

4. Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier.

5. What is the List interface?
The List interface provides support for ordered collections of objects.

Tuesday, November 4, 2014

Advanced Java

1. What is a transient variable?
A transient variable is a variable that may not be serialized.

2. Which containers use a border Layout as their default layout?
The Window, Frame and Dialog classes use a border layout as their default layout.

3. Why do threads block on I/O?
Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.

4. How are Observer and Observable used?
Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

5. What is synchronization and why is it important?
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.

Monday, November 3, 2014

Java Basics

1. What if the main() method is declared as private?
The program compiles properly but at runtime it will give "main() method not public." message.

2. What if the static modifier is removed from the signature of the main() method?
Program compiles. But at runtime throws an error "NoSuchMethodError".

3. What if I write static public void instead of public static void?
Program compiles and runs properly.

4. What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".

5. What is the first argument of the String array in main() method?
The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

Sunday, November 2, 2014

Java Basics

1. What is the difference between a constructor and a method?

A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

2. What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

3. Describe synchronization in respect to multithreading.
With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources.
Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

4. What is an abstract class?
Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not call its constructor), abstract class may contain static data.
Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.

5. What is the difference between an Interface and an Abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Saturday, November 1, 2014

Core Java

1. What is difference between Path and Classpath?
Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

2. What are local variables?
Local varaiables are those which are declared within a block of code like methods. Local variables should be initialised before accessing them.

3. What are instance variables?
Instance variables are those which are defined at the class level. Instance variables need not be initialized before using them as they are automatically initialized to their default values.

4. How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.
static final int PI = 3.14; is an example for constant.

5. Should a main() method be compulsorily declared in all java classes?
No not required. main() method should be defined only if the source class is a java application.