programming languages Online Quiz - 320
Description: programming languages Online Quiz - 320 | |
Number of Questions: 20 | |
Created by: Aliensbrain Bot | |
Tags: programming languages |
What is the output of the following program? public class example { public static void main(String args[]) { int x=0, y=2; do { x=++x; y--; } while(y>0); System.out.println(x); } }
- public class Test { 12. public static void main(String [] args) { 13. int x =5; 14. boolean b1 = true; 15. boolean b2 = false; 16. 17.if((x==4) && !b2) 18. System.out.print(”l “); 19. System.out.print(”2 “); 20. if ((b2 = true) && b1) 21. System.out.print(”3 “); 22. } 23. } What is the result?
Given: 1. 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. }
Which is correct? A. Shape s = new Shape(); s.setAnchor(10,10); s.draw(); B. Circle c = new Shape(); c.setAnchor(10,10); c.draw(); C. Shape s = new Circle(); s.setAnchor(10,10); s.draw(); D. Shape s = new Circle(); s->setAnchor(10,10); s->draw(); E. Circle c = new Circle(); c.Shape.setAnchor(10,10); c.Shape.draw();
Click the Exhibit button. 1. public class Test { 2. int x= 12; 3. public void method(int x) { 4. x+=x; 5. System.out.println(x); 6. } 7. } Given: 34. Test t = new Test(); 35. t.method(5); What is the output from line 5 of the Test class?
Given: 10. class Line { 11. public class Point { public int x,y; } 12. public Point getPoint() { return new Point(); } 13. } 14. class Triangle { 15. public Triangle() { 16. // insert code here 17. } 18. } Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
- Given: 10. class Line { 11. public static class Point { } 12. } 13. 14. class Triangle { 15. // insert code here 16. } Which code, inserted at line 15, creates an instance of the Point class defined in Line?
Given: 11. public class Test { 12. public enum Dogs {collie, harrier, shepherd}; 13. public static void main(String [] args) { 14. Dogs myDog = Dogs.shepherd; 15. switch (myDog) { 16. case collie: 17. System.out.print(”collie “); 18. case default: 19. System.out.print(”retriever “); 20. case harrier: 21. System.out.print(”harrier “); 22. } 23. } 24. } ‘What is the result?
Given: 11. public static void main(String[] args) { 12. String str = “null’; 13. if (str == null) { 14. System.out.println(”null”); 15. } else (str.length() == 0) { 16. System.out.println(”zero”); 17. } else { 18. System.out.println(”some”); 19. } 20. } ‘What is the result?
Given: 25.intx=12; 26. while (x < 10) { 27. x--; 28. } 29. System.out.print(x); What is the result?
Click the Exhibit button. Class TestException 1. public class TestException extends Exception { 2. } Class A: 1. public class A { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return “Hello “+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello(”John”)); Which two are true? (Choose two.)
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() { coount++; } 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.)
Click the Exhibit button. 1. import java.util.*; 2. 3. public class NameList { 4. private List names = new ArrayList(); 5. public synchronized void add(String name) { names.add(name); } 6. public synchronized void printAll() { 7. for (int i = 0; i
Click the Exhibit button. 10. public class Transfers { 11. public static void main(String[] args) throws Exception { 12. Record r1 = new Record(); 13. Record r2 = new Record(); 14. doTransfer(r1, r2, 5); 15. doTransfer(r2, r1, 2); 16. doTransfer(r1, r2, 1); 17. // print the result 18. System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get()); 19. } 20. private static void doTransfer( 21. final Record a, final Record b, final int amount) { 22. Thread t = new Thread() { 23. public void run() { 24. new Clerk().transfer(a, b, amount); 25. } 26. }; 27. t.start(); 28. } 29. } 30. class Clerk { 31. public synchronized void transfer(Record a, Record b, int amount){ 32. synchronized (a) { 33. synchronized (b) { 34. a.add(-amount); 35. b.add(amount); 36. } 37. } 38. } 39. } 40. class Record { 41.int num=10; 42. public int get() { return num; } 43. public void add(int n) { num = num + n; } 44. } If Transfers.main() is run, which three are true? (Choose three.)
Given: 11. class ClassA {} 12. class ClassB extends ClassA {} 13. class ClassC extends ClassA {} and: 21. ClassA p0 = new ClassA(); 22. ClassB p1 = new ClassB(); 23. ClassC p2 = new ClassC(); 24. ClassA p3 = new ClassB(); 25. ClassA p4 = new ClassC(); Which three are valid? (Choose three.)
Given: 10. abstract class A { 11. abstract void al(); 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(); Which four are valid examples of polymorphic method calls? (Choose four.)
Click the Exhibit button. 1. public class Car { 2. private int wheelCount; 3. private String vin; 4. public Car(String vin) { 5. this.vin = vin; 6. this.wheelCount = 4; 7. } 8. public String drive() { 9. return “zoom-zoom”; 10. } 11. public String getInfo() { 12. return “VIN: “+ vin + “wheels: “+ wheelCount; 13. } 14. } And: 1. public class MeGo extends Car { 2. public MeGo(String vin) { 3. this.wheelCount = 3; 4. } 5. } What two must the programmer do to correct the compilation errors? (Choose two.)
Given: 1. public class Blip { 2. protected int blipvert(int x) { return 0; } 3. } 4. class Vert extends Blip { 5. // insert code here 6. } Which five methods, inserted independently at line 5, will compile? (Choose five.)
Which four are true? (Choose four.)