hereral/Clara-Training-Data huggingface dataset:
Question: Can you provide a list of Java Circular Linked List Programs?
Answer: 1) Java program to create and display a Circular Linked List
2) Java program to create a Circular Linked List of N nodes and count the number of nodes
3) Java program to create a Circular Linked List of n nodes and display it in reverse order
4) Java program to delete a node from the beginning of the Circular Linked List
5) Java program to delete a node from the end of the Circular Linked List
6) Java program to delete a node from the middle of the Circular Linked List
7) Java program to find the maximum and minimum value node from a circular linked list
8) Java program to insert a new node at the beginning of the Circular Linked List
9) Java program to insert a new node at the end of the Circular Linked List
10) Java program to insert a new node at the middle of the Circular Linked List
11) Java program to remove duplicate elements from a Circular Linked List
12) Java program to search an element in a Circular Linked List
13) Java program to sort the elements of the Circular Linked List
Question: Can you provide a list of Java Doubly Linked List Programs?
Answer: 1) Java program to convert a given binary tree to doubly linked list
2) Java program to create a doubly linked list from a ternary tree
3) Java program to create a doubly linked list of n nodes and count the number of nodes
4) Java program to create a doubly linked list of n nodes and display it in reverse order
5) Java program to create and display a doubly linked list
6) Java program to delete a new node from the beginning of the doubly linked list
7) Java program to delete a new node from the end of the doubly linked list
8) Java program to delete a new node from the middle of the doubly linked list
9) Java program to find the maximum and minimum value node from a doubly linked list
10) Java program to insert a new node at the beginning of the Doubly Linked list
10) Java program to insert a new node at the end of the Doubly Linked List
12) Java program to insert a new node at the middle of the Doubly Linked List
13) Java program to remove duplicate elements from a Doubly Linked List
14) Java program to rotate doubly linked list by N nodes
15) Java program to search an element in a doubly linked list
16) Java program to sort the elements of the doubly linked list
Question: Can you provide a list of Java Tree Programs?
Answer: 1) Java Program to calculate the Difference between the Sum of the Odd Level and the Even Level Nodes of a Binary Tree
2) Java program to construct a Binary Search Tree and perform deletion and In-order traversal
3) Java program to convert Binary Tree to Binary Search Tree
4) Java program to determine whether all leaves are at same level
5) Java program to determine whether two trees are identical
6) Java program to find maximum width of a binary tree
7) Java program to find the largest element in a Binary Tree
8) Java program to find the maximum depth or height of a tree
9) Java program to find the nodes which are at the maximum distance in a Binary Tree
10) Java program to find the smallest element in a tree
11) Java program to find the sum of all the nodes of a binary tree
12) Java program to find the total number of possible Binary Search Trees with N keys
13) Java program to implement Binary Tree using the Linked List
14) Java program to search a node in a Binary Tree
Question: What is the difference between an object-oriented programming language and object-based programming language?
Answer: Object-based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object-based programming languages.
Question: What is Java Naming Convention?
Answer: Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc.

But, it is not forced to follow. So, it is known as convention not rule. These conventions are suggested by several Java communities such as Sun Microsystems and Netscape.

All the classes, interfaces, packages, methods and fields of Java programming language are given according to the Java naming convention. If you fail to follow these conventions, it may generate confusion or erroneous code.
Question: What is the Advantage of Naming Conventions in Java?
Answer: By using standard Java naming conventions, you make your code easier to read for yourself and other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
Question: What are the Naming Conventions of the Different Identifiers?
Answer: Naming Conventions of the Different Identifiers

The following table shows the popular conventions used for the different identifiers.

Identifiers Type: Class
Naming Rules: It should start with the uppercase letter.
It should be a noun such as Color, Button, System, Thread, etc.
Use appropriate words, instead of acronyms.
Examples: public class Employee
{
//code snippet
}


Identifiers Type: Interface
Naming Rules: It should start with the uppercase letter.
It should be an adjective such as Runnable, Remote, ActionListener.
Use appropriate words, instead of acronyms.
Examples: interface Printable
{
//code snippet
}


