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

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

  1. 0

  2. 1

  3. 2

  4. Compilation Error


Correct Option: C
  1. 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?
  1. 2

  2. 3

  3. 1 2

  4. 2 3

  5. 1 2 3

  6. Compilation fails.


Correct Option: D

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

AI Explanation

To answer this question, we need to understand the concept of method overriding in Java.

In the given code, we have two classes: TestA and TestB. TestB extends TestA, which means TestB is a subclass of TestA. TestB overrides the start() method of TestA.

In the main method, we create a new instance of TestB and cast it to TestA: (TestA)new TestB(). Then we call the start() method on this instance.

Since TestB overrides the start() method, the start() method of TestB will be called. Therefore, the output of the program will be "TestB".

Now, let's go through each option to understand why it is correct or incorrect:

Option A) TestA - This option is incorrect because the start() method of TestB is called, not the start() method of TestA.

Option B) TestB - This option is correct because the start() method of TestB is called, and the output of the program will be "TestB".

Option C) Compilation fails - This option is incorrect because there is no compilation error in the given code.

Option D) An exception is thrown at runtime - This option is incorrect because the program executes without throwing any exception.

The correct answer is 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
Explanation:

The output from line 5 of the Test class would be 10.

Explanation:

In the Test class, there is an instance variable x with a value of 12.

The method takes a parameter x and performs an addition operation x += x, which is equivalent to x = x + x.

When the method is called with an argument of 5 (t.method(5)), the local variable x inside the method is set to 5.

The addition operation x += x is then performed, resulting in x being updated to 10.

Finally, System.out.println(x) prints the value of x, which is 10.

Therefore, the output from line 5 of the Test class would be 10, so the correct answer is C.

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?

  1. Point p = Line.getPoint();

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

  3. Point p = (new Line()).getPoint();

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


Correct Option: D
  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

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Point p = new Point(); This option is incorrect because it tries to create an instance of the Point class without specifying the enclosing class.

Option B) Line.Point p = new Line.Point(); This option is correct because it correctly specifies the enclosing class (Line) and creates an instance of the Point class defined within it.

Option C) The Point class cannot be instantiated at line 15. This option is incorrect because the Point class can be instantiated, as shown in option B.

Option D) Line 1 = new Line() ; 1.Point p = new 1.Point(); This option is incorrect because it has a syntax error. The variable name "1" is not a valid identifier.

The correct answer is B. This option is correct because it correctly creates an instance of the Point class defined within the Line class.

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

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

  1. declare reset() using the synchronized keyword

  2. declare getName() using the synchronized keyword

  3. declare getCount() using the synchronized keyword

  4. declare the constructor using the synchronized keyword

  5. declare increment() using the synchronized keyword


Correct Option: A,C,E

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

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

  1. The output may be “r1 = 6, r2 = 14”.

  2. The output may be “r1 = 5, r2 = 15”.

  3. The output may be “r1 = 8, r2 = 12”.

  4. The code may run (and complete) with no output.

  5. The code may deadlock (without completing) with no output.

  6. M IllegalStateException or InterruptedException may be thrown at


Correct Option: A,B,E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) The output may be "r1 = 6, r2 = 14" - This option is true. In the given code, the doTransfer method is called three times, each time transferring a certain amount from one Record object to another. As the transfer method in the Clerk class is synchronized, only one thread can execute the transfer at a time. In the first transfer (doTransfer(r1, r2, 5)), the value of r1 is decreased by 5 and the value of r2 is increased by 5. Therefore, after the first transfer, the value of r1 will be 5 and the value of r2 will be 15. In the subsequent transfers, the values will be updated accordingly, resulting in "r1 = 6, r2 = 14" as a possible output.

Option B) The output may be "r1 = 5, r2 = 15" - This option is true. As explained in option A, after the first transfer, the value of r1 will be 5 and the value of r2 will be 15. Therefore, "r1 = 5, r2 = 15" is a possible output.

Option C) The output may be "r1 = 8, r2 = 12" - This option is false. The given code does not involve any transfers or operations that would result in r1 becoming 8 and r2 becoming 12.

Option D) The code may run (and complete) with no output - This option is true. The code may execute without any errors and complete without printing any output.

Option E) The code may deadlock (without completing) with no output - This option is true. Deadlock can occur in a multi-threaded program when two or more threads are waiting for each other to release a resource. In this code, there are multiple synchronized blocks and multiple threads running concurrently. If a deadlock occurs, the code may get stuck and not complete, resulting in no output.

Option F) IllegalStateException or InterruptedException may be thrown - This option is false. There are no specific conditions in the given code that would cause an IllegalStateException or InterruptedException to be thrown.

Therefore, the correct answers are A, B, and E.

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

  1. p0 = p1;

  2. p1 =p2;

  3. p2 = p4;

  4. p2 = (ClassC)p1;

  5. p1 = (ClassB)p3;

  6. p2 = (ClassC)p4;


Correct Option: A,E,F

AI Explanation

To determine which options are valid, let's go through each option and analyze whether the assignment is valid or not.

Option A) p0 = p1; This assignment is valid. Since ClassB extends ClassA, an object of ClassB can be assigned to a reference variable of type ClassA.

Option B) p1 = p2; This assignment is invalid. ClassB and ClassC are sibling classes that both extend ClassA. They are not in an inheritance relationship with each other. Therefore, an object of ClassC cannot be assigned to a reference variable of type ClassB.

Option C) p2 = p4; This assignment is invalid. Similar to option B, ClassC and ClassA are sibling classes. They are not in an inheritance relationship with each other. Therefore, an object of ClassA cannot be assigned to a reference variable of type ClassC.

Option D) p2 = (ClassC)p1; This assignment is invalid. Although ClassB extends ClassA, and ClassC extends ClassA, ClassB and ClassC are sibling classes and not in an inheritance relationship with each other. Therefore, an object of ClassB cannot be cast to ClassC.

Option E) p1 = (ClassB)p3; This assignment is valid. Since ClassB extends ClassA, and p3 refers to an object of ClassB, it is valid to cast p3 to ClassB and assign it to p1.

Option F) p2 = (ClassC)p4; This assignment is valid. Since ClassC extends ClassA, and p4 refers to an object of ClassC, it is valid to cast p4 to ClassC and assign it to p2.

Therefore, the three valid options are A, E, and F.

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

  1. x.a2();

  2. z.a2();

  3. z.c1();

  4. z.a1();

  5. y.c1();

  6. x.a1();


Correct Option: A,B,D,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

Which four are true? (Choose four.)

  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