Top 10 java interview questions on coding standards

1. Explain Java Coding Standards for classes, interface, method, variable, constants.

Coding Standard for Classes: The class name usually should start with noun and first letter of the class should start with uppercase letter. If Class name is combination of multiple word, every word should start with Upper case letter. Ex- Vehicle, Dog, AnimalKingdom etc.

Coding Standard for Interface: An Interface name should be adjective with first letter of the interface as Uppercase. If there are multiple words in an interface then every Inner word should start with uppercase letter. Example: Runnable, MyMovableInterfaceImpl etc.

Coding Standard for Methods: Method name should usually be a verb or verb noun and should start with a lower case letter. If there are multiple words in a method then each inner word should start with Uppercase letter. Example: run(), getSalary() etc.

Coding Standards for variable : A variable name should be noun and start with lower case letter. if there are multiple words in variable, each inner word should start with uppercase letter. Example: name, age, mobileNumber.

Coding standards for Constants: Constant should be a noun usually. It should contain only Uppercase letters. If there are multiple letters in a constant name, then each word should be separated by a underscore symbol (_). Usually we denote a constant with public static and final modifier. Example: public static final logger = SLF4j_LOGGER;

2.  What access modifiers are allowed for top level class ?

Access Modifiers in java help us define Accessibility, Scope and visibility of data members of class, method, constructor or any field. Java provides 4 types of access Modifier, and there are

Default: Whenever there is no Modifier defined, it is assumed as default access modifier. the scope of default access modifier is within the package.

Public: This is most common access level and whenever you specify an entity as public it can be accessed within same class and outside the class or within same package and outside the package.

Protected: Protected level modifier has scope within same package. The protected entity can be accessed outside that package but only through inherited class or child class.

private: Whenever you define any entity as private, its scope is within that class, it can not be accessed outside that class. Summary of access modifiers is available in the table below.

Access Specifier

Inside Class

Inside Package

Outside package subclass

Outside package

Private

Yes

No

No

No

Default

Yes

Yes

No

No

Protected

Yes

Yes

Yes

No

Public

Yes

Yes

Yes

Yes

3. How do you differentiate between access specifiers and access modifiers?

The interviewer may try to test your confidence by asking this question. In Java Officially there are two types of modifiers namely. (1) java access modifiers (2.) java Non-access Modifiers

“access modifier” is the official term for privateprotected, default and public used in the Java language specification. Access specifier is used in the Java API doc synonymously. Alternatively, You can say that Both are same.

4. Does java has limit on the class name length?

The Java language specification states that identifiers can be unlimited in length, In practice though, the filesystem will limit the length of the resulting file name. The java VM specification states that any constant string is restricted to 65535 bytes

5. Can we create constructor in abstract class ?

Yes, You can create Constructor in Abstract classes but you can not instantiate a abstract class directly, It can only be extended to it can be used only through subclasses constructor.

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

IS-A relationship is called inheritance in java, The classes which inherit the property are called subclasses or child class. while on the other hand HAS-A is a composition. This means that child class is a type of parent class, for example: Apple is a fruit, so we can extend fruit ot get apple.

class Apple extends Fruit {

}

On the other hand, Composition means creating an instance which has reference to other object. for example: A room has a table so we will create a room class and we will instantiate an object of table.

class Room {

    Table table = new Table();

}

HAS-A relationship is a dynamic binding(Run time) while IS-A relationship is static binding (Compile Time)

7. Can we write multiple classes in single file ?

Why not!! You are free to use, but you have to follow some conditions set be Java creators.

  • One java class can have multiple classes with the restriction that only one of them can be public.
  • The file name should be the name of the class which has public Modifier. ex: Test.java
public class Test{
}
class Test1{
// statements...
}
class Test2{
// statements...
}

8) Can we have more than one package statement in source file ?

There can be only one package statement in each source file and it applies to all types of files.

9. what is the convention followed in writing the package name?

package name should always be lower case.

10. What does null mean in java?

  • null is a literal in java is case-sensitive, we can write “NULL” or “0” as we don in c language
  • Any reference variable in java has default value as null.
  • null is neither an object nor a type. Its a specific value which can be assigned to any reference type and you can also typecast null to any reference type.

Leave a Reply