Gadgets

Friday, 19 February 2016

Interview questions on java source file

Interview questions on java source file structure. In this article i provided not only interview questions, i given explanation in program and diagrammatically for each and every question. this article is useful to quick reference also.  
1) Can a java Program contain multiple classes?
Yes, a java program contain multiple classes as shown in the below program.
Save the below program as “Demo.java” after compile the “Demo.java” (source file) we will get three .class files as“Demo.class, One.class and Two.class”.

Example:-
public class Demo {
               public static void main(String[] args)   {
                               System.out.println("Hello World!");
                               One one=new One();
                               System.out.println("class one: "+one.i);
                               Two t=new Two();
                               t.two();
               }
}
class One {
               int i=10;
}
class Two {
               void two() {
                               int i=20;
                               System.out.println("class two: "+i);
               }
}

2) If a java program contains multiple classes, is it allowed to take main method in every class?
Yes, a java program contain multiple classes and it is allowed to take main method in every class
Example:-
public class Demo {
               public static void main(String[] args){
                System.out.println("Hello Demo");
               }
}
class One{
               public static void main(String[] args) {
                  System.out.println("class one");
               }
}
class Two {
               public static void main(String[] args) {
                 System.out.println("class two");
               }
}

Output:-
D:\Java>javac Demo.java
D:\Java>java Demo
    Hello Demo
D:\Java>java One
    Class one
D:\Java>java Two
     Class two

3) If a java program contains multiple classes, then how many classes can be declared as public?
A java program contains multiple classes, and then at most one class can be declared as public. If there is a public class in a program then name of program and name of public class should matched otherwise we will get compile time error. If no class declared as public we can save program with any other name.
If a java program contains multiple public classes then we will get compile time error.
Example:-
public class Demo {
               public static void main(String[] args) {
                  System.out.println("Hello Demo");
               }
}
public class One {
               public static void main(String[] args)  {
                  System.out.println("class one");
               }
}

Output:-
D:\Java>javac Demo.java
Demo.java:8: error: class One is public, should be declared in a file named
 One.java
public class One
       ^
1 error

4) What is the purpose of import statement? If we are using fully qualified name is it required to use import statement?
The main purpose of import statement is to import one package classes into another package
package pack1;
import java.util.*;
class One
{
               public static void main(String[] args)
               {
                    System.out.println("enter any number");
                    Scanner sc = new Scanner(System.in);
                    int i = sc.nextInt();
                    System.out.println("the number is :"+i);
               }
}

In the above program we imported java.util package into pack1 package.
Import statement is not required to use when we are using fully qualified name as shown in the below program java.util.Scanner sc = new java.util.Scanner(); is fully qualified name. But it is not good programming practice. It increases length of the code and reduces readability of the code.
package pack1;
class One
{
               public static void main(String[] args)
               {
                  System.out.println("enter any number");
                  java.util.Scanner sc = new java.util.Scanner(System.in);
                  int i = sc.nextInt();
                  System.out.println("the number is :"+i);
               } 
}

5) Is it possible to use fully qualified name and import statement simultaneously?
Yes, it is possible to use fully qualified name and import statement simultaneously.
Example:-
package pack1;
import java.util.Scanner;//import statement
class One
{
     public static void main(String[] args){
               System.out.println("enter any number");
               java.util.Scanner sc = new java.util.Scanner(System.in);
              //fully qualified name
               int i = sc.nextInt();
               System.out.println("the number is :"+i);
        }
}

6) What is the difference between import java.util.* and import java.util.ArrayList?
import java.util.*;
import java.util.ArrayList;
In this approach compiler check for specific class in “util” package
In this approach compiler no need to search for ArrayList class because we specified class name explicitly
It will take some time to import
It will take less time to import
This type of import is called wildcard import statement
This type of import is called fully qualified import statement
If we want to import two or more classes from same package then it reduce import statements
If we want to import two or more classes from same package then it increase import statements
Compiler take more time to compilation
Compiler take less time to compilation

