Sei sulla pagina 1di 14

1. Which of the following statements about arrays is syntactically wrong?

(a) Person[] p = new Person[5]; (b) Person[5] p; (c) Person[] p []; (d) Person p[][] = new Person[2][]; 2. Given the following piece of code: public class Test { public static void main(String args[]) { int i = 0, j = 5 ; for( ; (i < 3) && (j++ < 10) ; i++ ) { System.out.print(" " + i + " " + j ); } System.out.print(" " + i + " " + j ); } } what will be the result? (a) 0 6 1 7 2 8 3 8 (b) 0 6 1 7 2 8 3 9 (c) 0 5 1 5 2 5 3 5 (d) compilation fails 3. Which of the following declarations is correct? (2 answers): Ans. [a] boolean b = TRUE; [b] byte b = 255; [c] String s = null; [d] int i = new Integer(56); 4. Suppose a class has public visibility. In this class we define a protected method. Which of the following statements is correct? (a) This method is only accessible from inside the class itself and from inside all subclasses. (b) In a class, you can not declare methods with a lower visibility than the visibility of the class in which it is defined. (c) From within protected methods you do not have access to public methods. (d) This method is accessible from within the class itself and from within all classes defined in the same package as the class itself. 5. Given the following piece of code: public class Company{ public abstract double calculateSalaries(); } which of the following statements is true? (a) The keywords public and abstract can not be used together. (b) The method calculateSalaries() in class Company must have a body (c) You must add a return statement in method calculateSalaries(). (d) Class Company must be defined abstract. 6. Given the following piece of code: public interface Guard{ void doYourJob();

} abstract public class Dog implements Guard{} which of the following statements is correct? (a) This code will not compile, because method doYourJob() in interface Guard must be defined abstract. (b) This code will not compile, because class Dog must implement method doYourJob() from interface Guard. (c) This code will not compile, because in the declaration of class Dog we must use the keyword extends in stead of implements. (d) This code will compile without any errors. 7. Given these classes: public class Person{ public void talk(){ System.out.print("I am a Person "); } } public class Student extends Person { public void talk(){ System.out.print("I am a Student "); } } what is the result of this piece of code: public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); } } (a) I am a Person (b) I am a Student (c) I am a Person I am a Student (d) I am a Student I am a Person 8. Given the following piece of code: public class Person{ private String firstName; public Person(String fn){ firstName = fn; } } public class Student extends Person{ private String studentNumber; public Student(String number) { studentNumber = number; } } Which of the following statements is true? (2 answers) [a] This code will compile if we define in class Person a no-argument constructor. [b] This code will compile if we define in class Student a no-argument constructor. [c] This code will compile if we add in the constructor of Student the following line of code as first statement: super(); [d] This code will compile if we call the constructor of Person from within the constructor of Student 9. Specify the correct characteristics of an enumeration type (2 answers) [a] enum can define static fields and methods [b] enum can contain a public constructor [c] enum can implement interfaces [d] enum is a reference to a variable set of constants