Identifiers Type: Method
Naming Rules: It should start with lowercase letter.
It should be a verb such as main(), print(), println().
If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as actionPerformed().
Examples: class Employee
{
// method
void draw()
{
//code snippet
}
}


Identifiers Type: Variable
Naming Rules: It should start with a lowercase letter such as id, name.
It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as firstName, lastName.
Avoid using one-character variables such as x, y, z.
Examples: class Employee
{
// variable
int id;
//code snippet
}


Identifiers Type: Package
Naming Rules: It should be a lowercase letter such as java, lang.
If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.
Examples: //package
package com.javatpoint;
class Employee
{
//code snippet
}

Identifiers Type: Constant
Naming Rules: It should be in uppercase letters such as RED, YELLOW.
If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY.
It may contain digits but not as the first letter.
Examples: class Employee
{
//constant
static final int MIN_AGE = 18;
//code snippet
}
Question: What is CamelCase in Java naming conventions?
Answer: Java follows camel-case syntax for naming the class, interface, method, and variable.

If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.
Question: What is Objects and Classes in Java?
Answer: In this page, we will learn about Java objects and classes. In object-oriented programming technique, we design a program using objects and classes.

An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only.
Question: What is an object in Java?
Answer: An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
An object has three characteristics:
-State: represents the data (value) of an object.
-Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
-Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
Question: What is a class in Java?
Answer: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
-Fields
-Methods
-Constructors
-Blocks
-Nested class and interface
Question: What is Instance variable in Java?
Answer: A variable which is created inside the class but outside the method is known as an instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when an object or instance is created. That is why it is known as an instance variable.
Question: What is Method in Java?
Answer: In Java, a method is like a function which is used to expose the behavior of an object.

Advantage of Method
-Code Reusability
-Code Optimization
Question: What is new keyword in Java?
Answer: The new keyword is used to allocate memory at runtime. All objects get memory in Heap memory area.
Question: Can you provide an example of Object and Class Example: main within the class in Java?
Answer: In this example, we have created a Student class which has two data members id and name. We are creating the object of the Student class by new keyword and printing the object's value.

Here, we are creating a main() method inside the class.
//Java Program to illustrate how to define a class and fields  
//Defining a Student class.  
class Student{  
 //defining fields  
 int id;//field or data member or instance variable  
 String name;  
 //creating main method inside the Student class  
 public static void main(String args[]){  
  //Creating an object or instance  
  Student s1=new Student();//creating an object of Student  
  //Printing values of the object  
  System.out.println(s1.id);//accessing member through reference variable  
  System.out.println(s1.name);  
 }  
}  
Output:

0 
null
Question: Can you provide an example of Object and Class Example: main outside the class in Java?
Answer: In real time development, we create classes and use it from another class. It is a better approach than previous one. Let's see a simple example, where we are having main() method in another class.

We can have multiple classes in different Java files or single Java file. If you define multiple classes in a single Java source file, it is a good idea to save the file name with the class name which has main() method.

//Java Program to demonstrate having the main method in   
//another class  
//Creating Student class.  
class Student{  
 int id;  
 String name;  
}  
//Creating another class TestStudent1 which contains the main method  
class TestStudent1{  
 public static void main(String args[]){  
  Student s1=new Student();  
  System.out.println(s1.id);  
  System.out.println(s1.name);  
 }  
}  
Output:
0 
null
Question: What are the 3 Ways to initialize object?
Answer: There are 3 ways to initialize object in Java.

-By reference variable
-By method
-By constructor
Question: Can you provide an example of Object and Class Example: Initialization through reference in Java?
Answer: 1) Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Let's see a simple example where we are going to initialize the object through a reference variable.
class Student{  
 int id;  
 String name;  
}  
class TestStudent2{  
 public static void main(String args[]){  
  Student s1=new Student();  
  s1.id=101;  
  s1.name="Sonoo";  
  System.out.println(s1.id+" "+s1.name);//printing members with a white space  
 }  
}  
Output:

