Thursday, March 21, 2013

Core Java Interview Question

  1. Core Java Interview Question - 1
  2. Core Java Interview Question - 2

Core Java Interview Question - 2

Question: What are the kinds of variables in Java? What are their uses?
Answer: Java has three kinds of variables namely, the instance variable, the local variable and the class variable.

Local variables are used inside blocks as counters or in methods as temporary variables and are used to store information needed by a single method.

Instance variables are used to define attributes or the state of a particular object and are used to store information needed by multiple methods in the objects.

Class variables are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same class or keeping track of global states.

Question: What is a literal? How many types of literals are there?
Answer: A literal represents a value of a certain type where the type describes how that value behaves.
There are different types of literals namely number literals, character literals, boolean literals, string literals,etc.

Question: What are operators and what are the various types of operators available in Java?
Answer: Operators are special symbols used in expressions.
The following are the types of operators:
1.     Arithmetic operators
2.     Assignment operators
3.     Increment & Decrement operators
4.     Logical operators
5.     Bitwise operators
6.     Comparison/Relational operators
7.     Conditional operators

Question: What is mean by garbage collection?
Answer: When an object is no longer referred to by any variable, Java automatically reclaims memory used by that object. This is known as garbage collection.

Question: What is immutable object? Can you write immutable object?
Answer: You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non-final but private and not modifying them except in constructor.

Question: Does all property of immutable object needs to be final?
Answer: Not necessary as stated above you can achieve same functionality by making member as non-final but private and not modifying them except in constructor.

Question: What is class loader?
Answer:  The class loader is a subsystem of JVM that is used to load classes and interfaces. There are many types of class loaders e.g. Bootstrap class loader, Extension class loader, System class loader, Plugin class loader etc.

Question: If you do not provide any arguments on the command line, then the String array of Main method will be empty or null?
Answer: It is empty. But not null.

Question: What if I write static public void instead of public static void?
Answer: Interchange the location of these words does not give any effect so Program compiles and runs successfully.

Question: What is the local variables and its default value?
Answer: Local variables are defined within the block or within the method. Local variables are not initialized to any default value, neither primitives nor object references.

Question: What will be the initial value of an object reference which is defined as an instance variable?
Answer: The object references are all initialized to null in Java.

Question: What is static variable?
Answer: Static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc.
Static variable gets memory only once in class area at the time of class loading.

Question: What is static method?
Answer: A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. Static method can access static data member and can change the value of it.

Question: Why main method is static?
Answer: because object is not required to call static method if It were non-static method, jvm create object first then call main() method that will lead to the problem of extra memory allocation.

Question: What is static block?
Answer: It is used to initialize the static data member. It is executed before the main method at the time of class loading.

Question: Can we execute a program without main() method?
Answer: Yes, one of the ways is, by static block.

Question: What if the static modifier is removed from the signature of the main method?
Answer: Program compiles. But at runtime throws an error "NoSuchMethodError".

Question: What is difference between static (class) method and instance method?
Answer: following are the differences:
1.     A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
2.     Object is not required to call static method. Object is required to call instance methods.
3.     Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class directly. static and non-static variables both can be accessed in instance methods.
4.     For example: public static int cube (int n) {return n*n*n ;} For example: public void msg(){...}.

Question: What is object cloning?
Answer: The object cloning is used to create the exact copy of an object.

Question: Can you have virtual functions in Java?
Answer: Yes, all functions in Java are virtual by default.

Question: What is blank final variable?
Answer: A final variable, not initialized at the time of declaration, is known as blank final variable.

Question: Can we initialize blank final variable?
Answer: Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

Question: Can you declare the main method as final?
Answer: Yes, such as, public static final void main(String[] args){}.

Question: When to use array instead of ArrayList?
Answer: I prefer to use Arrays over ArrayLists whenever I know I am only going to work with a fixed number of elements. My reasons are mostly subjective, but I'm listing them here anyway:

Using Collection classes for primitives is appreciably slower since they have to use autoboxing and wrappers.
I prefer the more straightforward [] syntax for accessing elements over ArrayList's get(). This really becomes more important when I need multidimensional arrays.

ArrayLists usually allocate about twice the memory you need now in advance so that you can append items very fast. So there is wastage if you are never going to add any more items.
(Possibly related to the previous point) I think ArrayList accesses are slower than plain arrays in general. The ArrayList implementation uses an underlying array, but all accesses have to got through the get(), set(), remove(), etc. methods which means it goes through more code than a simple array access. But I have not actually tested the difference so I may be wrong.

Having said that, I think the choice actually depends on what you need it for. If you need a fixed number of elements or if you are going to use multiple dimensions, I would suggest a plain array. But if you need a simple random access list and are going to be making a lot of inserts and removals to it, it just make a lot more sense to just use an Arraylist

Core Java Interview Question - 1

Question: Is Java is a pure Object Oriented Language?
Answer: Java is a OOPS language and it is not a pure Object Based Programming Language.
Many languages are Object Oriented. There are six qualities to be satisfied for a programming language to be pure Object Oriented. They are:

1. Encapsulation/Data Hiding
2. Inheritance
3. Polymorphism
4. All predefined types are objects
5. All operations are performed by sending messages to objects
6. All user defined types are objects.

JAVA is not because it supports Primitive data type such as int, byte, long... etc, to be used, which are not objects and jvm does not treat these primitive data type as an object. We can convert primitive to object but we can also use primitive data types in our program....if java gives us restriction to write primitive data type that time we can say that java is a pure object oriented language.

