Monday, October 29, 2018

Java Interview Questions?

  1. Can we overload main method?

    Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java run time environment will look for main method with syntax as public static void main(String args[]).
  2. Can we have multiple public classes in a java source file?

    We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.
  3. What is Java Package and which package is imported by default?

    Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modules based. A java class fully classified name contains package and class name. For example, java.lang.Object is the fully classified name of Object class that is part of java.lang package.
    java.lang package is imported by default and we don’t need to import any class from this package explicitly.
  4. What are access modifiers?

    Java provides access control through public, private and protected access modifier keywords. When none of these are used, it’s called default access modifier.
    A java class can only have public or default access modifier. 
  5. What is final keyword?

    final keyword is used with Class to make sure no other class can extend it, for example String class is final and we can’t extend it.
    We can use the final keyword with methods to make sure child classes can’t override it.
    final keyword can be used with variables to make sure that it can be assigned only once. However the state of the variable can be changed, for example, we can assign a final variable to an object only once but the object variables can change later on.
    Java interface variables are by default final and static.
  6. What is static keyword?

    static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.
    static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.
  7. What is finally and finalize in java?

    finally block is used with try-catch to put the code that you want to get executed always, even if any exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.
    finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.

No comments:

Post a Comment

Which Python course is best for beginners?

Level Up Your Python Prowess: Newbie Ninjas: Don't fret, little grasshoppers! Courses like "Learn Python 3" on Codecade...