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

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

AI Explanation

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

Option A) Compilation will succeed for all classes and interfaces. This option is incorrect. The compilation will not succeed for all classes and interfaces because there are errors in the code.

Option B) Compilation of class C will fail because of an error in line 2. This option is incorrect. There is no error in line 2 of class C. Line 2 in class C is a valid method declaration that overrides the method in class B.

Option C) Compilation of class C will fail because of an error in line 6. This option is correct. Line 6 in class C is attempting to override the method execute() from class B, but the return type of the overridden method in class C is Object, which is not compatible with the return type String in class B. This will result in a compilation error.

Option D) Compilation of class AImpl will fail because of an error in line 2. This option is incorrect. There is no error in line 2 of class AImpl. Line 2 is a valid method implementation that matches the method declaration in interface A.

The correct answer is C. Compilation of class C will fail because of an error in line 6.

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
  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: 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