Blog

JAVA INTERVIEW IMPORTANT QUESTIONS

JAVA TRAINING INSTITUTE

JAVA INTERVIEW IMPORTANT QUESTIONS

Hello friends, after so many wonderful feedback on our previous blog on JAVA INTERVIEW QUESTIONS we are back.

In this blog we will share some most important java interview questions you should prepare before appearing interview.

Q  : Tell me about java source file structure ?

A Java source file can have the following elements and they must be declared in bellow order :

  1. Package declaration to specify a package name(if any).. Only one package name we can specify in one source file.
  2. Zero or more import declarations.
  3. Any number of top-level class and interface declarations. The classes and interfaces can be defined in any order.
  4. At the most one public class definition per source file can be defined. If a public class is defined, the file name must match this public class.
java structure order
Order is important

Q : How many package statement and import statement allowed in a single java source file?

Only one package statement in each java source file is allowed, and  multiple import statements are allowed in a single java source file.

Q:  Why only one package statement is allowed and why multiple import statements allowed?

Because all generated .class file related with one java source file are stored in a folder or package.

Multiple import statements are allowed, because we can use more than one classes available in different packages in our class

Q : What are Java Packages? What’s the significance of packages?

Package is a collection sub packages ,related classes and interfaces, bundled together as they are related to each other. Developers uses packages to modularize the code and group the code for proper re-use.

Q : Which package is auto imported by default in any java class?

java.lang package and no import statement is necessary.

Q : Is there a performance impact due to a large number of import statements that are unused?

No

Q : Difference between Java IO and NIO packages.

         IO                                                        NIO

Stream oriented                              Buffer oriented

Blocking IO                                       Non blocking IO

No Selectors                                     Selectors available

Q : Difference between import and static import.

 import allows the Java programmer to access classes of a package without package qualification.

 Static imports let you avoid qualifying static members with class names.Once the static member is imported then you can use it in your code without the class name prefix.

The import provides accessibility to classes and interfaces whereas static import provides accessibility to static members of the class.

YOU ARE READING : JAVA INTERVIEW QUESTIONS

Q : When I should use static import?

Use it when you require frequent access to static members from one or two classes.

Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Static imports are used to save our time and typing. If we hate to type same thing again and again then we may find such imports interesting.

CORE JAVA TRAINING IN INDORE
BEST INSTITUTE FOR CORE JAVA IN INDORE

Q : Can I import same package/class twice?

Yes. JVM will internally load the class only once no matter how many times one imports the same class.

Q : Which package is always imported by default?

The java.lang package is always imported by default.

Q : Is there any difference or advantage normal import over static import ?

The only difference between a normal import and an import static is that the latter is for moving static members of some other class or interface — especially constants — into scope.

The main difference is Readability, Constant.USER_NAME is less readable when compared to USER_NAME.

There are no performance benefits or penalties to using them as they compile into identical byte code.

YOU ARE READING : JAVA INTERVIEW QUESTIONS

Q : Does importing a package imports the sub packages as well?

No. Developer have to import the sub packages explicitly.

Q : Should package be the first statement in a Java class?

Yes. Otherwise we will get compilation error.

Q : Can we overload main in Java?

Yes, we can overload the main method in Java, JVM will only call our specific main method

public static void main(String[] args) or  public static void main(String args…)

Can we override main in Java?

No, we can not override the main method in Java, because main is a static method and in Java static method is binded during compile time and we can not override static method in Java.

ADVANCE JAVA TRAINING INDORE
BEST INSTITUTE FOR ADVANCE JAVA TRAINING INDORE – PRESTIGE POINT

Q : Can we make main final in Java?

Yes

Q : Can we make main synchronized in Java?

Yes

Q : How to call a nonstatic method from main in Java?

Since nonstatic methods can not be called from static context directly, we need to first create an Object as local variable and then we can call nonstatic method using that object.

YOU ARE READING : JAVA INTERVIEW QUESTIONS

Q : Can we declare main() method as private or protected or with no access modifier?

No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM.

Q : Can We Declare main() Method As Non-Static?

No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class.

Q : Why main() method must be static?

 If main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class. While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument.

That’s why main() method must be static.

Q : Can we change return type of main() method?

No, the return type of main() method must be void only. Any other type is not acceptable.

Q : Can main() method take an argument other than string array?

No, argument of main() method must be string array. But, from the introduction of var args you can pass var args of string type as an argument to main() method.

Q : Can we run java class without main() method?

No, we can’t run java class without main method. But, there are some scenarios like if super class has main() method, then sub class can be run without defining main() method in it.

class A

{

    public static void main(String[] args)

    {

        System.out.println(1);

    }

}

 public class B extends A

{     }

Note : Before Java 7, you can run java class by using static initializers (static blocks). But, from Java 7 it is not possible.

Q : Does importing all classes in a package make my object file (.class or .jar) larger?

No, import only tells the compiler where to look for symbols.

Q : Is it less efficient to import all classes than only the classes I need?

 No. The search for names is very efficient so there is no effective difference.

Q : Doesn’t it provide better documentation to import each class explicitly?

This shows good intentions, but It’s hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports.

Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using “*” to import all classes prevents this dangerous naming accident.

Q : I’ve imported java.lang.*, why do I also need java.lang.reflect.*?

The wildcard “*” only makes the classes in this package visible, not any of the subpackages.

Q : Why don’t I need an import to use String, System, etc?

All classes in the java.lang package are visible without an import.

Q : Is the order of the imports important?

 No. Group them for readability.

Q : What are naming conventions to declare a package in Java?

All java packages should start with lower case letters only.

Ex: java.awt                           Java.io

I hope you like our blog. For more such technical do visit our website and facebook page.

IN OUR NEXT BLOG WE WILL DISCUSS ABOUT IMPORTANT BOOKS FOR JAVA.

THANK YOU FOR READING OUR BLOG. GET BEST CORPORATE AND TRAINING ON DIFFERENT PROGRAMMING LANGUAGES AT PRESTIGE POINT – BEST IT TRAINING INSTITUTE IN INDORE.

Leave a Reply