0

programming languages Online Quiz - 325

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

Given:interface Foo {}class Alpha implements Foo { }class Beta extends Alpha {} class Delta extends Beta {public static void main( String[] args) { Beta x = new Beta(); // insert code here}}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: A,B,C,D

AI Explanation

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

Option A) Alpha a = x; - This option is incorrect because it is a valid assignment. Since Beta extends Alpha, you can assign an object of type Beta to a variable of type Alpha.

Option B) Foo f = (Delta)x; - This option is incorrect because it is a valid assignment. Since Delta extends Beta and implements Foo, you can assign an object of type Beta to a variable of type Foo by casting it to Delta.

Option C) Foo f = (Alpha)x; - This option is incorrect because it is a valid assignment. Since Beta extends Alpha and implements Foo, you can assign an object of type Beta to a variable of type Foo by casting it to Alpha.

Option D) Beta b = (Beta)(Alpha)x; - This option is incorrect because it is a valid assignment. Since Beta extends Alpha, you can assign an object of type Beta to a variable of type Beta by casting it to Alpha.

None of the given options will cause a java.lang.ClassCastException. The correct answer is None (None of the given options will cause a java.lang.ClassCastException).

Note: A java.lang.ClassCastException occurs when you try to cast an object to a type that it is not compatible with. In this case, all the assignments are valid and do not involve incompatible types, so a ClassCastException cannot occur.

Given the following directory tree, you must enable assertions for everything except for FileTwo. base |firstLevelDirectory |_Foo |_Bar | secLevelDirectory |_FileOne |_FileTwo What command-line code should you use?

    1. java -ea -da:base.firstLevelDirectory.secLevelDirectory.FileTwo
    1. java -ea:base.firstLevelDirectory.secLevelDirectory.FileTwo -da
    1. java -ea:base... -da:base.firstLevelDirectory.secLevelDirectory...
    1. java -enableassertions -disableassertions:base.FileTwo

Correct Option: A

You are considering four different "if" statements for the following code: public class IsItSame { public static void main(String [] args ) { int TheAnswer = 0; IsItSame x = new IsItSame(); Object y = x; IsItSame z = new IsItSame(); if (z == x) TheAnswer = 0; if (y == x) TheAnswer = 1; if (y == z) TheAnswer = 2; if (y.equals(z) ) TheAnswer = 3: if (x.equals(z) ) TheAnswer = 4; if (z.equals(y) ) TheAnswer = 5; System.out.printIn("The answer is " + TheAnswer); } } What will be printed out once the code is compiled and executed?

    1. The answer is 0
    1. The answer is 1
    1. The answer is 2
    1. The answer is 3
    1. The answer is 4

Correct Option: B

Which of the following options is a benefit of a Set or SortedSet? Incorrect. The correct option is as follows

    1. Use of an index
    1. Lack of duplicates
    1. Use of non-unique keys
    1. Use of unique keys

Correct Option: C

