0

programming languages Online Quiz - 322

Description: programming languages Online Quiz - 322
Number of Questions: 20
Created by:
Tags: programming languages
Attempted 0/20 Correct 0 Score 0

12 You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java //Base.java package Base; class Base{ protected void amethod(){ System.out.println("amethod"); }//End of amethod }//End of class base package Class1; //Class1.java public class Class1 extends Base{ public static void main(String argv[]){ Base b = new Base(); b.amethod(); }//End of main }//End of Class1

  1. a. Compile Error: Methods in Base not found

  2. b. Compile Error: Unable to access protected method in base class

  3. c. Compilation followed by the output "amethod"

  4. d. Compile error: Superclass Class1.Base of class Class1.Class1 not found


Correct Option: D
  1. a. The elements in a Java array can only be of primitive types, not objects

  2. b. Arrays elements are initialized to default values wherever they are created using the keword new

  3. c. An array may be dynamically resized using the setSize method

  4. d. You can find out the size of an array using the size method


Correct Option: B
  1. a float f=1/3;

  2. b int i=1/3;

  3. c float f=1.01;

  4. d double d=999d;


Correct Option: A,B,D
  1. A System.out.println(1+1);

  2. B int i=2+'2';

  3. C String s="on"+'one';

  4. D byte b=255;


Correct Option: A,B

16 Which of the following statements are true?

  1. a. All of the variables in an interface are implicitly static

  2. b. All of the variables in an interface are implicitly final

  3. c. All of the methods in an interface are implicitly abstract

  4. d. A method in an interface can access class level variables


Correct Option: A,B,C

AI Explanation

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

Option A) All of the variables in an interface are implicitly static - This option is true. In an interface, all variables are implicitly static, meaning they belong to the interface itself rather than any specific instance.

Option B) All of the variables in an interface are implicitly final - This option is true. In an interface, all variables are implicitly final, meaning their values cannot be changed once they are assigned.

Option C) All of the methods in an interface are implicitly abstract - This option is true. In an interface, all methods are implicitly abstract, meaning they do not have a body and must be implemented by any class that implements the interface.

Option D) A method in an interface can access class-level variables - This option is false. Methods in an interface can only access variables declared within the interface itself. They cannot access variables from implementing classes or any other classes.

The correct answer is A, B, C. These options are correct because variables in an interface are implicitly static and final, and methods in an interface are implicitly abstract.

  1. a. An interface can only contain method and not variables

  2. b. Interfaces cannot have constructors

  3. c. A class may extend only one other class and implement only one interface

  4. d. Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.


Correct Option: B
  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error.

  4. d.


Correct Option: B

20 Read this piece of code carefully if(" String ".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal");

  1. a. the code will compile an print "Equal".

  2. b. the code will compile an print "Not Equal".

  3. c. the code will cause a compiler error

  4. d.


Correct Option: A

AI Explanation

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

Option A) The code will compile and print "Equal". In the given code snippet, the trim() method is used to remove leading and trailing whitespace from the string " String ". The trim() method returns a new string with the whitespace removed.

However, when comparing strings, the == operator checks for reference equality, not value equality. In Java, the == operator compares the memory addresses of the objects being compared.

In this case, the string " String " is not the same object as the string "String", even though they have the same value after trimming. Therefore, the == comparison will evaluate to false.

So, the code will not print "Equal".

Option B) The code will compile and print "Not Equal". As explained in Option A, the == comparison will evaluate to false because the strings being compared are different objects. Therefore, the code will print "Not Equal".

Option C) The code will cause a compiler error. There is no compilation error in the given code. The code is syntactically correct, and there are no issues that would cause a compiler error.

Option D) (No option provided) This is not a valid option.

The correct answer is A) The code will compile and print "Equal".

Given: 11. public static void main(String[] args) { 12. try { 13. args=null; 14. args[0] = “test”; 15. System.out.println(args[0]); 16. } catch (Exception ex) { 17. System.out.println(”Exception”); 18. } catch (NullPointerException npe) { 19. System.out.println(”NullPointerException”); 20. } 21. } What is the result?

  1. test

  2. Exception

  3. Compilation fails.

  4. NullPointerException


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) test - This option is incorrect because the code on line 13 assigns a null value to the args array, and then on line 14, it tries to access args[0] and assign it the value "test". Since args is null, a NullPointerException is thrown before reaching the System.out.println statement.

Option B) Exception - This option is incorrect because the catch block on line 16 catches any exception of type Exception. However, the exception that is thrown in this case is a NullPointerException, so the catch block on line 16 is not executed.

Option C) Compilation fails - This option is correct because on line 14, the code tries to assign a value to args[0] without first initializing the args array. This will result in a compilation error because the args array is null and cannot be accessed.

Option D) NullPointerException - This option is incorrect because the catch block on line 19 catches a NullPointerException, but since the code fails to compile, this block is not executed.

The correct answer is C) Compilation fails. This option is correct because attempting to assign a value to an uninitialized array (args[0]) will result in a compilation error.

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

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
- Hide questions