You are on page 1of 6

BC0047 JAVA PROGRAMMING

1. What do you mean by Java Virtual Machine? Explain. The key that allows Java to solve both the security and the portability problems are that the output of a Java compiler is not executable code. Rather, it is byte code. Byte code is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for byte code. Translating a Java program into byte code helps make it much easier to run a program in a wide variety of environments. The reason is straightforward: only the JVM needs to be implemented for each platform. Once the run-time package exists for a given system, any Java program can run on it. Although the details of the JVM will differ from platform to platform, all interpret the same Java byte code. If a Java program were compiled to native code, then different versions of the same program would have to exist for each type of CPU connected to the Internet. This is, of course, not a feasible solution. Thus, the interpretation of byte code is the easiest way to create truly portable programs. The fact that a Java program is interpreted also helps to make it secure. Because the execution of every Java program is under the control of the JVM, the JVM can contain the program and prevent it from generating side effects outside of the system.

2. Write a program in Java to find factorial of a number. How do you compile and execute this Java program? import java.util.Scanner; public class Factorial { public static void main (String[ ] args) { System.out.println("Enter the number for " + "factorial calculations : "); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int result = fact(a); System.out.println("Factorial of " +a+ " is :"+result); } static int fact(int b){ if(b <= 1) return 1; else return b * fact(b-1); } } The programs that we write in Java should be saved in a file, which has the following name format: <class_name>.java Compiling Java Programs: A program is a set of instructions. In order to execute a program, the operating system needs to understand the language. The only language an operating system understands is in terms of 0s and 1s i.e. the binary language. Programs written in language such as C and C++ are converted to binary code during the compilation process. However, that binary code can be understood only by the operating system for which the program is compiled. This makes the program or application operating system dependent. In Java, the program is compiled into byte code (.class file) that runs on the Java Virtual Machine, which can interpret and run the program on any operating system. This makes Java programs platform-independent. At the command prompt, type javac <filename>.java Executing a Java Program: When the code is compiled and error-free, the program can be executed using the command: java <filename>

3. Write a simple Java program to illustrate the use of if else statement. // This program checks whether the student has passed or failed public class Result { public static void main (String arg[]) { int mark=45; if (mark < 35) System.out.println(The Student has failed); else System.out.println(The Student has passed); } }

4. What do you mean by an array? Explain with an example. An array represents a number of variables which occupy contiguous spaces in the memory. Each element in the array is distinguished by its index. All elements in an array must be of the same data type. For example, you cannot have one element of the int data type and another belonging to the boolean data type in the same array. An array is a collection of elements of the same type that are referenced by a common name. Each element of an array can be referred to by an array name and a subscript or index. To create and use an array in Java, you need to first declare the array and then initialize it. data type [ ] variablename; Example int [ ] numbers; The above statement will declare a variable that can hold an array of the int type variables. After declaring the variable for the array, the array needs to be allocated in memory. This can be done using the new operator in the following way: numbers = new int [10]; This statement assigns ten contiguous memory locations of the type int to the variable numbers. The array can store ten elements. Iteration can be used to access all the elements of the array, one by one.

5. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. public class ExcepTest{ public static void main(String args[]){ int a[] = new int[2]; try{ System.out.println("Access element three :" + a[3]); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception thrown :" + e); } finally{ a[0] = 6; System.out.println("First element value: " +a[0]); System.out.println("The finally statement is executed"); } } } This would produce following result: Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed

6. Define (i) Inheritance (ii) Package and (iii) Interface. Inheritance: The philosophy behind inheritance is to portray things as they exist in the real world. For instance, a child inherits properties from both the parents. Inheritance means that a class derives a set of attributes and related behaviors from another class. Inheritance helps you to reduce redundancy in code. Code redundancy means writing the same code in different places, leading to unnecessary replication of code. Inheritance helps you to reuse code. Maintain code easily, as the code resides at one place (superclass). Any changes made to the superclass automatically change the behavior automatically. Inheritance helps you to extend the functionality of an existing class by adding more methods to the subclass. Package: A unique name had to be used for each class to avoid name collisions. After a while, without some way to manage the name space, you could run out of convenient, descriptive names for individual classes. You also need some way to be assured that the name you choose for a class will be reasonably unique and not collide with class names chosen by other programmers. Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is the package. The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world. Interface: Using the keyword interface, you can fully abstract a class' interface 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. In practice, this means that you can define interfaces which don't make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism. Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. This requirement by itself makes for a static and nonextensible classing environment. Inevitably in a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface.

You might also like