Scjp 6.0 _ Java _ Level 2

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By LanFang
L
LanFang
Community Contributor
Quizzes Created: 2 | Total Attempts: 648
Questions: 10 | Attempts: 169

SettingsSettingsSettings
Java Quizzes & Trivia

歡迎來挑戰 SCJP 6.0_JAVA_ level 2


Questions and Answers
  • 1. 

    Given: class One {     void foo() {     } } class Two extends One {     // insert method here } Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.) 

    • A.

      Int foo(){/* more code here */}

    • B.

      Void foo(){/* more code here */}

    • C.

      Public void foo(){/* more code here */}

    • D.

      Private void foo(){/* more code here */}

    • E.

      Protected void foo(){/* more code here */}

    Correct Answer(s)
    B. Void foo(){/* more code here */}
    C. Public void foo(){/* more code here */}
    E. Protected void foo(){/* more code here */}
    Explanation
    The correct answer is void foo(){/* more code here */}, public void foo(){/* more code here */}, and protected void foo(){/* more code here */}. These three methods correctly complete class Two because they have the same name and return type as the foo() method in the superclass One. Additionally, they have different access modifiers (void, public, and protected) which are allowed to be less restrictive than the access modifier in the superclass.

    Rate this question:

  • 2. 

    Given: class X {     X() {         System.out.print(1);     }     X(int x) {         this();         System.out.print(2);     } } public class Y extends X {     Y() {         super(6);         System.out.print(3);     }     Y(int y) {         this();         System.out.println(4);     }     public static void main(String[] a) {         new Y(5);     } } What is the result? 

    • A.

      13

    • B.

      134

    • C.

      1234

    • D.

      2134

    • E.

      2143

    • F.

      4321

    Correct Answer
    C. 1234
    Explanation
    The code creates two classes, X and Y. Class X has two constructors, one without any parameters and one with an int parameter. The constructor without parameters prints 1. The constructor with the int parameter calls the constructor without parameters using "this()" and then prints 2. Class Y extends class X and has two constructors, one without any parameters and one with an int parameter. The constructor without parameters calls the constructor in class X with the int parameter using "super(6)", which prints 1 and 2. It then prints 3. The constructor with the int parameter calls the constructor without parameters using "this()", which prints 1 and 2. It then prints 4. In the main method, a new object of class Y is created with the int parameter 5. Therefore, the output is 1234.

    Rate this question:

  • 3. 

    Given: 1. public class A{ 2.     public void doit(){ 3. } 4. public String doit(){ 5.     return "a"; 6. } 7. public double doit(int x){ 8.     return 1.0; 9. } 10.} What is the result? 

    • A.

      An exception is thrown at runtime.

    • B.

      Compilation fails because of an error in line 7.

    • C.

      Compilation fails because of an error in line 4.

    • D.

      Compilation succeeds and no runtime errors with class A occur.

    Correct Answer
    C. Compilation fails because of an error in line 4.
    Explanation
    The compilation fails because of an error in line 4. This is because the method "doit()" in line 4 has the same signature (same name and same return type) as the method "doit()" in line 2. In Java, methods are differentiated based on their signature, which includes the method name and the parameters. Since both methods have the same signature, the compiler cannot determine which method to call and therefore throws a compilation error.

    Rate this question:

  • 4. 

    Given: public class SimpleCalc {     public int value;     public void calculate() {         value += 7;     } } And: public class MultiCalc extends SimpleCalc {     public void calculate() {         value -= 3;     }     public void calculate(int multiplier) {         calculate();         super.calculate();         value *= multiplier;     }     public static void main(String[] args) {         MultiCalc calculator = new MultiCalc();         calculator.calculate(2);         System.out.println("Value is: " + calculator.value);     } } What is the result? 

    • A.

      Value is: 8

    • B.

      Compilation fails.

    • C.

      Value is: 12

    • D.

      Value is: -12

    • E.

      The code runs with no output.

    • F.

      An exception is thrown at runtime.

    Correct Answer
    A. Value is: 8
    Explanation
    The result is "Value is: 8" because the calculate() method in the MultiCalc class subtracts 3 from the value, then the calculate(int multiplier) method calls the calculate() method, adds 7 to the value using the super.calculate() method from the parent class, and finally multiplies the value by the multiplier. In the main method, the calculate(2) method is called, so the value is initially 0, then it becomes -3, then it becomes 4 (after adding 7), and finally it becomes 8 (after multiplying by 2).

    Rate this question:

  • 5. 

    Given: public class Base {     public static final String FOO = "foo";     public static void main(String[] args) {         Base b = new Base();         Sub s = new Sub();         System.out.print(Base.FOO);         System.out.print(Sub.FOO);         System.out.print(b.FOO);         System.out.print(s.FOO);         System.out.print(((Base) s).FOO);     } } class Sub extends Base {     public static final String FOO = "bar"; } What is the result? 

    • A.

      Foofoofoofoofoo

    • B.

      Foobarfoobarbar

    • C.

      Foobarfoofoofoo

    • D.

      Foobarfoobarfoo

    • E.

      Barbarbarbarbar

    • F.

      Foofoofoobarbar

    • G.

      Foofoofoobarfoo

    Correct Answer
    D. Foobarfoobarfoo
    Explanation
    The correct answer is "foobarfoobarfoo" because when the code is executed, the first print statement "System.out.print(Base.FOO)" prints "foo" because it is accessing the static variable FOO from the Base class. The second print statement "System.out.print(Sub.FOO)" prints "bar" because it is accessing the static variable FOO from the Sub class. The third print statement "System.out.print(b.FOO)" also prints "foo" because it is accessing the static variable FOO from the Base class using an instance of the Base class. The fourth print statement "System.out.print(s.FOO)" prints "bar" because it is accessing the static variable FOO from the Sub class using an instance of the Sub class. The fifth print statement "System.out.print(((Base) s).FOO)" prints "foo" because it is accessing the static variable FOO from the Base class using a reference of the Base class that points to an object of the Sub class.

    Rate this question:

  • 6. 

    Given: class Mammal { } class Raccoon extends Mammal {     Mammal m = new Mammal(); } class BabyRaccoon extends Mammal { } Which four statments are true? (Choose four.) 

    • A.

      Raccoon is-a Mammal.

    • B.

      Raccoon has-a Mammal.

    • C.

      BabyRaccoon is-a Mammal.

    • D.

      BabyRaccoon is-a Raccoon.

    • E.

      BabyRaccoon has-a Mammal.

    • F.

      BabyRaccoon is-a BabyRaccoon.

    Correct Answer(s)
    A. Raccoon is-a Mammal.
    B. Raccoon has-a Mammal.
    C. BabyRaccoon is-a Mammal.
    F. BabyRaccoon is-a BabyRaccoon.
    Explanation
    The first statement is true because Raccoon is a subclass of Mammal, indicating that Raccoon is a type of Mammal. The second statement is also true because Raccoon has a Mammal instance variable. The third statement is true because BabyRaccoon is a subclass of Mammal, indicating that BabyRaccoon is a type of Mammal. The fourth statement is true because BabyRaccoon is a subclass of Raccoon, indicating that BabyRaccoon is a type of Raccoon.

    Rate this question:

  • 7. 

    Which four statements are true? (Choose four.) 

    • A.

      Has-a relationships should never be encapsulated.

    • B.

      Has-a relationships should be implemented using inheritance.

    • C.

      Has-a relationships can be implemented using instance variables.

    • D.

      Is-a relationships can be implemented using the extends keyword.

    • E.

      Is-a relationships can be implemented using the implements keyword.

    • F.

      The relationship between Movie and Actress is an example of an is-a relationship.

    • G.

      An array or a collection can be used to implement a one-to-many has-a relationship.

    Correct Answer(s)
    C. Has-a relationships can be implemented using instance variables.
    D. Is-a relationships can be implemented using the extends keyword.
    E. Is-a relationships can be implemented using the implements keyword.
    G. An array or a collection can be used to implement a one-to-many has-a relationship.
    Explanation
    - Has-a relationships can be implemented using instance variables, as an object can have another object as a member variable.
    - Is-a relationships can be implemented using the extends keyword, as a class can inherit from another class.
    - Is-a relationships can also be implemented using the implements keyword, as a class can implement an interface.
    - An array or a collection can be used to implement a one-to-many has-a relationship, as multiple objects can be stored in an array or collection.

    Rate this question:

  • 8. 

    Given: public class Hello {     String title;     int value;     public Hello() {         title += " World";     }     public Hello(int value) {         this.value = value;         title = "Hello";         Hello();     } } And: Hello c = new Hello(5); System.out.print(c.title); What is the result? 

    • A.

      Hello

    • B.

      Hello World

    • C.

      Compilation fails.

    • D.

      Hello World 5

    • E.

      The code runs with no output.

    • F.

      An exception is thrown at runtime.

    Correct Answer
    C. Compilation fails.
    Explanation
    The code fails to compile because there is a syntax error in the constructor. The line "Hello();" is not a valid statement. It seems like the intention was to call the default constructor within the parameterized constructor, but the correct syntax for calling a constructor is "this()".

    Rate this question:

  • 9. 

    Given: public class ItemTest {     private final int id;     public ItemTest(int id) {         this.id = id;     }     public void updateId(int newId) {         id = newId;     }     public static void main(String[] args) {         ItemTest fa = new ItemTest(42);         fa.updateId(69);         System.out.println(fa.id);     } } Which four statments are true? 

    • A.

      Compilation fails.

    • B.

      An exception is thrown at runtime.

    • C.

      The attribute id in the ItemTest object remains unchanged.

    • D.

      The attribute id in the ItemTest object is modified to the new value.

    • E.

      A new ItemTest object is created with the preferred value in the id attribute.

    Correct Answer
    A. Compilation fails.
    Explanation
    The compilation fails because the variable "id" is declared as final, meaning it cannot be reassigned a new value once it has been initialized in the constructor. Therefore, the statement "id = newId;" in the "updateId" method causes a compilation error.

    Rate this question:

  • 10. 

    Given: class Foo {     private int x;     public Foo(int x) {         this.x = x;     }     public void setX(int x) {         this.x = x;     }     public int getX() {         return x;     } } public class Gamma {     static Foo fooBar(Foo foo) {         foo = new Foo(100);         return foo;     }     public static void main(String[] args) {         Foo foo = new Foo(300);         System.out.print(foo.getX() + "-");         Foo fooFoo = fooBar(foo);         System.out.print(foo.getX() + "-");         System.out.print(fooFoo.getX() + "-");         foo = fooBar(fooFoo);         System.out.print(foo.getX() + "-");         System.out.print(fooFoo.getX());     } } What is the output? 

    • A.

      300-100-100-100-100

    • B.

      300-300-100-100-100

    • C.

      300-300-300-100-100

    • D.

      300-300-300-300-100

    Correct Answer
    B. 300-300-100-100-100
    Explanation
    The output is 300-300-100-100-100.

    In the main method, a new instance of the Foo class is created with x = 300. The getX() method is called on this instance, returning 300, which is printed.

    Then, the fooBar() method is called with the foo instance as an argument. Inside fooBar(), a new instance of Foo is created with x = 100, and this new instance is returned.

    Back in the main method, the getX() method is called on the original foo instance, which still has x = 300, returning 300, which is printed.

    Then, the getX() method is called on the fooFoo instance, which was assigned the returned value from fooBar(). Since fooFoo is now referencing the new Foo instance with x = 100, 100 is returned and printed.

    Finally, the foo instance is assigned the returned value from calling fooBar() with fooFoo as an argument. This means foo is now referencing the new Foo instance with x = 100. The getX() method is called on foo, returning 100, which is printed.

    The getX() method is called on fooFoo again, returning 100, which is printed.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 18, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 02, 2013
    Quiz Created by
    LanFang
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.