preloader

All theoretical questions from Basic Java are covered with answers

Basic Java Interview

  • COURSES

    03 Month

  • DURATION

    03 Hours

Is Java Platform independent?

Yes java is platform independent we can write code in one platform and run any platform present.

Is Java Is pure Object oriented?

No java is not purely object oriented, it supports primitive data types also int, short, char where as in object oriented every thing will be happen through object only.

Difference between JDK,JRE and JVM?

JDK JRE JVM
JDK is platform dependent JRE is platform dependent JVM is platform independent
JRE + Development tool JVM + libraries to run the application JVM only runtime environment for executing the JAVA byte code
JDK is primary used for code execution and has primary functionality of development JRE is majorly responsible for creating environment for the code execution JVM on the other hand specifies all the implements and responsible to provide implementation to JRE

What is the ClassLoader in java?

Class Loader is an abstract class. it belongsto Java.lang package.

It defined as process of loading the class file from secondary storage into JVM memory.

  • BootStrap ClassLoader
  • Extension ClassLoader
  • System ClassLoader

BootStrap ClassLoader :

  • it loads standard JDK class files from rt.jar and other core class.
  • Parent of all class loader
  • When we call String.class Classloader() it return nulls.

Extension ClassLoader:

  • it deligates class loader request to its parent
  • it loads classes from jre/lib/ext.

System ClassLoader :

  • Load application from class path variable.it is child extension of ClassLoader.

Explain Static Keyword in java ?

Static is class level keyword which always loaded with class loading time do not wait for object creation and initialization.it is used with variable, method and block.

  • Static Variable: –
    when any variable is defined as static, it will be accessible anywhere in the class without reference of object, also loaded along with class.

    Static int I =10;
    Static String str=”name”;

  • Static method: –
    when any method is static, it will be directly accessible to other static method, loaded at the time of class loading itself only.

    Public static void Method() {}
    //code
    Main() {}
    method()// direct access no need to define along with object reference

  • Static block:-
    it is block which directly loaded before static method ,i.e loaded before main method but after java 7 main method is mandatory.

Final keyword in Java ?

Final is a restriction keyword which restrict the access of data to the user

  • Final variable :
    when we use final with variable ,it can not be reinitialized that means once value assigned it can not be changed furter
    ex; final int id =10;
  • Final Class :
    when we use final with class it can not be extended further that means can not be reused
    final class test{ }
  • Final method :
    Final keyword when used with any method that method can not be overridden further.
    final method(){ }

Explain Super keyword in java?

Super keyword inherit the property from parent class to child class using super keyword

  • When we use super.id in child class it will call parent class variable property to child class.
  • when we use only super() in child constructor it will call parent constructor property in child constructor.
  • when we use super.methodname in child method then parent class method property will be called .

Why main method is Static by nature?

Main() method is static by nature because it is loaded once class loaded as it is class level and not need object initialization to call the method ,which will save the memory and execute the code faster.

Why String is immutable in java?

String is immutable by nature, it stores the value in string pool, when new value store in string pool it will check the object refence, if the value is same then assign the same object reference. if string is non immutable so value will be changing every time which cause the problem for security aspect and also class loading.

Difference between String ,StringBuilder and StringBuffer?

String StringBuilder StringBuffer
String is Immutable StringBuilder is mutable StringBuffer is mutable
String stores its value in String Pool which is part of heap memory StringBuilder stores in heap memory StringBuffer stores its value in heap memory
String is non threadsafe and synchronized StringBuilder is good for single threaded application StringBuffer is good for multithreaded application
String is slower in comparision to StringBuilder StringBuilder is faster in execution StringBuffer is slower in execution in comparision to StringBuilder

Difference between “==” and .equals method?

.equals method compare the value or content of String and “==” compare the the object reference of the string object.

String s1 = "abc";
          String s2 = "abc";
          String s3 = new String("abc");
          if (s1 == s2) {
              return true;
          } else if (s1.equals(s2)) {
              return true;
          } else if (s1 == s3) {
              return false;
          } else if (s1.equals(s3)) {
              return true;
          }

Difference between Method and Constructor

Constructor Method
Constructor can not have return type Method must have return type
Constructor used by JVM to initialize the object Method used for specific purpose
Constructor name must be same as class name Method name may or may not be same as class name
If no constructor created JVM create default constructor with no parameter If method is not created JVM not create default constructor

what is constructor chaining?

Constructor chaining is calling one constructor from other constructor.

Can a Constructor be private?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class.We can use this private constructor in the Singleton Design Pattern

Conditions for Private Constructor

  • A private constructor does not allow a class to be sub classed.
  • A private constructor does not allow to create an object outside the class.
  • If all the constant methods are there in our class we can use a private constructor.
  • If all the methods are static then we can use a private constructor.
  • If we try to extend a class which is having private constructor compile time error will occur.

What are the access modifier in java?

Access
Modifier
Within
class
Within
package
Outside
package
Outside
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y

What are wrapper class in java?

Java support primitive datatypes. The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

What do you mean by Volatile and transient keyword?

Volatile Transient
Volatile keyword is used to make class thread safe like synchronization Transient keyword is used to prevent variable from serialization
Volatile keyword used during Multithreading Transient Keyword is used during Synchronization
implements changes directly in main method exclude variable during synchronization
Volatile are not initialized with a default value Transient variables are initialized with a default value
used with a static variable used with the static keyword
used with the final keyword used with the final keyword

What is Serialization in java? how to make any class serializable in java

Serialization in Java is a mechanism of writing the state of an object into a byte-stream. For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. Serializable is a marker interface (has no data member and method). It is used to “mark” Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces

How to make any class immutable in java?

To make any immutable we have to perform following steps:

  • Define the class final so that none of class extend its property.
  • Define the variable as private so that no further initialization.
  • Define all mutable field private
  • Do not define setter method only define getter method no one set new value.
  • All the initialization through constructor
  • Perform the object cloning.

What is object cloning in java ?

The object cloning is a way to create exact copy of an object. The clone() method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don’t implement Cloneable interface, clone() method generates CloneNotSupportedException.

protected Object clone() throws CloneNotSupportedException

Example :

class Student18 implements Cloneable {
          int rollno;
          String name;
      
          Student18(int rollno, String name) {
              this.rollno = rollno;
              this.name = name;
          }
      
          public Object clone() throws CloneNotSupportedException {
              return super.clone();
          }
      
          public static void main(String args[]) {
              try {
                  Student18 s1 = new Student18(101, "amit");
      
                  Student18 s2 = (Student18) s1.clone();
      
                  System.out.println(s1.rollno + " " + s1.name);
                  System.out.println(s2.rollno + " " + s2.name);
      
              } catch (CloneNotSupportedException c) {}
      
          }
      }

Define marker interface and its example?

Marker interface is an interface that does not contain methods, fields, and constants is known as marker interface.
Marker interface is used by JVM and compiler have additional information about an object. The Serializable and Cloneable interfaces are the example of marker interface. In short, it indicates a signal or command to the JVM.

public interface Serializable
{
//marker interface
}

Related Course

course thumb

OOP Concept

Inheritance,Polimorphism,Abstraction and Encapsulation

Visit
course thumb

Exception handling

Exception handling ,it type ,error vs Exception

Visit
course thumb

All Topics Questions

This section we will cover all topic wise java question

Visit