101 Sonoo
Question: Can you provide an example of Object and Class Example: Initialization through method in Java?
Answer: 2) Object and Class Example: Initialization through method
In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by invoking the displayInformation() method.
class Student{  
 int rollno;  
 String name;  
 void insertRecord(int r, String n){  
  rollno=r;  
  name=n;  
 }  
 void displayInformation(){System.out.println(rollno+" "+name);}  
}  
class TestStudent4{  
 public static void main(String args[]){  
  Student s1=new Student();  
  Student s2=new Student();  
  s1.insertRecord(111,"Karan");  
  s2.insertRecord(222,"Aryan");  
  s1.displayInformation();  
  s2.displayInformation();  
 }  
}  
Output:

111 Karan
222 Aryan
Question: Can you provide an example of Object and Class Initialization through a constructor in Java?
Answer: We will learn about constructors in Java later.

Let's see an example where we are maintaining records of employees.
class Employee{  
    int id;  
    String name;  
    float salary;  
    void insert(int i, String n, float s) {  
        id=i;  
        name=n;  
        salary=s;  
    }  
    void display(){System.out.println(id+" "+name+" "+salary);}  
}  
public class TestEmployee {  
public static void main(String[] args) {  
    Employee e1=new Employee();  
    Employee e2=new Employee();  
    Employee e3=new Employee();  
    e1.insert(101,"ajeet",45000);  
    e2.insert(102,"irfan",25000);  
    e3.insert(103,"nakul",55000);  
    e1.display();  
    e2.display();  
    e3.display();  
}  
}  
Output:

101 ajeet 45000.0
102 irfan 25000.0
103 nakul 55000.0
Question: What are the different ways to create an object in Java?
Answer: There are many ways to create an object in java. They are:

-By new keyword
-By newInstance() method
-By clone() method
-By deserialization
-By factory method etc.
Question: What is Anonymous object?
Answer: Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only.
If you have to use an object only once, an anonymous object is a good approach.

 For example:

new Calculation();//anonymous object 
 
Calling method through a reference:

Calculation c=new Calculation();  
c.fact(5);  

Calling method through an anonymous object

new Calculation().fact(5); 
 
Let's see the full example of an anonymous object in Java.

class Calculation{  
 void fact(int  n){  
  int fact=1;  
  for(int i=1;i<=n;i++){  
   fact=fact*i;  
  }  
 System.out.println("factorial is "+fact);  
}  
public static void main(String args[]){  
 new Calculation().fact(5);//calling method with anonymous object  
}  
}  

Output:
Factorial is 120
Question: How to create multiple objects by one type only in Java?
Answer: We can create multiple objects by one type only as we do in case of primitives.

Initialization of primitive variables:

int a=10, b=20;  

Initialization of refernce variables:

Rectangle r1=new Rectangle(), r2=new Rectangle();//creating two objects 

Let's see the example:

//Java Program to illustrate the use of Rectangle class which  
//has length and width data members  
class Rectangle{  
 int length;  
 int width;  
 void insert(int l,int w){  
  length=l;  
  width=w;  
 }  
 void calculateArea(){System.out.println(length*width);}  
}  
class TestRectangle2{  
 public static void main(String args[]){  
  Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects  
  r1.insert(11,5);  
  r2.insert(3,15);  
  r1.calculateArea();  
  r2.calculateArea();  
}  
}  


Output:

55 
45
Question: What is a method in Java?
Answer: A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. It is used to achieve the reusability of code. We write a method once and use it many times. We do not require to write code again and again. It also provides the easy modification and readability of code, just by adding or removing a chunk of code. The method is executed only when we call or invoke it.

The most important method in Java is the main() method.
Question: What is Method Declaration?
Answer: The method declaration provides information about method attributes, such as visibility, return-type, name, and arguments. It has six components that are known as method header.

Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method name and parameter list.

Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of the method. Java provides four types of access specifier:

-Public: The method is accessible by all classes when we use public specifier in our application.
-Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
-Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different package.
-Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is visible only from the same package only.

Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If the method does not return anything, we use void keyword.

Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is invoked by its name.

Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type and variable name. If the method has no parameter, left the parentheses blank.

Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of curly braces.
Question: How to name a Method?
Answer: While defining a method, remember that the method name must be a verb and start with a lowercase letter. If the method name has more than two words, the first name must be a verb followed by adjective or noun. In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example:

