0

programming languages Online Quiz - 320

Description: programming languages Online Quiz - 320
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

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. }

  1. TestA

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.


Correct Option: B

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();

  1. A

  2. B

  3. C

  4. D

  5. E


Correct Option: C

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?

  1. 5

  2. 10

  3. 12

  4. 17

  5. 24


Correct Option: B

Click the Exhibit button. 1. public interface A { 2. public void doSomething(String thing); 3. } 1. public class AImpl implements A { 2. public void doSomething(String msg) { } 3. } 1. public class B { 2. public A doit() { 3. // more code here 4. } 5. 6. public String execute() { 7. // more code here 8. } 9. } 1. public class C extends B { 2. public AImpl doit() { 3. // more code here 4. } 5. 6. public Object execute() { 7. // more code here 8. } 9. } Which statement is true about the classes and interfaces in the exhibit?

  1. Compilation will succeed for all classes and interfaces.

  2. Compilation of class C will fail because of an error in line 2.

  3. Compilation of class C will fail because of an error in line 6.

  4. Compilation of class AImpl will fail because of an error in line 2.


Correct Option: C
  1. 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?
  1. Point p = new Point();

  2. Line.Point p = new Line.Point();

  3. The Point class cannot be instatiated at line 15.

  4. Line 1 = new Line() ; 1.Point p = new 1.Point();


Correct Option: B

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?

  1. harrier

  2. shepherd

  3. retriever

  4. Compilation fails.

  5. retriever harrier

  6. An exception is thrown at runtime.


Correct Option: D

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?

  1. null

  2. zero

  3. some

  4. Compilation fails.

  5. An exception is thrown at runtime.


Correct Option: D

Given: 25.intx=12; 26. while (x < 10) { 27. x--; 28. } 29. System.out.print(x); What is the result?

  1. 0

  2. 10

  3. 12

  4. Line 29 will never be reached.


Correct Option: C

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.)

  1. Class A will not compile.

  2. Line 46 can throw the unchecked exception TestException.

  3. Line 45 can throw the unchecked exception TestException.

  4. Line 46 will compile if the enclosing method throws a TestException.

  5. Line 46 will compile if enclosed in a try block, where TestException


Correct Option: D,E
  1. An exception may be thrown at runtime.

  2. The code may run with no output, without exiting.

  3. The code may run with no output, exiting normally.

  4. The code may rum with output “A B A B C C “, then exit.

  5. The code may rum with output “A B C A B C A B C “, then exit.

  6. The code may ruin with output “A B C A A B C A B C “, then exit.


Correct Option: E,F

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.)

  1. insert a call to this() in the Car constructor

  2. insert a call to this() in the MeGo constructor

  3. insert a call to super() in the MeGo constructor

  4. insert a call to super(vin) in the MeGo constructor

  5. change the wheelCount variable in Car to protected

  6. change line 3 in the MeGo class to super.wheelCount = 3;


Correct Option: D,E

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.)

  1. public int blipvert(int x) { return 0; }

  2. protected long blipvert(int x, int y) { return 0; }

  3. private int blipvert(long x) { return 0; }

  4. protected long blipvert(int x) { return 0; }

  5. protected int blipvert(long x) { return 0; }

  6. protected long blipvert(long x) { return 0; }


Correct Option: A,B,C,E,F
  1. Has-a relationships should never be encapsulated.

  2. Has-a relationships should be implemented using inheritance.

  3. Has-a relationships can be implemented using instance variables.

  4. Is-a relationships can be implemented using the extends keyword.

  5. Is-a relationships can be implemented using the implements keyword.

  6. An array or a collection can be used to implement a one-to-many has-a relationship.


Correct Option: C,D,E,F
- Hide questions