Java question Two

Source: Internet
Author: User
Tags assert

QUESTION 37
Given:

    1. Class Super {
    2. private int A;
    3. protected Super (int a) {this.a = A;}
    4. } ...
    5. Class Sub extends Super {
    6. Public Sub (int a) {super (a);}
    7. Public Sub () {THIS.A = 5;}
    8. }
      Which, independently, would allow a Sub to compile? (Choose.)
      A. Change Line 2 to:
      public int A;
      B. Change Line 2 to:
      protected int A;
      C. Change line:
      Public Sub () {this (5);}
      D. Change line:
      Public Sub () {super (5);}
      E. Change line:

Public Sub () {super (a);}

A.B changes the properties of the parent class, does not work for the this.a of the subclass, the subclass invokes the property of the parent class, SUPER.A

The subclass constructor implicitly calls the parent class without a parameter constructor, if the parent class does not need to display the call to the parameter constructor

QUESTION 38
Given:

    1. public class Base {
    2. public static final String foo = "Foo";
    3. public static void Main (string[] args) {
    4. Base B = new Base ();
    5. Sub s = new Sub ();
    6. System.out.print (Base.foo);
    7. System.out.print (Sub.foo);
    8. System.out.print (B.foo);
    9. System.out.print (S.foo);
    10. System.out.print ((Base) s). FOO);
    11. } }
    12. 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

((Base) s). FOO) from within the outward transition
QUESTION 40
Given:

    1. public class Hi {
    2. void M1 () {}
    3. protected void () m2 {}
    4. }
    5. Class Lois extends Hi {
    6. Insert code here
    7. }
      Which four code fragments, inserted independently on line 7, would compile? (Choose four.)
      A. public void M1 () {}
      B. protected void M1 () {}
      C. private void M1 () {}
      D. void m2 () {} Protected>default level
      E. public void M2 () {}
      F. protected void M2 () {}
      G. private void M2 () {}

Sub-class override method cannot reduce access level
To investigate overrides, the scope of overridden methods in subclasses cannot be reduced (decreased). Public>protected>default>private

QUESTION 41
Which fragments is most likely to cause a stackoverflowerror? (Choose.)
A. int []x = {1,2,3,4,5};
for (int y = 0; y < 6; y++)
System.out.println (X[y]);
B. Static int[] x = {7,6,5,4};
static {x[1] = 8;
X[4] = 3; }//Not Stackoverflowerror exception
C. for (int y = ten; y < y++)
Dostuff (y);
D. void Doone (int x) {dotwo (x);}
void dotwo (int y) {dothree (y);}
void Dothree (int z) {dotwo (z);}
E. for (int x = 0; x < 1000000000; × x + +)
Dostuff (x);
F. void counter (int i) {counter (++i);}

Stackoverflowerror, recursive too deep error, recursive no exit
QUESTION 42
Given:

    1. Class A {
    2. public void process () {System.out.print ("A,");}
    3. Class B extends A {
    4. public void process () throws IOException {
    5. Super.process ();
    6. System.out.print ("B,");
    7. throw new IOException ();
    8. }
    9. public static void Main (string[] args) {
    10. try {new B (). process ();}
    11. catch (IOException e) {System.out.println ("Exception");}
    12. }
      What is the result?
      A. Exception
      B. a,b,exception
      C. Compilation fails because of an error on line 20.
      D. Compilation fails because of an error on line 14.
      E. A NullPointerException is thrown at runtime.
      Answer:d

The 14th row throws a parent class without an exception, an error. Subclasses are not allowed to throw exceptions that the parent class does not have.

QUESTION 43
Given:

    1. public void Go (int x) {
    2. ASSERT (x > 0);
    3. Switch (x) {
    4. Case 2:;
    5. Default:assert false;
    6. }
    7. }
    8. private void Go2 (int x) {assert (x < 0);}
      Which statement is true?
      A. All of the Assert statements is used appropriately.
      B. Only the Assert statement on line used appropriately.
      C. Only the Assert statement in line used appropriately.
      D. The Assert statement on line used appropriately.
      E. Only the assert statements on lines and used appropriately.
      F. Only the assert statements on lines and used appropriately.
      G. Only the assert statements on lines and used appropriately.

Use the Assert principle: 1. Only parameters in the private method can be verified, parameters of the public method cannot be verified, 2. Command-line arguments cannot be verified, 3. You can verify the branch statements that should not occur; 4. You cannot change the state of the program in any way.

QUESTION Given:

    1. public static Voidmain (string[] args) {

    2. String str = "NULL";

    3. if (str = = null) {

    4. SYSTEM.OUT.PRINTLN ("null");

    5. } else (str.length () = = 0) {//This line should be the else if

    6. System.out.println ("Zero");

    7. } else {

    8. System.out.println ("some");

    9. }

    10. }

What is the result?

A. Null

B. Zero

C. Some

D. Compilationfails.

E. An exception is thrown atruntime.

Java question Two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.