Single-word method name: sum(), area()

Multi-word method name: areaOfCircle(), stringComparision()

It is also possible that a method has the same name as another method name in the same class, it is known as method overloading.
Question: What are the Types of Method?
Answer: There are two types of methods in Java:

-Predefined Method
-User-defined Method
Question: What is Predefined Method?
Answer: In Java, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. When we call any of the predefined methods in our program, a series of codes related to the corresponding method runs in the background that is already stored in the library.

Each and every predefined method is defined inside a class. Such as print() method is defined in the java.io.PrintStream class. It prints the statement that we write inside the method. For example, print("Java"), it prints Java on the console.

Let's see an example of the predefined method.
Demo.java
public class Demo   
{  
public static void main(String[] args)   
{  
// using the max() method of Math class  
System.out.print("The maximum number is: " + Math.max(9,7));  
}  
}

Output:
The maximum number is: 9


In the above example, we have used three predefined methods main(), print(), and max(). We have used these methods directly without declaration because they are predefined. The print() method is a method of PrintStream class that prints the result on the console. The max() method is a method of the Math class that returns the greater of two numbers.



We can also see the method signature of any predefined method by using the link https://docs.oracle.com/. When we go through the link and see the max() method signature, we find the following:

public static int max(int a, int b) returns the greater of two int values.
same value.

Parameters:
a an argument.

b
another argument.

Returns:
the larger of a and b.


In the above method signature, we see that the method signature has access specifier public, non-access modifier static, return type int, method name max(), parameter list (int a, int b). In the above example, instead of defining the method, we have just invoked the method. This is the advantage of a predefined method. It makes programming less complicated.

Similarly, we can also see the method signature of the print() method.
Question: What is User-defined Method?
Answer: The method written by the user or programmer is known as a user-defined method. These methods are modified according to the requirement.
How to Create a User-defined Method
Let's create a user defined method that checks the number is even or odd. First, we will define the method.

//user defined method  
public static void findEvenOdd(int num)  
{  
//method body  
if(num%2==0)   
System.out.println(num+" is even");   
else   
System.out.println(num+" is odd");  
}  

We have defined the above method named findevenodd(). It has a parameter num of type int. The method does not return any value that's why we have used void. The method body contains the steps to check the number is even or odd. If the number is even, it prints the number is even, else prints the number is odd.

How to Call or Invoke a User-defined Method

Once we have defined a method, it should be called. The calling of a method in a program is simple. When we call or invoke a user-defined method, the program control transfer to the called method.