10. Given the following piece of code: class Person { public int number; } public class Test{ public void doIt(int i , Person p){ i = 5; p.number = 8; } public static void main(String args[]){ int x = 0; Person p = new Person(); new Test().doIt(x, p); System.out.println(x + " " + p.number); } } What is the result? (a) 0 8 (b) 5 0 (c) 0 0 (d) 5 8 11. Given the following piece of code: class SalaryCalculationException extends Exception{} class Person{ public void calculateSalary() throws SalaryCalculationException { //... throw new SalaryCalculationException(); //... } } class Company{ public void paySalaries(){ new Person().calculateSalary(); } } Which of the following statements is correct? (2 answers) [a] This code will compile without any problems. [b] This code will compile if in method paySalaries() we return a boolean in stead of void. [c] This code will compile if we add a try-catch block in paySalaries() [d] This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries(). 12. Which of the following statements regarding static methods are correct? (2 answers) [a] static methods are difficult to maintain, because you can not change their implementation. [b] static methods can be called using an object reference to an object of the class in which this method is defined. [c] static methods are always public, because they are defined at class-level. [d] static methods do not have direct access to non-static methods which are defined inside the same class 13. Given the following piece of code: class Person{ public void talk(){} } public class Test{

public static void main(String args[]){ Person p = null; try{ p.talk(); } catch(NullPointerException e){ System.out.print("There is a NullPointerException. "); } catch(Exception e){ System.out.print("There is an Exception. "); } System.out.print("Everything went fine. "); } } what will be the result? (a) If you run this program, the outcome is: There is a NullPointerException. Everything went fine. (b) If you run this program, the outcome is: There is a NullPointerException. (c) If you run this program, the outcome is:There is a NullPointerException. There is an Exception. (d) This code will not compile, because in Java there are no pointers. 14. Which of the following statement about Generics are correct? (2 answers) [a] Generics are typed subclasses of the classes from the Collections framework [b] Generics are used to parameterize the collections in order to allow for static type checking at compile tIme of the objects in the collection. [c] Generics can be used to perform type checking of the objects in a collection at runtime. [d] Generics can be used to iterate over a complete collection in an easy way, using theenhanced for loop. 15. Which collection class associates values witch keys, and orders the keys according to their natural order? (a) java.util.HashSet (b) java.util.LinkedList (c) java.util.TreeMap (d) java.util.SortedSet 16. Which of the following statements about GUI components is wrong? (a) Swing exists since version 1.2 of the jdk. (b) AWT stands for Abstract Window Toolkit (c) You can not place AWT components on Swing containers. (d) The AWT classes are deprecated. 17. Which of the following statements about events are correct? (2 answers) [a] Event objects are placed on a Queue, where they are fetched by subscribers (objects of classes which implement the interface Subscriber). [b] The listener of an event must implement the method public void listen(EventObject obj). [c] Each event object must be an object of a subclass of EventObject. [d] Each event listener can investigate about the source of an event by calling the method getSource() on the event object. 18. How can you serialize an object? (a) You have to make the class of the object implement the interface Serializable.

(b) You must call the method serializeObject() (which is inherited from class Object) on the object. (c) You should call the static method serialize(Object obj) from class Serializer, with as argument the object to be serialized. (d) You dont have to do anything, because all objects are serializable by default. 19. Which statements about IO are correct (2 answers)? [a] OutputStream is the abstract superclass of all classes that represent an outputstream of bytes. [b] Subclasses of the class Reader are used to read character streams. [c] To write characters to an outputstream, you have to make use of the class CharacterOutputStream. [d] To write an object to a file, you use the class ObjectFileWriter. 20. Given the following piece of code: public class MyThread extends Thread{ public String text; public void run(){ System.out.print(text); } } public class Test{ public static void main(String args[]){ MyThread t1 = new MyThread(); t1.text = "one "; MyThread t2 = new MyThread(); t2.text = "two "; t1.run(); t2.run(); System.out.print("three "); } } Which of the following statements is true? (a) If you execute this program, the result is always one two three (b) If you execute this program, the result is always three one two (c) The result of this program is undetermined. (d) Compilation will fail. 21. Given Integer.MIN_VALUE = -2147483648 Integer.MAX_VALUE = 2147483647 What is the output of following { float f4 = Integer.MIN_VALUE; float f5 = Integer.MAX_VALUE; float f7 = -2147483655f; System.out.println("Round f4 is " + Math.round(f4)); System.out.println("Round f5 is " + Math.round(f5)); System.out.println("Round f7 is " + Math.round(f7)); } a) Round f4 is -2147483648 Round f5 is 2147483647 Round f7 is -2147483648 b) Round f4 is -2147483648 Round f5 is 2147483647 Round f7 is -2147483655

22) If 1 Boolean b1 = new Boolean("TRUE"); 2 Boolean b2 = new Boolean("true"); 3 Boolean b3 = new Boolean("JUNK"); 4 System.out.println("" + b1 + b2 + b3); a) Compiler error b) RunTime error c) Truetruefalse d) Truetruetrue If 1 Boolean b1 = new Boolean("TRUE"); 2 Boolean b2 = new Boolean("true"); 3 Boolean b3 = new Boolean("JUNK"); 4 System.out.println("" + b1 + b2 + b3); 23) In the above question if line 4 is changed to System.out.println(b1+b2+b3); The output is a) Compile time error b) Run time error c) truetruefalse d) truetruetrue Q24. What is the output { Float f1 = new Float("4.4e99f"); Float f2 = new Float("-4.4e99f"); Double d1 = new Double("4.4e99"); System.out.println(f1); System.out.println(f2); System.out.println(d1); } a) Runtime error b) Infinity -Infinity 4.4E99 c) Infinity - Infinity Infinity d) 4.4E99 -4.4E99 4.4E99

