Sei sulla pagina 1di 4

How is Java source code files named?

Java source code file takes the name of a public class or interface that is defined by the
programmer while coding within the file. Source code file may contain maximum one public
class or interface in the file. Two cases me occur while naming of the java source code file:
When public class or interface is defined within a source code file then the java source
code file must take the name of the public class or interface defined while coding.
When no public class or interface is defined inside a source code file, then the java source
code file take a name that is different than its classes and interfaces. Source code files use
the .java extension.
What restrictions are placed on method overriding?
The restrictions on method overloading are the signature of the method.
Signature is number, type, and order of the arguments passed to a method.
Overridden methods must have the same name, argument list, and return type.
Any method which has the same name cannot have the same signature.
They can have the same return types in the same scope.
The compiler uses the signature to detect which overloaded method to refer when an
overloaded method is called.
If two methods have the same name and signature the compiler will throw a runtime
error.
Does networking is support in Java ?
Yes, Java supports two types of classes:
Low-Level Classes: provide support for socket programming like Socket,
DatagramSocket, and ServerSocket classes.
High-Level Classes: provide web programming URL, URLEncoder, and URLConnection
classes.
Networking programming classes ease the programming of network applications. Java
networking like anything else in Java is platform-independent.
What is difference between StringBuffer and StringBuilder in Java?
The difference between StringBuffer and StringBuilder are following:
StringBuffer is uses mutable String, all public methods are synchronized which makes it
thread-safe but same time slow but In JDK 5 they provided StringBuilder which is copy
of StringBuffer without synchronization.
StringBuilder is compatible with StringBuffer, but synchronization property is may not
compatible.
StringBuilder is a little faster than StringBuffer.
What are the difference between throw and throws?
The differences are between throw and throws are:
Throw is used to trigger an exception where as throws is used in declaration of exception.
Without throws, Checked exception cannot be handled where as checked exception can
be propagated with throws.
Throw is used inside the method where as throws is used with the method signature.
Throw is followed by an instance but throws is followed by class.
What is difference between preemptive scheduling and time slicing?
Differences between preemptive and time scheduling are:
In Preemptive scheduling the highest priority task executes until it enters the waiting or
dead stated or a higher priority task comes into existence.
Time slicing, a task executes for a predefined time period and then the pool of ready
tasks. The scheduler then determines which task should execute next, based on priority
and other factor.
What is the difference between final, finally and finalize methods in Java?
Final a key word to define constants.
After declaring a class as final we cannot divide it into subclasses.
When a method is marked final, it cannot be overridden by the subclass.
When a field is marked final, its value once set, cannot be reset.
Finally The finally block is always used with try and catch blocks, except that, when try block
uses System.exit(0) call.
It is ensured for unexpected error, the finally block do the execution which is mention in
last clause of a try catch block.
It is a block of statements that is executed irrespective if or if not an exception was
caught in the preceding try block.
Finalize() This method is linked with garbage collection.
This method is invoked automatically, just before the collection of garbage value.
Describe what happens when an object is created in Java.
Several things happen in a particular order to ensure the object is constructed properly:
1. Memory allocation: To hold all instance variables and implementation-specific data of
the object and its super classes.
2. Initialization: the objects are initialized to their default values.
3. Constructor: Constructors call the constructors for its super classes. This process
continues until the constructor for java.lang.Object is called, as java.lang.Object is the
base class for all objects in java.
4. Execution: Before the body of the constructor is executed all instance variable
initializes and initialization blocks must get executed. Then the body of the constructor is
executed.
Define Vector class? Differentiate the Vector and ArrayList.
Vector canbe said a legacy class which has been introduced to implement the List interface
since Java 2 platform v1.2
Vector is always synchronized but ArrayList is not.
When Vector class is synchronized, if we will run in multithreading environment we've to use
ArrayList with Collections.
Vector has a default size i.e 10 while arrayList has no default size .
ArraayList is not having any method returning Enumerations where as vector list is having.

What are the alternatives to inheritance?
Delegation is an alternative to inheritance.

Delegation denotes that you include an instance of any class as an instance variable, and
forward messages to the instance.

It is safer than inheritance because it ceases you to think about forwarded message , because the
instance is of a known class, rather than a new class, and because it doesnt force you to accept
all the methods of the super class: you can provide only the methods that really make sense.

On the other hand, it makes you write more code, and it is harder to re-use (because it is not a
subclass).
What is RMI and how it is useful?
Remote method invocation is called RMI.

One can work with remote object using RMI .

It gives a impression that you are working with a object that resides within your own JVM
though it is somewhere.

The protocol used by RMI is RMI-IIOP
How many forms of Polymorphism are there?
polymorphism exists in three different forms in Java:

Method overloading
Method overriding through inheritance
Method overriding through the Java interface
In a Java , how can you send program messages on the system console, but error
messages, to a file?
The class System has a variable out that denotes the standard output,
the standard error device represents the variable err .
Naturally, they both point at the system console.

In this way, the standard output can be sent to the file:

Stream x = new Stream(new FileOutputStream("error.txt"));
System.setErr(x);
System.Out(x);
Why is explicit object casting needed?
In order to assign a superclass object in a variable to a subclass,one needs to do explicit casting.

For example:

Person person;
Man man;
man = (Man)person;

An automatic casting takes place when we typecast a object in subclass as parent class object
What are the types of casting?
There are two types of casting:
Casting between primitive numeric types, and
Casting between object references.
Casting between numeric types is used to convert larger values, like double values, to smaller
values, such as byte values. Casting between object references is used to refer to an object by a
compatible class, interface, or array type reference

Potrebbero piacerti anche