Java review day 2 --- JavaSE Basics

Source: Internet
Author: User

[1] Which of the following statements about JVM are true?

A. JVM runs on the operating system and depends on the operating system.

B. The JVM runs on the operating system and has nothing to do with the operating system.

C. JVM supports Java program running, which can directly run Java bytecode files

D. JVM supports Java program running, which can directly run Java source code files

Source code files (text files) with the extension of. java: files that write code During Development

The. class extension is a bytecode file (Binary): The file executed during the system runtime.

[2] The correct expression in the following options is:

A. byte B = 128; [-128 ~ 127 bytes]

B. boolean n = null; [null can assign values to all referenced data types and cannot assign values to basic data types]

C. double d = 0.313d; [0.313 is of the double type by default. It can be omitted. This is not an error]

D. float f = 0.823; [0.823 is of the double type by default, while the double type occupies 4 bytes in the memory, and the float type occupies 2 bytes. Therefore, values cannot be assigned directly, float f = 0.823f; on the surface, it can be assigned a value only when it is a float data type]

Basic Data Type

Integer: byte, short, int, long

Decimal type: float and double

Character Type: char

Boolean: boolean

The packaging type corresponding to the basic data type. The packaging type can provide some features that are available as "classes ".

Integer: Byte, Short, Integer, Long

Decimal type: Float and Double

Character Type: Character

Boolean: Boolean

The difference between the basic data type and the reference type. The value of the basic data type is stored in the stack memory, and the value of the reference data type is a space opened up in the heap memory, stack memory stores the address of this space

[3] Which of the following identifiers is valid ()

A. class [keyword]

B. $ abc [$ existence is to take care of programmers with PHP development background]

C.1234 [cannot start with a number]

D. _ name age [it can start with _, and there cannot be spaces in the middle of the identifier]

Identifier: the place where our programmers name themselves-variables, classes, methods, packages

Rule: may consist of letters, numbers, underscores, and $. It cannot start with a number.

Specifications:

① Variable: camper name, lowercase appleBoy

② Class (Interface): name the camper, with the first letter PeopleStudy

③ Method: Same as the variable

Pack 4: lowercase letters

[4] Which of the following class members in the same package have the strictest access modifier restrictions?

A. public [class internal √ same package √ subclass √ outside package √]

B. private [class internal √ same package × subclass × outside package ×]

C. protected [class internal √ same package √ subclass √ outside package ×]

D. transient [This Is Not A permission modifier, it is an interference item here]

E. No modifier is required. [internal class √ same package √ subclass × outside package ×]

[5]

1. public class Test {

2. int x = 12;

3. public void method (int x ){

4. x + = x;

5. System. out. println (x );

6 .}

7 .}

Given:

34. Test t = new Test ();

35. t. method (5 );

What is the output from line 5 of the Test class?

A. 5 B. 10 C. 12 D. 17 E. 24

[6] Which of the following statements returns true?

A. "john "! = "John"

B. "john". equals ("john ")

C. "john" = "john ".

D. "john". equals (new Button ("john "))

String comparison: it is unsafe to use "=" for comparison, and "=" for comparison of two equal signs, it is their value for the basic data type, for the referenced data type, the memory reference address is compared. In short, it is the value in the stack memory.

Comparison string: Use the equals () method to compare the content of two strings.

[7] compile and run the following code:

Public class Test {

Public static void main (String args []) {

Int age;

Age = age + 1;

System. out. println ("The age is" + age );

}

}

A. Compile and run without output

B. Compile and output after running: The age is 1

C. Compilation, but errors are generated during runtime

D. Compilation fails.

Local variables must be initialized before they can be used. Unlike member variables, member variables are initialized during class loading.

[8] if you add a break statement to a for statement, which of the following statements can be used ________

A. The for statement execution is not affected.

B. Interrupt this loop and continue the next loop

C. Exit the for loop and execute the statement following the for statement.

D. None of the above statements

[9] Given:

55. int [] x = {1, 2, 3, 4, 5 };

56. int y [] = x;

57. System. out. println (y [2]);

Which is true?

A. Line 57 will print the value 2.

B. Line 57 will print the value 3.

C. Compilation will fail because of an error in line 55.

D. Compilation will fail because of an error in line 56.

[10] what will happen if the following program is compiled and run:

Public class Q {

Public static void main (String argv []) {

Int anar [] = new int [5];

System. out. println (anar [0]);

}

}

A. Error: anar is not initialized

B. null