7) Which packages are not required to import in java?
import java.lang package is not required to import. JVM import by default it.

8) Whenever we are importing a package is sub package classes are available?
When declare import statement with specific class name (import java.util.Date;)
Then it will access specific class properties.
When we declare import statement with package name (import java.util.*;)
Then it will access all the classes’ properties of that package.

9) What is the difference between general import and static import?

General import
Static import
General import allows to access classes of a package without package qualification. In normal import we can use class name to access static members
Usually we can access static members by using class name. Whenever we are using static import it is not required to use class name and we can access static members directly 
import java.lang.Math;
import java.lang.*; 
import static java.lang.Math.*;
import static java.lang.Math.random;
import provides accessibility to classes and interface
static import provides accessibility to static members of the class
import java.lang.Math;
public class Pack {
    public static void main(String arg[]){
        System.out.println(Math.random());
    }
  }
import static java.lang.Math.random;
public class Demo {
    public static void main(String arg[]){
        System.out.println(random());
    }
  }

10) Explain the new features introduced in java 1.5 versions?
There are many new features introduced in java1.5 version. Here I included few features only.
Generics, enhanced for loop(for each), Autoboxing, Enums, Var-args, Static import.

11) What is the purpose of package statement? Whenever we are using package statement then how to compile and run java program?
It is an encapsulation mechanism to group of related class and interfaces into a single unit.
Example: the set of all classes and interfaces which can be used for file IO operations are grouped into a separate package is java.io package.
The main advantages of package statement are:
To resolve naming conflicts.
To improve modularity of the application.
To provide security for our components.

Example for compile and run if java program contain package:
package pack1;
class Demo {
               public static void main(String[] args) {
                   System.out.println("Hello World!");
               }

Compile and run as like below:
-d mean destination to place generated .class file (.class file save in pcak1 folder)
. (dot)mean current working directory(we can give C: or E: or F: instead of .(dot))

D:\Java>javac –d Demo.java
D:\Java>java pack1.Demo
     Hello World!

12) Is it possible to take more than one package statement?
It is not possible to take more than one package statement.
package pack1;
class Demo {
               public static void main(String[] args) {
                   System.out.println("Hello World!");
               }
}
 Compile and run as like below:
-d mean destination to place generated .class file (.class file save in pcak1 folder). (dot)mean current working directory(we can give C: or E: or F: instead of .(dot))
D:\Java>javac –d Demo.java
D:\Java>java pack1.Demo
     Hello World!

If we use more than one package we will get compile time error as shown below. 
package pack1;
package pack2;
class Demo {
               public static void main(String[] args) {
                   System.out.println("Hello World!");
               }
} 
Compile time error:
D:\Java>javac –d Demo.java
Demo.java:2: error: class, interface, or enum expected
package pack2;
^
1 error

13) Is it possible to take package statement anywhere?
In any java program the first statement should be package statement, if it is available.
package pack1;
import java.io.*;
class Demo {
               public static void main(String[] args)  {
                    System.out.println("Hello World!");
               }
}
If we take package statements anywhere in the java program, then we will get compile time error.
import java.io.*;
package pack1;
class Demo
{
               public static void main(String[] args) 
               {
                    System.out.println("Hello World!");
               }
}

In the above program we will get compile time error as shown below.
D:\Java>javac –d Demo.java
Demo.java:2: error: class, interface, or enum expected
package pack1;
^
1 error

14) What is the current sequence of statement of java program?
The following is valid java source file structure.
Package statement—at most one
Import statement—any number
Class or interface or enum—any number
Example of java source file structure:-
 package pack1;
import java.io.*;
class Demo
{
               public static void main(String[] args)
               {
                    ystem.out.println("Hello World!");
               }
}
15) Is empty source file valid java program?
Yes, empty java program source file is valid. We can compile and run that empty java file but we don’t get any output because it is empty file. Below all are valid java programs.


1 comment: