Shared Flashcard Set

Details

Java Interview Questions
Questions that you may see in a Java Related Job Interview
75
Computer Science
Professional
07/27/2011

Additional Computer Science Flashcards

 


 

Cards

Term
What is garbage collection? What is the process that is responsible for doing that in java?
Definition
Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process
Term
What kind of thread is the Garbage collector thread?
Definition
It is a daemon thread.
Term
What is a daemon thread?
Definition
These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.
Term
How will you invoke any external process in Java?
Definition
Runtime.getRuntime().exec(….)
Term
What is the finalize method do?
Definition
Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.
Term
What is mutable object and immutable object?
Definition
If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)
Term
What is the basic difference between string and stringbuffer object?
Definition
String is an immutable object. StringBuffer is a mutable object.
Term
What is the purpose of Void class?
Definition
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.

It is used primarily in reflection. To determine the return type of a method.
 
 
 
 
 
 
 
 
Term
What is reflection?
Definition
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.

Uses: Debuggers and Test Tools, Class Browsers and Visual Dev Environments
 
 
 
 
 
Term
What is the base class for Error and Exception?
Definition
Throwable
Term
What is the byte range?
Definition
-128 to 127
Term
What is the implementation of destroy method in java.. is it native or java code?
Definition
This method is not implemented.

The GC takes care of it.
 
 
Term
What is a package?
Definition
To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.
Term
What are the approaches that you will follow for making a program very efficient?
Definition
  • By avoiding too much of static methods
  • avoiding the excessive and unnecessary use of synchronized methods
  • Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user)
  • Usage of appropriate design patterns
  • Using cache methodologies for remote invocations
  • Avoiding creation of variables within a loop and lot more.
Term
What is a DatabaseMetaData?
Definition
Comprehensive information about the database as a whole.
Term
What is Locale?
Definition
A Locale object represents a specific geographical, political, or cultural region
Term
How will you load a specific locale?
Definition
Using ResourceBundle.getBundle(…);
Term
What is JIT and its use?
Definition
Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.

Code compiled with JIT should run faster on the given architecture it's compiled for.

Term
Is JVM a compiler or an interpreter?
Definition
Interpreter
Term
When you think about optimization, what is the best way to findout the time/memory consuming process?
Definition
Use a profiler, which can be used to determine a number of different metrics to analyze the performance of the code.
Term
What is the purpose of assert keyword used in JDK1.4.x?
Definition
In order to validate certain expressions. It effectively replaces the if block and automatically throws the AssertionError on failure. This keyword should be used for the critical arguments. Meaning, without that the method does nothing.
Term
How will you get the platform dependent values like line separator, path separator, etc., ?
Definition
Using Sytem.getProperty(…) (line.separator, path.separator, …)
Term
What is skeleton and stub? what is the purpose of those?
Definition
Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.

Client creates a stub...delivered to the server where it is a skeleton, and it then does the action on the server.
 
 
Term
What is the final keyword denotes?
Definition
final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more. Anything declared static is by definition final.
 
 
 
Term
What is the significance of ListIterator?
Definition
You can iterate back and forth.
Term
What is the major difference between LinkedList and ArrayList?
Definition
LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.
Term
What is nested class?
Definition
If a inner class is declared within another class then it is a nested class. Ususally means the nested class is static.
Term
What is inner class?
Definition
The class is defined wholey within another class, and it's non-static.
Term
What is composition?
Definition
A class that references another class. This is different from inheritance where where each class is a subclass of the parent class.
Term
What is aggregation?
Definition
It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.

A Composition that refences another object, or multiple objects. Has a relation ship and sometimes a one to many relationship.

Ex. Airplane Class
Aggregates Wing, Fuselage, Engine, and Tail classes. All Airplane interactions with those classes go through the Airplane class.

Not Parent Child classes though.
 
 
 
 
Term
What are the methods in Object?
Definition
  • clone
  • equals
  • wait
  • finalize
  • getClass
  • hashCode
  • notify
  • notifyAll
  • toString