When a C. 0 array element is created, the system allocates an initial value based on its data type.

D. 5

[11]

The following code prints the result:
Int I = 1;

Switch (I ){

Case 0:

System. out. println ("zero ");

Break;

Case 1:

System. out. println ("one ");

Case 2:

System. out. println ("two ");

Default:

System. out. println ("default ");

}

A. one B. one, default C. one, two, default D. Default

After the switch structure is entered, if the break is not met, the default statement will be executed all the time. If no branch enters, the code corresponding to the default statement will be executed.

[12]

In the following program snippet

String a = "ABCD ";

String B = a. toLowerCase ();

B. replace ('A', 'D ');

B. replace ('B', 'C ');

System. out. println (B );

What are the compilation and running results?

A. abcd

B. ABCD

C. dccd

D. dcba

E. Compilation Error

F. An exception is thrown during running.

// In Java, a string is an unchangeable Character Sequence

String a = "ABCD ";

// ToLowerCase () does not change the original string, but returns a New String

String B = a. toLowerCase ();

// Note that a itself has not changed

System. out. println ("a =" + );

// B accepts the value after a changes

System. out. println ("B =" + B );

B = B. replace ('A', 'D ');

B = B. replace ('B', 'C ');

// Use B to accept the return value of the replace () method

System. out. println ("B =" + B );

[13] Code:

For (int I = 0; I <4; I + = 2 ){

System. out. print (I + "");

}

System. out. println (I );

What is the result?

A. 0 2 4

B. 0 2 4 5

C. 0 1 2 3 4

D. Compilation error.

E. An exception is thrown during running.

In Java, the lifecycle of a variable (scope) is {}.

[14] Which of the following statements about class inheritance are true ()

A. A class can inherit multiple parent classes at the same time. [Java is A single inheritance]

B. One class can have only one subclass. [one class in Java can have multiple subclasses]

C. Subclass can directly call all methods of the parent class. [permission restriction]

D. One Class inherits the other class and requires the extends keyword

[15]

11. class Payload {

12. private int weight;

13. public Payload (int wt) {weight = wt ;}

13. public void setWeight (int w) {weight = w ;}

15. public String toString (){

Return Integer. toString (weight );

}

16 .}

18. public class TestPayload {

19. static void changePayload (Payload p ){

20./* insert code here */

21 .}

22.

23. public static void main (String [] args ){

24. Payload p = new Payload (10 );

25. p. setWeight (1024 );

26. changePayload (p );

27. System. out. println ("The value of p is" + p );

28 .}

29 .}

Which statement, placed at line 20, causes (making) the code to print "The value of p is 420 ."?

A. p. setWeight (420 );

B. p. changePayload (420 );

C. p = new payloads (420 );

D. Payload. setWeight (420 );

E. p = Payload. setWeight (420 );

F. p = new Payload (); p. setWeight (420 );

[16]

1. abstract class implements Actit {

2. abstract float getFloat ();

3 .}

4. public class AbstractTest extends Actit {

5. private float f1 = 1.0f;

6. private float getFloat () {return f1 ;}

7 .}

What is the result?

A. Compilation is successful.

B. An error on line 6 causes a runtime failure.

C. An error at line 6 causes compilation to fail.

D. An error at line 2 causes compilation to fail.

V. Task

Task 1. multiple choice questions

[1] in Java, which of the following statements cannot be compiled? (B)

A. String s = "join" + 3;

B. int a = "join" + 3;

C. int a = 'A' + 5;

D. float f = 5 + 5.5F;

Cause of analysis: the int type is an integer type and cannot be added or subtracted from the limit type.

[2] Which of the following data types are not basic data types? (B)

A. byte B. String C. boolean D. char

[3] The value range of the basic data type byte is:

A.0 ~ 65,535

B. (-128 )~ 127

C. (-32,768 )~ 32,767

D. (-256 )~ 255

[4] Which of the following lines of code will cause errors or warnings during compilation:

A. float f = 1.3f;

B. char c = 'a ';

C. byte B = 57;

D. boolean B = null;

E.int I = 10;

Cause Analysis: Fourth. Null can assign values to all referenced data types and cannot assign values to basic data types.

[5] The parameter description of the public static void main method is:

A. String [] args

B. Strings args []

C. String args

D. String [] args []

Cause analysis: (Describe where the wrong option is not selected)

Select A for this question,

B error: because this is a String, the String cannot be followed by s,

C error: because it does not conform to the data format

