Tag: programming languages

Questions Related to programming languages

Given: 11. static classA { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B “); } 16. } 17. public static void main(String[] args) { 18.A a=new B(); 19. a.process(); 20.} What is the result?

  1. B

  2. The code runs with no output.

  3. An exception is thrown at runtime.

  4. Compilation fails because of an error in line 15.

  5. Compilation fails because of an error in line 18.

  6. Compilation fails because of an error in line 19.


Correct Option: F

Given: 8. public class test { 9. public static void main(String [] a) { 10. assert a.length == 1; 11. } 12. } Which two will produce an AssertionError? (Choose two.)

  1. java test

  2. java -ea test

  3. java test file1

  4. java -ea test file1

  5. java -ea test file1 file2

  6. java -ea:test test file1


Correct Option: B,E

AI Explanation

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

Option A) java test - This option will not produce an AssertionError because the -ea flag is not used to enable assertions.

Option B) java -ea test - This option will produce an AssertionError because the -ea flag is used to enable assertions. The assertion on line 10 (assert a.length == 1;) will fail if the length of a is not equal to 1.

Option C) java test file1 - This option will not produce an AssertionError because the -ea flag is not used to enable assertions.

Option D) java -ea test file1 - This option will not produce an AssertionError because the -ea flag is used to enable assertions, but the program does not have any assertions to check.

Option E) java -ea test file1 file2 - This option will produce an AssertionError because the -ea flag is used to enable assertions. The assertion on line 10 (assert a.length == 1;) will fail if the length of a is not equal to 1.

Option F) java -ea:test test file1 - This option will not produce an AssertionError because the -ea flag is used to enable assertions, but the program does not have any assertions to check.

The correct answers are B and E. These options will produce an AssertionError because they include the -ea flag to enable assertions, and the assertion on line 10 will fail if the length of a is not equal to 1.

Given: 11. public static Iterator reverse(List list) { 12. Collections.reverse(list); 13. return list.iterator(); 14. } 15. public static void main(String[] args) { 16. List list = new ArrayList(); 17. list.add(” 1”); list.add(”2”); list.add(”3”); 18. for (Object obj: reverse(list)) 19. System.out.print(obj + “,”); 20. } ‘What is the result?

  1. 3,2, 1,

  2. 1, 2, 3,

  3. Compilation fails.

  4. The code runs with no output.

  5. An exception is thrown at runtime.


Correct Option: C

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

AI Explanation

To answer this question, let's go through each option and analyze the code:

Option A) null - This option is incorrect because the code does not print "null" in this scenario.

Option B) zero - This option is incorrect because the code does not print "zero" in this scenario.

Option C) some - This option is incorrect because the code does not print "some" in this scenario.

Option D) Compilation fails - This option is correct. The code will fail to compile due to a syntax error on line 13. The line String str = "null'; has a syntax error because the closing double quote is missing. It should be String str = "null";.

Option E) An exception is thrown at runtime - This option is incorrect because the code does not throw an exception at runtime. The code fails to compile before it can be executed.

Therefore, the correct answer is D) Compilation fails.

Given That: 10. interface Foo {} 11. class Alpha implements Foo { } 12. class Beta extends Alpha {} 13. class Delta extends Beta { 14. public static void main( String[] args) { 15. Beta x = new Beta(); 16. // insert code here 17. } 18. } Which code, inserted at line 16, will cause a java.lang.ClassCastException?

  1. Alpha a = x;

  2. Foo f= (Delta)x;

  3. Foo f= (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;


Correct Option: B
Explanation:

To solve this question, the user needs to know about inheritance in Java and how casting works. The key to answering this question correctly is understanding how casting works between different classes in an inheritance hierarchy.

Now, let's go through each option and explain why it is right or wrong:

A. Alpha a = x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

B. Foo f= (Delta)x; - This code is invalid because Delta is a subclass of Beta, so it cannot be downcast to Delta from Beta. This will throw a ClassCastException.

C. Foo f= (Alpha)x; - This code is valid because Beta is a subclass of Alpha, so it can be upcast to an Alpha without issue. This will not cause a ClassCastException.

D. Beta b = (Beta)(Alpha)x; - This code is invalid because Alpha is a superclass of Beta, so it cannot be downcast to Beta from Alpha. This will throw a ClassCastException.

Therefore, the correct answer is:

The Answer is: B

Perl was develped by

  1. Larry floor

  2. Larry wall

  3. Denise Ritche

  4. Micheal Jackson


Correct Option: B
  1. An interpreter language

  2. A compiler language

  3. Partial compiler and interpreter language

  4. An Indian language


Correct Option: C