Tag: programming languages

Questions Related to programming languages

  1. The number of bytes is compiler dependent

  2. 2

  3. 4

  4. 8


Correct Option: D

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