Udemy REST Assured free

Java Classes Objects and Methods Interview Questions

Showing 1 - 10 of 58 results

What is object-oriented paradigm?

It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.

  • Follows the bottom-up approach in program design.
  • Focus on data with methods to operate upon the object's data
  • Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
  • Implements the real-time approach like inheritance, abstraction, etc.
  • The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.

What is the purpose of static methods and variables?

The methods or variables defined as static are shared among all the objects of the class. The static is the part of the class and not of the object. The static variables are stored in the class area, and we do not need to create the object to access such variables. Therefore, static is used in the case, where we need to define variables or methods which are common to all the objects of the class.

For example, In the class simulating the collection of the students in a college, the name of the college is the common attribute to all the students. Therefore, the college name will be defined as static.

What is a constructor overloading in Java?

In Java, constructor overloading is a technique of adding any number of constructors to a class each having a different parameter list. The compiler uses the number of parameters and their types in the list to differentiate the overloaded constructors.

class Demo
{
    int i;

    public Demo(int a)
    {
        i = k;
    }
    
    public Demo(int a, int b)
    {
        //body
    }
}

In above example, Demo() constructor is overloaded.

What is a copy constructor in Java?

Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.

What is object cloning in Java?

Object cloning in Java is the process of creating an exact copy of an object. It basically means the ability to create an object with a similar state as the original object. To achieve this, Java provides a method clone() to make use of this functionality.

This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone() is a protected method, thus you need to override it.

What will be the output of below Java code?

Example Code: 

public class A
{
    public static void main(String[] args)
    {
        String x = null;
        m2(x);
        
        System.out.println(x);
    }
    static void m2(String s)
    {
        s = "Vinay";
    }
}

Output: null

How many ways you can create an Object in Java?

There are the five ways to create the object or get the object in java:

1. By using the new operator

Test t = new Test();

2.  By using the newInstance() Method

Test t = (Test)class.forName("Test").newInstance();

3. By using the factory Method

Runtime r = Runtime.getRuntime();
DateFormat d = Dateformat.getInstance();

4. By using the clone() Method

Test t = new Test();
Test t1= (Test)t.clone();

5. Bu using the deserialization

FileInputStream fis = new FileInputStream("Vinay");
ObjectInputStream obj = new ObjectInputStream(fis);
Dog d = (Dog)obj.readObject();

Not:- There is another way to create the object in Java but this is not standard, it is applicable only for the String

String s = "Vinay";

What will be the output of below Java code?

Example Code: 

public class Test
{
    int i =10;

    public static void main(String[] args)
    {
        System.out.println(i);
    }
}

Output: Error, Cannot make a static reference to the non-static field i.

What will be the output of below Java code?

Example Code: 

public class A
{
    int i = 999;
    System.out.println(i);
}

public class B extends A
{
    int i = 777;
    System.out.println(i);
}

public class C extends B
{
    int i = 888;
    System.out.println(i);
}

public class Test
{
    public void main(String args[])
    {
        C c = new C();
        c.i;               //Line :- 1
       ((B)c).i;        //Line :-2
       ((A)(B)c).i;   //Line :- 3
    }
}

Output

Line 1:- 888
Line 2:- 777
Line 3:- 999

Subscribe to Java Classes Objects and Methods Interview Questions