Java answers



Pdf👇👇👇

Short answer 👇👇

1. What is use of static keyword in java?
Static: static keyword is used to call variable and Method without using an object.
class A {
    static int a=19;
    static void print() {
        System.out.println("example on static");
    }
    public static void main(String ar[]) {
        System.out.println("a value:"+a);
        print();
    }
}
Output: example on static
a value:19
2. Write a  Program to demonstrate addition of two number using command line argument.
public class SumOfNumbers4
{
    public static void main(String args[])
    {
        int x = Integer.parseInt(args[0]); //first arguments
        int y = Integer.parseInt(args[1]); //second arguments
        int sum = x + y;
        System.out.println("The sum of x and y is: " +sum);
    }
}
output: The sum of x and y is: 101
3. Write a program to catch divide by zero exception.
package q;
public class ThrowThrowsExample {
    public static void main(String[] args) {
        int a = 50, b = 0, c;
        try {
            c = a / b;
            System.out.println(a + "/" + b + "=" + c);
        } catch (ArithmeticException e) {
            System.out.println(e);
        }
    }
output: java.lang.ArithmeticException: divide by zero
4. What are the various access specifier in Java?
•private (accessible within the class where defined)
•default or package private (when no access specifier is specified)
•protected: it can be access from sub-class buy not from sub-sub class.
•public (accessible from any class)
5. What is constructor?
A method which have same name of class is called as constructor
It is mainly used for initializing the variable class
6. What is default and parameterized constructor?
Default constructor: a constructor which can be called with no argument, either they define in empty parameter list or with default argument provided provided for every parameter
Parameterized constructor: the constructor which are having a specific number of argument to be passed.