What is the output of the following program?Class a{ public static void main(string[] s) { system.Out.Println(s[1] + s[2] + s[3]);}}Java a 12345options(i) 1(ii) 12(iii)1234(iv)234(v) compilation error
Answered by: abuthahir.d
View all answers by abuthahir.d
Member Since Jan-2008 | Answered On : Apr 14th, 2008
Khadar , i tried this code . it gives that exception
for the foll reason :
when u give a CLA(command line argument) , it will be inserted in the index 0 of the array and then will proceed to 1 ,2 ....
so as per the question , "12345" will be at s[0] . and length of that aray will be 1when u try to access s[1] , we are trying for 2nd element which is unavailable.....
Hope this answers u :-)
exception will be come when using class: java.lang.ArrayIndexOutOfBoundsException: 1 , because 12345 input comes at s[0] places and when we are getting s[1] it come java.lang.ArrayIndexOutOfBounds...
v) Compilation Error
What is need of polymorphism in Java? What could have happen if polymorphism is not present?
Answered by: syamala235
View all answers by syamala235
Member Since Mar-2010 | Answered On : Apr 8th, 2010
Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation.
Polymorphism exists in three distinct forms in Java:
• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface
jdbc,servlets,jsp have come through polymorphism,if not there we have to remember all dependent classes related to DB,Servers...to use in our java coding
The basic reason behind polymorphism is that we can implement different behaviors of the same object depending upon the reference type passed to an object. In case of static polymorphism(Method OverL...
In how many ways we can create an object? Explain with example.
Answered by: vishrutha
Member Since Feb-2009 | Answered On : Feb 13th, 2009
1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance(); 3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
5. Using class loader
one more is through creation of object using classloader
like
this.getClass().getClassLoader().loadClass(”com.amar.myobject”).newInstance();
Now you know how to create an object. But its advised to create objects only when it is necessary to do so.
There are 5 ways:
1-> new operaotor
2-> Class.forName()
3-> Cloning
4-> Deserialization
5-> Class.newInstance()
All sorts of syntax sugar can also be named: 6. New String as a result of concatenation: String varName = getClass().getName(); //to avoid compile-time optimization String a = "Prefix" + varName; ...
Can we override the main method?
Answered by: ashish_setia
View all answers by ashish_setia
Member Since Oct-2006 | Answered On : Oct 3rd, 2006
check out this example
class Checkmain{
public static void main(String args[]){
args[1]="ashish";
System.out.println("hello ");
}
}
class Checkmain1 extends Checkmain{
public static void main(String args[]){
System.out.println("how r u");
}
}
class Jo{
public static void main(String args[]){
String S[]=new String[10] ;
Checkmain.main(S);
Checkmain1.main(S);
}}
Overloading is always in inheritances ie this happens always within class and there in no chance to specifies main method two time in class because execution of class is started from main method.
Suppose a super class method throws an exception and this method is overriden in the subclass with no exception. Now if you create a super class reference that has a subclass object. This throws a compile time error, why?
Answered by: bharathnav
View all answers by bharathnav
Member Since Oct-2008 | Answered On : Oct 13th, 2008
if the object has been created to the subclass and both the methods are non static then the method we overriden doesnt produce any errors or exceptions.
throws exception in super class doesnt affest over-riding:code eg as follows: "java public class Ride{ void clay(){ System.out.println("am in base class"); } public static voi...
What are all the methods used for inter thread communication and what is the class in which these methods are defined?
Answered by: maneesh.ce.2007
View all answers by maneesh.ce.2007
Member Since Nov-2008 | Answered On : Dec 15th, 2008
There are three ways in which Threads communicate with each other :
wait: It tells the calling thread to give up the monitor until some other thread enters the same monitor an calls Notify or Notifyall...
notify: It wakes up the first thread that called Wait() on the same object...
NotifyAll: It allows all the threads that called Wait() on the same object.The thread having the highest priority will run first...
why we use 'wait()' inside a synchronized method
Three methods are used. They are
1. wait() - makes one thread wait for some time till another thread is being executed
2. notify() - makes one wait process into ready state process
3. notifyall() - makes all the waiting processes into ready state processes
Answered by: Mitchel K
Answered On : Jun 27th, 2005A design pattern describes a proven solution to a recurring design problem, placing particular emphasis on the context and forces surrounding the problem, and the consequences and impact of the solution.
There are many good reasons to use design patterns. Here are three:
1) They are proven. You tap the experience, knowledge and insights of developers who have used these patterns successfully in their own work.
2) They are reusable. When a problem recurs, you don't have to invent a new solution; you follow the pattern and adapt it as necessary.
3) They are expressive. Design patterns provide a common vocabulary of solutions, which you can use to express larger solutions succinctly.
The goal of the MVC design pattern is to separate the application object (model) from the way it is represented to the user (view) from the way in which the user controls it (controller).
The MVC architecture has the following benefits:
1) Multiple views using the same model: The separation of model and view allows multiple views to use the same enterprise model. Consequently, an enterprise application's model components are easier to implement, test, and maintain, since all access to the model goes through these components.
2) Easier support for new types of clients: To support a new type of client, you simply write a view and controller for it and wire them into the existing enterprise model.
3) Clarity of design: By glancing at the model's public method list, it should be easy to understand how to control the model's behavior. When designing the application, this trait makes the entire program easier to implement and maintain.
4) Efficient modularity: of the design allows any of the components to be swapped in and out as the user or programmer desires - even the model! Changes to one aspect of the program aren't coupled to other aspects, eliminating many nasty debugging situations. Also, development of the various components can progress in parallel, once the interface between the components is clearly defined.
5) Ease of growth: Controllers and views can grow as the model grows; and older versions of the views and controllers can still be used as long as a common interface is maintained.
6) Distributable: With a couple of proxies one can easily distribute any MVC application by only altering the startup method of the application.
Mvc architecture says that in an event that contains buttons, textboxs and panels or checkboxs..... the operation that changes this state is mvc-> clearly m-->model say button., view-->button...
MVC -
Model
View
Controller
Model represents the business logic implementation of it i.e, service implementation
View represents the presentation logic i.e, display
Controller represents the communicator between these two or main driving components that helps interaction of these 2 components.
Thanks
Rama
Class a{ public static void main(string[] a){ system.Out.Print(string.Valueof(1)+string.Valueof(2)); string s1="s1"; string s2=s1.Tostring(); system.Out.Print(","+(s1==s2)); }}
Answered by: rave123nitb
View all answers by rave123nitb
Member Since Jul-2009 | Answered On : Jul 23rd, 2009
12,true
The output is
12,true
output:12,true
Explain readline function in core Java.
Answered by: omkar.raj
Member Since Jul-2010 | Answered On : Jul 8th, 2010
ReadLine() function is used to take the input from user in string format.
Actually the readLine() method will reads the data through the bufferdReader object. It reads only one line of data through keyboard. We can't enter the multiple lines of data at runtime.
This is the main functionality of the readLine() method.
It will read the data from the keyboard but line by line not character by character.
When and on what conditions will you decide whether to use linked lists or arrays, also which process is fastest and which is slowest in arrays as well as linked list i.E, sorting, searching, add, delete etc.
Answered by: Aqan12
View all questions by Aqan12 View all answers by Aqan12
Member Since Apr-2010 | Answered On : Jun 12th, 2010
Memory:
When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.
LinkedList on the other hand extends dynamically as the objects are added to the linked list.
Accessing Elements:
Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].
To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.
LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.
Adding/Inserting Elements:
Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.
To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.
LinkedList can grow dynamically.
Deleting Elements:
To delete an element from Array you'd have to move down all the elements after the deleted element.
LinkedList elements can be deleted dynamically.
To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
Search and Sort would be similar performance on both.
In all way Linked list is best then Arrays, since its a next level of Array 1. Linked list is dynamic allocation where as Arrays are not 2. We can get and set the value with the use of get, set method...
Linked list is best under the feature of the manipulation e.g. insertion, deletion etc. In the other way the array will go through a good way if we focus on the accessibility.
Explain the use of serialization process in real time projects?
Answered by: Sarje
Member Since Aug-2009 | Answered On : Aug 28th, 2009
Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to save the state of the object (content of the object) to a persistent storage area, such a file.
For example if you want to maintain record of the temprature on a particular place at every hour then you can serialize your object and save the temprature at every hour in a file.
Only an object that implements the java.io.Serializable interface can be saved.
The use of serialization process in real time project is to transfer the object over the network.
Serialization is writing the state of the object to a byte stream. This is useful when you want to save the state of the object in a persistent storage such as file.
Answered by: Faisal Ghauri
Answered On : Jul 24th, 2005Performance does take a hit when using synchronization. I think the way to reduce this hit is to synchronize only a block of code that will be accessed by threads and not synchronize the entire method.
There is degradation in performance when you make a method synchronized. Even when a program contains only a single thread running on a single processor, a synchronized method call is still slow...
The Synchronization may be useful in avoiding the deadlock, helpful in maintaining the consistent object state etc,. Only one thread will be accessed at a time by acquiring lock on the synchronized bl...
What is the output of the following Java program
Public interface i1{static string x="inside i1";public void somemethod();}class a implements i1{ public static void main(string args[[]){ system.Out.Println("calling i1 var ..."+i1.X);}}
Answered by: abuthahir.d
View all answers by abuthahir.d
Member Since Jan-2008 | Answered On : Apr 14th, 2008
Compilation error due to 2 reasons.
1. All variables defined in the Interfaces are implicitly public static final. So need not specify "Static" in interface.
2. If any of the member functions of a Interface is not Overriden , a compilation error will be thrown
It is right that you dont need to explicitly define a varible static but if you do so then it will not give compliler error..
Compilation error will come because you must have to override the abstract method of the implememted interface. After the implementation of the abstract method the program will compile and run properly and print "inside l1".
When there is no methods in the marker interface, then what is the use of marker interface. What is the functionality of the interface. Why do we need to implement marker interface?
Answered by: Saroj_mi2
Member Since Sep-2009 | Answered On : Sep 14th, 2009
Marker interface is an interface with out having any implementation methods.
By implementing this interfaces, we will specify some special behaviour to implemented class.
Examples: Serializable, Cloneable
For example if we want to create a clone for a an object of class using clone()(clone()-- which is an object class method), then class need to implement Cloneable interface.
This mark is to JVM to handle the interface.If you are implimenting this interface the compliler will know some specific job need to be done.
For Cloneable interface even it does not have any methode the JVM looks for clone() method to execute. But the complier will not fail if the clone() meth is not implimented.
Marker interface do not have any method. It is used only to inform JVM thatsuch class can do some special function like clone, serialize.
When you need to perform some behavioral function on any class without implementing method in implemented interface we implement an interface such interface called marker interface for exmple serializabe, cloneable.
Is null a keyword? If not what is it?
Answered by: interviewprep9
View all answers by interviewprep9
Member Since Jun-2008 | Answered On : Jun 14th, 2008
Capital N, Null is not a keyword or a reserved word.
All small letters, null is not a keyword either. but, it's a reserved word. you cannot use null as follows:
String null = "a";
null means that a reference is not pointing to any object.
Null is not a keyword but null is reserved literal. null is the default value for instance variable.
String str = null;
means instance variable str does not hold reference of any object.
Nullmeans showing nothing
Whenever one object reference refers null i.e
ServletConfig application=null;
Then there is nothing in the object reference application.
Difference between throw and throws
What exactly is the difference between between throw and throws? If both are used for the same purpose then why are both needed instead of one?
Answered by: vegetto
Member Since Jun-2008 | Answered On : Jun 15th, 2008
hi there,
throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your code, when executed is capable of having some exception then you use throw keyword as:
throw new MyException(arguments if any);
this leads to instantiating of the MyException class and exception is thrown
throws: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).in this you do not use try catch block but write using the throw clause at appropriate point in you code and the exception is thrown to caller of the method and is handeled by it. eg:
void trouble()throws IOException,any other exceptions just separate them with commas.......
{
/* throw statement at appropriate step,
no need to use try catch here,exception would be thrown to caller and u should provide try catch block to handle exception there else this process cotinues further till
appropriately it is handeled or prog terminates abruptly */
}
this is called as HANDLE OR DECLARE RULE i.e either u handle exceptions using try catch block or by declaring them
hi there, throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your ...
they are not same."throws" declares that your method is capable of throwing an exception."throw" actually does the work , of throwing the exception.example :public void...
Editorial / Best Answer
Answered by: Srimant Misra
Answered On : Aug 12th, 2005A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang.Class instance associated with the object. It is similar to saying:
synchronized(XYZ.class)
{
}
If you synchronized on static method ,its lock on class level not only that method.
yes a method can be static and synchronized