Java Programming Language – Interview Questions & Answers (Page -2)

Write a “Hello, World!” Program in Java?

    public class Main 
    {

        private  static void main(String[] args) {

            System.out.println("Hello, World!");
      }
    }
    

What is GC (Garbage Collection) in Java and how it works?

Garbage Collection collects the memory occupied the objects those are no longer in use of program, so that memory can be reclaimed or reused in the program.

Is Java supports const and how can we declare a constant?

No, Java does not support the const keyword, to declare a constant final keyword used. With the help of final keyword, we can declare a constant.

What is a final class in Java?

A class with final keyword that cannot be extended i.e. you cannot extend a final class on another class.

What will happen, if main () is declared as private?

main() method must be public, if you declared main as a private, complier returns an error “main() method not public.”

What will happen, if static modifier removed from the signature of main()?

Program compiles, but at the run time, it returns an error “NoSuchMethodError”.

What is the difference between public static void main() and static public void main()?

There is no difference between these two statements in functionality, but in respect of conventions use the visibility operators like public, private first followed by static.

How can you determine that array is null or empty?

It is very easy, you can determine it by using args.length, print the args.length, if it returns 0 , it means array is empty, if it returns an error “NullPointerException”, it means array is null.

Can a Java Program has multiple classes having main () methods?

Yes, it possible to have multiple classes with main () method in a Java Application. Because when you start a program, you have to pass class name with it. Just use that class which has main () method.

Can you have multiple main () method in a class?

No, It cannot possible, it return an error that main () has already defined.

Can I import same package/class multiple times?

Yes, you can import same package/class multiple times in your application; neither compiler not JVM will not return error. And JVM loads the package/class only once internally.

Can a top level class be private or protected?

No, a top class can never be private or protected, it always public. If you declare a top level class as either private or protected, it throws an error that private modifier cannot be used here. Either public or no access modifier type needed with the top level class.

What is the difference between declaring and defining a variable in Java?

These two concepts are different; when you declare a variable it tells the type and name of the variable. Declaration does not occupy the memory for the variable. When you define a variable (initializing the variable), it reserve the memory bytes.
For example:
    String siteName;				// it is just a declaration
    String siteName=”includehelp.com”;	// it is declaration + initialization (definition)
    

What is serialization?

It is the mechanism by which you can save the state of an object by converting it to a byte stream. If you want to serialize the object, use serialization mechanism.
  • Page 2 of 4
  • 1
  • 2
  • 3
  • 4