Core Java Interview Questions and Answers

 Core Java Interview Questions and Answers. Part 1

Most Frequently asked Java Interview Questions And Answers with Examples :  

Que 1. What is Java ?

Java is  a most popular , high- level & platform independent programming language.

Java is collection of Class & objects. It's owned by Oracle and develops by Sun Microsystems of USA in 1991. 
 It use to Develop 
  • Mobile Application 
  • Web application 
  • Desktop Application 
  • Game 
  • Database Connection
  • Web & application server's 
  • Electronic device's and much more .
Que 2. What are Static blocks and static initializers in java ?
Static blocks or static initializers are used to initialize static fields in java. we declare static blocks when we want to initialize static fields in our class. Static blocks gets executed exactly once when the class is loaded . Static blocks are executed even before the constructors are executed.

Que 3. What is Method Overriding in java ?
➤ If we have with same signature (same name, same return type ) in super class (/ Parent / Derived class) and subclass (Child class) then we say subclass (Child class) method is overriding by super class.
When to use overriding in java if we want same method with different behavior in superclass and subclass then we go for overriding. When we call overridden method with subclass (Child class) reference subclass (Child class) method is called hiding the superclass method. 
 


Example 
 

// File Save override_learn.java name class Base{
void color(){
System.out.println("Dont Show");
}
}
class Sub_class extends Base {
@Override
void color(){
System.out.println("Red,Blue,& more");
super.color(); //call For base class color method
}
}
class override_learn{
public static void main(String[] args) {
Base obj=new Sub_class();
obj.color();
}
}


Que 4. Why Java is platform independent ?
➤ The most unique feature of java is platform independent. In any programming language sources code is compiled in to executable code . This cannot be run across all platforms. When " javac " compiles a java program it generates an executable file called " .class " file. class file contains byte codes. Byte codes are interpreted only by JVM’s . Since these JVM’s are made available across all platforms by Sun Microsystems, we can execute this byte code in any platform. Byte code generated in windows environment can also be executed in Linux environment. This makes java platform independent.



Que 5. How to call one constructor from the other constructor (Constructor Chaining ) ?
 With in the same class if we want to call one constructor from other we use "this()" method. Based on the number of parameters we pass appropriate "this()" method is called. Restrictions for using this method : 
        1) this must be the first statement in the constructor 
        2)we cannot use two this() methods in the constructor
Example 
      
    // File Save constructor_call.java name
      class constructor_call{
        constructor_call(){
            // default constructor
            System.out.println("Red ");
        }
        constructor_call(int a)
        {
            // parameterized constructor
            this(); /*invokes (Calling) default constructor
            using this() keyword */ 
            System.out.println(a);
        }
        public static void main(String[] args) {
            new constructor_call(10);
        }
      }

Output ➤  

Red 10

Que 6. What is Super keyword in Java ?
➤ Whenever a subclass needs to refer to its immediate superclass it can do so by using "Super" keyword.
    'Super' keyword has two forms:
    1. It is used to call super class constructor.
    2.It is used to access a member of super class that is hidden by member of sub class. 
Syntax :-  
super(parameter-list);
Previous Post Next Post

Contact Form