exampracticeonline.com
Java basic interview

What is Java?

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

What is difference between JDK,JRE and JVM?

  • JVM stands for Java Virtual Machine which provides runtime environment for Java Byte Codes to be executed.
  • JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime.
  • JDK (Java Development Kit) consists of JRE along with the development tools required to write and execute a program.

What are the supported platforms by Java Programming Language?

Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

What are major features of Java?

  • Object-Oriented – java is based on object-oriented programming where the class and methods describe about the state and behavior of object.
  • Portable – Java program gets converted into Java Byte Codes that can be executed on any platform without any dependency.
  • Platform independent – java works on “write once and run anywhere” as it supports multiple platforms like Windows, Linux, Mac, Sun Solaris, etc.
  • Robust – Java has a strong memory management as there is no pointer allocations. It has automatic garbage collection that prohibits memory leaks.
  • Interpreted – java compiler converts the codes into Java Byte Codes which are then interpreted and executed by Java Interpreter.

How many types of memory areas are allocated by JVM?

  1. Class(Method) Area
  2. Heap
  3. Stack
  4. Program Counter

What is access modifier in Java?

In Java, access modifiers are the keywords used before a class/method name which defines the access scope.

What are the various access modifiers for Java classes?

1. Public : Class,Method,Field is accessible from anywhere.

2. Protected:Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.

3. Default: Method,Field,class can be accessed only from the same package and not from outside of it's native package.

4. Private: Method,Field can be accessed from the same class to which they belong.

Why Java is platform independent?

Java is platform independent means java can “write once run anywhere”. Java is called platform independent because of its byte codes which can run on any operating system.

Is java 100% Object-oriented?

No,  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.

What do you mean by Constructor?

Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

  1. Default constructor
  2. Parameterized constructor

Example:

package com.db;

public class JavaConstructor {

    public JavaConstructor() {  //default constructor
        System.out.println("Default constructor");
    }
    
    public JavaConstructor(int arg) {  //parametrized constructor
        System.out.println("Paramaterized constructor");
    }
    
    public static void main(String[] args) {
        
        JavaConstructor obj1=new JavaConstructor();
        
        JavaConstructor obj2=new JavaConstructor(5);
        
    }

}


Output:

Default constructor
Paramaterized constructor

What happen if we have only parametrized constructor in Java?

Program works fine without default constructor and there is no exception but we can create Object by using paramaterize constructor only. We can't use default constructor of Java class.

If we define parametrize constructor in our class then we have to provide default constructor as well. JVM will not provide default constructor.


Example:

package com.db;

public class JavaConstructor {

    public JavaConstructor(int arg) { // parametrized constructor
        System.out.println("Paramaterized constructor");
    }
    
    public static void main(String[] args) {
        
        // JavaConstructor obj1=new JavaConstructor(); // It will throw compile error without default constructor
        
        JavaConstructor obj2=new JavaConstructor(5);
        
    }

}


Output:

Paramaterized constructor