Anda di halaman 1dari 7

1. How is Java different than any other OOP language like C++? Explain.

Answer
Java is a simple language that can be learned easily, even if you have just started programming. A Java programmer need not know the internal of Java. The syntax of Java is similar to C++. Unlike C++, in which the programmer handles memory manipulation, Java handles the required memory manipulations, and thus prevents errors that arise due to improper memory usage. Java defines data as objects with methods that support the objects. Java is purely object-oriented and provides abstraction, encapsulation, inheritance and polymorphism. Even the most basic program has a class. Any code that you write in Java is inside a class. Java is tuned of Web. Java programs can access data across the Web as easily as they access data from a local system. You can build distributed applications in Java that use resources from any other networked computer. Java is both interpreted and compiled. The code is compiled to a bytecode that is binary and platform independent. When the program has to be executed, the code is fetched into the memory and interpreted on the users machine. As an interpreted language, Java has simple syntax. When you compile a piece of code, all the errors are listed together. You can execute only when all the errors are rectified. An interpreter, on the other hand, verifies the code and executes it line by line. Only when the execution reaches the statement with error is the error reported. This makes it easy for a programmer to debug the code. The drawback is that this takes more time than compilation. Compilation is the process of converting the code that you type, into a language that the computer understands- machine language. When you compile a program using a compiler, the compiler checks for syntactic errors in code and list all the errors on the screen. You have to rectify the errors and recompile the program to get the machine language code. The Java compiler compiles the code to a bytecode that is understood by the Java environment. Bytecode is the result of compiling a Java program. You can execute this code on any platform. In other words, due to the bytecode compilation process and interpretation by a browser, Java programs can be executed on a variety of hardware and operating systems. The only requirement is that the system should have a Java-enabled Internet browser. The Java interpreter can execute Java code directly on any machine on which a Java interpreter has been installed. Thanks to bytecode, a Java program can run on any machine that has a Java interpreter. The bytecode supports connection to multiple databases. Java code is portable. Therefore, other people can use the programs that you write Java event if they have different machines with different operating systems. Java forces you to handle unexpected errors. This ensures that Java programs are robust (reliable), and bug free and do not crash. Due to strong type-checking done by Java on the users machine, any changes to the program are tagged as error and the program will not execute. Java is, therefore, secure. Java is faster than other interpreter-based language like BASIC since it is compiled and interpreted. Multithreading is the ability of an application to perform multiple tasks at the same time. You can create multithreading programs using Java. The core of Java is also multithreaded. The following definition of Java by Sun Microsystems lists all the features of Java. Java is a simple, Object-oriented, distributed, interpreted, robust, secure, architecture neural, portable, highperformance, multithreaded, and dynamic language.

2. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? Answer
public class Assignment1 { public static void main (String[] args) { //Declare 5 integers int number1; int number2; int number3; int number4; int number5; //Declare input as a Scanner to allow for user input Scanner input = new Scanner(System.in); //Accept input for the 5 numbers System.out.print("Enter the first number: "); number1 = input.nextInt(); System.out.print("Enter the second number: "); number2 = input.nextInt(); System.out.print("Enter the third number: "); number3 = input.nextInt(); System.out.print("Enter the fourth number: "); number4 = input.nextInt(); System.out.print("Enter the fith number: "); number5 = input.nextInt(); //Declare 2 integers, for highest and lowest number intnumHigh; intnumLow; //Assign numHigh and numLow as the first number so we can compare numHigh = number1; numLow = number1; //Find the highest number if (numHigh< number2) {numHigh = number2;} if (numHigh< number3) {numHigh = number3;} if (numHigh< number4) {numHigh = number4;} if (numHigh< number5) {numHigh = number5;} //Find the lowest number if (numLow> number2) {numLow = number2;} if (numLow> number3) {numLow = number3;} if (numLow> number4) {numLow = number4;} if (numLow> number5) {numLow = number5;} //Print out the results System.out.printf("\nThe Highest number is %d\n", numHigh);

} }

System.out.printf("The Lowest number is %d\n", numLow);

This program can compile using Command Prompt and Execute in Interpreter such as Java Virtual Machine

3. Write a simple Java program to illustrate the use of for loop statement. Answer
Code of the program : public class ForLoop{ public static void main(String[] args){ for(int i = 1;i <= 5;i++){ for(int j = 1;j <= i;j++){ System.out.print(i); } System.out.println(); } } } Output of the program : 1 22 333 4444 55555

4. What do you mean by an array? Explain with an example. Answer


Array: Array is the most important thing in any programming language. By definition, array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional. Declaration of an array: intnum[]; or intnum = new int[2]; Some times user declares an array and it's size simultaneously. You may or may not be define the size in the declaration time. such as: intnum[] = {50,20,45,82,25,63}; In this program we will see how to declare and implementation. This program illustrates that the array working way. This program takes the numbers present in the num[] array in unordered list and prints numbers in ascending order. In this program the sort() function of the java.util.*; package is using to sort all the numbers present in the num[] array. The Arrays.sort() automatically sorts the list of number in ascending order by default. This function held the argument which is the array name num. Here is the code of the program:public class array{ public static void main(String[] args){ int num[] = {50,20,45,82,25,63}; int l = num.length; int i,j,t; System.out.print("Given number : "); for (i = 0;i < l;i++ ){ System.out.print(" " + num[i]); } System.out.println("\n"); System.out.print("Accending order number : "); Arrays.sort(num); for(i = 0;i < l;i++){ System.out.print(" " + num[i]); } } } Output of the program: Given number :50 20 45 82 25 63 Ascending order number : 20 25 45 50 63 82

5. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. Answer
Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program. In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes. Code of the program : import java.io.*; public class exceptionHandle{ public static void main(String[] args) throws Exception{ try{ int a,b; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); a = Integer.parseInt(in.readLine()); b = Integer.parseInt(in.readLine()); } catch(NumberFormatException ex){ System.out.println(ex.getMessage() + " is not a numeric value."); System.exit(0); } } }

6. Define (i) Inheritance (ii) Package and (iii) Interface. Answer


(i) Inheritance: Java Inheritance defines an is-a relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used. Class Inheritance in java mechanism is used to build new classes from existing classes. The inheritance relationship is transitive: if class x extends class y, then a class z, which extends class x, will also inherit from class y. For example a car class can inherit some properties from a General vehicle class. Here we find that the base class is the vehicle class and the subclass is the more specific car class. A subclass must use the extends clause to derive from a super class which must be written in the header of the subclass definition. The subclass inherits members of the superclass and hence promotes code reuse. The subclass itself can add its own new behavior and properties. The java.lang.Object class is always at the top of any Class inheritance hierarchy. (ii) Package: A Java package is a set of classes which are grouped together. Every class is part of some package. All classes in a file are part of the same package. You can specify the package using a package declaration: package name ; as the first (non-comment) line in the file. Multiple files can specify the same package name. If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files). If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name). You can access public classes in another (named) package using: package-name.class-name You can access the public fields and methods of such classes using: package-name.class-name.field-or-metho You can avoid having to include the package-name using: import package-name.*; Examples of java packages: java.applet,java.awt and java.awt.coloretc (iii) Interface: An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interfacekeyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final). An interface may never contain method definitions. Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class. Object references in Java may be specified to be of an interface type; in which case, they must either be null, or be bound to an object that implements the interface.

Anda mungkin juga menyukai