JAVA Today - Exercise 11

JAVA Practices Everyday [Packaging and Inheritance] ...
1. Radio Question 1
2. Radio Question 2
3. Single Topic 3
4. Radio Topic 4
5. Single Topic Five

JAVA Practices Everyday [Packaging and Inheritance]

1. Radio Question 1

With regard to packages, the following errors are: ()
A. Package is a collection of classes
B. Packages prevent name conflicts for classes
The C.import statement can import a specified package
D.import static can import some static methods

Correct answer: C

[Analysis]
A. Packages are a collection of classes. A package can contain several class files and can also contain several packages
B.Java uses package s as a mechanism to prevent naming conflicts, access control, provide search and location class es, interfaces, enumerations, annotation s, and so on.
D. Import static allows you to use static methods in a package directly without invoking the package name. Static import functionality is provided after JDK 1.5. If all methods in a class are static methods declared using static, import static can be used directly at import time.

2. Radio Question 2

What is the result of the following code?
A.Base
B.BaseBase
C. Compilation failure
D. Code running but no output
E.Runtime throws an exception

class Base { Base() { System.out.print("Base"); } } public class Alpha extends Base { public static void main( String[] args ) { new Alpha(); //Call parent class parameterless construction method new Base(); } }

Correct answer: B

[Analysis]: Subclass constructors are executed while instantiating subclasses, but parent constructors are executed first when subclass constructors are executed

3. Single Topic 3

The output of the following program is ()
A.BD
B.DB
C.C
D. Compilation errors

class Base{ public Base(String s){ System.out.print("B"); } } public class Derived extends Base{ public Derived (String s) { System.out.print("D"); } public static void main(String[] args){ new Derived("C"); } }

Correct answer: D

[Analysis]: The parameterless construction method of the parent class is implicitly called before the construction method of the subclass is invoked. If the parent class does not have a parameterless construction method, the parameterized construction method of the calling parent class must be displayed. Obviously, the parent Base of this topic does not have a parameterless construction method, nor does it invoke the parameterized construction method of the parent class, so the compilation fails.

4. Radio Topic 4

The following program executes as a result: ()
A.ZYXX
B.ZYXY
C.YXYZ
D.XYZX

class X{ Y y=new Y(); public X(){ System.out.print("X"); } } class Y{ public Y(){ System.out.print("Y"); } } public class Z extends X{ Y y=new Y(); public Z(){ System.out.print("Z"); } public static void main(String[] args) { new Z(); } }

Correct answer: C

[Analysis]

The first knowledge point is inheritance. Subclasses inherit the parent class and the parent class is loaded first during program execution, which is why overrides occur. So the program loads class X first.
Second point of knowledge: because the java program loads global variables first when it executes. The member variables in class X are Y b = new Y();When the program loads b, it calls the parameterless construction method of class Y and outputs Y.
Third knowledge point: Constructor. The default invocation of a program call is that the constructor is a parameterless constructor, so when inheriting class X, the program first executes the parameterless constructor output X of X (x is not output when a parameter is given to this parameterless constructor). When a new object is called, the constructor of this class. The third output is similar to the fourth output above.

class X{ Y y=new Y(); // ******************************First Output public X(){ System.out.print("X"); // **********Second Output } } class Y{ public Y(){ System.out.print("Y"); } } public class Z extends X{ Y y=new Y(); public Z(){ System.out.print("Z"); //******************************************Third Output } public static void main(String[] args) { new Z(); // Fourth Output } }

5. Single Topic Five

The following descriptions of java encapsulation are correct: ()
A.super keyword is a reference within a subclass object that refers to its parent object
The B.super keyword can refer not only to the direct parent of a child class, but also to the parent of a parent class.
C. Subclasses can only call methods of the parent class, not properties of the parent class, through the super keyword
D. Subclasses can only call attributes of the parent class, not methods of the parent class, through the super keyword

Correct answer: A

[Analysis]
Super keyword: We can use the super keyword to access the parent class members, referencing the parent class of the current object.
Super can be understood as a pointer to a super (parent) class object that is closest to itself.

30 September 2021, 13:06 | Views: 8274

Add new comment

For adding a comment, please log in
or create account

0 comments