You are on page 1of 9

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

Ans. The key that allows Java to solve both the security and the portability problems just described is that the output of a Java compiler is not executable code. Rather, it is bytecode. Bytecode 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 bytecode. This may come as a bit of a surprise. As you know, C++ is compiled to executable code. In fact, most modern languages are designed to be compiled, not interpreted mostly because of performance concerns. However, the fact that a Java program is executed by the JVM helps solve the major problems associated with downloading programs over the Internet. Java is robust, consider two of the main reasons for program failure: memory management mistakes and mishandled exceptional conditions (that is, run-time errors). Memory management can be a difficult, tedious task in traditional programming environments. For example, in C/C++, the programmer must manually allocate and free all dynamic memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to free some memory that another part of their code is still using. Java virtually eliminates these problems by managing memory allocation and deallocation for you. (In fact, deallocation is completely automatic, because Java provides garbage collection for unused objects.) Exceptional conditions in traditional environments often arise in situations such as division by zero or "file not found," and they must be managed with clumsy and hard-to-read constructs. Java helps in this area by providing object-oriented exception handling. In a wellwritten Java program, all run-time errors can and should be managed by your program.

2. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program?

Ans. int[] nums; // assume this is an array of length 5 and you want to find the largest // finding it with a loop int max = nums[0]; for(int i = 1; i < nums.length; ++i) { if(nums[i] > max) { max = nums[i]; } } // max is now the largest

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 bytecode (.class file) that run 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 for loop statement. Ans.

for Loop The usage of for loop is as follows for (initial statement; termination condition; increment instruction) statement; When multiple statements are to be included in the for loop, the statements are included inside flower braces. for (initial statement; termination condition; increment instruction) { statement1; statement2;

} The example below prints numbers from 1 to 10

//print numbers from 1 to 10 class Test { public static void main(String arg[]) { int i; for(i=1;i<=10;i++) { System.out.println(i); } } }

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

Ans. Arrays 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.

