For Java Only.... Q.1) Please consider the following - TopicsExpress



          

For Java Only.... Q.1) Please consider the following code: 1. class CreateFile { 2. public static void main(String[] args) { 3. try { 4. File directory = new File(f1); 5. File file = new File(directory,f2); 6. if(!file.exists()) { 7. file.createNewFile(); 8. } 9. } catch (IOException e) { } 10. } 11. } The current directory does NOT contain a directory named f1. Which of the following are true? Answer Choice A Line 7 is never executed. B An exception is thrown at runtime. C Line 4 creates a File object named f1. D Line 5 creates a File object named f2. E Line 4 creates a directory named f1 in the file system. F Line 7 creates a directory named f1 and a file f2 within it in the file system. G Line 5 creates a file named f2 inside of the directory named f1 in the file system. Ans : B,C,D Explanation : Given a scenario involving navigating file systems, reading from files, or writing to files, develop the correct solution using the following classes (sometimes in combination), from java.io: BufferedReader,BufferedWriter, File, FileReader, FileWriter and PrintWriter. Q.2) Please consider the following code: class Test { public static void main(String[] args) { byte[] b = {a, b, c}; char[] c = {a, b, c}; String s = abc; StringBuffer sb = new StringBuffer(abc); byte b2 = a; char c2 = a; String s1 = new String(b); // 1 String s2 = new String(c); // 2 String s3 = new String(s); // 3 String s4 = new String(sb); // 4 String s5 = new String(b2); // 5 String s6 = new String(c2); // 6 }} Which of the above lines will generate compile time error when you compile the code? Answer Choice A 1 B 2 C 3 D 4 E 5 F 6 Ans : E,F Explanation : Discuss the differences between the String, StringBuilder, and StringBuffer classes. The overloaded contructors for the String class accept a parameter of type String or StringBuffer or an array of byte or char. There is no constructor that will accept a primitive byte or char. Please note that if you want to convert a primitive to a String then you can use the static String.valueOf method. Hence compile time error will be generated at Line 5 and Line 6. Q.3) Which of the following option will parse currencyValue at line 19? Answer Choice A Number n = nf.parse(currencyValue); B Number n = nf.parseCurrency(currencyValue); C Number n = NumberFormatter.getCurrencyInstance(Locale.getDefault()).parse(currencyValue); D Number n = Currency.parse(currencyValue); E Number n = NumberFormat.parseCurrency(currencyValue); Ans: A Explanation : Use standard J2SE APIs in java.text package to correctly format or parse currency values for a specific locale. Q.4) What will happen if you compile and run the following code fragment? Integer i1 = new Integer(2); //line 1 Integer i2 = new Integer(5); //line 2 System.out.println(i1*i2); //line 3 Answer Choice A Compilation error at line 2 B Compilation error at line 3 C Runtime exception at line 3 D Runtime exception at line 2 Ans: B Explanation : Develop code that uses the primitive wrapper classes (such as Boolean, Character, Double, Integer, etc.), and/or autoboxing & unboxing. You will get a compilation error indicating that the operator * is undefined for the type(s) java.lang.Integer, java.lang.Integer. The operator * can used with primitive types but not with wrapper classes. Q.5) Please consider the following code: 11. String st= a1b2c3; 12. String[] tokens = st.split(\d); 13. for(String s: tokens) System.out.print(s + ); What is the result? Answer Choice A a b c B 1 2 3 C a1b2c3 D a1 b2 c3 E Compilation fails. F The code runs with no output. G An exception is thrown at runtime. Ans : A Explanation : Write code that uses standard J2SE APIs in the java.util and java.util.regex packages to format or parse strings or streams. For strings, write code that uses the Pattern and Matcher classes and the String.split method. Recognize and use regular expression patterns for matching (limited to: . (dot), * (star), + (plus), ?, \d, \s, \w, [], ()). The use of *, +, and ? will be limited to greedy quantifiers, and the parenthesis operator will only be used as a grouping mechanism, not for capturing content during matching. For streams, write code using the Formatter and Scanner classes and the PrintWriter.format/printf methods. Recognize and use formatting parameters (limited to: %b, %c, %d, %f, %s) in format strings. Q.6) Please consider the following code: 33. Date d = new Date(0); 34. String ds = December 15, 2004; 35. // insert code here 36. try { 37. d = df.parse(ds); 38. } 39. catch(ParseException e) { 40. System.out.println(Unable to parse + ds); 41. } 42. // insert code here too Which of the following will create the appropriate DateFormat object and add a day to the Date object? Answer Choice A 35. DateFormat df= DateFormat.getDateFormat(); 42. d.setTime( (60 * 60 * 24) + d.getTime()); B 35. DateFormat df= DateFormat.getDateJnstance(); 42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime()); C 35. DateFormat df= DateFormat.getDateFormat(); 42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime()); D 35. DateFormat df= DateFormat.getDateJnstance(); 42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime()); Ans : B Explanation : Use standard J2SE APIs in java.text package to correctly format or parse dates and numbers. Q.7) Please consider the following code: 1. public class AddTwo { 2. Integer i; 3. int x; 4. public AddTwo(int y) { 5. x=i+y; 6. System.out.println(x); 7. } 8. public static void main(String[] args) { 9. new AddTwo(new Integer(4)); 10. } 11. } What is the result? Answer Choice A The value 4 is printed at the command line. B Compilation fails because of an error in line 5. C Compilation fails because of an error in line 9. D A NullPointerException occurs at runtime. E A NumberFormatException occurs at runtime. F An IllegalStateException occurs at runtime. Ans : D Explanation : Develop codes that uses the primitive wrapper classes (such as Boolean, Character, Double, Integer, etc) and/or autoboxing/unboxing. Q.8) Please consider the following code: 1. Date d = new Date(1000000000000L); 2. DateFormat df = DateFormat.getInstance(); What outputs the current locales country name and the appropriate version of ds date? Answer Choice A Locale loc = Locale.getLocale(); System.out.println(loc.getDisplayCountry()+ + df.format(d)); B Locale loc = Locale.getDefault(); System.out.println(loc.getDisplayCountry()+ + df.format(d)); C Locale bc = Locale.getLocale(); System.out.println(loc.getDisplayCountry()+ + df.setDateFormat(d)); D Locale loc = Locale.getDefault(); System.out.println(loc.getDispbayCountry()+ + df.setDateFormat(d)); Ans : B Explanation : Given a scenario, determine the appropriate methods to use if you want to use the default locale or a specific locale. Describe the purpose and use of the java.util.Locale class. Q.9) Please consider the following code: 1. public class Bank { 2. private static Object account = new Object(); 3. public Bank() { 4. Teller t1 = new Teller(1, 2000L); 5. Teller t2 = new Teller(2, 6000L); 6. t1.start(); 7. t2.start(); 8. t2.interrupt(); 9. delay(8000); 10. } 11. private static void delay(long n) { 12. try { Thread.sleep(n); } 13. catch (Exception e) { System.out.print(Error ); } 14. } 15. public static void main(String[] args) { 16. System.out.print(BankStarted, ); 17. new Bank(); 18. System.out.print(BankClosed ); 19. } 20. class Teller extends Thread { 21. private int idx; 22. private long delay; 23. public Teller( int id, long delay){ 24. idx = id;this.delay = delay; 25. } 26. public void run() { 27. synchronized (account) { 28. System.out.print(Start+idx+ ); 29. delay(delay); 30. System.out.print(End+idx+ ); 31. } 32. } 33. } 34. } Assume that sleep(n) executes in exactly m milliseconds, and all other code executes in an insignificant amount of time. What is the output if the Bank class is executed? Answer Choice A Compilation fails. B Deadlock occurs. C BankStarted Start1 Error BankClosed End1 D BankStarted Start1 BankClosed End1 Start2 End2 E BankStarted Start1 Error Start2 BankClosed End2 End1 F BankStarted Start1 Start2 Error End2 BankClosed End1 G BankStarted Start1 BankClosed End1 Start2 Error End2 Ans : G Explanation : Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems. Q.10) Which of the following statement is true about invoking yield() on a running thread? Answer Choice A A running thread will be placed in suspended state. B A running thread will be placed in sleeping state. C A running thread will be placed in ready state. D Neither of the above. Ans : C Explanation: Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another. If a thread enters the runnable state, and it has a higher priority than any of the threads in the pool and higher than the currently running thread, the lower-priority running thread usually will be bumped back to runnable(Ready) and the highest-priority thread will be chosen to run. Hence option C is correct.
Posted on: Fri, 05 Dec 2014 08:10:26 +0000

Trending Topics



Recently Viewed Topics




© 2015