slug
type
status
category
date
summary
tags
password
icon
Key Points and Sample Codes
- Objects: Instances of Classes (2.1)
- Key Points: Objects are instances of classes, encapsulating data and methods that operate on data.
- Sample Code:
- Creating and Storing Objects (Instantiation) (2.2)
- Key Points: Objects are created using the
newkeyword and stored in variables. - Sample Code:
- Calling a Void Method (2.3)
- Key Points: Void methods perform actions but do not return a value.
- Sample Code:
- Calling a Void Method with Parameters (2.4)
- Key Points: Methods can accept parameters to customize their behavior.
- Sample Code:
- Calling a Non-void Method (2.5)
- Key Points: Non-void methods return a value.
- Sample Code:
- String Objects: Concatenation, Literals, and More (2.6)
- Key Points: Strings can be concatenated using
+, and literals are defined using double quotes. - Sample Code:
- String Methods (2.7)
- Key Points: String class provides methods like
length(),substring(),indexOf(). - Sample Code:
- Wrapper Classes: Integer and Double (2.8)
- Key Points: Wrapper classes provide a way to use primitive data types as objects.
- Sample Code:
- Using the Math Class (2.9)
- Key Points: The Math class provides methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
- Sample Code:
20 AP Style MCQs
- Which of the following is used to create a new object in Java?
- A)
new - B)
class - C)
newClass - D)
instance
- What will the following code print?
- A) Test
- B) obj
- C) Hello World
- D) Nothing
- What is the output of the following code?
- A) Java Programming
- B) JavaProgramming
- C) Java
- D) Programming
- What does the following method return type signify?
- A) Returns an integer
- B) Returns nothing
- C) Returns a string
- D) Returns a boolean
- Which method would you use to determine the length of the string "APCSA"?
- A)
getLength() - B)
length() - C)
size() - D)
lengthOf()
- What will be the output of the following Java code snippet?
- A) True
- B) False
- C) Error
- D) None of the above
- What is the result of the following expression in Java?
- A) 6
- B) 8
- C) 5
- D) 9
- In Java, which of the following can be used to concatenate two strings?
- A)
+ - B)
append() - C)
concatenate() - D)
attach()
- How can you create a string that represents the name "John Doe" using string literals?
- A)
String name = "John" + "Doe"; - B)
String name = "John" + " " + "Doe"; - C)
String name = "John, Doe"; - D)
String name = "John-Doe";
- Which method of the
Stringclass returns the position of the first occurrence of a specified character string in a String? - A)
find() - B)
search() - C)
locate() - D)
indexOf()
- Consider the following Java class:
- A)
Vehicle.start(); - B)
Vehicle myCar = start(); - C)
myCar.start(); - D)
start(myCar);
What is the correct way to call the
start method on an object of Vehicle named myCar?- If you create a
Stringobject as follows: - A) 12
- B) 13
- C) 14
- D) An error occurs
What is the result of
greeting.length()?- Which of the following code snippets correctly creates an
Integerobject representing the number 42? - A)
Integer num = new Integer("42"); - B)
Integer num = Integer.valueOf("42"); - C)
Integer num = 42; - D) All of the above
- What is the return type of the
Math.abs()method when passed an integer argument? - A)
int - B)
double - C)
long - D)
float
- What is the purpose of the
voidkeyword in a method declaration? - A) Indicates that the method returns no value
- B) Declares a variable inside the method
- C) Makes the method invisible to other classes
- D) None of the above
- Which of the following is a correct way to use the
Math.random()method to generate a random integer between 0 and 99? - A)
(int) (Math.random() * 100) - B)
(int) (Math.random() * 99) - C)
(Math.random() * 99) - D)
Math.random(100)
- In Java, what does the
Stringmethodsubstring(1, 4)return when called on the string"example"? - A)
"xam" - B)
"xamp" - C)
"exam" - D)
"xample"
- What will be the result of executing the following code segment?
- A)
true - B)
false - C) Error
- D) None of the above
- How can you convert a
doubleto aStringin Java? - A)
String.valueOf(double) - B)
Double.toString(double) - C)
double.toString() - D) Both A and B are correct
- Which wrapper class method is used to parse a
Stringinto anInteger? - A)
Integer.parseInt() - B)
Integer.valueOf() - C)
Integer.toString() - D)
Integer.parseString()
Answers to MCQs
- A
- C
- A
- B
- B
- B
- B
- A
- B
- D
- C
- B
- D
- A
- A
- A
- A
- B
- D
- A
第6题解析:
输出结果为 false。
原因如下:
- 使用
new关键字会每次创建一个新的对象,因此num1和num2分别指向两个不同的Integer实例,即使它们存储的值都是 100。
==操作符比较的是对象的引用,也就是它们是否指向同一个内存地址,而不是比较对象内部的值。
- 如果想比较两个对象的值是否相等,可以使用
equals()方法,比如num1.equals(num2),这样会返回 true。
第18题解析:
这段代码会输出
false。解释:
str1使用字符串字面量"test",该字符串会被存储在 Java 的字符串常量池中。
str2使用new String("test")创建,这会在堆内存中创建一个新的字符串对象,而不是使用常量池中的对象。
==运算符比较的是两个对象的引用是否相同,而不是它们的内容。由于str1和str2指向的是不同的对象,所以str1 == str2的结果是false。
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/usingobjects
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。








