– public: accessible anywhere by anyone
– private: Only accessible within this class
– Protect: accessible only to the class itself and to it’s subclasses or other classes in the same package.
– Default: default access if no access modifier is provided. Accessible to all classes in the same package.
Q: What is the difference between Abstract and Interface? When should we use any of these two?
Ans: An abstract class is a class which may have the usual flavors of class members (private, protected, etc.) with some abstract methods. An abstract class can have instance methods with default behavior. A class can extend only one abstract class. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. Methods are implicitly public and variables are implictly public static Final. Which one to choose depends on the design requirement.
Ans: HashMap is unsynchronised while HashTable is synchronised but we can synchronise the HashMap with use of Collections.synchronizedMap(new HashMap()); HashMap permits null values in it, while Hashtable doesn’t. HashTable has method which Returns an enumeration of the values in hashtable which is not fail safe. But there is no such method in HashMap.
Ans: Checked exceptions are subclass’s of Exception excluding class RuntimeException and its subclasses. Checked Exceptions forces programmers to deal with the exception that may be thrown. Example: Arithmetic exception. When a checked exception occurs in a method, the method must either catch the exception and take the appropriate action, or pass the exception on to its caller.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are Unchecked exceptions, compiler doesn’t force the programmers to either catch the exception or declare it in a throws clause. In fact, the programmers may not even know that the exception could be thrown. Example: ArrayIndexOutOfBounds Exception. They are either irrecoverable (Errors) and the program should not attempt to deal with them, or they are logical programming errors. (Runtime Exceptions). Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.
Q: What is use of static for Main method and what will happen if we remove static keyword from signature?
Ans: Static method can be accessed without creating any object. In this way main method provides the entry point of the program. If we remove static keyword it will not be access by JVM and program can not be started.
Ans: In Java everything except primitives are object. These primitives don’t get the benefit of being object. Wrapper classes are created to provide the same to primitives. Integer class is wrapper for int primitive.
What is the Java Virtual Machine? What is its role?
Java was designed with a concept of ‘write once and run everywhere’. Java Virtual Machine plays the central role in this concept. The JVM is the environment in which Java programs execute. It is software that is implemented on top of real hardware and operating system. When the source code (.java files) is compiled, it is translated into byte codes and then placed into (.class) files. The JVM executes these byte codes. So Java byte codes can be thought of as the machine language of the JVM. A JVM can either interpret the byte code one instruction at a time or the byte code can be compiled further for the real microprocessor using what is called a just-in-time compiler. The JVM must be implemented on a particular platform before compiled programs can run on that platform.
Java has powerful features. The following are some of them:-
Portable (Platform Independent)
Abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.
* Hides the implementation details of a class.
* Forces the user to use an interface to access data
* Makes the code more maintainable.
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the existence of the classes or methods in different forms or single name denoting different implementations.
With extensive set of routines to handle TCP/IP protocols like HTTP and FTP java can open and access the objects across net via URLs.
One of the powerful aspects of the Java language is that it allows multiple threads of execution to run concurrently within the same program A single Java program can have many different threads executing independently and continuously. Multiple Java applets can run on the browser at the same time sharing the CPU time.
Java was designed to allow secure execution of code across network. To make Java secure many of the features of C and C++ were eliminated. Java does not use Pointers. Java programs cannot access arbitrary addresses in memory.
Automatic garbage collection is another great feature of Java with which it prevents inadvertent corruption of memory. Similar to C++, Java has a new operator to allocate memory on the heap for a new object. But it does not use delete operator to free the memory as it is done in C++ to free the memory if the object is no longer needed. It is done automatically with garbage collector.
Helping Material CS506 Mid Term Spring 2012
MUHAMMAD IQBAL (MCS) ATTOCK
Q.1: How can a GUI component handle its own events? 3 marks
Ans: By implementing the required event-listener interface and adding itself as its own event listener.
Q.2 What Difference between serialization and de-serialization? 3 marks
Ans: Serialization and Deserialization are complementing to each other.
Whenever the state of an Object is saved to some file, the process is known as Serialization.
Deserialization is the process of creating the object back from the serialized file.
Q.3 Write Different between abstract class and interface class? 2 marks
Ans: An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
Q.4 Write 4 types of JDBC Driver. 2 marks
Ans: The four types are:
Q.5: If a method is declared as protected where can be the method accessed? 2
Ans: A protected method may only be accessed by classes or interfaces of the same package or by subclasses of the class in which it is declared.
Q.6: What is the difference between Applet and Application? (5)
Ans: In simple terms, an applet runs under the control of a browser, whereas an application runs stand-alone, with the support of a virtual machine. As such, an applet is subjected to more stringent security restrictions in terms of file and network access, whereas an application can have free reign over these resources.
Applets are great for creating dynamic and interactive web applications, but the true power of Java lies in writing full blown applications. With the limitation of disk and network access, it would be difficult to write commercial applications (though through the user of server based file systems, not impossible). However, a Java application has full network and local file system access, and its potential is limited only by the creativity of its developers.
Q.7 Can an anonymous class be declared as implementing an inheritance and extending a class? (3)
Ans: No. An anonymous class can extend a class or implement an interface, but it cannot be declared to do both.
Q.8: Difference between character oriented and byte oriented streams (2)
Ans: A byte stream access the file byte by byte. A byte stream is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself.
A character stream will read a file character by character. A character stream needs to be given the file’s encoding in order to work properly.
Q.9: Why is executeUpdate() used (2)
Ans: executeQuery() — This is used generally for reading the content of the database. The output will be in the form of ResultSet. Generally SELECT statement is used.
executeUpdate() — This is generally used for altering the databases. Generally DROP TABLE or DATABASE, INSERT into TABLE, UPDATE TABLE, DELETE from TABLE statements will be used in this. The output will be in the form of int. This int value denotes the number of rows affected by the query.
Q.10: Name component subclasses that support painting (2)
Ans: The Canvas, Frame, Panel, and Applet classes support painting.
Q.11: Does garbage collection guarantee that a program will not run out of memory?
Ans: No, it doesn?t. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.
Q.12: What is advantage of collection over array? 
Ans: The main advantage of the arrayList is that its provides a collection similar to an array, but difference is that it grows dynamically as the number of elements increase or decrease.
Below are the advantages of the ArrayList over array
1) An array can be of any data type and contain only one data type while array list can contain any data type.
2) With array we cannot increase or decrease the size of array dynamically. We must the define the size of the array. We can change the size of the array with redim statement but still you have to define type. On other hand, with array list you can make list of any sizes.
Q.13: Which method of ResultMetaData objective is used to return the suggested value to column label for printout?
Ans: An object that can be used to get information about the types and properties of the columns in a
ResultSet object. The following code fragment creates the
ResultSet object rs, creates the
ResultSetMetaData object rsmd, and uses rsmd to find out how many columns rs has and whether the first column in rs can be used in a