import java.util.Scanner;  
public class EvenOdd  
{  
public static void main (String args[])  
{  
//creating Scanner class object     
Scanner scan=new Scanner(System.in);  
System.out.print("Enter the number: ");  
//reading value from the user  
int num=scan.nextInt();  
//method calling  
findEvenOdd(num);  
}  

In the above code snippet, as soon as the compiler reaches at line findEvenOdd(num), the control transfer to the method and gives the output accordingly.

Let's combine both snippets of codes in a single program and execute it.

EvenOdd.java
import java.util.Scanner;  
public class EvenOdd  
{  
public static void main (String args[])  
{  
//creating Scanner class object     
Scanner scan=new Scanner(System.in);  
System.out.print("Enter the number: ");  
//reading value from user  
int num=scan.nextInt();  
//method calling  
findEvenOdd(num);  
}  
//user defined method  
public static void findEvenOdd(int num)  
{  
//method body  
if(num%2==0)   
System.out.println(num+" is even");   
else   
System.out.println(num+" is odd");  
}  
}  

Output 1:
Enter the number: 12
12 is even

Output 2:
Enter the number: 99
99 is odd
Let's see another program that return a value to the calling method.

In the following program, we have defined a method named add() that sum up the two numbers. It has two parameters n1 and n2 of integer type. The values of n1 and n2 correspond to the value of a and b, respectively. Therefore, the method adds the value of a and b and store it in the variable s and returns the sum.
Addition.java

public class Addition   
{  
public static void main(String[] args)   
{  
int a = 19;  
int b = 5;  
//method calling  
int c = add(a, b);   //a and b are actual parameters  
System.out.println("The sum of a and b is= " + c);  
}  
//user defined method  
public static int add(int n1, int n2)   //n1 and n2 are formal parameters  
{  
int s;  
s=n1+n2;  
return s; //returning the sum  
}  
}  

Output:
The sum of a and b is= 24
Question: What is Static Method?
Answer: A method that has static keyword is known as static method. In other words, a method that belongs to a class rather than an instance of a class is known as a static method. We can also create a static method by using the keyword static before the method name.

The main advantage of a static method is that we can call it without creating an object. It can access static data members and also change the value of it. It is used to create an instance method. It is invoked by using the class name. The best example of a static method is the main() method.

Example of static method

Display.java

public class Display  
{  
public static void main(String[] args)   
{  
show();  
}  
static void show()   
{  
System.out.println("It is an example of static method.");  
}  
}  
Output:

It is an example of a static method.
Question: What is Instance Method?
Answer: The method of the class is known as an instance method. It is a non-static method defined in the class. Before calling or invoking the instance method, it is necessary to create an object of its class. Let's see an example of an instance method.

InstanceMethodExample.java

public class InstanceMethodExample  
{  
public static void main(String [] args)  
{  
//Creating an object of the class  
InstanceMethodExample obj = new InstanceMethodExample();  
//invoking instance method   
System.out.println("The sum is: "+obj.add(12, 13));  
}  
int s;  
//user-defined method because we have not used static keyword  
public int add(int a, int b)  
{  
s = a+b;  
//returning the sum  
return s;  
}  
}  

Output:
The sum is: 25
Question: There are two types of instance method?
Answer: There are two types of instance method:

-Accessor Method
-Mutator Method
Question: What is Accessor Method in Java?
Answer: The method(s) that reads the instance variable(s) is known as the accessor method. We can easily identify it because the method is prefixed with the word get. It is also known as getters. It returns the value of the private field. It is used to get the value of the private field
.
Example:

public int getId()    
{    
return Id;    
}
Question: What is Mutator Method in Java?
Answer: The method(s) read the instance variable(s) and also modify the values. We can easily identify it because the method is prefixed with the word set. It is also known as setters or modifiers. It does not return anything. It accepts a parameter of the same data type that depends on the field. It is used to set the value of the private field.

Example:

public void setRoll(int roll)   
{  
this.roll = roll;  
}
Question: Can you provide an Example of accessor and mutator method?
Answer: Student.java

public class Student   
{  
private int roll;  
private String name;  
public int getRoll()    //accessor method  
{  
return roll;  
}  
public void setRoll(int roll) //mutator method  
{  
this.roll = roll;  
}  
public String getName()   
{  
return name;  
}  
public void setName(String name)   
{  
this.name = name;  
}  
public void display()  
{  
System.out.println("Roll no.: "+roll);  
System.out.println("Student name: "+name);  
}  
}
Question: What is Abstract Method in Java?
Answer: The method that does not has method body is known as abstract method. In other words, without an implementation is known as abstract method. It always declares in the abstract class. It means the class itself must be abstract if it has abstract method. To create an abstract method, we use the keyword abstract.

Syntax
abstract void method_name();
Question: Can you provide an Example of abstract method?
Answer: Demo.java

abstract class Demo //abstract class  
{  
//abstract method declaration  
abstract void display();  
}  
public class MyClass extends Demo  
{  
//method impelmentation  
void display()  
{  
System.out.println("Abstract method?");  
}  
public static void main(String args[])  
{  
//creating object of abstract class  
Demo obj = new MyClass();  
//invoking abstract method  
obj.display();  
}  
}  

Output:
Abstract method...
Question: What is Factory method in Java?
Answer: It is a method that returns an object to the class to which it belongs. All static methods are factory methods. For example, NumberFormat obj = NumberFormat.getNumberInstance();
Question: What is Constructors in Java?
Answer: In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

There are two types of constructors in Java: no-arg constructor, and parameterized constructor.

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.
Question: What are the Rules for creating Java constructor?
Answer: There are two rules defined for the constructor.

1) Constructor name must be the same as its class name
2) A Constructor must have no explicit return type
3) A Java constructor cannot be abstract, static, final, and synchronized
Question: What are theTypes of Java constructors?
Answer: There are two types of constructors in Java:

