Monday, September 7, 2009

Java Packages

java.lang & java.util Packages

What is the ultimate ancestor of all java classes

Object class is the ancestor of all the java classes

What are important methods of Object class

wait(), notify(), notifyAll(), equals(), toString().

What is the difference between = = and equals()

= = does shallow comparison, It retuns true if the two object points to the same address in the memory, i.e if the same the same reference
equals() does deep comparison, it checks if the values of the data in the object are same

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

Give example of a final class

Math class is final class and hence cannot be extended

What is the difference between String and StringBuffer

String is an immutable class, i.e you cannot change the values of that class
Example :
String str =
“java”; // address in memory say 12345
And now if you assign a new value to the variable str then
str =
“core java”; then the value of the variable at address 12345 will not change but a new memory is allocated for this variable say 54321
So in the memory address 12345 will have value
“java”
And the memory address 54321 will have value
“core java” and the variable str will now be pointing to address 54321 in memory

StringBuffer can be modified dynamically
Example:
StringBuffer strt =
java // address in memory is say 12345
And now if you assign a new value to the variable str then
Str =
“core java”; then value in the address of memory will get replaced, a new memory address is not allocated in this case.

What will be the result if you compare StringBuffer with String if both have same values

It will return false as you cannot compare String with StringBuffer

What is Collection API

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.

What are different types of collections

A collection has no special order and does not reject duplicates
A list is ordered and does not reject duplicates
A set has no special order but rejects duplicates
A map supports searching on a key field, values of which must be unique

Tell me something about Arrays

Arrays are fast to access, but are inefficient if the number of elements grow and if you have to insert or delete an element

Difference between ArrayList and Vector

Vector methods are synchronized while ArrayList methods are not

Iterator a Class or Interface? What is its use?

Iterator is an interface which is used to step through the elements of a Collection

Difference between Hashtable and HashMap

Hashtable does not store null value, while HashMap does
Hashtable is synchronized, while HashMap is not

 

 

Why do we need public static void main(String args[]) method in Java

We need

  • public: The method can be accessed outside the class / package
  • static: You need not have an instance of the class to access the method
  • void: Your application need not return a value, as the JVM launcher would return the value when it exits
  • main(): This is the entry point for the application

If the main() was not static, you would require an instance of the class in order to execute the method.
If this is the case, what would create the instance of the class? What if your class did not have a public constructor?

What is the difference between an Interface and an Abstract class

In abstract class you can define as well as declare methods, the methods which are declared are to be marked as abstract.
In interface all we just declare methods and the definition is provided by the class which is implementing it

Explain serialization

Serialization means storing a state of a java object by coverting it to byte stream

What are the rules of serialization

Rules:

1. Static fileds are not serialized because they are not part of any one particular object
2. Fileds from the base class are handled only if hose are serializable
3. Transient fileds are not serialized

What is difference between error and exception

Error occurs at runtime and cannot be recovered, Outofmemory is one such example. Exceptions on the other hand are due conditions which the application encounters such as FileNotFound exception or IO exceptions

What do you mean by object oreiented programming

In object oreinted programming the emphasis is more on data than on the procedure and the program is divided into objects.
The data fields are hidden and they cant be accessed by external functions.
The design approach is bottom up.
The functions operate on data that is tied together in data structure

What are 4 pillars of object oreinted programming

1.Abstraction
It means hiding the details and only exposing the essentioal parts

2.Polymorphism
Polymorphism means having many forms. In java you can see polymorphism when you have multiple methods with the same name

3.Inheritance
Inheritance means the child class inherits the non private properties of the parent class

4.Encapsulation
It means data hiding. In java with encapsulate the data by making it private and even we want some other class to work on that data then the setter and getter methods are provided

Difference between procedural and object oreinted language

In procedural programming the instructions are executed one after another and the data is exposed to the whole program
In OOPs programming the unit of program is an object which is nothing but combination of data and code and the data is not exposed outside the object

What is the difference between constructor and method

Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.

What is the difference between parameters and arguments

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

What is reflection in java

Reflection allows Java code to discover information about the fields, methods and constructors of loaded classes and to dynamically invoke them

What is a cloneable interface and how many methods does it contain

It is not having any method because it is a TAGGED or MARKER interface

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

Can you make an instance of abstract class

No you cannot create an instance of abstract class

What are parsers

Parsers are used for processing XML documents. There are 2 types of parsers DOM parser and SAX Parser

Difference between SAX and DOM parser

DOM parsers are Object based and SAX parsers are event based
DOM parsers creates Tree in the memory whereas SAX parser does not and hence it is faster than DOM
DOM parser are useful when we have to modify the XML, with SAX parser you cannot modify the xml, it is read only

What is the difference between Java Bean and Java Class

Basically a Bean is a java class but it has getter and setter method and it does not have any logic in it, it is used for holding data.
On the other hand the Java class can have what a java bean has and also has some logic inside it

What are null or Marker interfaces in Java

The null interfaces are marker interfaces, they do not have function declarations in them, they are empty interfaces, this is to convey the compiler that they have to be treated differently

Does java Support multiple inheritance

Java does not support multiple inheritance directly like C++, because then it is prone to ambiguity, example if a class extends 2 other classes and these 2 parent classes have same method names then there is ambiguity. Hence in Java Multiple inheritance is supported using Interfaces

What are virtual function

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.

The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

Does java support virtual functions

No java does not support virtual functions direclty like in C++, but it supports using Abstract class and interfaces

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 is allocated from heap to hold all instance variables and implementation-specific data of the
object and its superclasses. Implemenation-specific data includes pointers to class and method data.

2. The instance variables of the objects are initialized to their default values.

3. The constructor for the most derived class is invoked. The first thing a constructor does is call the
consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called,
as java.lang.Object is the base class for all objects in java.

4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks
are executed. Then the body of the constructor is executed. Thus, the constructor for the base class
completes first and constructor for the most derived class completes last.

 

What is the purpose of System Class

The purpose of the system class is to provide the access to the System reources

What is instanceOf operator used for

It is used to if an object can be cast into a specific type without throwing Class cast exception

Why we should not have instance variable in an interface

Since all data fields and methods in an Interface are public by default, then we implement that interface in our class then we have public members in our class and this class will expose these data members and this is violation of encapsulation as now the data is not secure

What is JVM

When we install a java package. It contains 2 things
* The Java Runtime Environment (JRE)
* The Java Development Kit (JDK)

The JRE provides runtime support for Java applications. The JDK provides the Java compiler and other development tools. The JDK includes the JRE.

Both the JRE and the JDK include a Java Virtual Machine (JVM). This is the application that executes a Java program. A Java program requires a JVM to run on a particular platform

Can abstract class be final

No, abstract class cannot be final

When a new object of derived Class is created, whose constructor will be called first, childs or parents

Even when the new object of child class is created, first the Base class constructor gets executed and then the child classes constructor

What is a singleton class

A singleton is an object that cannot be instantiated. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.

Can an abstract class have final method

Yes

Can a final class have an abstract method

No, the compiler will give an error

What is the difference between Authentication and Authorization

Authentication is a process for verifying that an individual is who they say they are. Authorization is an additional level of security, and it means that a particular user (usually authenticated), may have access to a particular resource say record, file, directory or script.

 

 

No comments:

Post a Comment