Given: 11. public class Ball { 12. public enum Color { RED, GREEN, BLUE }; 13. public void foo() { 14. // insert code here 15. { System.out.println(c); } 16. } 17. } Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?

  1. for( Color c : Color.values())

  2. for( Color c = RED; c <= BLUE; c++)

  3. for( Color c; c.hasNext() ; c.next())

  4. for( Color c = Color[0]; c <= Color[2]; c++)

  5. for( Color c = Color.RED; c <= Color.BLUE; c++)


Correct Option: A

You have the following code: public class Java { static double dSyntaxs; public static void main (String [] args) { Java IsItTrue = new Java(); IsItTrue.TrueSyntax(); } public void TrueSyntax() { System.out.println("Its: " + dSyntaxs); } } What would the output be?

    1. Its: 1
    1. Its: 0
    1. Its: true
  1. 4 Its: false

    1. Its: 0.0

Correct Option: E

which methods would prevent the execution of a thread? (Choose all that apply)

    1. sleep()
    1. yield()
    1. run()
    1. join()

Correct Option: A,B,D

Given: 10. public class Fabric 11. public enum Color { 12. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 13. private final int rgb; 14. Color( int rgb) { this.rgb = rgb; } 15. public int getRGB() { return rgb; } 16. }; 17. public static void main( String[] argv) { 18. // insert code here 19. } 20. } Which two code fragments, inserted independently at line 18, allow the Fabric class to compile?

  1. Color skyColor = BLUE;

  2. Color treeColor = Color.GREEN;

  3. Color purple = new Color( 0xff00ff);

  4. if( RED.getRGB() < BLUE.getRGB() ) {}

  5. Color purple = Color.BLUE + Color.RED;

  6. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}


Correct Option: B,F

Which of the following conversions paths are widening conversions? (Choose two) You have not made any correct choices. The correct options are as follows

  1. 1From a int to a short

  2. 2From a char to an int

  3. 3From a short to an int

  4. 4From a double to a float

  5. 5From a float to a long


Correct Option: B,C

A method is declared as protected. What classes can access this method? (Choose two. Each option is a correct answer.)

    1. Classes in the same package
    1. All classes
    1. Only the class within the package that defined the method
    1. Classes that inherit access

Correct Option: A,D

What is the purpose of the break statement?

    1. It jumps out of the loop back to the calling method.
    1. It jumps out of the loop to the next statement after the loop.
    1. It stops the execution of the program and of the virtual machine.
    1. It stops the innermost iteration and continues with the next iteration of the loop.

Correct Option: B

Between which two lines should you insert a break or a continue statement to print the message "Please close the door. I feel a draft. Thank you"? (Line numbers are included only for clarity's sake) 1. String door="open"; 2. boolean draft=false; 3. boolean shivers=false; 4. while (door=="open") { 5. draft=true; 6. System.out.println("Please close the door."); 7. if (draft) { 8. shivers=true; 9. System.out.println("I feel a draft."); 10. door="close"; 11. if (shivers) { 12. System.out.println("I'm shivering."); 13. } 14. } 15. } 16. if (door=="close") { 17. System.out.println("Thank you"); 18. shivers=false; 19. }

    1. Replace lines 7 to 14 with a break statement.
    1. Replace lines 7 to 14 with a continue statement.
    1. Replace line 12 with a continue statement.
    1. Insert a break statement between lines 16 and 17.

Correct Option: C

What argument types are legal for switch statements? (Choose all that apply)

    1. char
    1. byte
    1. short
    1. int
    1. boolean

Correct Option: A,B,C,D

The following are data type conversions that would be performed when initializing variables. Which one may lose information in the conversion process?

    1. From an int to a long
    1. From a byte to an int
    1. From a short to a byte
    1. From an int to a double
  1. 5) All of the above


Correct Option: E

Which of the following are appropriate ways to use assertions? (Choose all that apply) You have not made any correct choices. The correct options are as follows

  1. 1 public void myMethod(int x) {

  2. 2 private void myMethod(int x) {

  3. 3 switch(z) {

  4. 4 private void anotherMethod() {


Correct Option: B,C

You are writing an assertion expression in your code. Which code is legal?

  1. 1 int z = 0;

  2. 2 assert (z = 9);

  3. 3 assert (x ==5);

  4. 4 int z = 0;

  5. 5 boolean x = true;


Correct Option: C

you assign a value to the primitive variables x and y as indicated below. Which declaration for z must be used for this code to properly compile? byte x = 3; byte y = 2; // insert declaration for z here z = x + y;

    1. byte z;
    1. int z;
    1. char z
    1. None of these options are correct

Correct Option: B

You have declared a single dimensional array of type int using the following code: int [] MyInt = new int [10]; You are attempting to initialize all of the arrays segments with a for loop using the following code: for (int x = 0; x <= 10; x++) { MyInt[x] = x; } What will happen when you attempt to compile the program that contains this code?

    1. The program will run successfully with no errors or warnings.
    1. The program will halt with a runtime exception.
    1. The program will halt with a compiler error.
    1. The program will run successfully with no errors but a warning will be generated.

Correct Option: B

What is the result of int 8 >> 1?

    1. 0000 0000 0000 0000 0000 0000 0000 0100
    1. 0000 0000 0000 0000 0000 0000 0000 1000
    1. 0000 0000 0000 0000 0000 0000 0001 0000
    1. 0000 0000 0000 0000 0000 0000 0000 0111
    1. 0000 0000 0000 0000 0000 0000 0000 1001

Correct Option: A

The following code throws an exception. public class ContainerClass { public void draw() throws IOException { // additional code to write values to file. system.out.println("This container was drawn and saved to file."); return true; } } What code overrides this method? (Note that Exception itself is a superclass of IOException)

  1. 1 public class Cup extends ContainerClass {

  2. 2 public class Basket extends ContainerClass {

  3. 3 public class Caldron extends ContainerClass {

  4. 4 public class Bag extends ContainerClass {


Correct Option: B
- Hide questions