1) Default constructor (no-arg constructor)
2) Parameterized constructor
Question: What is the Java Default Constructor?
Answer: A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:

<class_name>(){}
Question: Can you provide an Example of default constructor?
Answer: In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.



//Java Program to create and call a default constructor  

class Bike1{  
//creating a default constructor  
Bike1(){System.out.println("Bike is created");}  
//main method  
public static void main(String args[]){  
//calling a default constructor  
Bike1 b=new Bike1();  
}  
}  

Output:
Bike is create
Question: Can you provide an Example of default constructor that displays the default values?
Answer: //Let us see another example of default constructor  
//which displays the default values  
class Student3{  
int id;  
String name;  
//method to display the value of id and name  
void display(){System.out.println(id+" "+name);}  
  
public static void main(String args[]){  
//creating objects  
Student3 s1=new Student3();  
Student3 s2=new Student3();  
//displaying values of the object  
s1.display();  
s2.display();  
}  
}  

Output:
0 null
0 null
Question: What is Java Parameterized Constructor?
Answer: A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
Question: Can you provide an Example of parameterized constructor in Java?
Answer: In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

//Java Program to demonstrate the use of the parameterized constructor.  

class Student4{  
    int id;  
    String name;  
    //creating a parameterized constructor  
    Student4(int i,String n){  
    id = i;  
    name = n;  
    }  
    //method to display the values  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    //creating objects and passing values  
    Student4 s1 = new Student4(111,"Karan");  
    Student4 s2 = new Student4(222,"Aryan");  
    //calling method to display the values of object  
    s1.display();  
    s2.display();  
   }  
}  

Output:
111 Karan
222 Aryan
Question: What is Constructor Overloading in Java?
Answer: In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
Question: Can you provide an Example of Constructor Overloading?
Answer: //Java program to overload constructors  
class Student5{  
    int id;  
    String name;  
    int age;  
    //creating two arg constructor  
    Student5(int i,String n){  
    id = i;  
    name = n;  
    }  
    //creating three arg constructor  
    Student5(int i,String n,int a){  
    id = i;  
    name = n;  
    age=a;  
    }  
    void display(){System.out.println(id+" "+name+" "+age);}  
   
    public static void main(String args[]){  
    Student5 s1 = new Student5(111,"Karan");  
    Student5 s2 = new Student5(222,"Aryan",25);  
    s1.display();  
    s2.display();  
   }  
}  

Output:
111 Karan 0
222 Aryan 25
Question: What is Java Copy Constructor?
Answer: 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.
//Java program to initialize the values from one object to another object.  
class Student6{  
    int id;  
    String name;  
    //constructor to initialize integer and string  
    Student6(int i,String n){  
    id = i;  
    name = n;  
    }  
    //constructor to initialize another object  
    Student6(Student6 s){  
    id = s.id;  
    name =s.name;  
    }  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    Student6 s1 = new Student6(111,"Karan");  
    Student6 s2 = new Student6(s1);  
    s1.display();  
    s2.display();  
   }  
}  

Output:
111 Karan
111 Karan
Question: How to copy values without constructor in Java?
Answer: We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.

class Student7{  
    int id;  
    String name;  
    Student7(int i,String n){  
    id = i;  
    name = n;  
    }  
    Student7(){}  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    Student7 s1 = new Student7(111,"Karan");  
    Student7 s2 = new Student7();  
    s2.id=s1.id;  
    s2.name=s1.name;  
    s1.display();  
    s2.display();  
   }  
}  

Output:
111 Karan
111 Karan
Question: What is Java static keyword?
Answer: The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

The static can be:

1) Variable (also known as a class variable)
2) Method (also known as a class method)
3) Block
4) Nested class
Question: What is Java static variable?
Answer: If you declare any variable as static, it is known as a static variable.

