OOP's Interview Questions

41 - What is the difference between PATH and CLASSPATH?
Path is use for setting up an environment for operating system. Operating system will look in this PATH for executables.
Classpath is use for setting up the environment for Java. Java will use to find compiled classes (i.e. .class files).
42 - Can i write directly byte code inside my java class. so that that part will not compile by java?
No, you can't write the byte code in your java class because byte code is generated by a compiler after compiling source code and any change to .class file corrupts the .class file.
43 - Where can I initialize a final variable other than the place it is declared (constructor)?
You can initialize final variable value in
  • Constructor
  • Inside any method
  • In init block
  • In static block but variable should be declare as static final.
Once the final variable value is initialized it cannot be changed hence variable will behave as a constant.
44 - What are the benefits of immutable objects?
  • Immutable objects can safely shared between many threads, which is very important for multithreaded programming and to avoid any synchronization issues in Java, Immutability also makes instance thread-safe in Java.
  • Immutable in Java is to allow to cache its hashcode, being immutable Java caches its hashcode, and do not calculate every time we call hashcode, which makes it very fast as hashmap key to be used in hashmap in Java.
  • Immutable object create new memory in heap every time when modified

45 - How to make an object immutable in java?
  • Make the class final.
  • Make all fields private, set them explicitly, in a static block, or in the constructor.
  • No setter method should be there in class.
  • Object must be properly constructed i.e. object reference must not leak during construction process

46 - What are the differences between JDK 1.7 and JDK 1.6?
JDK 1.7
JDK 1.6
JDK1.7 code name is Project Dolphin JDK1.6 code name is project mustang
Enhanced support for dynamically typed languages, Strict type checking verifier, Upgrading of class loader architecture Enhanced Creation of additional classes in java.awt package, Extended support for internationalization, Improvements in javax.swing.
Improve Locale Support for Unicode 6.0 and lot more Introduction to new type of compilers and lot more
Improvements in I/O and Networking, Security, Cryptography, Database Connectivity, Client, Web, and Management. Improvements in I/O and Networking, Security, Cryptography, Database Connectivity, Client, Web, and Management.

47 - What if I write static public void instead of public static void?
Code will compile and run without any error and warning.
48 - What if the static modifier is removed from the signature of the main method?
Program will compile but at runtime it give error Main method is not static in class Demo, please define the main method as: public static void main(String arg[]).
49 - What is the default value of an object reference declared as an instance variable?
All reference type instance variable by default is null.
50 - What is the difference between declaring a variable and defining a variable?
int a; is called declaring variable
a=10; is called defining a variable
51 - What is the main difference between final and abstract?
  • You can't create instance an abstract class whereas you can create instance of final class.
  • Abstract class is inheritable by its subclasses whereas final class is not inherited by any class.
  • You can override abstract method but you can't override final methods.

52 - Do you know Arbitrary Number of Arguments in a method?
Varargs enables use of variable number of arguments in a method call. While invoking the varargs method you can use any number of arguments of type specified and separated by comma.
53 - What is Absolute Path and what is Relative Path?
Relative Path : Its relative to the present directory where you are in.
Absolute Path: Its a path from the root drive which is the starting point of all the paths in that drive.
54 - What is Lazy loading in java?
Lazy loading means that you delaying loading until you need it. Lazy loading is is used by JVM for the classes from the core API. For example you not cache the whole records of database at one single call to JDBC but every time you need record the jdbc hit the database to find and get the specific record ,it is called lazy loading.
55 - Explain coupling and decoupling in java.
Coupling happens when two or more classes depends on each other, suppose you have two classes A and B they A coupled with B when B must change behavior only because A changed. Let's take an example for decoupling, suppose you created an application that uses database through JDBC but now you want to use hibernate, in decoupled application use only need to change DAO(data access object) level code and rest part of application is same. But if application is coupled whole project have to change.
56 - We are able to extends one interface from another but not able to implements. Why?
Implements means implementation , that why one interface extends another not implements. Where interface is meant to declare just to provide interface not for implementation.
57 - What is class loader?
Class Loader loads a java class file into JVM. There are 3 class loaders
  • Bootstrap class loader - It loads java's core classes like java.lang, java.util etc.
  • Extensions class loader - It loads classes from JAVA_HOME/jre/lib/ext folder.
  • System class loader - It loads Java classes that are available in the java classpath.

58 - Why java is not support pointer?
Java does not supports pointers because using pointer the memory area can be directly accessed, by which security can be lost.
59 - How to passing an entire array to one class to another class?
There are several ways as follows
  • Make array as field variable, it could be accessed from another class by class instance.
  • Make array field private and make getter and setter methods to access it from another class.
  • Pass array in class constructor.

60 - Are there any global variables in Java, which can be accessed by other part of your program
public class Demo 
{
	public static int a;
	public static int b;
}
Like this way you can create variables global which can be accessed from anywhere.
61 - Can we have virtual functions in java?
Virtual methods are those methods which can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of OOP. All methods in java are by default virtual.
62 - What is static factory method? What is static import?
Public static factory method , which is simply a static method that returns an instance of the class. Static factory method added in java 1.4.
63 - How many ways to load class in memory?
Two ways to load class
  • Dynamic loading [ Class.forName(String class) ]
  • Static Loading [ MyClass obj = new MyClass() ]