Ans. When an exception is raised, the rest of the statements in the try block are ignored. Sometimes, it is necessary to process certain statements irrespective of whether an exception is raised or not. The finally block is used for this purpose. try { openFile(); writeFile(); //may cause an exception } catch () { //process the exception } In the above example, the file has to be closed irrespective of whether an exception is raised or not. You can place the code to close the file in both the try and catch blocks. To avoid duplication of code, you can place the code in the finally block. The code in the finally block is executed regardless of whether an exception is thrown or not. The finally block follows the catch blocks. You have only one finally block for an exception-handler. However, it is not mandatory to have a finally block.

finally { closeFile (); }

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

Ans. Inheritance Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and add its own, unique elements.
Packages 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 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. 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.

Assignment Set 2 (40 Marks)

1. What are the uses of FileInputStream and FileOutputStream? Write short notes on each

Ans. The FileInputStream and FileOutputStream Classes These streams are classified as mode streams as they read and write data from disk files. The classes associated with these streams have constructors that allows you to specify the path of the file to which they are connected. The FileInputStream class allows you to read input from a file in the form of a stream. The FileOutputStream class allows you to write output to a file stream. Example FileInputStream inputfile = new FileInputStream (Employee.dat); FileOutputStream outputfile = new FileOutputStream (binus.dat);
2. Write an applet program to change the background colour of an applet window as soon as you click on a button?

Ans. // A button which changes the background colour // of the applet window when a button is clicked import java.applet.*; import java.awt.*; public class ChangeColor extends Applet { Button switch = new Button ("click"); boolean light = true; public void init () { add (switch); } public void paint (Graphics g) { if (light) setBackground (Color.lightGray); else setBackground (Color.darkGray); } public boolean action (Event e, Object o) { if (e.target == switch) { light = !light; repaint(); return true; } return false; }

3. What are the uses of ODBC, JDBC and Driver Manager?.

Ans. ODBC ODBC is an abbreviation of Open Database Connectivity, a standard database access method developed by Microsoft Corporation. The goal of ODBC is to make it possible to access any data from any application, regardless of which database management system (DBMS) is handling the data. ODBC manages this by inserted a middle layer, called a driver, between an application and the DBMS. The purpose of this layer is to translate the queries of the application into commands that the DBMS understands. For this to work, both the application and the DBMS must be ODBC-compliant-that is, the application must be capable of issuing ODBC commands and the DBMS must be capable of responding to them. JDBC JDBC provides a database-programming interface for Java programs. Since the ODBC is written in C language, a Java program cannot directly communicate with an ODBC driver. JavaSoft created the JDBC-ODBC Bridge driver that translates the JDBC API to the ODBC API. It is used with ODBC drivers. JDBC Driver Manager The JDBC driver manager is the backbone of the JDBC architecture. The function of the JDBC driver manager is to connect a Java application to the appropriate driver.
4. Write short notes on (i) RMI and (ii) CORBA

Ans. RMI Terminology RMI is built upon the specification of how local and remote objects interoperate. Local objects are objects that execute on the local machine. Remote objects are objects that execute on all the other machines. Objects on remote hosts are exported so that they can be invoked remotely. An object exports itself by registering itself with a Remote Registry Server. A Remote Registry Server is a service that runs on a server and helps the objects on other hosts to remotely access its registered objects. The registry service maintains a database of all the named remote objects.

A Distributed Application Created Using RMI Objects that are exported for remote access must implement the interface RemoteInterface. This interface identifies the object to be accessed remotely. All the methods that are to be invoked remotely must throw a RemoteException. The exception is used to handle the errors that may occur during the invocation of a remote method. Javas RMI approach is organized into a client/server framework. A local object that invokes a method of a remote object is referred to as a client object, and the remote object whose methods are invoked is referred to as a server object. Javas RMI approach makes use of stubs and skeletons. A stubs is a local object on the clients machine that acts as a proxy for a remote object. The stub provides the methods of the remote object. Local objects invoke the methods of the stubs as if they were methods of the remote objects. The stub communicates this method invocation to the remote object through a skeleton that is implemented on a remote host. The skeleton is the proxy of the client machines object that is located on the remote host.

Usage of Stubs and Skeletons to support Client/Server Communication The stub and the skeleton communicate through a remote reference layer. This layer provides stubs the capability to communicate with skeletons through a transport protocol. RMI uses the TCP protocol for transporting information.
CORBA

CORBA stands for Common Object Request Broker Architecture. RMI, discussed in previous section with RMI is that, it needs the 2 objects participating in communication be written in Java CORBA is a distributed computing technology where the participating objects need not only be written in Java.

Java IDL is a technology for distributed objects-that is, objects interacting on different platforms across a network. Java IDL is based on the Common Object Request Brokerage Architecture (CORBA), an industry-standard distributed object model. A key feature of CORBA is IDL, a language-neutral Interface Definition Language. Each language that supports CORBA has its own IDL mapping and as its name implies, Java IDL supports the mapping for Java. CORBA and the IDL mappings are the work of an industry consortium known as the OMG, or Object Management Group. To support interaction between objects in separate programs, Java IDL provides an Object Request Broker, or ORB. The ORB is a class library that enables low-level communication between Java IDL applications and other CORBA-compliant applications.
5. Write an essay on history of web application.

Ans. History of Web Application While servlets can be used to extend the functionality of any Java-enabled server, today they are most often used to extend web servers, providing a powerful, efficient replacement for CGI scripts. When you use servlet to create dynamic content for a web page or otherwise extend the functionality of a web server, you are in effect creating a Web application. While a web page merely displays static content and lets the user navigate through that content, a web application provides a more interactive experience. A web application may be as simple as a key word search on a document archive or as complex as an electronic storefront. Web applications are being deployed on the internet and on corporate intranets and extranets, where they have the potential to increase productivity and change role of servlets in any web application it is necessary to understand the architecture of any current web application.

You might also like