Term
Can you instantiate the Math class?
Definition
You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.
Term
What is singleton?
Definition
It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. For eg., public class Singleton { private static final Singleton s = new Singleton(); private Singleton() { } public static Singleton getInstance() { return s; } // all non static methods … }
Term
What is DriverManager?
Definition
The basic service to manage set of JDBC drivers.
Term
What is Class.forName() does and how it is useful?
Definition
It loads the class into the ClassLoader. It returns the Class. Using that you can get the instance ( “class-instance”.newInstance() ).
Term
Mutable Object
Definition

Mutable objects have fields that can be changed

Term
Immutable Oject
Definition
Immutable objects have no fields that can be changed after the object is created. Ex. String Class
Term
What is the difference between private, protected, and public?
Definition
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
Term
What's the difference between an interface and an abstract class? Also discuss the similarities. 
Definition
Differences are as follows:

* Interfaces provide a form of multiple inheritance. A class can extend only one other class.
* Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
* A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
* Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast. 

Similarities:

* Neither Abstract classes or Interface can be instantiated.
Term
Question: How you can force the garbage collection?
Definition
Can't be forced, you can request with System.gc(), but may not start immediately.
Term
What's the difference between constructors and normal methods?
Definition
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times and it can return a value or can be void.
Term
Can you call one constructor from another if a class has multiple constructors
Definition
Yes. Use this() to call a constructor from an other constructor.
Term
What are the benifits of using an interface.
Definition
Allows you to use a method from another class without knowing the true classs.  Example database calls.  Different db classes implement the same interface
Term
What are some advantages and disadvantages of Java Sockets?
Definition

Some advantages of Java Sockets: 
Sockets are flexible and sufficient.


Some disadvantages of Java Sockets:
Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network, and can only send raw data.

Term
Explain the usage of the keyword transient?
Definition
Transient keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).
Term
 What's the difference between the methods sleep() and wait()
Definition

The code sleep(1000); puts thread aside for exactly one second.


he code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call.

Term
What would you use to compare two String variables - the operator == or the method equals()?
Definition
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.
Term
Why would you use a synchronized block vs. synchronized method?
Definition
Synchronized blocks place locks for shorter periods than synchronized methods.
Term
What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?
Definition
You do not need to specify any access level, and Java will use a default package access level.
Term
Can an inner class declared inside of a method access local variables of this method?
Definition
It's possible if these variables are final.
Term
What can go wrong if you replace && with & in the following code:
String a=null; if (a!=null && a.length()>10) {...}
Definition

A single ampersand here would lead to a NullPointerException.  It's a bitwise operator



Term
What's the main difference between a Vector and an ArrayList?
Definition
Java Vector class is internally synchronized and ArrayList is not synchronized.
Term
 Describe the wrapper classes in Java.
Definition
Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean  - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void
Term
18. How could Java classes direct program messages to the system console, but error messages, say to a file?
Definition

standard.error

standard.out

Term
How do you know if an explicit object casting is needed?
Definition
If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example:
Object a; Customer b; b = (Customer) a;
Term
 If a class is located in a package, what do you need to change in the OS environment to be able to use it?
Definition
set in classpath 
Term
Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
Definition
Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.
Term
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?
Definition
Arraylist
Term
How can a subclass call a method or a constructor defined in a superclass?
Definition
super.methodname
Term
What do you understand by Synchronization?
Definition
Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time
Term
 What's the difference between a queue and a stack?
Definition
FIFO and LIFO
Term
If you're overriding the method equals() of an object, which other method you might also consider?
Definition
hashCode()
Term
What is Collection API?
Definition
The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces. 
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
Term
How would you make a copy of an entire Java object with its state?
Definition
Have this class implement Cloneable interface and call its method clone().
Term
 How can you minimize the need of garbage collection and make the memory use more effective?
Definition
Use object pooling and weak object references.
Term
Explain the Encapsulation principle.
Definition
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper. 
Term
Explain the Inheritance principle.
Definition
Inheritance is the process by which one object acquires the properties of another object. 
Term
Explain the Polymorphism principle.
Definition

One class multiple actions


"one interface, multiple methods". 


Method overloading
* Method overriding through inheritance
* Method overriding through the Java interface


Term
Explain the user defined Exceptions?
Definition
Used to catch specific user defined exceptions.
Term
What is OOPS?
Definition
Object Oriented Programming
Term
What is SOAP?
Definition

Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) for its message format, and usually relies on other Application Layer protocols, most notably Remote Procedure Call (RPC) andHypertext Transfer Protocol (HTTP), for message negotiation and transmission.


The foundation for any web service being created.


Term
Pros and Cons of SOAP
Definition

Pros: 

Can use a wide variety of transfer protocols

Cons"

XML Wrapper can cause some inefficiencies

 

Term
What is static keyword in java?
Definition
Static means one per class.  Means it doesn't have to be instatiated, and it is by definitiong final.
Term
What is Dependency injection
Definition
Dependency injection (DI) is a design pattern in object-oriented computer programming whose purpose is to reduce the coupling between software components. It is similar to the factory method pattern. Frequently an object uses (depends on) work produced by another part of the system. With DI, the object does not need to know in advance about how the other part of the system works. Instead, the programmer provides (injects) the relevant system component in advance along with a contract that it will behave in a certain way.
Supporting users have an ad free experience!