us
25. Which of the following wrapper classes can not take a "String" in constructor 1) Boolean

2) Integer 3) Long 4) Character 5) Byte 6) Short 26. What is the output of following Double d2 = new Double("-5.5"); Double d3 = new Double("-5.5"); System.out.println(d2==d3); System.out.println(d2.equals(d3)); a) true true b) false false c) true false d) false true 27) Which one of the following always honors the components's preferred size. a) FlowLayout b) GridLayout c) BorderLayout 28) Look at the following code import java.awt.*; public class visual extends java.applet.Applet{ static Button b = new Button("TEST"); public void init(){ add(b); } public static void main(String args[]){ Frame f = new Frame("Visual"); f.setSize(300,300); f.add(b); f.setVisible(true); } } What will happen if above code is run as a standalone application a) Displays an empty frame b) Displays a frame with a button covering the entire frame c) Displays a frame with a button large enough to accomodate its label. 29 If the code in Q33 is compiled and run via appletviewer what will happen a) Displays an empty applet b) Displays a applet with a button covering the entire frame c) Displays a applet with a button large enough to accomodate its label.

30. What is the output public static void main(String args[]){ Frame f = new Frame("Visual"); f.setSize(300,300); f.setVisible(true); Point p = f.getLocation(); System.out.println("x is " + p.x); System.out.println("y is " + p.y); } a) x is 300 y is 300 b) x is 0 y is 0 c) x is 0 y is 300 31) Which one of the following always ignores the components's preferred size. a) FlowLayout b) GridLayout c) BorderLayout 32) Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE \dir1\IO.class -- IO.class is under dir1 Consider the following code import java.io.*; public class IO { public static void main(String args[]) { File f = new File("..\\12345.msg"); try{ System.out.println(f.getCanonicalPath()); System.out.println(f.getAbsolutePath()); }catch(IOException e){ System.out.println(e); } } } What will be the output of running "java IO" from C:\java\dir1 a) C:\java\12345.msg C:\java\dir1\..\12345.msg b) C:\java\dir1\12345.msg C:\java\dir1\..\12345.msg c) C:\java\dir1\..\12345.msg C:\java\dir1\..\12345.msg 33) Suppose we copy IO.class from C:\java\dir1 to c:\java What will be the output of running "java IO" from C:\java. a) C:\java\12345.msg

C:\java\..\12345.msg b) C:\12345.msg C:\java\..\12345.msg c) C:\java\..\12345.msg C:\java\\..\12345.msg 34) Which one of the following methods of java.io.File throws IOException and why a) getCanonicalPath and getAbsolutePath both require filesystem queries. b) Only getCannonicalPath as it require filesystem queries. c) Only getAbsolutePath as it require filesystem queries. 35) What will be the output if Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE \dir1\IO.class -- IO.class is under dir1 import java.io.*; public class IO { public static void main(String args[]) { File f = new File("12345.msg"); String arr[] = f.list(); System.out.println(arr.length); } } a) Compiler error as 12345.msg is a file not a directory b) java.lang.NullPointerException at run time c) No error , but nothing will be printed on screen 36) What will be the output Consider a directory structure like this (NT or 95) C:\JAVA\12345.msg --FILE import java.io.*; public class IO { public static void main(String args[]) { File f1 = new File("\\12345.msg"); System.out.println(f1.getPath()); System.out.println(f1.getParent()); System.out.println(f1.isAbsolute()); System.out.println(f1.getName()); System.out.println(f1.exists()); System.out.println(f1.isFile()); } } a) \12345.msg \ true 12345.msg true true b) \12345.msg \ true

