Sie sind auf Seite 1von 25

Test(60min)

1. Consider the following code: class Super { static String ID = "QBANK"; } class Sub extends Super { static { System.out.print("In Sub"); } } class Test { public static void main(String[] args) { System.out.println(Sub.ID); } }

What will be the output when class Test is run ? Select 1 correct option. a It will print 'In Sub' and 'QBANK'. b It will print 'QBANK'. c Depends on the implementation of JVM. d It will not even compile. e None of the above.

2.

The following code snippet will print 'true'.

short s = Short.MAX_VALUE; char c = s; System.out.println( c == Short.MAX_VALUE); Select 1 correct option. a True b False

3. Consider the following class and interface definitions:

public class Sample implements IInt { public static void main(String[] args) { Sample s = new Sample(); //1 int j = s.thevalue; int k = IInt.thevalue; int l = thevalue; } } public interface IInt { //2 //3 //4

int thevalue = 0; }

What will happen when the above code is compiled and run? Select 1 correct option. a It will give an error at compile time at line //1. b It will give an error at compile time at line //2. c It will give an error at compile time at line //3 d It will give an error at compile time at line //4. e It will compile and run without any problem.

4. Given that OurClass is a MyClass and OurClass has a YourClass. Which of the following options are correct? Select 2 correct options a MyClass refers to OurClass b OurClass refers to MyClass c MyClass refers to YourClass d OurClass refers to YourClass e OurClass inherits from MyClass

5 Consider the following classes... The above code will print (ColoredPoint, Point) when compiled and run.

class Point { int x, y; } class ColoredPoint extends Point { int color; } class Test { static void test(ColoredPoint p, Point q) { System.out.println("(ColoredPoint, Point)"); } static void test(Point p, ColoredPoint q) { System.out.println("(Point, ColoredPoint)"); } public static void main(String[] args) { ColoredPoint cp = new ColoredPoint(); test(cp, cp); } } Select 1 correct option. a True b False

6. What will the following class print when run?

class TestClass { int i = getInt(); int k = 20; public int getInt() { return k+1; } public static void main(String[] args) { TestClass t = new TestClass(); System.out.println(t.i+" "+t.k); } } Select 1 correct option. a It will not even compile. b It will compile but throw an exception at runtime. c It will print 1 20 d It will print 21 20 e None of the above.

7. Which of these statements concerning the use of modifiers are true? Select 1 correct option. a By default (ie. no modifier) the member is only accessible to classes in the same package and subclasses of the class. b You cannot specify visiblity of local variables. c Local variable always have default accessiblity. d Local variables can be declared as private. e Local variables can only be declared as public.

8. Which one of the following class definitions is/are a legal definition of a class that cannot be instantiated?

class Automobile { abstract void honk(); //(1) } abstract class Automobile { void honk(); //(2) } abstract class Automobile { void honk(){}; //(3) } abstract class Automobile { abstract void honk(){} //(4) } abstract class Automobile { abstract void honk(); //(5) } Select 2 correct options

a 1 b 2 c 3 d 4 e 5

9. Consider the following classes...

class Car { public int gearRatio = 8; public String accelerate() { return "Accelerate : Car"; } } class SportsCar extends Car { public int gearRatio = 9; public String accelerate() { return "Accelerate : SportsCar"; } public static void main(String[] args) { Car c = new SportsCar(); System.out.println( c.gearRatio+" "+c.accelerate() ); } }

What will be printed when SportsCar is run?

Select 1 correct option. a 8 Accelerate : Car b 9 Accelerate : Car c 8 Accelerate : SportsCar d 9 Accelerate : SportsCar e None of the above.

10. class ClassA { 2. public int numberOfInstances; 3. protected ClassA(int numberOfInstances) { 4. this.numberOfInstances = numberOfInstances; 5. } 6. } 7. public class ExtendedA extends ClassA { 8. private ExtendedA(int numberOfInstances) { 9. super(numberOfInstances); 10. } 11. public static void main(String[] args) { 12. ExtendedA ext = new ExtendedA(420); 13. System.out.print(ext.numberOfInstances); 14. } 15. } Which statement is true? A. 420 is the output.

B. An exception is thrown at runtime. C. All constructors must be declared public. D. Constructors CANNOT use the private modifier. E. Constructors CANNOT use the protected modifier.

11. abstract class A { 11. abstract void a1(); 12. void a2() { } 13. } 14. class B extends A { 15. void a1() { } 16. void a2() { } 17. } 18. class C extends B { void c1() { } } and: A x = new B(); C y = new C(); A z = new C(); What are four valid examples of polymorphic method calls? (Choose four.) A. x.a2(); B. z.a2(); C. z.c1(); D. z.a1(); E. y.c1(); F. x.a1();

12.

class TestA { 2. public void start() { System.out.println("TestA"); } 3. } 4. public class TestB extends TestA { 5. public void start() { System.out.println("TestB"); } 6. public static void main(String[] args) { 7. ((TestA)new TestB()).start(); 8. } 9. } What is the result? A. TestA B. TestB C. Compilation fails. D. An exception is thrown at runtime.

13. Consider the following code snippet:

for(int i=INT1; i<INT2; i++) { System.out.println(i); }

Where, INT1 and INT2 can be any two integers.

Which of the following will produce the same result? Select 1 correct option. a for(int i=INT1; i<INT2; System.out.println(++i)); b for(int i=INT1; i++<INT2; System.out.println(i)); c int i=INT1; while(i++<INT2) { System.out.println(i); } d int i=INT1; do { System.out.println(i); }while(i++<INT2); e None of these.

14. Consider the following method ... What will it return if the method is called with the input "0.0" ? public float parseFloat( String s ) { float f = 0.0f; try { f = Float.valueOf( s ).floatValue(); return f ; } catch(NumberFormatException nfe) { f = Float.NaN ; return f; } finally {

f = 10.0f; return f; } } Select 1 correct option. a It won't even compile. b It will return 10.0 c It will return Float.Nan d It will return 0.0 e None of the above.

15. What will be printed by the following code if it is run with command line: java TestClass --0.50 ? (There are two minuses before 0.) public class TestClass { public static int getSwitch(String str) { return (int) Math.round( Double.parseDouble(str.substring(1, str.length()-1)) ); } public static void main(String args []) { switch(getSwitch(args[0])) { case 0 : System.out.println("Hello"); case 1 : System.out.println("World"); break;

default : System.out.println("Good Bye"); } } } Select 1 correct option. a Hello b World c Hello World d Hello World Good Bye e Good Bye

16. Which of these for statements are valid? 1. for (int i=5; i=0; i--) { } 2. int j=5; for(int i=0, j+=5; i<j ; i++) { j--; } 3. int i, j; for (j=10; i<j; j--) { i += 2; } 4. int i=10; for ( ; i>0 ; i--) { } 5. for (int i=0, j=10; i<j; i++, --j) {;} Select 1 correct option. a 1, 2 b 3, 4 c 1, 5 d 4, 5

e 5

17. public class BreakTest { public static void main(String[] args) { int i = 0, j = 5; lab1 : for( ; ; i++) { for( ; ; --j) if( i >j ) break lab1; } System.out.println(" i ="+i+" , j = "+j); } } What will it print? Select 1 correct option. a i = 1, j = -1 b i = 1, j = 4 c i = 0, j = 4 d i = 0, j = -1 e It will not compile.

18. What will the following program print when run?

public class TestClass { public static void main(String[] args) { try { System.exit(0); } finally { System.out.println("finally is always executed!"); } } } Select 1 correct option. a It'll print "finally is always executed!"; b It will not compile as there is no catch block. c It will not print anything. d An exception will be thrown e None of the above.

19. public float parseFloat( String s ) { float f = 0.0f; try // 1

{ f = Float.valueOf( s ).floatValue(); return f ; } catch(NumberFormatException nfe) { f = Float.NaN ; return f; } finally { return f; } return f ; } What can be done to get the following code compile and run? Select 4 correct options a Remove line 3, 6 b Remove line 5 c Remove line 5, 6 d Remove line 7 e Remove line 3, 7 // 7 // 6 // 5 // 4 // 3 // 2

20. class MyException extends Throwable{} class MyException1 extends MyException{} class MyException2 extends MyException{}

class MyException3 extends MyException2{} public class ExceptionTest { void myMethod() throws MyException { throw new MyException3(); } public static void main(String[] args) { ExceptionTest et = new ExceptionTest(); try { et.myMethod(); } catch(MyException me) { System.out.println("MyException thrown"); } catch(MyException3 me3) { System.out.println("MyException3 thrown"); } finally { System.out.println(" Done"); }

} } What is the result of compiling and running this code? Select 1 correct option. a MyException thrown

b MyException3 thrown

c MyException thrown Done

d MyException3 thrown Done

e It fails to compile

21. import java.util.*; class KeyMaster { public int i; public KeyMaster(int i) { this.i = i; } public boolean equals(Object o) { return i == ((KeyMaster)o).i; }

} public class MapIt {

public static void main(String[] args) { Set<KeyMaster> set = new HashSet<KeyMaster>(); KeyMaster k1 = new KeyMaster(1); KeyMaster k2 = new KeyMaster(2); set.add(k1); set.add(k1); set.add(k2); set.add(k2); System.out.print(set.size()); k2.i = 1; System.out.print(set.size()); set.remove(k1); System.out.print(set.size()); set.remove(k2); System.out.print(set.size()); } } What is the result? A. 4:4:2:2 B. 4:4:3:2 C. 2:2:1:0 D. 2:2:0:0 E. 2:1:0:0 F. 2:2:1:1 G. 4:3:2:1

22. import java.util.*; public class Group extends HashSet<Person> { public static void main(String[] args) { Group g = new Group();

g.add(new Person("Hans")); g.add(new Person("Lotte")); g.add(new Person("Jane")); g.add(new Person("Hans")); g.add(new Person("Jane")); System.out.println("Total: " + g.size()); } public boolean add(Person o) { System.out.println("Adding: " + o); return super.add(o); }} class Person { private final String name; public Person(String name) { this.name = name; } public String toString() { return name; } } Which of the following occur at least once when the code is compiled and run? (Choose all that apply.) A. Adding Hans B. Adding Lotte C. Adding Jane D. Total: 3 E. Total: 5 F. The code does not compile. G. An exception is thrown at runtime.

23. Given:

34. HashMap props = new HashMap(); 35. props.put("key45", "some value"); 36. props.put("key12", "some other value"); 37. props.put("key39", "yet another value"); 38. Set s = props.keySet(); 39. // insert code here What, inserted at line 39, will sort the keys in the props HashMap? A. Arrays.sort(s); B. s = new TreeSet(s); C. Collections.sort(s); D. s = new SortedSet(s);

24 1. public class Person { 2. private String name; 3. public Person(String name) { this.name = name; } 4. public boolean equals(Person p) { 5. return p.name.equals(this.name); 6. } 7. } Which statement is true? A. The equals method does NOT properly override the Object.equals method. B. Compilation fails because the private attribute p.name cannot be accessed in line 5. C. To work correctly with hash-based data structures, this class must also implement the hashCode method.

D. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

25. Given: 11. // insert code here 12. private N min, max; 13. public N getMin() { return min; } 14. public N getMax() { return max; } 15. public void add(N added) { 16. if (min == null || added.doubleValue() < min.doubleValue()) 17. min = added; 18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added; 20. } 21. } Which two, inserted at line 11, will allow the code to compile? (Choose two.) A. public class MinMax<?> { B. public class MinMax<? extends Number> { C. public class MinMax<N extends Object> { D. public class MinMax<N extends Number> { E. public class MinMax<? extends Object> { F. public class MinMax<N extends Integer> {

26.
Given:

1. public class Threads2 implements Runnable { 2. 3. public void run() { 4. System.out.println("run."); 5. throw new RuntimeException("Problem"); 6. } 7. public static void main(String[] args) { 8. Thread t = new Thread(new Threads2()); 9. t.start(); 10. System.out.println("End of method."); 11. } 12. } Which two can be results? (Choose two.) A. java.lang.RuntimeException: Problem B. run. java.lang.RuntimeException: Problem C. End of method. java.lang.RuntimeException: Problem D. End of method. run. java.lang.RuntimeException: Problem E. run. java.lang.RuntimeException: Problem End of method. 27. Given: public class TestSeven extends Thread { private static int x; public synchronized void doThings() { int current = x; current++; x = current; } public void run() { doThings(); } } Which statement is true? A. Compilation fails. B. An exception is thrown at runtime. C. Synchronizing the run() method would make the class thread-safe. D. The data in variable "x" are protected from concurrent access problems. E. Declaring the doThings() method as static would make the class thread-safe. F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread safe.

28.
Given: public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; }

public String getName() { return name; } public void increment() { count++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.) A. declare reset() using the synchronized keyword B. declare getName() using the synchronized keyword C. declare getCount() using the synchronized keyword D. declare the constructor using the synchronized keyword E. declare increment() using the synchronized keyword 29. Given: 7. void waitForSignal() { 8. Object obj = new Object(); 9. synchronized (Thread.currentThread()) { 10. obj.wait(); 11. obj.notify(); 12. } 13. } Which statement is true? A. This code may throw an InterruptedException. B. This code may throw an IllegalStateException. C. This code may throw a TimeoutException after ten minutes. D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()". E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally. F. A call to notify()or notifyAll() from another thread may cause this method to complete normally.

30. Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.) A. new Thread() { public void run() { doStuff(); } }; B. new Thread() { public void start() { doStuff(); }

}; C. new Thread() { public void start() { doStuff(); } }.run(); D. new Thread() { public void run() { doStuff(); } }.start(); E. new Thread(new Runnable() { public void run() { doStuff(); } }).run(); F. new Thread(new Runnable() { public void run() { doStuff(); } }).start();

Das könnte Ihnen auch gefallen