Question: What do you mean by platform independence?
Answer: Platform independence means that we can write and compile the java code in one platform (e.g. Windows) and can execute the class in any other supported platform e.g. (Linux, Solaris, etc.).

Question: What is JVM?
Answer: A Java virtual machine (JVM), an implementation of the Java Virtual Machine Specification, interprets compiled 
Java binary code (called byte code) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions. Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform.
The Java Virtual Machine Specification defines an abstract -- rather than a real -- machine or processor. The Specification specifies an instruction set, a set of 
registers, a stack, a "garbage heap," and a method area. Once a Java virtual machine has been implemented for a given platform, any Java program (which, after compilation, is called byte code) can run on that platform. A Java virtual machine can either interpret the byte code one instruction at a time (mapping it to a real processor instruction) or the byte code can be compiled further for the real processor using what is called a just-in-time compiler.

Question: What is JRE?
Answer: It is used to provide runtime environment. it is the implementation of JVM. It physically exists. It contains set of libraries plus other files that JVM uses at runtime. Implementations of JVMs are also actively released by other companies besides oracle.

Question: What is JDK?
Answer: JDK or the Java Development Kit is a set of a Java compiler, a Java interpreter, developer tools, Java API libraries, documentation which can be used by Java developers to develop Java-based applications.

Question: What is JIT Compiler?
Answer: A just-in-time (JIT) compiler is a program that turns Java 
byte code (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor. After you've written a Java program, the source language statements are compiled by the Java compiler into byte code rather than into code that contains instructions that match a particular hardware platform's processor (for example, an Intel Pentium microprocessor or an IBM System/390 processor). The byte code is platform-independent code that can be sent to any platform and run on that platform.

Question: What gives Java its "write once and run anywhere" nature?
Answer: The byte code. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

Question: What is the use of bin and lib in JDK?
Answer: Bin contains all tools such as javac, applet viewer, awt tool etc., whereas Lib contains all packages and variables.

OOPS Interview Question

Question: What is meant by Object Oriented Programming? 
OOP is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.

Question: What is a Class? 
Class is a template for a set of objects that share a common structure and a common behavior.

Question: What is an Object? 
Object is an instance of a class. It has state, behavior and identity. It is also called as an instance of a class.

Question: What is an Instance? 
An instance has state, behavior and identity. The structure and behaviour of similar classes are defined in their common class. An instance is also called as an object.

Question: What are the core OOP’s concepts? 
Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts.

Question: What is meant by abstraction? 
Abstraction defines the essential characteristics of an object that distinguish it from all other kinds of objects. Abstraction provides crisply-defined conceptual boundaries relative to the perspective of the viewer. Its the process of focusing on the essential characteristics of an object. Abstraction is one of the fundamental elements of the object model.

Question: What is meant by Encapsulation? 
Encapsulation is the process of compartmentalizing the elements of an abstraction that defines the structure and behavior  Encapsulation helps to separate the contractual interface of an abstraction and implementation.

Question: What is meant by Inheritance? 
Inheritance is a relationship among classes, wherein one class shares the structure or behavior defined in another class. This is called Single Inheritance. If a class shares the structure or behavior from multiple classes, then it is called Multiple Inheritance. Inheritance defines “is-a” hierarchy among classes in which one subclass inherits from one or more generalized super classes.

Question: What is meant by Polymorphism? 
Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class.

Question: What is an Abstract Class? 
Abstract class is a class that has no instances. An abstract class is written with the expectation that its concrete subclasses will add to its structure and behavior  typically by implementing its abstract operations.

Question: What is an Interface? 
Interface is an outside view of a class or object which emphasizes its abstraction while hiding its structure and secrets of its behavior.

Question: What is a base class? 
Base class is the most generalized class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

Question: What is a subclass
Subclass is a class that inherits from one or more classes.

Question: What is a superclass? 
     Superclass is a class from which another class inherits.

Question: What is a constructor? 
Constructor is an operation that creates an object and/or initializes its state.

Question: What is a destructor? 
Destructor is an operation that frees the state of an object and/or destroys the object itself. In Java, there is no concept of destructors. It’s taken care by the JVM.

Question: What is meant by Binding? 
Binding denotes association of a name with a class.

Question: What is meant by static binding? 
Static binding is a binding in which the class association is made during compile time. This is also called as early binding.

Question: What is meant by Dynamic binding? 
Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as late binding.

Question: Define Modularity? 
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.

Question: What is meant by Persistence?
     Persistence is the property of an object by which its existence transcends space and time.

Question: What is collaboration? 
Collaboration is a process whereby several objects cooperate to provide some higher level behavior.

Question: In Java, How to make an object completely encapsulated? 
All the instance variables should be declared as private and public getter and setter methods should be provided for accessing the instance variables.

Question: How is polymorphism achieved in java? 
Inheritance, Overloading and Overriding are used to achieve Polymorphism in java.

Question: What is the difference between abstraction and encapsulation?

Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.

Abstraction solves the problem in the design side while Encapsulation is the Implementation.

Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.

Question: What is Inheritance?

v Inheritance is the process by which objects of one class acquire the properties of objects of another class.

v A class that is inherited is called a super class.

v The class that does the inheriting is called a subclass.

v Inheritance is done by using the keyword extends.

v The two most common reasons to use inheritance are:

§  To promote code reuse

§  To use polymorphism