Final exam - review questions: Keep in mind you are also responsible for material relating to class lectures, readings in java by dissection, and machine homeworks- such as file/io problem, gui problem, stl problem, and socket problem. Item 1(SUN) Given: 1. public abstract class Prod { 2. public abstract void prmth1(); 3. public static void prmth2() { 4. int mth2 = 30; 5. System.out.println("prmth2 = " + mth2); 6. } 7. public abstract void prmth3(); 8. } What is the result? A Compilation succeeds. B Compilation fails because of an error on line 1. C Compilation fails because of an error on line 3. D Compilation fails because of an error on line 7. Item 2(SUN) Given: 1. public class NewGarb { 2. public static Object getIt() { 3. Object rg = new Integer(3); 4. Object dg[][] = new Object[1][2]; 5. dg[0][1] = rg; 6. dg[0][0] = rg; 7. rg = null; 8. return rg; 9. } 10. } Which statement is true? A The NewGarb class will not compile. B The getIt() method must not be declared as static. C The NewGarb class compiles, but an exception is received because dg is not set to null. D The rg object is eligible for garbage collection after a call to the getIt() method has returned. Item 3(MAJII) What will happen if you compile/run this code? 1: public class Q1 extends Thread 2: { 3: public void run() 4: { 5: System.out.println("Before start method"); 6: this.stop(); 7: System.out.println("After stop method"); 8: } 9: 10: public static void main(String[] args) 11: { 12: Q1 a = new Q1(); 13: a.start(); 14: } 15: } A) Compilation error at line 7. B) Runtime exception at line 7. C) Prints "Before start method" and "After stop method". D) Prints "Before start method" only. Item 4 (Majii) What will happen if you compile/run the following code? 1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Show method in Test class"); 6: } 7: } 8: 9: public class Q2 extends Test 10: { 11: static void show() 12: { 13: System.out.println("Show method in Q2 class"); 14: } 15: public static void main(String[] args) 16: { 17: Test t = new Test(); 18: t.show(); 19: Q2 q = new Q2(); 20: q.show(); 21: 22: t = q; 23: t.show(); 24: 25: q = t; 26: q.show(); 27: } 28: } A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class" B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class" C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class" D) Compilation error. Item(Majii) In the following applet, how many Buttons will be displayed? 1: import java.applet.*; 2: import java.awt.*; 3: 4: public class Q16 extends Applet 5: { 6: Button okButton = new Button("Ok"); 7: 8: public void init() 9: { 10: add(okButton); 11: add(okButton); 12: add(okButton); 13: add(okButton); 14: 15: add(new Button("Cancel")); 16: add(new Button("Cancel")); 17: add(new Button("Cancel")); 18: add(new Button("Cancel")); 19: 20: setSize(300,300); 21: } 22: } A) 1 Button with label "Ok" and 1 Button with label "Cancel" . B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" . C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" . D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" . ITEM(Majii) What will happen if you compile/run the following code? 1: public class Q21 2: { 3: int maxElements; 4: 5: void Q21() 6: { 7: maxElements = 100; 8: System.out.println(maxElements); 9: } 10: 11: Q21(int i) 12: { 13: maxElements = i; 14: System.out.println(maxElements); 15: } 16: 17: public static void main(String[] args) 18: { 19: Q21 a = new Q21(); 20: Q21 b = new Q21(999); 21: } 22: } A) Prints 100 and 999. B) Prints 999 and 100. C) Compilation error at line 3, variable maxElements was not initialized. D) Compillation error at line 19. www.javaprepare.com/guests/test.html # Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class. # Which of the following is true. Select the two correct answers. 1. A class that is abstract may not be instantiated. 2. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++. 3. A static variable indicates there is only one copy of that variable. 4. A method defined as private indicates that it is accessible to all other classes in the same package. # What all gets printed when the following program is compiled and run. Select the two correct answers. public class test { public static void main(String args[]) { int i, j=1; i = (j>1)?2:1; switch(i) { case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } } } 1. 0 2. 1 3. 2 4. 3 # What all gets printed when the following program is compiled and run. Select the one correct answer. public class test { public static void main(String args[]) { int i=0, j=2; do { i=++i; j--; } while(j>0); System.out.println(i); } } 1. 0 2. 1 3. 2 4. The program does not compile because of statement "i=++i;" # What all gets printed when the following gets compiled and run. Select the three correct answers. public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i/j > 1) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } 1. 0 2. 1 3. 2 4. 3 5. 4 # What all gets printed when the following gets compiled and run. Select the two correct answers. public class test { public static void main(String args[]) { int i=1, j=1; try { i++; j--; if(i == j) i++; } catch(ArithmeticException e) { System.out.println(0); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(1); } catch(Exception e) { System.out.println(2); } finally { System.out.println(3); } System.out.println(4); } } 1. 0 2. 1 3. 2 4. 3 5. 4 # What all gets printed when the following gets compiled and run. Select the two correct answers. public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = "abc"; if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } } 1. 1 2. 2 3. 3 4. 4 # What all gets printed when the following gets compiled and run. Select the two correct answers. public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = new String("abc"); if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); } } 1. 1 2. 2 3. 3 4. 4 # Which of the following are legal array declarations. Select the three correct answers. 1. int i[5][]; 2. int i[][]; 3. int []i[]; 4. int i[5][5]; 5. int[][] a; # What is the range of values that can be specified for an int. Select the one correct answer. 1. The range of values is compiler dependent. 2. -231 to 231 - 1 3. -231-1 to 231 4. -215 to 215 - 1 5. -215-1 to 215 # How can you ensure that the memory allocated by an object is freed. Select the one correct answer. 1. By invoking the free method on the object. 2. By calling system.gc() method. 3. By setting all references to the object to new values (say null). 4. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object. # What gets printed when the following code is compiled and run. Select the one correct answer. public class test { public static void main(String args[]) { int i = 1; do { i--; } while (i > 2); System.out.println(i); } } 1. 0 2. 1 3. 2 4. -1 # Which of these is a legal definition of a method named m assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer. 1. void m() throws IOException{} 2. void m() throw IOException{} 3. void m(void) throws IOException{} 4. m() throws IOException{} 5. void m() {} throws IOException # Which of the following are legal identifier names in Java. Select the two correct answers. 1. %abcd 2. $abcd 3. 1abcd 4. package 5. _a_long_name # At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer. void method X() { String r = new String("abc"); String s = new String("abc"); r = r+1; //1 r = null; //2 s = s + r; //3 } //4 1. Before statement labeled 1 2. Before statement labeled 2 3. Before statement labeled 3 4. Before statement labeled 4 5. Never. # String s = new String("xyz"); Assuming the above declaration, which of the following statements would compile. Select the one correct answer. 1. s = 2 * s; 2. int i = s[0]; 3. s = s + s; 4. s = s >> 2; 5. None of the above. # Which of the following statements related to Garbage Collection are correct. Select the two correct answers. 1. It is possible for a program to free memory at a given time. 2. Garbage Collection feature of Java ensures that the program never runs out of memory. 3. It is possible for a program to make an object available for Garbage Collection. 4. The finalize method of an object is invoked before garbage collection is performed on the object. # If a base class has a method defined as void method() { } Which of the following are legal prototypes in a derived class of this class. Select the two correct answers. 1. void method() { } 2. int method() { return 0;} 3. void method(int i) { } 4. private void method() { } # In which all cases does an exception gets generated. Select the two correct answers. int i = 0, j = 1; 1. if((i == 0) || (j/i == 1)) 2. if((i == 0) | (j/i == 1)) 3. if((i != 0) && (j/i == 1)) 4. if((i != 0) & (j/i == 1)) # Which of the following statements are true. Select the two correct answers. 1. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state. 2. The wait(), notify(), and notifyAll() methods must be executed in synchronized code. 3. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state. 4. The Thread class is an abstract class. # Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer. 1. transient 2. volatile 3. synchronized 4. native 5. static 6. final # Which of the following are true about interfaces. Select the two correct answers. 1. Methods declared in interfaces are implicitly private. 2. Variables declared in interfaces are implicitly public, static, and final. 3. An interface can extend any number of interfaces. 4. The keyword implements indicate that an interface inherits from another. # Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer. 1. test(); 2. super.test(); 3. super.super.test(); 4. ::test(); 5. C.test(); 6. It is not possible to invoke test() method defined in C from a method in A. # What is the return type of method round(double d) defined in Math class. # What gets written on the screen when the following program is compiled and run. Select the one right answer. public class test { public static void main(String args[]) { int i; float f = 2.3f; double d = 2.7; i = ((int)Math.ceil(f)) * ((int)Math.round(d)); System.out.println(i); } } 1. 4 2. 5 3. 6 4. 6.1 5. 9 # Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object. 1. true 2. false # Which of these classes defined in java.io and used for file-handling are abstract. Select the two correct answers. 1. InputStream 2. PrintStream 3. Reader 4. FileInputStream 5. FileWriter # Name the collection interface used to represent collections that maintain unique elements. # What is the result of compiling and running the following program. public class test { public static void main(String args[]) { String str1="abc"; String str2="def"; String str3=str1.concat(str2); str1.concat(str2); System.out.println(str1); } } 1. abc 2. def 3. abcabc 4. abcdef 5. defabc 6. abcdefdef # Select the one correct answer. The number of characters in an object of a class String is given by 1. The member variable called size 2. The member variable called length 3. The method size() returns the number of characters. 4. The method length() returns the number of characters. # Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type. 1. valueOf 2. intValue 3. getInt 4. getInteger # Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object. # Which of the following are correct. Select the one correct answer. 1. An import statement, if defined, must always be the first non-comment statement of the file. 2. private members are accessible to all classes in the same package. 3. An abstract class can be declared as final. 4. Local variables cannot be declared as static. # Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer. 1. static 2. final 3. abstract 4. native 5. volatile 6. transient # Which of these are core interfaces in the collection framework. Select the one correct answer. 1. Tree 2. Stack 3. Queue 4. Array 5. LinkedList 6. Map # Which of these statements are true. Select the two correct answers. 1. For each try block there must be at least one catch block defined. 2. A try block may be followed by any number of finally blocks. 3. A try block must be followed by at least one finally or catch block. 4. If both catch and finally blocks are defined, catch block must precede the finally block. The remaining questions are related to AWT, event classes, and layout managers. These topics are not included in 1.4 version of the exam. # The default layout manager for a Frame is ... 1. FlowLayout 2. BorderLayout 3. GridLayout 4. GridBagLayout 5. CardLayout # Which of the following are valid adapter classes in Java. Select the two correct answers. 1. ComponentAdapter 2. ActionAdapter 3. AdjustmentAdapter 4. ItemAdapter 5. FocusAdapter # Which method defined in the EventObject class returns the Object that generated an event. The method should be given in the format - return_type method_name(); # Which of the following object receives ActionEvent. Select the four correct answers. 1. List 2. Button 3. Choice 4. CheckBox 5. TextField 6. MenuItem # Name the class that may be used to create submenus in pull-down menus. # In which class is the wait() method defined. Select the one correct answer. 1. Applet 2. Runnable 3. Thread 4. Object # Which is the only layout manager that always honors the size of a component. Select the one correct answer. 1. FlowLayout 2. GridLayout 3. BorderLayout 4. CardLayout 5. GridBagLayout # Which of these are valid Event Listener interfaces. Select the two correct answers. 1. MouseMotionListener 2. WindowListener 3. DialogListener 4. PaintListener