Java Programming
Java Programming
Set-1
1. Define the concept of Java Bytecode and its importance.
Ans: Bytecode is highly optimized set of instruction deigned to executed by the Java-
run-time system, which is called the Java Virtual Machine (JVM). In standard form
JVM is an interpreter for bytecode.
Translating a Java program into bytecode helps make it much easier to run a
program in a wide variety of environment. The reason is straightforward : only the
JVM need to be implemented for each platform. Once the run time package exists
for a given system, any java program can run on it. JVM will be differ from platform to
platform, all the interpret the same Java bytecode. The interpreter of bytecode is the
easiest way to create truly portable programs. A java program is interpreted also
helps to make it secure.
2. How will you compile a Java program?
Ans: A compiler converts the Java program into an intermediate language
representation called Bytecode which is platform independent. A Java file will have
the extension .java.
Suppose I have created a java file named Dharmendra.java. When this file is
compiled we get a file called as Dharmendra.class. Now after compiling the
program into bytecode(.class file) that run on the Java Virtual Machine which can
interpret and run the program on any operating system.
This make Java programs platrform-independent.
3. Write a program to perform the basic arithmetic operations:
a) Addition
b) Subtraction
c) Multiplication
d) Division
Use 4 different methods for the above and invoke them as necessary from
the main() method.
4. Explain the concept of interfaces in Java with a suitable example for the
same.
Ans: With the help of interface you can fully abstract a class’ from its
implementation
That is using interface, you can specify what a class must do but not
how it does it. Interfaces are syntactically similar to classes, but they
lack instance variables, and their methods are declared without any
body. By providing the interface keyword, java allows you to fully
utilize the “one interface, multiple methods” aspect of polymorphisms.
5. What are the different exception handling techniques availablein Java.
Ans: When an unexpected error occurs in a method java creates an
object of the appropriate exception class. After creating the exception
objects, java passes it to the program, by an action called throwing an
excetion. The exception object contains information about the type of
error and the state of the program when the exceptinon-handler and
process the exception.
Following keywords are use in exception-handling in java:
a) try
b) catch
c) finally
The try block :-
The statements that may throw an exception in the try block.
The following skeletal code illustrates the use of the try block.
Try{
//statement that may cause an exception
}
The try block governs the statements that are enclosed within it and
defines the scope of the exception handlers associated with it. In other
words if an exception occurs within try block the appropriate
exception-handler that is associated with the try block handles
The exception. A try block must have at least one catch block that
follow it immediately. The try statement can be nested. That is a try
statement can be inside the block of another try. Each time a try
Cybotech Campus Page 3
Java Programming BT0052
statement is entered, the context of that exception is pushed on the
stack. If an inner try statement does not have a catch handler for a
particular exception the stack is unwound and the next try statement‘s
catch handlers are inspected for a match.
Example:
Class Nesttry{
public static void main(String args[])
{
try{
int a =args.length;
int b=42/a;
System.out.println(“a=”+a);
try{
if(a==1) a=a/(a-a);
if (a==2){
int c[]={1};
c[42]=99;
}
}
catch(ArrayIndexOutOfBoundsException e )
{}
}catch(ArithmeticException e)
{
System.out.println(“Divide by 0:” + e);
}
}
}
}
public Insets getInsets()
{
setBackground(c);
return new Insets(100,80,80,80);
}
}