Java Interview Questions and Answer for Fresher

  1. What are the different access specifiers for Java classes?
    Access classifiers are said to be the keywords that are used before a class name which refers the access scope in Java. The below types of access classifiers is highly important and also frequently asked Java Interview Questions.
    public: field, method, class is accessible from anywhere.
    protected: field and method can be accessed from the same class to where they belong or from subclasses, and from same package class, but not from outside.
    default: field, method, the class can be accessed from the same package only and not from outside of the native package.
    private: field, the method can be accessed from the same class to where they belong.
    2.What is the difference between an Inner class and a Subclass?
    It is a Core Java Interview Questions, when you are a beginner go through the answer carefully.
    An inner class has been nested within the other class. An inner class has access rights for the class that is nesting it and can access all methods and variables that are defined in the outer class.
    A subclass inherits from the other class known as the superclass. Sub-class can access all protected and public fields and methods of its superclass.
    3.What is known as data encapsulation and explain it’s significance?
    Encapsulation is the concept which is asked frequently in the most Core Java Interview Questions for Freshers.
    Encapsulation is said to be a concept in Object Oriented Programming for combining methods and properties in a single unit. Encapsulation helps the programmers in following a modular approach for the software development as each object has its unique set of variables and methods and serves its functions independent of other objects. Encapsulation is also used for data hiding purpose.
    What is the purpose of static variables and static methods?
    When there is a need to share a variable or a method between multiple objects of a class rather than creating separate copies for each object. To make a variable or method shared for all objects, we use.
    What is called Loops in Java? What are the three types of loops?
    This is a Java Basic Interview Questions to know mandatorily before attending any interviews.
    Looping is used in programming to implement a statement or a statement block repeatedly. There are three types of loops in Java:
    a. For Loops:
    For loops are used in java to implement statements regularly for a given no of times. For loops are used when no of times to implement the statements is refer to the programmer.
    b. while Loops:
    While loop is used if certain statements require being implemented regularly until a condition is satisfied. The condition is tested first before implementation of statements in while loops.
    c. Do While Loops:
    Do while loop is similar to while loop with an only difference which condition is examined after implementation of statement blocks. Thus in do while loop case, statements are implemented at least once.
    What is a singleton class? Give an example
    In Java, a singleton class can have only one instance and thus all its variables and methods belong to one instance. The concept of a singleton class is used for the situations while there is a requirement to limit the no of objects for a class.
    The good example of singleton usage scenario is while there is a limit of having an only single connection to a database due to some limitations of the driver or any licensing issues.
    Explain the difference between break and continue statement?
    When you are asked to explain Break and Continue concept in Java Interview Questions for Freshers, I suggest you to give examples to attract the interviewer.
    Break and continue statements are the two main keywords used in loops. When a break keyword is used in a loop, instantly the loop is broken while when continue keyword is used, the current iteration is broken and the loop continues with the next iteration.
    When counter reaches 4, loop is broken in the below example.
    1 For (counter=0; counter<10; counter++)
    2 System.out.println(counter);
    3 if (counter==4)
    4 break;}
    5 }
    When counter reaches 4, loop just jumps to the next iteration in the below example and after the continue keyword any statements are skipped for the current iteration.
    1 for (counter=0; counter<10; counter++)
    2 system.out.println(counter);
    What is known as infinite loop? Explain how infinite loop is declared?
    An infinite loop runs infinitely and without any condition. An infinite loop may be broken by explaining any breaking logic in the statements blocks body.
    Infinite loop is proclaimed as follows
    1 for (;;)
    3 {
    5 // Statements to execute
    7 // Add any loop breaking logic
    9 }
    What is known as a final keyword in Java? Give an example
    A constant is proclaimed using the keyword final in Java. The value may be assigned once and after the assignment, the value of a constant cannot be changed.
    A constant with the name const_val has been assigned and declared a value in the below example:
    Private Final int const_val=100
    It cannot be overridden by the subclasses if a method is declared as final. This method is considered to be faster than any other method because they have been resolved at the compiled time.
    It can’t be subclassed, when a class is declared as final. E.g., integer, string, and other wrapper classes.
    Explain the difference between float and double variables in Java?
    Float always takes about 4 bytes in memory while double takes about 8 bytes in memory in Java. The float is said to be single precision floating point decimal number and the double is the double precision decimal number.

Leave a Reply