Core Java Interview


                                       
                                                         Java Interview Guide - Udemy

Why Java is platform-independent? 
  • The bytecode of java program called platform-independent because you can run a java program in any platform(Windows, Linux, Mac, etc.) without modifying the code.
  • You can write a program on any platform and then the same program can be executed and compiled in the other platform.
  • All java programs run on JVM(Java Virtual Machine). Each operating system has it's own JVM. So, since JVM can run java codes and there are different JVMs for different platforms, hence java can run on any platform.

What is the difference between ‘throw’ and ‘throws’?

throw

  • With the help of throw keyword, we can throw an Exception from inside a function or static block.
    void developerLibs(){  
         throw new ArithmeticException("error");  
    }   
    
  • We can propagate only unchecked exceptions with the help of throw keyword.
  • It can throw only one exception at a time.

throws

  • throws keyword is used with the method signature. It is used when the function has some statements that can lead to some exceptions. throws is used to indicate which Exception can possibly be thrown by this method.
    void developerLibs() throws ArithmeticException, NullPointerException 
    { 
        // to do. 
        throw new ArithmeticException(); 
    }    
    
  • We can propagate checked exceptions with the help of throws keyword.
  • throws keyword can be used to declare multiple exceptions by using commas.

Is there any scenario where you can skip the finally block in a try-catch? 
  • We can do it, by Calling System.exit(0) in try or catch block. If System.exit(0) exits the JVM without throwing that exception then finally block will not execute. But, if System.exit(0) does throw security exception then finally block will be executed.


What are anonymous classes?
 

  • An anonymous class has no name. It combines the class declaration and creation of an instance of the class in a single step. 
  • The object of the anonymous class can not be instantiated from outside the class in which the anonymous class is defined. 
  • The object of the anonymous class can only be instantiated from within the same scope in which it is defined.
Rules:
  • It must extend a super class or implement an interface but it can't have an explicit extends or implements clause. 
  • It must implement all the abstract methods in the super class or the interface. 
  • It always uses the default constructor from the super class to create an instance.
    Example:
    DevButton.setOnClickListener(new Button.OnClickListener {
           @override
              public void onClick(View view){
                  //to do
              }
       }     );   

Why the main() method is static in Java?
The main() method in Java is the entry point of any application. So, we have to make the main() method as static due to the following reasons:

  • A static main() method can be called by the JVM without creating an instance of a class that contains the main method. 
  • If we do not declare static with main() then it'll throw some unexpected errors when overloading a method that takes a variable-length argument. These errors involve ambiguity because both the methods are valid for invocation. 
  • If the static keyword is removed from the main method, the program compiles successfully but at runtime throws an error NoSuchMethodError.

What is the garbage collector?
  • The Java programs compile to bytecode that runs on a Java Virtual Machine(JVM) and creates objects on the heap, which is a portion of memory dedicated to the program. After some time, some objects will no longer be needed. The garbage collector finds unused objects and deletes them to free up memory. So, the garbage collector is the program that performs automatic memory management.
Explain OOPs concept
  • Object-Oriented Programming(OOPs) is a programming style that involves concepts such as Classes, objects, Abstraction, Encapsulation, Inheritance, Polymorphism.

What are Classes and Objects in Java?
  • Classes: A class is a group of objects which have common properties. It is a template or blueprints to create objects. It is a logical entity that can't be physical. A class in Java can contain Fields, Methods, Constructors, Blocks, Nested class, and interface.
  • Object: An entity that has state and behavior is known as an object such as a chair, pen, table, car, etc. It can be physical or logical (tangible and intangible). An object has three characteristics:
    • State: represents the data or value of an object.
    • Behavior: represents the behavior or 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.
     
What is Abstraction?
  • Abstraction is a process of hiding the internal working and showing only functionality to the user. Example, printing a page from printer where you hit the print command. You don't know the internal processing about the printing process. Abstraction lets you focus on what the object does instead of how it does it.

In Java, we can achieve abstraction with the help of Abstract classes and interfaces.









Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...