Tag: technology

Questions Related to technology

class Sub extends Super{ String name; Sub(String s) { super(s); name=s; } public static void main(String args[]){ Super sup = new Super("First"); Sub sub = new Sub("Second"); System.out.println(sub.name + " " + sup.name); } } class Super{ String name; Super(String s){ name =s ; } }

  1. Error: Super(java.lang.String) in Super cannot be applied to ()

  2. First Second

  3. Second First

  4. Runtime Exception


Correct Option: C

Click the Exhibit button. 10. class Inner { 11. private int x; 12. public void setX( int x) { this.x = x; } 13. public int getX() { return x; } 14. } 15. 16. class Outer { 17. private Inner y; 18. public void setY( Inner y) { this.y = y; } 19. public Inner getY() { return y; } 20. } 21. 22. public class Gamma { 23. public static void main( String[] args) { 24. Outer o = new Outer(); 25. Inner i = new Inner(); 26.int n=10; 27. i.setX(n); 28. o.setY(i); 29. // insert code here 30. System.out.println( o.getY().getX()); 31. } 32. } Which three code fragments, added individually at line 29, produce the output 100? (Choose three.)

  1. n = 100;

  2. i.setX( 100);

  3. o.getY().setX( 100);

  4. i = new Inner(); i.setX( 100);

  5. o.setY( i); i = new Inner(); i.setX( 100);

  6. i = new Inner(); i.setX( 100); o.setY( i);


Correct Option: B,C,F

AI Explanation

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

Option A) n = 100; - This option is incorrect because it assigns the value of 100 to the variable n, but it does not modify the value of x in the Inner object i that is stored in the Outer object o.

Option B) i.setX(100); - This option is correct because it directly sets the value of x in the Inner object i to 100.

Option C) o.getY().setX(100); - This option is correct because it first retrieves the Inner object i stored in the Outer object o using the getY() method, and then sets the value of x in the Inner object to 100 using the setX() method.

Option D) i = new Inner(); i.setX(100); - This option is incorrect because it creates a new Inner object i and sets its value of x to 100, but it does not update the Inner object that is stored in the Outer object o.

Option E) o.setY(i); i = new Inner(); i.setX(100); - This option is incorrect because it first sets the Inner object i as the value of y in the Outer object o, but then it creates a new Inner object i and sets its value of x to 100. This new Inner object is not stored in the Outer object o.

Option F) i = new Inner(); i.setX(100); o.setY(i); - This option is correct because it first creates a new Inner object i and sets its value of x to 100. Then, it sets this Inner object as the value of y in the Outer object o.

The correct answers are B, C, and F. These options modify the value of x in the Inner object i that is stored in the Outer object o, resulting in the output of 100 when o.getY().getX() is called at line 30.

Given: 1. public class TestString3 { 2. public static void main(String[] args) { 3. // insert code here 5. System.out.println(s); 6. } 7. } Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.) A. String s = “123456789”; s = (s-”123”).replace(1,3,”24”) - “89”; B. StringBuffer s = new StringBuffer(”123456789”); s.delete(0,3).replace( 1,3, “24”).delete(4,6); C. StringBuffer s = new StringBuffer(”123456789”); s.substring(3,6).delete( 1 ,3).insert( 1, “24”); D. StringBuilder s = new StringBuilder(”123456789”); s.substring(3,6).delete( 1 ,2).insert( 1, “24”); E. StringBuilder s = new StringBuilder(”123456789”); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);

  1. A

  2. B

  3. C

  4. D

  5. E


Correct Option: B,E

AI Explanation

To determine which code fragments inserted independently at line 3 generate the output 4247, let's go through each option:

Option A) String s = "123456789"; s = (s-"123").replace(1,3,"24") - "89"; This code will not compile because you cannot subtract a string from another string. Additionally, the replace() method does not have a subtract operator. Therefore, this option is incorrect.

Option B) StringBuffer s = new StringBuffer("123456789"); s.delete(0,3).replace(1,3, "24").delete(4,6); This code fragment will generate the output 4247.

  • The delete(0,3) method deletes the characters from index 0 to index 3 in the string, resulting in "456789".
  • The replace(1,3, "24") method replaces the characters from index 1 to index 3 with "24", resulting in "424789".
  • The delete(4,6) method deletes the characters from index 4 to index 6 in the string, resulting in "4247".

Therefore, this option is correct.

Option C) StringBuffer s = new StringBuffer("123456789"); s.substring(3,6).delete(1 ,3).insert(1, "24"); This code will not generate the output 4247.

  • The substring(3,6) method returns the substring from index 3 to index 6, resulting in "456".
  • The delete(1,3) method deletes the characters from index 1 to index 3 in the substring, resulting in "46".
  • The insert(1, "24") method inserts the string "24" at index 1 in the substring, resulting in "4246".

Therefore, this option is incorrect.

Option D) StringBuilder s = new StringBuilder("123456789"); s.substring(3,6).delete(1,2).insert(1, "24"); This code will not generate the output 4247.

  • The substring(3,6) method returns the substring from index 3 to index 6, resulting in "456".
  • The delete(1,2) method deletes the character at index 1 in the substring, resulting in "46".
  • The insert(1, "24") method inserts the string "24" at index 1 in the substring, resulting in "4246".

Therefore, this option is incorrect.

Option E) StringBuilder s = new StringBuilder("123456789"); s.delete(0,3).delete(1,3).delete(2,5).insert(1, "24"); This code fragment will generate the output 4247.

  • The delete(0,3) method deletes the characters from index 0 to index 3 in the string, resulting in "456789".
  • The delete(1,3) method deletes the characters from index 1 to index 3 in the string, resulting in "479".
  • The delete(2,5) method deletes the characters from index 2 to index 5 in the string, resulting in "47".
  • The insert(1, "24") method inserts the string "24" at index 1 in the string, resulting in "4247".

Therefore, this option is correct.

The correct answers are B and E.

  1. Source file

  2. Directories

  3. Binary files

  4. All the above


Correct Option: D