CORE JAVA INTERVIEW QUESTIONS AND ANSWERS

Q. What is static variable?
A static variable is shared by all instances of a class. It is also known as a class variable and it is declared with keyword ‘static’.  Instances (objects) of the same class share a single copy of static variables.
class School
    int rollno; 
    String section;
    String name; 
    static String schoolName ="InspireSkills Edu"; 
    // constructor 
    School(int r, String s, String n){ 
        rollno = r; 
        section = s;
        name = n; 
    } 
    void display ()
    {
        System.out.println(rollno + " " + section + "   " + name + " " + schoolName);
    } 
    public static void main(String args[])
    { 
        School ob1 = new School(1, "A", "Mukesh"); 
        School ob2 = new School(2, "B", "Aryan"); 

        ob1.display(); 
        ob2.display(); 
    } 

Q. What is the static method?
When a method defines with static keyword before a data type is called a static method.
class  Example
{
            public static void print()
            {
                        System.out.println(“This is a static method”);
            }
}

Q. What is Garbage collection?
When an object is no longer required, java automatically free the memory space is known as Garbage Collection.

Q. What is java package?
A java package is a Java language element used to group related classes under a common name. Packages can be nested inside one another. The main package is java in java language

Q. What is escape sequence?
The combination of characters is preceded by a code extension. It comes with the backslash (\).For example, \n stands for new line; even though it looks two characters, Java treats them as one character with ASCII code of 10. Escape sequence, being a single character, should be put within single quotes.

Escape Sequences
Escape Sequence
Description
\t
Insert a tab in the text at this point.
\b
Insert a backspace in the text at this point.
\n
Insert a newline in the text at this point.
\r
Insert a carriage return in the text at this point.
\f
Insert a form feed in the text at this point.
\'
Insert a single quote character in the text at this point.
\"
Insert a double quote character in the text at this point.
\\
Insert a backslash character in the text at this point.

No comments:

Post a Comment