已完成 0/10 个问题
问题:
Java 考试五(函数)
您已完成过测验,因此您不能再测验。
测验载入中...
您必须登入或注册才能开始测验。
您必须先完成以下测验才能开始:
答对 0/10 个问题
答题时间:
时间已花费
以下代码输出结果为:
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun()
{
return 20;
}
}Main 类中 main() 是一个静态函数, fun() 是一个非静态函数, Java 静态函数中不能调用非静态函数的方法。
Main 类中 main() 是一个静态函数, fun() 是一个非静态函数, Java 静态函数中不能调用非静态函数的方法。
以下代码输出结果为:
public class Main {
public static void main(String args[]) {
String x = null;
giveMeAString(x);
System.out.println(x);
}
static void giveMeAString(String y)
{
y = "FACESOHO";
}
}Java 中参数通过值传递,所以 x 传到函数中不会影响原来的值。
Java 中参数通过值传递,所以 x 传到函数中不会影响原来的值。
以下代码输出结果为:
class Main {
public static void swap(Integer i, Integer j) {
Integer temp = new Integer(i);
i = j;
j = temp;
}
public static void main(String[] args) {
Integer i = new Integer(10);
Integer j = new Integer(20);
swap(i, j);
System.out.println("i = " + i + ", j = " + j);
}
}
Java 函数参数通过值传递。
Java 函数参数通过值传递。
以下代码输出结果为:
class intWrap {
int x;
}
public class Main {
public static void main(String[] args) {
intWrap i = new intWrap();
i.x = 10;
intWrap j = new intWrap();
j.x = 20;
swap(i, j);
System.out.println("i.x = " + i.x + ", j.x = " + j.x);
}
public static void swap(intWrap i, intWrap j) {
int temp = i.x;
i.x = j.x;
j.x = temp;
}
}
在 Java 应用程序中永远不会传递对象,而只传递对象引用。因此是按引用传递对象。
在 Java 应用程序中永远不会传递对象,而只传递对象引用。因此是按引用传递对象。
以下代码输出结果为:
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun(int x = 0)
{
return x;
}
}
Java 函数不允许参数设置默认值。
Java 函数不允许参数设置默认值。
以下代码输出结果为:
class Test
{
public void demo(String str)
{
String[] arr = str.split(";");
for (String s : arr)
{
System.out.println(s);
}
}
public static void main(String[] args)
{
char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ',
'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};
String str = new String(array);
Test obj = new Test();
obj.demo(str);
}
}
String 类有一个内置的构造函数 String(character_array),它可以将字符数组初始化成一个字符串。split() 根据指定的规则或分隔符来分隔字符串,并返回数组。
String 类有一个内置的构造函数 String(character_array),它可以将字符数组初始化成一个字符串。split() 根据指定的规则或分隔符来分隔字符串,并返回数组。
以下代码输出结果为:
class Main
{
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("facesoho");
StringBuffer b = new StringBuffer("com");
a.delete(1,3);
a.append(b);
System.out.println(a);
}
}
delete(x, y) 函数删除字符串的 ‘x’(包含) 到 ‘y-1’(包含) 的位置元素。append() 函数用于连接字符串
delete(x, y) 函数删除字符串的 ‘x’(包含) 到 ‘y-1’(包含) 的位置元素。append() 函数用于连接字符串
以下代码输出结果为:
class Main
{
public static void main(String[] args)
{
String obj1 = new String("facesoho");
String obj2 = new String("facesoho");
if(obj1.hashCode() == obj2.hashCode())
System.out.println("object1 与 object2 哈希码相等");
if(obj1 == obj2)
System.out.println("object1 与 object2 内存地址一样");
if(obj1.equals(obj2))
System.out.println("object1 与 object2 值相等");
}
}
obj.hashCode() 函数返回对象的 32 位哈希值。 obj1.equals(obj2) 用于判断两个对象的值是否相等。 obj1 == obj2 在两个对象引用同一个对象时才会相等。
obj.hashCode() 函数返回对象的 32 位哈希值。 obj1.equals(obj2) 用于判断两个对象的值是否相等。 obj1 == obj2 在两个对象引用同一个对象时才会相等。
以下代码输出结果为:
class Test implements Cloneable
{
int a;
Test cloning()
{
try
{
return (Test) super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println("CloneNotSupportedException is caught");
return this;
}
}
}
class Main
{
public static void main(String args[])
{
Test obj1 = new Test();
Test obj2;
obj1.a = 10;
obj2 = obj1.cloning();
obj2.a = 20;
System.out.println("obj1.a = " + obj1.a);
System.out.println("obj2.a = " + obj2.a);
}
}
clone( ) 方法调用时会生成多个对象的拷贝。 类只有在实现 Cloneable 接口才可以实现克隆。
clone( ) 方法调用时会生成多个对象的拷贝。 类只有在实现 Cloneable 接口才可以实现克隆。
以下代码输出结果为:
class Main
{
public static void main(String[] args)
{
String str = "facesoho";
str.toUpperCase();
str += "wwwfacesohocom";
String string = str.substring(0,14);
string = string + str.charAt(3);
System.out.println(string);
}
}
str.toUpperCase() 将字符串小写字母转换为大写字母,但是它不会改变原始的字符串。 str.substring(x, y) 返回 ‘x'(包含) 到 ‘y'(不包含) 位置的字符串。 str.charAt(x) 返回 x 位置的字符。
str.toUpperCase() 将字符串小写字母转换为大写字母,但是它不会改变原始的字符串。 str.substring(x, y) 返回 ‘x'(包含) 到 ‘y'(不包含) 位置的字符串。 str.charAt(x) 返回 x 位置的字符。