April 06, 2016

#Java: Part 1-Core Java Interview Questions and Answers

< < Part 2-Core Java Interview Questions and Answers (Polymorphism, Overloading and Overriding)

What is Association, Aggregation and Composition?
Association is a relationship between two separate classes which can be of any type say one to one, one to may etc. It joins two entirely separate entities.

Aggregation is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends. In our example if Wallet class is not present, it does not mean that the Money class cannot exist.

Composition is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.

What is the difference between JVM, JRE and JDK in Java programming language?
JVM(Java Virtual Machine) is at heart of Java programming language and provide several feature to Java programmer including Memory Management and Garbage Collection, Security and other system level services. Java Virtual Machine can be customized e.g we can specify starting memory or maximum memory of heap size located inside JVM at the time of JVM creation. If we supplied invalid argument to java command it may refuse to create Java Virtual Machine by saying "failed to create Java virtual machine: invalid argument". In short Java Virtual Machine or JVM is the one who provides Platform independence to Java.

When our Java project builds, it translates the source code (contained in *.java source files) to Java bytecode (*.class files). This bytecode is a collection of compact instructions; easier for a machine to interpret, but is less readable. When we run a Java application on our computer, cellphone, or any other Java-enabled platform, we essentially pass this Java bytecode to the Java Virtual Machine.

The interpreter in the Java Virtual Machine usually starts compiling the entire bytecode at runtime. That means, JVM is responsible for converting byte code into machine specific code and that's why you have different JVM for Windows, Linux or Solaris but one JAR can run on all this operating system.

JRE(Java Runtime Environment) is a set of software tools for development of Java applications. It combines the Java Virtual Machine (JVM), platform core classes and supporting libraries. JRE is part of the Java Development Kit (JDK), but can be downloaded separately. JRE was originally developed by Sun Microsystems Inc., a wholly-owned subsidiary of Oracle Corporation.

JDK(Java Development Kit) contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc… Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method. Just like JRE, JDK is also platform specific and you need to use separate installer for installing JDK on Linux and Windows.

In short the difference is:
1). JRE and JDK come as installer while JVM are bundled with them.
2). JRE only contain environment to execute java program but doesn’t contain other tool for compiling java program.
3). JVM comes along with both JDK and JRE and created when you execute Java program by giving “java” command.

What is Constructors in Java?
Constructor is a block of code that allows you to create an object of class. Constructor looks like a method but unlike methods, constructors don’t have any return type not even void.

Types of Constructors
1). Default constructor: If you do not define any constructor in your class, java generates one for you by default. This constructor is known as default constructor. You would not find it in your source code but it would present there.
2). no-arg constructor: Constructor with no arguments is known as no-arg constructor. The signature is same as default constructor, however body can have any code unlike default constructor where the body does nothing.
3). Parameterized constructor: Constructor with arguments is known as parameterized constructor.
  • Every class has a constructor whether it’s normal one or an abstract class (but we cannot initialize an abstract class).
  • Constructor overloading is possible but overriding is not possible.  
  • A constructor can also invoke another constructor of the same class – By using this(). If you wanna invoke a arg-constructor then give something like: this(parameter list).
What is Constructor Chaining?
Constructor chaining is nothing but a scenario where in one constructor calls the constructor of its super class implicitly or explicitly. Suppose there is a class which inherits another class, in this case if you create the object of child class then first super class(or parent class) constructor will be invoked and then child class constructor will be invoked.

What is difference between Path and Classpath?
Path and Classpath are operating system level environment variables. Path is used to define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

What are the different access modifiers in Java?
Java has four access modifier:

private
  • private access modifier is accessible only within class. It can be applied to member field, method or nested class in Java.
  • Top level class can not be declared as private.
  • private is the highest form of Encapsulation Java API provides and should be used as much as possible.
  • If a method is declared as private, then it cannot be overridden.
  • private keyword can also be applied to the constructor and if you make constructor private you prevent it from being sub-classed.
protected
  • The protected access modifier is accessible within package and outside the package but through inheritance only.
  • The protected access modifier can be applied on the data member, method and constructor and can't be applied on the class.
public
  • public access modifier is accessible everywhere.
  • It has the widest scope among all other modifiers.
default
  • It is provided by Java if no access modifier is specified.
  • Package or default access level is second highest restrictive access modifier after private.
  • Any variable, method or class declared is only accessible on the package it belongs.
What is final keyword in Java?
  • final is a special keyword in Java that is used as a non-access modifier. 
  • final variable: If the final is used with a variable, then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.
  • final method: It can’t be overridden by the inheriting class.
  • final class: it can’t be extended by any subclass class but it can extend other class.
Can you change the contents of a final array?
final int[] array = new int[10];
array[0] = 0;

Yes, we can actually change the contents of the array even though it is marked as final. The array variable points to a particular start location in the memory where the contents of the array are placed. The location or the memory address can’t be changed. If you execute below LOC's you will get compilation error:

final int[] array = new int [10]
array = new int[5];
Explain public static void main(String args[])?
  • public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
  • static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.
  • void : It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[] : It is the parameter passed to the main methods.
Define a Java Class.
A Java class is a blueprint which includes all your data. It contains fields (variables) and methods to describe the behavior of an object. e.g:

class Student {
   //class variables and methods
}

What is an object in Java?
  • An object is a real-world entity that has a state and behavior. An object has three characteristics: state, behavior and identity.
  • An object is created using the 'new' keyword. Student stu = new Student();
What is Object Oriented Programming?
Object-oriented programming or popularly known as OOPs is a programming model or approach where the programs are organized around objects rather than logic and functions. 

Why java is not 100% Object-oriented?
Java is not 100% Object-oriented because it makes use of eight primitive datatypes such as boolean, byte, char, int, float, double, long, short which are not objects.

Why Java is platform independent?
Because of byte codes in Java can run on any system irrespective of its underlying operating system.

What is a package in Java? What are the advantages of packages?
Packages are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. The code within the packages can be imported by other classes and reused.

Advantages:
  • Packages help in avoiding name clashes.
  • They provide easier access control on the code.
  • Packages can also contain hidden classes which are not visible to the outer classes and only used within the package.
  • Creates a proper hierarchical structure which makes it easier to locate the related classes.
What is the difference between a local variable and an instance variable?
  • A variable which is used inside a method, constructor, or a block is called local variable because it has local scope. This variable can be used only within the scope of a block.
  • A variable which is bounded to its object itself is called an instance variable. These variables are declared within a class, but outside a method. Every object of that class will create it’s own copy of instance variable while using it. That's why if any changes are made to the variable it won’t reflect in any other instances of that class and will be bound to that particular instance only.
How can you pass multiple arguments to a method on each invocation call?
To pass multiple arguments to a method, we need to use varargs feature. In the below example, we are passing multiple arguments of the same type to printNames() method.

public static void printNames(String... names) {
     for (int i = 0; i < names.length; i++)
          System.out.println(names[i]);
}

The above method can be invoked by:
printNames();
printNames("Tiger", "Himaanshu");
printNames("Tiger", "Himaanshu", "Andhrew");
printNames("Tiger", "Himaanshu", "Andhrew", "Michael");

The type name is followed by three dots, a space, and then the name of variable.
  • The varargs variable is treated like an array.
  • The varargs variable must appear at the last in the method signature.
  • There can only be a single varargs in a method signature.

ALSO CHECKCore Java Interview Questions And Answers

-K Himaanshu Shuklaa..

No comments:

Post a Comment