-The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
-The static variable gets memory only once in the class area at the time of class loading.

Advantages of static variable
It makes your program memory efficient (i.e., it saves memory).
Understanding the problem without static variable

class Student{  
     int rollno;  
     String name;  
     String college="ITS";  
}  

Suppose there are 500 students in my college, now all instance data members will get memory each time when the object is created. All students have its unique rollno and name, so instance data member is good in such case. Here, "college" refers to the common property of all objects. If we make it static, this field will get the memory only once.
Question: Can you provide an Example of static variable in Java?
Answer: //Java Program to demonstrate the use of static variable  
class Student{  
   int rollno;//instance variable  
   String name;  
   static String college ="ITS";//static variable  
   //constructor  
   Student(int r, String n){  
   rollno = r;  
   name = n;  
   }  
   //method to display the values  
   void display (){System.out.println(rollno+" "+name+" "+college);}  
}  
//Test class to show the values of objects  
public class TestStaticVariable1{  
 public static void main(String args[]){  
 Student s1 = new Student(111,"Karan");  
 Student s2 = new Student(222,"Aryan");  
 //we can change the college of all objects by the single line of code  
 //Student.college="BBDIT";  
 s1.display();  
 s2.display();  
 }  
}  

Output:

111 Karan ITS
222 Aryan ITS
Question: Can you provide an example Program of the counter without static variable in Java?
Answer: In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable. If it is incremented, it won't reflect other objects. So each object will have the value 1 in the count variable.

//Java Program to demonstrate the use of an instance variable  
//which get memory each time when we create an object of the class. 
 
class Counter{  
int count=0;//will get memory each time when the instance is created  
  
Counter(){  
count++;//incrementing value  
System.out.println(count);  
}  
  
public static void main(String args[]){  
//Creating objects  
Counter c1=new Counter();  
Counter c2=new Counter();  
Counter c3=new Counter();  
}  
}  

Output:

1
1
1
Question: Can you provid an example Program of counter by static variable in Java?
Answer: static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.


//Java Program to illustrate the use of static variable which  
//is shared with all objects.  

class Counter2{  
static int count=0;//will get memory only once and retain its value  
  
Counter2(){  
count++;//incrementing the value of static variable  
System.out.println(count);  
}  
  
public static void main(String args[]){  
//creating objects  
Counter2 c1=new Counter2();  
Counter2 c2=new Counter2();  
Counter2 c3=new Counter2();  
}  
}

Output:
1
2
3
Question: What is Java static method?
Answer: If you apply static keyword with any method, it is known as static method.

-A static method belongs to the class rather than the object of a class.
-A static method can be invoked without the need for creating an instance of a class.
-A static method can access static data member and can change the value of it.
Question: Can you provide an Example of static method in Java?
Answer: //Java Program to demonstrate the use of a static method.  
class Student{  
     int rollno;  
     String name;  
     static String college = "ITS";  
     //static method to change the value of static variable  
     static void change(){  
     college = "BBDIT";  
     }  
     //constructor to initialize the variable  
     Student(int r, String n){  
     rollno = r;  
     name = n;  
     }  
     //method to display values  
     void display(){System.out.println(rollno+" "+name+" "+college);}  
}  
//Test class to create and display the values of object  
public class TestStaticMethod{  
    public static void main(String args[]){  
    Student.change();//calling change method  
    //creating objects  
    Student s1 = new Student(111,"Karan");  
    Student s2 = new Student(222,"Aryan");  
    Student s3 = new Student(333,"Sonoo");  
    //calling display method  
    s1.display();  
    s2.display();  
    s3.display();  
    }  
}  
Output:
111 Karan BBDIT
       222 Aryan BBDIT
       333 Sonoo BBDIT
Question: What is Java static block?
Answer: -Is used to initialize the static data member.
-It is executed before the main method at the time of classloading.
Question: What is static keyword in Java?
Answer: The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.
The static can be:
-Variable (also known as a class variable)
-Method (also known as a class method)
-Block
-Nested class
Question: Can you provide an Example of static block in Java?
Answer: class A2{  
  static{System.out.println("static block is invoked");}  
  public static void main(String args[]){  
   System.out.println("Hello main");  
  }  
}  
Output:static block is invoked
       Hello main
