Core Java - OOPs Concepts: Initial OOPs Interview Questions

23) 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.

24) What is an object?

The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.


25) What is the difference between an object-oriented programming language and object-based programming language?

There are the following basic differences between the object-oriented language and object-based language.

  • Object-oriented languages follow all the concepts of OOPs whereas, the object-based language doesn't follow all the concepts of OOPs like inheritance and polymorphism.
  • Object-oriented languages do not have the inbuilt objects whereas Object-based languages have the inbuilt objects, for example, JavaScript has window object.
  • Examples of object-oriented programming are Java, C#, Smalltalk, etc. whereas the examples of object-based languages are JavaScript, VBScript, etc.

26) What will be the initial value of an object reference which is defined as an instance variable?

All object references are initialized to null in Java.


Core Java - OOPs Concepts: Constructor Interview Questions


27) What is the constructor?

The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.

More Details.


28) How many types of constructors are used in Java?

Based on the parameters passed in the constructors, there are two types of constructors in Java.

  • Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.
  • Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values. In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.


29) What is the purpose of a default constructor?

The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.

  1. class Student3{  
  2. int id;  
  3. String name;  
  4.   
  5. void display(){System.out.println(id+" "+name);}  
  6.   
  7. public static void main(String args[]){  
  8. Student3 s1=new Student3();  
  9. Student3 s2=new Student3();  
  10. s1.display();  
  11. s2.display();  
  12. }  
  13. }  

Test it Now

Output:

0 null
0 null

Explanation: In the above class, you are not creating any constructor, so compiler provides you a default constructor. Here 0 and null values are provided by default constructor.


More Details.


30) Does constructor return any value?

Ans: yes, The constructor implicitly returns the current instance of the class (You can't use an explicit return type with the constructor). More Details.


31)Is constructor inherited?

No, The constructor is not inherited.


32) Can you make a constructor final?

No, the constructor can't be final.


33) Can we overload the constructors?

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. Consider the following example.

  1. class Test   
  2. {  
  3.     int i;   
  4.     public Test(int k)  
  5.     {  
  6.         i=k;  
  7.     }  
  8.     public Test(int k, int m)  
  9.     {  
  10.         System.out.println("Hi I am assigning the value max(k, m) to i");  
  11.         if(k>m)  
  12.         {  
  13.             i=k;   
  14.         }  
  15.         else   
  16.         {  
  17.             i=m;  
  18.         }  
  19.     }  
  20. }  
  21. public class Main   
  22. {  
  23.     public static void main (String args[])   
  24.     {  
  25.         Test test1 = new Test(10);  
  26.         Test test2 = new Test(12, 15);  
  27.         System.out.println(test1.i);  
  28.         System.out.println(test2.i);  
  29.     }  
  30. }  
  31.       

In the above program, The constructor Test is overloaded with another constructor. In the first call to the constructor, The constructor with one argument is called, and i will be initialized with the value 10. However, In the second call to the constructor, The constructor with the 2 arguments is called, and i will be initialized with the value 15.


34) What do you understand by copy constructor in Java?

There is no copy constructor in java. However, we can copy the values from one object to another like copy constructor in C++.

There are many ways to copy the values of one object into another in java. They are:

  • By constructor
  • By assigning the values of one object into another
  • By clone() method of Object class

In this example, we are going to copy the values of one object into another using java constructor.

  1. //Java program to initialize the values from one object to another  
  2. class Student6{  
  3.     int id;  
  4.     String name;  
  5.     //constructor to initialize integer and string  
  6.     Student6(int i,String n){  
  7.     id = i;  
  8.     name = n;  
  9.     }  
  10.     //constructor to initialize another object  
  11.     Student6(Student6 s){  
  12.     id = s.id;  
  13.     name =s.name;  
  14.     }  
  15.     void display(){System.out.println(id+" "+name);}  
  16.    
  17.     public static void main(String args[]){  
  18.     Student6 s1 = new Student6(111,"Karan");  
  19.     Student6 s2 = new Student6(s1);  
  20.     s1.display();  
  21.     s2.display();  
  22.    }  
  23. }  

Test it Now

Output:

111 Karan
111 Karan

35) What are the differences between the constructors and methods?

There are many differences between constructors and methods. They are given below.

Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to expose the behavior of an object.
A constructor must not have a return type.A method must have a return type.
The constructor is invoked implicitly.The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class.The method is not provided by the compiler in any case.
The constructor name must be same as the class name.The method name may or may not be same as class name.


36) What is the output of the following Java program?

  1. public class Test   
  2. {  
  3.     Test(int a, int b)  
  4.     {  
  5.         System.out.println("a = "+a+" b = "+b);  
  6.     }  
  7.     Test(int a, float b)  
  8.     {  
  9.         System.out.println("a = "+a+" b = "+b);  
  10.     }  
  11.     public static void main (String args[])  
  12.     {  
  13.         byte a = 10;   
  14.         byte b = 15;  
  15.         Test test = new Test(a,b);  
  16.     }  
  17. }  

The output of the following program is:

a = 10 b = 15

Here, the data type of the variables a and b, i.e., byte gets promoted to int, and the first parameterized constructor with the two integer parameters is called.


37) What is the output of the following Java program?

  1. class Test   
  2. {  
  3.     int i;   
  4. }  
  5. public class Main   
  6. {  
  7.     public static void main (String args[])   
  8.     {  
  9.         Test test = new Test();   
  10.         System.out.println(test.i);  
  11.     }  
  12. }  

The output of the program is 0 because the variable i is initialized to 0 internally. As we know that a default constructor is invoked implicitly if there is no constructor in the class, the variable i is initialized to 0 since there is no constructor in the class.


38) What is the output of the following Java program?

  1. class Test   
  2. {  
  3.     int test_a, test_b;  
  4.     Test(int a, int b)   
  5.     {  
  6.     test_a = a;   
  7.     test_b = b;   
  8.     }  
  9.     public static void main (String args[])   
  10.     {  
  11.         Test test = new Test();   
  12.         System.out.println(test.test_a+" "+test.test_b);  
  13.     }  
  14. }  

There is a compiler error in the program because there is a call to the default constructor in the main method which is not present in the class. However, there is only one parameterized constructor in the class Test. Therefore, no default constructor is invoked by the constructor implicitly.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值