Programming





Interview Question and Answers Part 4

Interview Question and Answers Part 4


by Administrator | 31-Mar-2020
Java

Tags:  Interview  Question And Answer  Difference

Share: 


  1. What is the difference between Procedural and Object-Oriented Programming language?

In procedural programming, the instructions are executed one after another and the data is exposed to the whole program. In OOPs programming, the unit of program is an object which is nothing but a combination of data and code and the data is not exposed outside the object.

  1. Can you create a class as static?

Yes, you can have an inner class as static.

  1. Can a public class MyClass be defined in a source file named YourClass.java?

No the source file name, if it contains a public class, must be the same as the public class name itself with a .java extension.

  1. What modifiers may be used with top-level class?

public, abstract and final can be used for top-level class.

  1. What happens to a static variable that is defined within a method of a class?

It’ll throw a compilation error.

  1. Name some methods every Java class will have.

All the methods from Object class are available to all the classes. These are the methods:

  1. public String toString();
  2. public Object clone();
  3. public boolean equals();
  4. public int hashCode();
  5. What is a singleton class?

A singleton is an object that cannot be instantiated. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM)- by preventing direct instantiation we can ensure that developers don't create a second copy. You can achieve this by having a private constructor in the class.

public class Singleton { 
    private static final Singleton s = new Singleton(); 
    private Singleton() {} 
    public static Singleton getInstance() { 
        return s; 
    }
}  
  1. How can we clone the singleton object?

Not possible. But if we try to clone it will not throw an Exception. Both references will point to the same object.

  1. What are the different ways to create an object in Java?

There are many ways to create an object in java. They are:

  1. by new keyword
  2. by newInstance() method
  3. By clone() method
  4. By factory method 
  1. What is the difference between a class and an object?

A class is a blueprint that describes the details of an object. A class has a state (instance variables) and behavior (methods).  

An object is an instance of a class. You can have n number of objects of one class. An object represents to things state and behavior. 

  1. What causes the memory leak in java?

The following things cause a memory leak in Java:

  1. static variable
  2. thread-local variable
  3. Poorly implemented data structures

12. How many objects are created in the following piece of code?

MyClass c1, c2, c3;

c1 = new MyClass ();

c3 = new MyClass ();

Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized. 

  1. Can you instantiate the Math class?

You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public. 

  1. How to make an object immutable in java?
    1. Make the class as final.
    2. Mark all the fields as private, set them explicitly, in a static block, or in the constructor.
    3. No setter method should be provided.
    4. Object must be properly constructed i.e. object reference must not leak during the construction process.
  2. What are the benefits of immutable objects?

Immutable objects can safely be 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 a new memory in heap every time when modified

  1. What are Encapsulation, Inheritance, and Polymorphism?

Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.

Inheritance is the process by which one object acquires the properties of another object.

Polymorphism is the feature that allows one interface to be used for general class actions.

  1. Give an example of a final class.

Math class is a final class and cannot be extended

  1. When can an object reference be cast to an interface reference?

An object reference is cast to an interface reference when the object implements the referenced interface.

  1. What is mean by garbage collection?

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.

  1. What are methods and how are they defined?

Methods are functions that operate on instances of classes in which they are defined. Objects can communicate with each other using methods and can call methods in other classes. Method definition has four parts. They are the name of the method, type of object or primitive type the method returns, a list of parameters and the body of the method. A method's signature is a combination of the first three parts mentioned above.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.