Вы находитесь на странице: 1из 14

JAVA questions :(Dont panic.. Most are 2 - 3 mark questions.) 1)String and Stringbuffer class and methods.

2)Programs on manipulation of strings using string class methods. 3)Vector class and vector methods.Wrapper class,advantages and methods. 4)Programs to convert to decimal to hexadecimal, to octal and to binary and vice versa. 5)Thread life Cycle, methods of creating thread with examples. 6)Producer- Consumer program using threads 7)Applet programs done in class. 8)Swings- Lightweight and heavyweight components 9)JDBC driver types and when to use it. 10)Comparision of byte-oriented and Character-oriented streams.( Read from balaguruswamy ) 11)Program to read and write the appropriate datas into console like Emp Name, Emp Number, Age using primitive data type I/O interfaces? 12)Write a string of text "We are enjoying this" to a file in 3 ways. (i) byte by byte (ii)all at once (iii)in part using FileOutputStream.

Q2 What is a String? In java and unlike C++, the string data type is now an object type. Notice that the class name is starts with an uppercase. If you mess this up, Java will not know what you are referring to. By it's conception, a string is anything and everything grouped together between 2 double quotes. For example, "Hello world" is one string while "sdad anesd ccn " is another. Using Strings Strings can be manipulated in numerous ways. The first is with some member functions of the string class. Let's see an example first before we continue.
public class StringExample{ public static void main(String args[]){ String word; //assign the string to the variable: word = "Alexander"; //preform some actions on the string: //1. retrieve the length by calling the //length method: int length = word.length(); System.out.println("Length: " + length); //2. use the case functions: System.out.println("toUpperCase: " + word.toUpperCase()); System.out.println("toLowerCase: " + word.toLowerCase()); //3. use the trim function to eliminate leading //or trailing white spaces: word = word.trim(); System.out.println("trim: " + word); //4. check for a certain character using indexOf() System.out.println("indexOf('s'): " + word.indexOf('s'));

//5. print out the beginning character using charAt() System.out.println("first character: " + word.charAt(0)); //6. make the string shorter word = word.substring(0, 4); System.out.println("shorter string: " + word);

Length: 9

toUpperCase: ALEXANDER toLowerCase: alexander trim: Alexander indexOf('s'): -1 first character: A shorter string: Alex A lot certainly has happened in this program. Let's observe some of the most used functions in the String class. int length() The length function will simply return the length of a string as an integer. String toUpperCase() The toUpperCase function will return the uppercase version of a string. Say you have a string "Welcome". This function will return "WELCOME". String toLowerCase() The toLowerCase function will return the lowercase version of a string. Say you have a string "Welcome TO Earth". This function will return "welcome to earth". String trim()The trim function will return the string without leading or trailing white space characters. Say that a string was " hello ". There are 4 spaces in the front and 3 spaces at the end. The trim function would make this "hello".

int indexOf(int ch) int indexOf(int ch, int begin) int indexOf(String ch) int indexOf(String ch, int begin) Notice that there are 4 different functions listed here. All of them preform the same overall action of returning an integer, which represents the FIRST OCCURRANCE of a character or String contained in that string. So say that we have the string "Hello" and we say indexOf('l'). This function will use the first function above and return 2. Notice it doesn't reutrn 3. We can also say, using the same string "Hello", indexOf("He"). It will return 0 as the string that you are searching for starts at index 0. By default, if a string or character is not found in the string, the any of the functions will return 1. char charAt(int index) This function will look for a character at a specific index. It will return that character if it is found. Say we have the string "Hello" and we say charAt(4). It will return 'o' as that character is at index 4. String substring(int begin) String substring(int begin, int end) Either of these functions will shorten a string when called. If no ending index is specified, it will return the rest of that string. Say we have the string "Hello there" and we say substring(4), the function will return "o there". Let's use substring(2,5), the function will return "llo". String Equalities With Java, there are numerous methods they provide that check for two strings being equal. To show this, here is a small program that will check for a certain string as a command line argument.

Example 2: String equalities Download source code here (Right click - Save Tagret As...) public class StringExample2{ private static String word = null; private static String keyword = "HELLO"; public static void main(String args[]){ //standard error check: if(args.length < 1){ System.out.println("Argument requires one word."); System.exit(1); } //retreive the sentence: word = args[0]; //check for the keyword using different equals methods: if(word.equals(keyword)){ System.out.println("equals true: " + word); } if(word.equalsIgnoreCase(keyword)){ System.out.println("ignoreCase true: " + word); } //check for the keyword using different compareTo methods: if(word.compareTo(keyword) != -1){

System.out.println("found using compareTo: " + word.compareTo(keyword)); } if(word.compareToIgnoreCase(keyword) != -1){ System.out.println("found using compareToIgnoreCase: " + word.compareToIgnoreCase(keyword)); } } } This program will accept as a command-line argument, one word which we will call our "keyword". It will then preform 4 checks using different member functions of the String class. Here is the output when you run the program with the argument "hello" SPELT EXACTLY AS IS (all lowercase). ignoreCase true: hello found using compareTo: 32 found using compareToIgnoreCase: 0 Let's discover why. Here are the descriptions of the member functions: boolean equals(String another) The equals() function will take another String as its parameter and check if it is EXACTLY like the initial String. The initial string is what you called the member function from. So in the above program, the initial string is the variable word. This method reutrns a boolean value. If true, the string parameter is an exact copy of the initial string. If false, the argument is not a copy as something differs in the string. As noted, the above program returns false when the equals() method is used because of the case difference. boolean equalsIgnoreCase(String another)

This method is similar to the regular equals() method with the exception of this method not taking into account the case (either upper or lower) of BOTH strings. So in the program above, this function returns true because of the ignoring of the case. If the keyword above was "HelloA", this function will return false because they are still different strings. int compareTo(String another) This method compares two Strings lexicographically, meaning it will compare the strings in alphabetical order character by character. The method will return a negative number of the differences between the strings, a 0 if they are equal or a positive number representing how much they differ. The negative return means that string comes before the other lexicographically while the positive return means it comes after the other. So in the above program, the function returns a 32 because of the following reason; when they compare the first characters of both string (initial string = 'h' and argument string = 'H'), they differ by an ASCII value of 32. The function returns 32 to the user. ASCII values are computer codes for different letters and numbers. int compareToIgnoreCase(String another) This method preforms the same actions of the regualr compareTo() method but this one will ignore any case difference. The method returns the same values as the compareTo(). There are of course many other methods in the String class but these are the most frequently used ones. Q9 JDBC driver types JDBC drivers are divided into four types or levels. Each type defines a JDBC driver implementation with increasingly higher levels of platform independence, performance, and deployment administration. The four types are:

Type 1: JDBC-ODBC Bridge Type 2: Native-API/partly Java driver Type 3: Net-protocol/all-Java driver Type 4: Native-protocol/all-Java driver

Type 1: JDBC-ODBC Bridge The type 1 driver, JDBC-ODBC Bridge, translates all JDBC calls into ODBC (Open DataBase Connectivity) calls and sends them to the ODBC driver. As such, the ODBC driver, as well as, in many cases, the client database code, must be present on the client machine. Figure 1 shows a typical JDBC-ODBC Bridge environment. Page 2 of 11

Figure 1. Type 1: JDBC-ODBC Bridge Pros The JDBC-ODBC Bridge allows access to almost any database, since the database's ODBC drivers are already available. Type 1 drivers may be useful for those companies that have an ODBC driver already installed on client machines. Cons

The performance is degraded since the JDBC call goes through the bridge to the ODBC driver, then to the native database connectivity interface. The result comes back through the reverse process. Considering the performance issue, type 1 drivers may not be suitable for large-scale applications.

The ODBC driver and native connectivity interface must already be installed on the client machine. Thus any advantage of using Java applets in an intranet environment is lost, since the deployment problems of traditional applications remain. Type 2: Native-API/partly Java driver JDBC driver type 2 -- the native-API/partly Java driver -- converts JDBC calls into databasespecific calls for databases such as SQL Server, Informix, Oracle, or Sybase. The type 2 driver communicates directly with the database server; therefore it requires that some binary code be present on the client machine. Page 3 of 11

Figure 2. Type 2: Native-API/partly Java driver Pros Type 2 drivers typically offer significantly better performance than the JDBC-ODBC Bridge. Cons The vendor database library needs to be loaded on each client machine. Consequently, type 2 drivers cannot be used for the Internet. Type 2 drivers show lower performance than type 3 and type 4 drivers.

ype 3: Net-protocol/all-Java driver JDBC driver type 3 -- the net-protocol/all-Java driver -- follows a three-tiered approach whereby the JDBC database requests are passed through the network to the middle-tier server. The middle-tier server then translates the request (directly or indirectly) to the database-specific native-connectivity interface to further the request to the database server. If the middle-tier server is written in Java, it can use a type 1 or type 2 JDBC driver to do this.

Figure 3. Type 3: Net-protocol/all-Java driver Pros The net-protocol/all-Java driver is server-based, so there is no need for any vendor database library to be present on client machines. Further, there are many opportunities to optimize portability, performance, and scalability. Moreover, the net protocol can be designed to make the client JDBC driver very small and fast to load. Additionally, a type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced system administration such as logging and auditing. Cons Type 3 drivers require database-specific coding to be done in the middle tier. Additionally, traversing the recordset may take longer, since the data comes through the backend server. Type 4: Native-protocol/all-Java driver

The native-protocol/all-Java driver (JDBC driver type 4) converts JDBC calls into the vendorspecific database management system (DBMS) protocol so that client applications can communicate directly with the database server. Level 4 drivers are completely implemented in Java to achieve platform independence and eliminate deployment administration issues.

Figure 4. Type 4: Native-protocol/all-Java driver

Pros Since type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good. Moreover, the native-protocol/all-Java driver boasts better performance than types 1 and 2. Also, there's no need to install special software on the client or server. Further, these drivers can be downloaded dynamically. Cons With type 4 drivers, the user needs a different driver for each database.

Q6 // ProdCons1.java class ProdCons1 { public static void main (String [] args)

{ Shared s = new Shared (); new Producer (s).start (); new Consumer (s).start (); } } class Shared { private char c = '\u0000'; void setSharedChar (char c) { this.c = c; } char getSharedChar () { return c; } } class Producer extends Thread { private Shared s; Producer (Shared s) { this.s = s; } public void run () { for (char ch = 'A'; ch <= 'Z'; ch++) { try { Thread.sleep ((int) (Math.random () * 4000)); } catch (InterruptedException e) {} s.setSharedChar (ch); System.out.println (ch + " produced by producer."); }

} } class Consumer extends Thread { private Shared s; Consumer (Shared s) { this.s = s; } public void run () { char ch; do { try { Thread.sleep ((int) (Math.random () * 4000)); } catch (InterruptedException e) {} ch = s.getSharedChar (); System.out.println (ch + " consumed by consumer."); } while (ch != 'Z'); } } consumed by consumer. A produced by producer. B produced by producer. B consumed by consumer. C produced by producer.

C consumed by consumer. D produced by producer. D consumed by consumer. E produced by producer. F produced by producer. F consumed by consumer.

Вам также может понравиться