Question: what are the Usage of Java this keyword?
Answer: Here is given the 6 usage of java this keyword.

1.this can be used to refer current class instance variable.
2.this can be used to invoke current class method (implicitly)
3.this() can be used to invoke current class constructor.
4.this can be passed as an argument in the method call.
5.this can be passed as argument in the constructor call.
6.this can be used to return the current class instance from the method.
Question: What is this keywoard in Java?
Answer: The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.
Question:  How to use "this" to invoke current class method in Java?
Answer: You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example
class A{  
void m(){System.out.println("hello m");}  
void n(){  
System.out.println("hello n");  
//m();//same as this.m()  
this.m();  
}  
}  
class TestThis4{  
public static void main(String args[]){  
A a=new A();  
a.n();  
}}  

Output:

hello n
hello m
Question:  How to use this() : to invoke current class constructor?
Answer: The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.
Calling default constructor from parameterized constructor:

class A{  
A(){System.out.println("hello a");}  
A(int x){  
this();  
System.out.println(x);  
}  
}  
class TestThis5{  
public static void main(String args[]){  
A a=new A(10);  
}}  
Output:

hello a
10
Question: What is the Real usage of this() constructor call?
Answer: The this() constructor call should be used to reuse the constructor from the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see the example given below that displays the actual use of this keyword.

class Student{  
int rollno;  
String name,course;  
float fee;  
Student(int rollno,String name,String course){  
this.rollno=rollno;  
this.name=name;  
this.course=course;  
}  
Student(int rollno,String name,String course,float fee){  
this(rollno,name,course);//reusing constructor  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}  
}  
class TestThis7{  
public static void main(String args[]){  
Student s1=new Student(111,"ankit","java");  
Student s2=new Student(112,"sumit","java",6000f);  
s1.display();  
s2.display();  
}}  
Output:

111 ankit java 0.0
112 sumit java 6000.0
Question: how to use this: to pass as an argument in the method in Java?
Answer: The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Let's see the example:

class S2{  
  void m(S2 obj){  
  System.out.println("method is invoked");  
  }  
  void p(){  
  m(this);  
  }  
  public static void main(String args[]){  
  S2 s1 = new S2();  
  s1.p();  
  }  
}  
Output:

method is invoked
Question: how to use this: to pass as argument in the constructor call in Java?
Answer: We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example:

class B{  
  A4 obj;  
  B(A4 obj){  
    this.obj=obj;  
  }  
  void display(){  
    System.out.println(obj.data);//using data member of A4 class  
  }  
}  
  
class A4{  
  int data=10;  
  A4(){  
   B b=new B(this);  
   b.display();  
  }  
  public static void main(String args[]){  
   A4 a=new A4();  
  }  
}  
Output:10
Question: Can you provide an Example of this keyword that you return as a statement from the method in Java?
Answer: class A{  
A getA(){  
return this;  
}  
void msg(){System.out.println("Hello java");}  
}  
class Test1{  
public static void main(String args[]){  
new A().getA().msg();  
}  
}  
Output:

Hello java
Question: how to provee this keyword in java?
Answer: Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same.

class A5{  
void m(){  
System.out.println(this);//prints same reference ID  
}  
public static void main(String args[]){  
A5 obj=new A5();  
System.out.println(obj);//prints the reference ID  
obj.m();  
}  
}  
Output:

A5@22b3ea59
A5@22b3ea59
Question: What is Inheritance in Java?
Answer: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Question: Why use inheritance in java?
Answer: -For Method Overriding (so runtime polymorphism can be achieved).
-For Code Reusability.
Question: What are the Terms used in Inheritance?
Answer: -Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.

-Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.

-Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.

-Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Question: What are The syntax of Java Inheritance?
Answer: class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.
Question: Whaht are the Types of inheritance in java?
Answer: On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only.

When one class inherits multiple classes, it is known as multiple inheritance.