Interview
questions on java development. In this article I have given solutions in programmatically
and diagrammatically for each and every question.
Here
the questions are how to compile and run java programs, how to compile multiple
java source files etc.
1) What is the
purpose of javac command?
We use javac
command to compile a single or group of java source files.
Example:- (D:\Java)
class Test {
public static void main(String[] args) {
System.out.println("Hello Test Class");
}
}
D:\Java>javac
Test.java
We can compile
multiple classes simultaneously.
D:\Java>javac
One.java Two.java
Or
D:\Java>javac
*.java
All java source
files will be compile which are available in D:\Java
Example:- (D:\Java)
package com.programmer;
class Test {
public static void main(String[] args) {
System.out.println("Hello Test Class");
}
}
If a java program
contain a package then we should compile as shown in the below.
D:\Java>javac
–d Test.java
After compile the
above program that corresponding .class file will be generate in the following
folder Structure.
D:\Java\com\programmer
2) What is the
purpose of java command?
The purpose of java
command is to execute .class file.
Example:- (D:\Java)
class Test {
public static void main(String[] args) {
System.out.println("Hello Test Class");
}
}
D:\Java>java
[options] Test
Options (-version,
-cp/-classpath, -ea/-esa/-da/-dsa, -D)
“Test” is name of
the .class file.
D:\Java>java
Test
Output:
Hello Test
class
If the .class file
in other directory then we can run as shown in the below.
C:\>java
–cp D:/Java Test
Output:
Hello Test
class
Or
C:\>java
–classpath D:/Java Test
Output:
Hello Test
class
If .class file
contain in current directory then we use .(dot) instead of D:
D:\>java
–cp ./Java Test
Output:
Hello Test
class
In the above
command Test.class file available in D:/Java folder.
Example:- (D:\Java)
package com.programmer;
class Test {
public static void main(String[] args) {
System.out.println("Hello Test Class");
}
}
D:\Java>java
com.programmer Test
Output:
Hello Test class
We can run .class
file from another directory as sown in the below.
C:\>java
–cp D:/Java com.programmer Test
Output:
Hello Test
class
3) Is it
possible to compile multiple classes simultaneously?
Yes, it is possible
to compile multiple classes simultaneously. But we can run only single .class
file at a time.
D:\Java>javac
One.java Two.java
Or
D:\Java>javac
*.java
4) What is the
difference between path and class path?
Path and classpath
are system level environment variables.
Path is used to
define where the system can find the executable files (.exe) whereas classpath
is used to specify the location of .class file.
Example of path:
If we want compile
and execute a java program, we must required that corresponding files (java.exe
and javac.exe). this files are available in the following location “C:\Program Files\Java\jdk1.8.0_31\bin” so we set this path in environment variables as path
variable value.
Example for
CLASSPATH:
classpath
describes the location where required .class files are available. By default
JVM will search for required .class file in the current working directory.
C:\>java
–classpath D:/Java Test
Output:
Hello Test
class
In
the above command Test.class file available in D:/Java, when we run that
command JVM will search for Test.class file in the D:/Java location.
5) What are
various ways to set classpath? And which approach is recommended?
There
are 3 ways to set java classpath:
By using environment variable classpath:-
By using environment variable classpath:-
Go to MyComputer
properties -> advanced tab -> environment variables -> new tab of user
variable -> write path in variable name -> past the path of bin folder
location in variable value -> click ok.
This way of set
classpath is permanent.
By
using set command at command prompt:-
Copy the location of .class file and open command prompt pest that location as shown in the below.
Copy the location of .class file and open command prompt pest that location as shown in the below.
C:\>set
classpath=D:/Java
C:\>java
Test
Output:
Hello Test
Class
This
way of setting classpath is available only for the current command prompt. Once
we close command prompt we will be lost the classpath.
By using –cp/-classpath option at command level:-
By using –cp/-classpath option we set classpath at command level. Once command execute we lost classpath again we set and execute
Among 3 ways this is approach is recommended because dependent classes are varied from command to command.
By using –cp/-classpath option at command level:-
By using –cp/-classpath option we set classpath at command level. Once command execute we lost classpath again we set and execute
Among 3 ways this is approach is recommended because dependent classes are varied from command to command.
C:\>java
-classpath D:/Java Test
Output:
Hello Test
Class
6) Consider the
command java –cp C:;D:;E: if the required .class file is available in C:, D:,
E: location then which class file will be considered?
If we have
Test.class file in C:, D: and E: directories then if we use command
as following in the below.
E:\>java
-cp D:;. Test
D directory Test
Class
In the above
command first JVM search for Test.class file in D: location if it is available
in D: then
execute otherwise
search in next directory .(dot mean current working directory E:).
E:\>java
-cp .;D: Test
E directory Test
Class
In the above
command first JVM search for Test.class file in E: directory .(dot mean current
working
directory E:) if it
is not available in E: then search in D: location if it is available in D: then
execute.
7) What is the
purpose of jar file?
A Jar file contains
mostly .class files and optionally other files like sound files, image files
for Java applications, gathered into a single file in the compressed format.
If we grouped all
those .class files into a zip file then we can make that zip file available in
classpath. No need to set classpath individually to each and every .class file.
Example:- to write JDBC based application all dependent
classes are available in “OJDBC14.jar” , we have to place this jar file in
classpath then only our JDBC program will be compile and run.
8) Explain the
process of creation and extraction of jar file?
Create a JAR file
Syntax: jar [options] file_names
Various options to create a JAR file
jar –cvf
calculator.jar A.class
jar –cvf
calculator.jar A.class B.class
jar –cvf
calculator.jar *.class
jar –cvf
calculator.jar *.*
c mean create.
v mean verbose output.
f mean filename.
Calculator is jar
file name, here .class files are grouped into calculator.jar file.
Manifest file
contain the .class file name which class contain main method.
Example:-
Creation and
execution of jar file
D:\Java>jar
–cvfm testjar.jar manifest.MF Test.class Test$1.class
D:\Java>java
–jar testjar.jar
View contents of a JAR file
Syntax: jar tf jar_file_name
Example: D:\Java>jar tf Demo.jar
View contents with detail of a JAR file
Syntax: jar tvf jar_file_name
Example:
D:\Java>jar
tvf Demo.jar
Executing a JAR file
Syntax: java -jar jar_file_name
Example:
D:\Java>java
–jar Demo.jar
9) How many ways we can run java program?
We can run a java program
in the following 4 ways.
1. From command
prompt we can run .class file with “java” command.
2. From command
prompt we can run .jar file with “java” command.
3. By double
clicking a jar file.
4. By double
clicking a batch file.
Batch file:- batch file contains commands, what command we want
to execute we can write that command in a notepad and save it as abc.bat
(with .bat extension) and execute it in command prompt
Example:-“java Test” save this in abc.bat file and open command
prompt execute it
D:\Java>abc
then Test.class file will be executes.
10) What is the
difference between jar,war,ear?
There
are no structural differences between the files; they are all archived using
zip/jar compression. However, they are intended for different purposes.
Jar files (java
archive):-
A jar file contains
a group of .class files. The extension of jar file is .jar.
Example
of jar creation:-
jar –cvf
calculator.jar *.class
War files (web
archive):-
A war file
represents a web application which contains servlets, jsp, HTML, CSS etc.
The main advantage
of maintaining web application in the form of “war” file is project delivery,
project transportation and project deployment will become easy.
Example
of war creation:-
jar –cvf
mywebapp.war *.*
Ear files (enterprise
archive):-
An “ear” file
represents an enterprise application which contains jsp, servlet, Ejb, jms
components etc.
In general “ear
file contain several “war and jar” file.
Example
of ear creation:-
jar –cvf
myapp.ear *.*
11) What is the
difference between jdk,jre,jvm?
JVM
JVM stands for Java
Virtual Machine, it is an interpreter which provides the runtime environment in
which java byte code can be executed line by line.
JVMs are available
for many hardware and software platforms (so JVM is platform dependent).
JRE
JRE stands for Java
Runtime Environment to run java application. It is the implementation of JVM
and physically exists. It contains JVM+set of library file (like .jar files)
and other files.
JDK
JDK stands for Java
Development Kit to develop and run java applications. It is physically exists.
It contains JRE + development tools(like java, javac etc.).
12) Explain
about system properties and write program to display all system properties?
For every system
some persistence information will be maintain in the form of system properties.
This many include OS name, Java version, JVM vendor etc.
Program to display
system properties:
import java.util.*;
class Test
{
public static void main(String[] args)
{
Properties p=System.getProperties();
p.list(System.out);
}
}
13) How to set system properties from command prompt?
We can set system
properties from the command prompt by using “-D” option
D:\Java>java
–Dprogrammer=developer Test
-D means to set
system property, Programmer mean property name, and Developer mean property
value.
The main advantage
of setting system property is we can customize behavior of java program.
14) What is Web
application and Enterprise application?
A web application
can be developed by only web related technologies like servlet, jsp, HML, CSS
etc.
Example:- library management system
An enterprise
application can be developed by any technology from java/j2ee like servlet, jsp
,ejb, jms components etc.
Example:- banking application telecom based applications
15) What are the
web server and enterprise server?
Web server provides
environment to run web application. Web server provides support only for web
applications.
Web servers:- tomcat, oracle HTTP Server.
Application server
provides environment to run enterprise application. Application server can
provides support for any technologies like web applications and enterprise
applications.
Application servers:- weblogic, websphere, jboss etc.
the blog is about Java Development #java it is useful for students and java Developers for more updates on python follow the link
ReplyDeleteJava online training