GeekInterview.com
Series: Subject: Topic:

Core Java Interview Questions

Showing Questions 1 - 17 of 17 Questions
Sort by: 
 | 

Can a method be static and synchronized

Asked By: Interview Candidate | Asked On: Aug 16th, 2005

Star Read Best Answer

Editorial / Best Answer

Answered by: Srimant Misra

Answered On : Aug 12th, 2005

A 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) 

}

Answered by: Akii on: May 21st, 2013

If you synchronized on static method ,its lock on class level not only that method.

Answered by: sampra on: Mar 11th, 2008

yes a method can be static and synchronized

Output of Java program

Asked By: khadarzone | Asked On: Apr 11th, 2008

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

Star Read Best Answer

Editorial / Best Answer

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  :-)

Answered by: akii on: Apr 30th, 2013

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...

Answered by: d on: Jan 19th, 2013

v) Compilation Error

Need of polymorphism

Asked By: shanky_06 | Asked On: Dec 22nd, 2009

What is need of polymorphism in Java? What could have happen if polymorphism is not present?

Star Read Best Answer

Editorial / Best Answer

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

Answered by: avinash on: Sep 26th, 2012

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

Answered by: Rupesh Raghani on: May 31st, 2012

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...

Object creation

Asked By: justjiten | Asked On: Dec 8th, 2008

In how many ways we can create an object? Explain with example.

Star Read Best Answer

Editorial / Best Answer

Answered by: vishrutha

View all answers 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.

Answered by: sampra on: Mar 13th, 2012

There are 5 ways:
1-> new operaotor
2-> Class.forName()
3-> Cloning
4-> Deserialization
5-> Class.newInstance()

Answered by: Max on: Sep 7th, 2011

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?

Asked By: diensh | Asked On: Sep 21st, 2006

Star Read Best Answer

Editorial / Best Answer

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);
}}

Answered by: sampra on: Mar 6th, 2012

yes we can

Code
  1. package com.san;
  2.  
  3. public class Test {
  4.         public static void main(String[] args) {
  5.                 System.out.println("main");
  6.  
  7.         }
  8. }
  9.  
  10. class Hello extends Test {
  11.  
  12.         public static void main(String[] args) {
  13.                 System.out.println("main---");
  14.  
  15.         }
  16.  
  17. }

Answered by: vishal on: Dec 18th, 2011

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.

Overriding methods

Asked By: vishalanand_154 | Asked On: Jul 31st, 2008

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?

Star Read Best Answer

Editorial / Best Answer

Answered by: bharathnav

View all answers by bharathnav

Member Since Oct-2008 | Answered On : Oct 13th, 2008

"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 
Latest Answer: Because at run time, the super class will compiled first, so it will gives the run time error. ..."

if the object has been created to the subclass and both the methods are non static then the method we overriden doesn't produce any errors or exceptions.

Answered by: sampra on: Mar 6th, 2012


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.

Answered by: techbuz on: Dec 23rd, 2011

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...

Inter thread communication

Asked By: renijoym | Asked On: Dec 12th, 2008

What are all the methods used for inter thread communication and what is the class in which these methods are defined?

Star Read Best Answer

Editorial / Best Answer

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...

Answered by: chakry on: Nov 5th, 2011

why we use 'wait()' inside a synchronized method

Answered by: kutiarul on: Nov 21st, 2009

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

What is mvc architecture

Asked By: Interview Candidate | Asked On: Sep 7th, 2005

Star Read Best Answer

Editorial / Best Answer

Answered by: Mitchel K

Answered On : Jun 27th, 2005

A 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.

Answered by: dhilip88 on: Nov 23rd, 2010

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...

Answered by: coolguy_krkr on: May 11th, 2008

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

Find the output

Asked By: aruneverb4u | Asked On: Jul 21st, 2009

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)); }}

Star Read Best Answer

Editorial / Best Answer

Answered by: rave123nitb

View all answers by rave123nitb

Member Since Jul-2009 | Answered On : Jul 23rd, 2009

12,true

Answered by: zshekhtm on: Dec 12th, 2010

The output is
12,true

Answered by: v1257 on: Nov 29th, 2010

output:

12,true

Readline() function

Asked By: shashikant pandit | Asked On: Jul 1st, 2010

Explain readline function in core Java.

Star Read Best Answer

Editorial / Best Answer

Answered by: omkar.raj

View all answers 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.

Answered by: muralidhar.9e on: Sep 7th, 2010

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.

Answered by: sailendra.n.jena on: Aug 26th, 2010

It will read the data from the keyboard but line by line not character by character.

Arrays/linked list

Asked By: bhagyashree010 | Asked On: May 27th, 2010

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.

Star Read Best Answer

Editorial / Best Answer

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.

Answered by: jamesjohnney on: Aug 12th, 2010

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...

Answered by: Anubhavs on: Aug 6th, 2010

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.

Serialization process

Asked By: narayanamsaideva | Asked On: Aug 27th, 2009

Explain the use of serialization process in real time projects?

Star Read Best Answer

Editorial / Best Answer

Answered by: Sarje

View all answers 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.

Answered by: dh_maan on: Jun 6th, 2010

The use of serialization process in real time project is to transfer the object over the network.

Answered by: winoth4 on: Oct 27th, 2009

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.

Will there be a performance penalty if you make a method synchronized? If so, can you make any design changes to improve the performance

Asked By: Interview Candidate | Asked On: Jul 25th, 2005

Star Read Best Answer

Editorial / Best Answer

Answered by: Faisal Ghauri

Answered On : Jul 24th, 2005

Performance 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. 
 

Answered by: sumeesudha on: Feb 5th, 2010

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...

Answered by: sripatnaiky on: Feb 4th, 2010

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

Asked By: khadarzone | Asked On: Apr 11th, 2008

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);}}

Star Read Best Answer

Editorial / Best Answer

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

Answered by: shanu_rastogi on: Feb 3rd, 2010

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..

Answered by: sarojmohanty on: Nov 19th, 2009

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".

Marker interface

Asked By: Kamleshnit07 | Asked On: Jun 29th, 2009

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?

Star Read Best Answer

Editorial / Best Answer

Answered by: Saroj_mi2

View all answers 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.

Answered by: Narotam on: Jan 19th, 2010

Marker interface do not have any method. It is used only to inform JVM thatsuch class can do some special function like clone, serialize.

Answered by: uncer on: Jan 15th, 2010

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.

Null keyword

Asked By: gkumawat | Asked On: Jun 13th, 2008

Is null a keyword? If not what is it?

Star Read Best Answer

Editorial / Best Answer

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.

Answered by: Sarje on: Aug 17th, 2009

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.

Answered by: varmasuman on: Sep 13th, 2008

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

Asked By: Kartheek11 | Asked On: Jun 11th, 2008

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?

Star Read Best Answer

Editorial / Best Answer

Answered by: vegetto

View all answers 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
 

 

Answered by: vegetto 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 ...

Answered by: krishna_kanth83 on: Jun 14th, 2008

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...

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.