\12345.msg false false c)12345.msg \ true 12345.msg false false d) \12345.msg \ true 12345.msg false false 37) If in question no 41 the line File f1 = new File("\\12345.msg"); is replaced with File f1 = new File("12345.msg"); What will be the output a) 12345.msg \ true 12345.msg true true b) 12345.msg null true 12345.msg true true c) 12345.msg null false 12345.msg true true d) \12345.msg \ true 12345.msg false false 38. What will happen when you attempt to compile and run this code? abstract class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); }

} public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My Func"); } public void amethod(){ myfunc(); } } 1) The code will compile and run, printing out the words "My Func" 2) The compiler will complain that the Base class has non abstract methods 3) The code will compile but complain at run time that the Base class has non abstract methods 4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to loop it 39. What will happen when you attempt to compile and run this code? public class MyMain{ public static void main(String argv){ System.out.println("Hello cruel world"); } } 1) The compiler will complain that main is a reserved word and cannot be used for a class 2) The code will compile and when run will print out "Hello cruel world" 3) The code will compile but will complain at run time that no constructor is defined 4) The code will compile but will complain at run time that main is not correctly defined 40. Which of the following are Java modifiers? 1) public 2) private 3) friendly 4) transient 5) vagrant 41.What will happen when you attempt to compile and run this code? class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){

System.out.println("My func"); } public void amethod(){ myfunc(); } } 1) The code will compile and run, printing out the words "My Func" 2) The compiler will complain that the Base class is not declared as abstract. 3) The code will compile but complain at run time that the Base class has non abstract methods 4) The compiler will complain that the method myfunc in the base class has no body, nobody at all to looove it 42) Why might you define a method as native? 1) To get to access hardware that Java does not know about 2) To define a new data type such as an unsigned integer 3) To write optimised code for performance in a language such as C/C++ 4) To overcome the limitation of the private scope of a method 43.What will happen when you attempt to compile and run this code? class Base{ public final void amethod(){ System.out.println("amethod"); } } public class Fin extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); } } 1) Compile time error indicating that a class with any final methods must be declared final itself 2) Compile time error indicating that you cannot inherit from a class with final methods 3) Run time error indicating that Base is not defined as final 4) Success in compilation and output of "amethod" at run time. 44.What will happen when you attempt to compile and run this code? public class Mod{ public static void main(String argv[]){ } public static native void amethod(); } 1) Error at compilation: native method cannot be static 2) Error at compilation native method must return value 3) Compilation but error at run time unless you have made code containing native a method available 4) Compilation and execution without error

45. What will happen when you attempt to compile and run this code? private class Base{} public class Vis{ transient int iVal; public static void main(String elephant[]){ } } 1) Compile time error: Base cannot be private 2) Compile time error indicating that an integer cannot be transient 3) Compile time error transient not a data type 4) Compile time error malformed main method 46. What happens when you attempt to compile and run these two files in the same directory? //File P1.java package MyPackage; class P1{ void afancymethod(){ System.out.println("What a fancy method"); } } //File P2.java public class P2 extends P1{ public static void main(String argv[]){ P2 p2 = new P2(); p2.afancymethod(); } } 1) Both compile and P2 outputs "What a fancy method" when run 2) Neither will compile 3) Both compile but P2 has an error at run time 4) P1 compiles cleanly but P2 has an error at compile time 47.You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it.? public class MyAr{ public static void main(String argv[]){ int[] i = new int[5]; System.out.println(i[5]); } } 1) An error at compile time 2) An error at run time 3) The value 0 will be output 4) The string "null" will be output 48. You want to loop through an array and stop when you come to the last element. Being a good java programmer and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use? 1) myarray.length(); 2) myarray.length; 3) myarray.size 4) myarray.size();

49. What best describes the appearance of an application with the following code? import java.awt.*; public class FlowAp extends Frame{ public static void main(String argv[]){ FlowAp fa=new FlowAp(); fa.setSize(400,300); fa.setVisible(true); } FlowAp(){ add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); }//End of constructor

}//End of Application 1) A Frame with buttons marked One to Four placed on each edge. 2) A Frame with buutons marked One to four running from the top to bottom 3) A Frame with one large button marked Four in the Centre 4) An Error at run time indicating you have not set a LayoutManager 50. How do you indicate where a component will be positioned using Flowlayout? 1) North, South,East,West 2) Assign a row/column grid reference 3) Pass a X/Y percentage parameter to the add method 4) Do nothing, the FlowLayout will position the component

Potrebbero piacerti anche