Java Job Interview Questions-4

Posted by Unknown at 10:11

Part-4

1.     Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

2.     If a class is located in a package, what do you need to change in the OS environment to be able to use it?

You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let’s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:/dev/com.xyz.hr.Employee.java. In this case, you’d need to add c:/dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:\>java com.xyz.hr.Employee
3.     What’s the difference between J2SDK 1.5 and J2SDK 5.0?
There’s no difference, Sun Microsystems just re-branded this version.

4.    What would you use to compare two String variables - the operator == or the method equals()?
I’d use the method equals() to compare the values of the Strings and the = = to check if two variables point at the same instance of a String object.

5.  Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
A. Yes, it does. The FileNoFoundException is inherited from the IOException. Exception’s subclasses have to be caught first.

6.     Can an inner class declared inside of a method access local variables of this method?
It’s possible if these variables are final.

7.     What can go wrong if you replace && with & in the following code:
8.     String a=null;
9.     if (a!=null && a.length()>10)
{...}
A single ampersand here would lead to a NullPointerException.

10.    What’s the main difference between a Vector and an ArrayList
Java Vector class is internally synchronized and ArrayList is not.

11.    When should the method invokeLater()be used?
This method is used to ensure that Swing components are updated through the event-dispatching thread.

12.     How can a subclass call a method or a constructor defined in a superclass?
Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass’s constructor.

13.     What’s the difference between a queue and a stack?
Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

14.    You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

15.   What comes to mind when you hear about a young generation in Java?
Garbage collection.

16.    What comes to mind when someone mentions a shallow copy in Java?
Object cloning.

17.    If you’re overriding the method equals() of an object, which other method you might also consider?
hashCode()

18.   You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
ArrayList

19.     How would you make a copy of an entire Java object with its state?
Have this class implement Cloneable interface and call its method clone().

20.    How can you minimize the need of garbage collection and make the memory use more effective?
Use object pooling and weak object references.

21.   There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?
If these classes are threads I’d consider notify() or notifyAll(). For regular classes you can use the Observer interface.

22.   What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
You do not need to specify any access level, and Java will use a default package access level.


click the Below link download this file 


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

Blog Archive

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top