D. Error: because two [] are not required, it is the same as C.

[6] What is the value of code B below? ()

Public class {

Public static void main (String args []) {

Float a = 3.5;

Int B = (int) a + 2;

System. out. println (B );

}

}

A. Compilation error B .5.5 C.5 D.6

Cause of analysis: Because 3.5 is double type data, it cannot be directly modified with float

[7] Given:

11. public class Test {

12. public static void main (String [] args ){

13. int x = 5;

14. boolean b1 = true;

15. boolean b2 = false;

16. if (x = 4 )&&! B2)

17. System. out. print ("1");

19. System. out. print ("2");

20. if (b2 = true) & b1)

21. System. out. print ("3");

22 .}

23 .}

What is the result?

A. 2 B. 3 C. 1 2 D. 2 3E. 1 2 3

F. Compilation fails

G. An error is reported during running.

Cause analysis:

Because the first if statement returns false, the corresponding output below the if statement cannot be output, followed by 2, and then the second if statement returns true, SO 3 outputs, select D;

Task 2. programming questions

[1] write a program and print the 99 multiplication table.

Public class test {

Public static void main (String [] args ){

For (int I = 1; I <10; I ++ ){

For (int j = 1; j <= I; j ++ ){

If (I = j ){

System. out. println (I + "*" + j + "=" + I * j );

} Else {

System. out. print (I + "*" + j + "=" + I * j + "");

}

}

}

}

}

[2] According to the implementation class.

Create an object of the Cylinder class in the TestCylinder class, use the constructor to set the bottom radius and height of the Cylinder, and output the surface area and volume of the Cylinder.

Circle class: package zuoye; public class Circle {private double radius; public Circle () {} public Circle (double radius) {this. setRadius (radius);} public double getRadius () {return radius;} public void setRadius (double radius) {this. radius = radius;} public double findArea () {double area = 3.14 * this. getRadius () * this. getRadius (); return area ;}} Cylinder. javapackage zuoye; public class Cylinder extends Circle {private double length; public Cylinder (double radius, double length) {setRadius (radius); this. length = length;} public void Cylinder (double length) {this. length = length;} public double findArea () {double area = (2*3.14 * getRadius () + (3.14*2 * getRadius () * length ); return area;} public double findVolume () {double Volume = super. findArea () * length; return Volume;} TestCylinder. javapackage zuoye; public class TestCylinder {public static void main (String [] args) {Cylinder c = new Cylinder (2, 3); double area = c. findArea (); double volume = c. findVolume (); System. out. println ("the cylindrical surface area is" + area); System. out. println ("cylindrical volume" + volume );}}


[3] It is very important to implement the list mode.

package danzi;public class single {private single(){}private static single Instance = new single();public static single getInstance(){return Instance;}}
package danzi;public class singleTest {public static void main(String[] args) {single s1 = single.getInstance();single s2 = single.getInstance();System.out.println(s1==s2);}}


Task 3. Short answer

[1] what is method rewriting? [Test with program code and then attach it to it]

A: Override indicates that the method in the subclass can have the same name and parameter as a method in the parent class. When this method is called through the instance object created by the subclass, the definition method in the subclass is called, this is equivalent to overwriting the exactly same method defined in the parent class, which is also a manifestation of object-oriented programming polymorphism.

Rewrite requirements:

① The subclass method overwrites the parent class method, which is also called overwrite.

② Same method name

③ The parameter list is the same

④ The Return Value Type of the subclass method cannot be greater than that of the parent method.

⑤ The exception thrown by the subclass method during compilation cannot be larger than that thrown by the parent method, and it does not matter if the exception occurs during runtime.

⑥ The access permissions of subclass methods cannot be stricter than that of parent Methods

[2] what is method overloading? [Test with program code and then attach it to it]

A: overloading Overload indicates that multiple methods with the same name can exist in the same class, but the parameter lists of these methods are different (that is, the number or type of parameters are different ).

Requirements for heavy load:

① In the same class

② Same method name

③ List of different parameters

[3] how to compare two strings? Why?

It is best to use the equals () function for comparison, because if we simply use = for comparison, it is only applicable to the comparison defined by String s1 = "xxx" and String s2 = "xxx, if it is String t1 = new String ("xxx"), this definition cannot be compared, because no matter how the previous type is defined, its address remains unchanged, so it must be equal, but the new String ("xxx") definition changes his address, so it is different, so = is not safe, it is best to use equals () function comparison. More comprehensive.

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.