JAVA INTERVIEW QUESTIONS

  • What are abstract methods in java?

    An abstract method is the method which doesn’t have any body. Abstract method is declared with keyword abstract and semicolon in place of method body. Signature : public abstract void <method_name>(); Ex : public abstract void getDetails(); It is the responsibility of subclass to provide implementation to abstract method defined in abstract class.

  • Can we create constructor in abstract class in Java?

    We can create constructor in abstract class , it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class.

  • Explain about abstract classes in java?

    Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract. Any class that contains one…

  • What is final access modifier in java?

    final access modifier can be used for class, method and variables. The main advantage of final access modifier is security no one can modify our classes, variables and methods. The main disadvantage of final access modifier is we cannot implement oops concepts in java. Ex : Inheritance, polymorphism. final class : A final class cannot…

  • Explain what access modifiers can be used for variables in Java?

    We can use all access modifiers public, private, protected and default for variables.public : When a variables is declared as public it can be accessed1) In the same class2) In the same package subclass3) In the same package non subclass4) In the different package subclass5) In the different package non subclass. default : When a…

  • Explain what access modifiers can be used for methods in Java?

    We can use all access modifiers public, private, protected and default for methods.public : When a method is declared as public it can be accessed1) In the same class2) In the same package subclass3) In the same package non subclass4) In the different package subclass5) In the different package non subclass. default : When a…

  • What access modifiers can be used for class in Java?

    We can use only two access modifiers for class public and default.public: A class with public modifier can be visible1) In the same class2) In the same package subclass3) In the same package nonsubclass4) In the different package subclass5) In the different package non subclass.default : A class with default modifier can be accesed1) In…

  • What is the difference between access specifiers and access modifiers in java?

    In C++ we have access specifiers as public, private, protected and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java we have access modifiers and non access modifiers. Access Modifiers : public, private, protected, default Non Access Modifiers : abstract, final,…

  • What are access modifiers in java?

    The important feature of encapsulation is access control. By preventing access control we can misuse of class, methods and members. A class, method or variable can be accessed is determined by the access modifier. There are three types of access modifiers in java. public, private, protected. If no access modifier is specified then it has…

  • What are identifiers in java?

    Identifiers are names in java program. Identifiers can be class name, method name or variable name. Rules for defining identifiers in java: 1) Identifiers must start with letter, Underscore or dollar($) sign.2) Identifiers can’t start with numbers .3) There is no limit on number of characters in identifier but not recommended to have more than…

  • Can we define package statement after import statement in java?

    We can’t define package statement after import statement in java. Package statement must be the first statement in source file. We can have comments before the package statement

  • What are packages in java?

    Package is a mechanism to group related classes ,interfaces and enums in to a single module. Package can be declared using the following statement : Syntax : Coding Convention : package name should be declared in small letters. package statement defines the namespace. The main use of package is: 1) To resolve naming conflicts 2)…

  • What all access modifiers are allowed for top class ?

    For top level class only two access modifiers are allowed. public and default. If a class is declared aspublic it is visible everywhere.If a class is declared default it is visible only in same package.If we try to give private and protected as access modifier to class we get the below compilation error.Illegal Modifier for…

  • Can we have multiple classes in single file ?

    Yes we can have multiple classes in single file but it people rarely do that and not recommended. We canhave multiple classes in File but only one class can be made public. If we try to make two classes in Filepublic we get following compilation error.“The public type must be defined in its own file”

  • What does null mean in java?

    When a reference variable doesn’t point to any value it is assigned null.Example : Employee employee;In the above example employee object is not instantiate so it is pointed no where.

  • Explain about instanceof operator in java?

    Instanceof operator is used to test the object is of which type. Syntax : instanceof Instanceof returns true if reference expression is subtype of destination type. Instanceof returns false if reference expression is null. Example : Since a is integer object it returns true.There will be a compile time check whether reference expression is subtype…

  • Difference between ‘IS-A’ and ‘HAS-A’ relationship in java?

    IS-A RELATIONSHIP HAS- A RELATIONSHIP Is a relationship also known as inheritance Has a relationship also known as composition or aggregation. For IS-A relationship we uses extends keyword For Has a relationship we use new keyword Ex : Car is a vehicle. Ex : Car has an engine. We cannot say Car is an engine…

  • What is ‘HAS A’’ relationship in java?

    ‘Has a ‘ relationship is also known as “composition or Aggregation”. As in inheritance we have ‘extends’keyword we don’t have any keyword to implement ‘Has a’ relationship in java. The main advantage of‘Has-A‘ relationship in java code reusability.

  • What is ‘IS-A ‘ relationship in java?

    ‘is a’ relationship is also known as inheritance. We can implement ‘is a’ relationship or inheritance in javausing extends keyword. The advantage or inheritance or is a relationship is reusability of code instead ofduplicating the code.Ex : Motor cycle is a vehicleCar is a vehicle Both car and motorcycle extends vehicle.

  • Explain Java Coding Standards for Constants?

    Constants in java are created using static and final keywords.1) Constants contains only uppercase letters.2) If constant name is combination of two words it should be separated by underscore.143) Constant names are usually nouns.Ex:MAX_VALUE, MIN_VALUE, MAX_PRIORITY, MIN_PRIORITY