java 测试用例核心代码_核心Java测验

Explore fundamental concepts and coding skills in Java through a series of multiple-choice questions covering topics such as array instantiation, reserved keywords, OOPs concepts, exception handling, and more. Test your understanding of Java basics and challenge yourself with code snippets." 87886790,7335718,Unity UGUI UI拖拽与范围限制实现,"['Unity3D', 'UI开发', 'UGUI', '游戏开发', '交互设计']
摘要由CSDN通过智能技术生成

java 测试用例核心代码

Welcome to Core Java Quiz. Java is an object-oriented programming language.

欢迎来到Core Java测验。 Java是一种面向对象的编程语言。

核心Java测验 (Core Java Quiz)

In this quiz, you will be tested on Core Java basics and OOPS concepts. There are some code snippets too to test your basic Java coding skills.

在本测验中,您将接受Core Java基础知识和OOPS概念的测试。 也有一些代码片段可以测试您的基本Java编码技能。

Some of the questions have multiple answers. You can click on the “Reveal Answer” button for the correct answer and explanation.

有些问题有多个答案。 您可以单击“ 显示答案 ”按钮以获取正确的答案和说明。

Give it a try and share it with others if you like it.

试试看,并与他人分享。

1.以下哪种方法是在Java中实例化数组的有效方法? (1. Which of the below is valid way to instantiate an array in java?)

A. int myArray [] = {1, 3, 5}; 
B. int myArray [] [] = {1,2,3,4}; 
C. int [] myArray = (5, 4, 3); 
D. int [] myArray = {“1”, “2”, “3”};

A. int myArray [] = {1,3,5};
B. int myArray [] [] = {1,2,3,4};
C. int [] myArray =(5,4,3);
D. int [] myArray = {“ 1”,“ 2”,“ 3”};

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

int [] myArray = {“1”, “2”, “3”}; is invalid because String can’t be converted to an int.

int [] myArray = (5, 4, 3); is invalid because array elements should be defined in curly braces ({}).

int myArray [] [] = {1,2,3,4}; is invalid because myArray is a two-dimensional array whereas in this case it’s being defined as a one-dimensional array. The compiler will complain as Type mismatch: cannot convert from int to int[].

正确答案:A

int [] myArray = {“ 1”,“ 2”,“ 3”}; 无效,因为无法将String转换为int。

int [] myArray =(5,4,3); 无效,因为数组元素应使用大括号({})定义。

int myArray [] [] = {1,2,3,4}; 之所以无效,是因为myArray是二维数组,而在这种情况下,它被定义为一维数组。 编译器会抱怨类型不匹配:无法从int转换为int []。

2.以下哪些是Java中的保留关键字? (2. Which of the below are reserved keyword in Java?)

A. array
B. goto
C. null
D. int

A.数组
B.转到
C.空
D.int

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B, D

The goto and int are reserved keywords in java. array and null are not keywords in java.

正确答案:B,D

gotoint是Java中的保留关键字。 arraynull不是Java中的关键字。

3.如果我们尝试在程序下编译并运行,将会发生什么? (3. What will happen if we try to compile and run below program?)
interface Foo{ int x = 10;}

public class Test { 
    public static void main(String[] args) { 
        Foo.x = 20; 
        System.out.println(Foo.x); 
    }
}

A. Prints 10 
B. Prints 20 
C. Compile Time Error 
D. Runtime error because Foo.x is final.

A.版画10
B.版画20
C.编译时错误
D.运行时错误,因为Foo.x是最终的。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: C

By default, any field of the interface is public, static, and final. So we can’t change is, hence compile-time error at the statement Foo.x = 20;.

正确答案:C

默认情况下,接口的任何字段都是public,static和final。 因此我们无法更改,因此语句Foo.x = 20;时的编译时错误Foo.x = 20;

4.以下程序的输出是什么? (4. What will be the output of the below program?)
public class Test {
	public static void main(String[] args) {
		char c = 65;
		System.out.println("c = " + c);
	}
}

A. Compile Time Error 
B. Prints “c = A” 
C. Runtime Error 
D. Prints “c = 65”

A.编译时错误
B.打印“ c = A”
C.运行时错误
D.打印“ c = 65”

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

Java compiler tries to automatically convert int to char. Since 65 gets converted to A, hence output will be “c = A”. The char values range is from u0000 to uffff. So char c = 65535; is valid but char c = 65536; will give compile time error.

正确答案:B

Java编译器尝试自动将int转换为char。 由于65转换为A,因此输出将为“ c = A”。 字符值范围是从u0000到uffff。 所以char c = 65535; 有效,但char c = 65536; 将给出编译时错误。

5.下面的程序将输出什么? (5. What will be output of below program?)
public class Test { 
    public void main(String[] args) {
        int x = 10*20-20; 
        System.out.println(x); 
    }
}

A. Runtime Error
B. Prints 180
C. Prints 0
D. Compile-time error.

A.运行时错误
B.版画180
C.打印0
D.编译时错误。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

Runtime error because main method is not static. The error message will be Main method is not static in class Test, please define the main method as: public static void main(String[] args)

正确答案:A

运行时错误,因为main方法不是静态的。 错误消息将是Main method is not static in class Test, please define the main method as: public static void main(String[] args)

6. Java中static关键字的有效语句是什么? (6. What are the valid statements for static keyword in Java?)

A. We can have static block in a class. 
B. The static block in a class is executed every time an object of class is created.
C. We can have static method implementations in interface.
D. We can define static block inside a method.

答:我们可以在一个类中包含静态块。
B.每次创建类的对象时,都会执行类中的静态块。
C.我们可以在接口中使用静态方法实现。
D.我们可以在方法内部定义静态块。

点击以显示答案 (Click to Reveal Answer)

Correct Answers: A, C

We can have static block in a class, it gets executed only once when class loads. From java 8 onwards, we can have static method implementations in interfaces.

正确答案:A,C

我们可以在一个类中有静态块,当类加载时,它只能执行一次。 从Java 8开始,我们可以在接口中使用静态方法实现。

7.选择OOPS的所有核心概念。 (7. Select all the core concepts of OOPS.)

A. Abstraction
B. Inheritance
C. Interface
D. Polymorphism
E. Generics

A.抽象
B.继承
C.界面
D.多态性
E.泛型

点击以显示答案 (Click to Reveal Answer)

Correct Answers: A, B, D

OOPS core concepts are;

正确答案:A,B,D

OOPS核心概念是;

  1. Abstraction

    抽象化
  2. Encapsulation

    封装形式
  3. Polymorphism

    多态性
  4. Inheritance

    遗产
  5. Composition

    组成
  6. Association

    协会
  7. Aggregation

    聚合

Read more at OOPS Concepts

OOPS Concepts了解更多

8.以下哪个语句对于Java继承是正确的? (8. Which of the following statements are true for inheritance in Java?)

A. The “extend” keyword is used to extend a class in java.
B. You can extend multiple classes in java.
C. Private members of the superclass are accessible to the subclass.
D. We can’t extend Final classes in java.

答:“ extend”关键字用于扩展java中的类。
B.您可以在Java中扩展多个类。
C.子类可以访问超类的私有成员。
D.我们不能在Java中扩展Final类。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: D

Inheritance is one of the core concepts in Java. You should be familiar with it. Please read the following articles to learn more about the answer choices – Inheritance in Java, Multiple Inheritance in Java.

正确答案:D

继承是Java的核心概念之一。 您应该熟悉它。 请阅读以下文章,以了解有关答案选择的更多信息-Java中的继承,Java中的多重继承

9.以下程序的输出是什么? (9. What will be the output of below program?)
package com.journaldev.java;

public class Test {
	public static void main(String[] args) {
		Super s = new Subclass();
		s.foo();
	}
}

class Super {
	void foo() {
		System.out.println("Super");
	}
}

class Subclass extends Super {
	static void foo() {
		System.out.println("Subclass");
	}
}

A. Compile time error
B. Super
C. Subclass
D. Runtime error

A.编译时错误
B.超级
C.子类
D.运行时错误

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

Subclass foo() method can’t be static, it will give compile time error This static method cannot hide the instance method from Super.

正确答案:A

子类foo()方法不能是静态的,它将产生编译时错误。 This static method cannot hide the instance method from Super

10.以下程序的输出是什么? (10. What will be the output of below program?)
package com.journaldev.java;

public class Test {
	public static void main(String[] args) {
		Subclass s1 = new Subclass();
		s1.foo(); // line 6
		Super s = new Subclass();
		s.foo(); // line 8
	}
}

class Super {
	private void foo() {
		System.out.println("Super");
	}
}

class Subclass extends Super {
	public void foo() {
		System.out.println("Subclass");
	}
}

A. Compile time error at line 6
B. Compile time error at line 8
C. Compile time error at both line 6 and 8
D. Works fine and prints “Subclass” two times.

A.第6行的编译时间错误
B.第8行的编译时间错误
C.第6行和第8行的编译时间错误
D.工作正常,并打印两次“子类”。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

Compile time error at line 8 because Super class foo() method is private. The error message is The method foo() from the type Super is not visible.

正确答案:B

由于Super class foo()方法是私有的,因此在第8行出现编译时错误。 错误消息是The method foo() from the type Super is not visible

11.以下程序的输出是什么? (11. What will be the output of below program?)
import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		try {
			throw new IOException("Hello");
		} catch (IOException | Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

A. Compile-time error
B. Prints “Hello”
C. Runtime Error

A.编译时错误
B.打印“你好”
C.运行时错误

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

Compile-time error as The exception IOException is already caught by the alternative Exception.

正确答案:A

编译时错误为The exception IOException is already caught by the alternative Exception

12.以下程序的输出是什么? (12. What will be the output of below program?)
public class Test {
	public static void main(String[] args) {
		String x = "abc";
		String y = "abc";
		x.concat(y);
		System.out.print(x);
	}
}

A. abcabc
B. abc
C. null

A.abcabc
B.abc
C.空

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

x.concat(y); will create a new string but it’s not assigned to x, so the value of x is not changed.

正确答案:B

x.concat(y); 将创建一个新字符串,但未分配给x,因此x的值不会更改。

13.以下哪些是Java中未经检查的异常? (13. Which of the below are unchecked exceptions in java?)

A. RuntimeException
B. ClassCastException
C. NullPointerException
D. IOException

A.RuntimeException
B. ClassCastException
C.NullPointerException
D. IOException

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, C

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor’s throws clause.

正确答案:A,B,C

RuntimeException及其子类是未经检查的异常。 未经检查的异常不需要在方法或构造函数的throws子句中声明。

14.以下程序的输出是什么? (14. What will be the output of below program?)
package com.journaldev.java;

import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		try {
			throw new Exception("Hello ");
		} catch (Exception e) {
			System.out.print(e.getMessage());
		} catch (IOException e) {
			System.out.print(e.getMessage());
		} finally {
			System.out.println("World");
		}
	}
}

A. Compile-time error
B. Hello
C. Hello World
D. Hello Hello World

A.编译时错误
B.你好
C.你好世界
D.你好世界

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

Compile-time error Unreachable catch block for IOException. It is already handled by the catch block for Exception.

正确答案:A

编译时错误Unreachable catch block for IOException. It is already handled by the catch block for Exception Unreachable catch block for IOException. It is already handled by the catch block for Exception

15.以下哪个语句对Java是正确的? (15. Which of the following statement(s) are true for java?)

A. JVM is responsible for converting Byte code to the machine-specific code.
B. We only need JRE to run java programs.
C. JDK is required to compile java programs.
D. JRE doesn’t contain JVM

答:JVM负责将字节码转换为机器特定的代码。
B.我们只需要JRE来运行Java程序。
C.编译Java程序需要JDK。
D. JRE不包含JVM

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, C

For a complete explanation, read JDK, JRE, and JVM.

正确答案:A,B,C

有关完整的说明,请阅读JDK,JRE和JVM

16. java类中可以有两个主要方法吗? (16. Can we have two main methods in a java class?)

A. Yes
B. No

答:是的
B.不

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A

This was a tricky question. We can have multiple methods having name as “main” in java through method overloading.

正确答案:A

这是一个棘手的问题。 通过方法重载,我们可以在Java中使用名称为“ main”的多个方法。

17.关于Java中的注释,以下哪项是正确的? (17. Which of the following statements are true about annotations in java?)

A. @interface keyword is used to create custom annotation
B. @Override is a built-in annotation in java
C. Annotations can’t be applied to fields in a class.
D. @Retention is one of the meta annotation in java.
E. Java annotation information gets lost when class is compiled.

A. @interface关键字用于创建自定义注释
B. @Override是Java中的内置注释
C.注释不能应用于类中的字段。
D. @Retention是Java中的元注释之一。
E.编译类时,Java注释信息会丢失。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, D

For a complete explanation, read Java Annotations.

正确答案:A,B,D

有关完整的解释,请阅读Java Annotations

18.关于Java中的Enum,以下哪些陈述是正确的? (18. Which of the following statements are true about Enum in java?)

A. All java enum implicitly extends java.lang.Enum class.
B. Java enum can implement interfaces.
C. We can create instance of enum using new operator.
D. Enums can’t be used in switch statements.
E. Enum constants are implicitly static and final.

答:所有java枚举都隐式扩展了java.lang.Enum类。
B. Java枚举可以实现接口。
C.我们可以使用new运算符创建enum实例。
D.枚举不能在switch语句中使用。
E.枚举常量是隐式静态的和最终的。

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, E

Read more at Enum in Java.

正确答案:A,B,E

在Java的Enum中阅读更多内容。

19.以下哪些是Java中的内置类加载器? (19. Which of the below are built-in class loaders in java?)

A. Bootstrap Class Loader
B. Extensions Class Loader
C. Runtime Class Loader
D. System Class Loader

A. Bootstrap类加载器
B.扩展类加载器
C.运行时类加载器
D.系统类加载器

点击以显示答案 (Click to Reveal Answer)

Correct Answer: A, B, D

Read more at Classloaders in Java.

正确答案:A,B,D

在Java的Classloader中阅读更多内容。

20.以下程序的输出是什么? (20. What will be the output of below program?)
package com.journaldev.util;

public class Test {
	public static String toString() {
		System.out.println("Test toString called");
		return "";
	}

	public static void main(String args[]) {
		System.out.println(toString());
	}
}

A. “Test toString called”
B. Compile-time error
C. “Test@7fh2bd8” (Object class toString() method is being called)

A.“测试toString称为”
B.编译时错误
C.“ Test @ 7fh2bd8”(正在调用对象类toString()方法)

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

We will get a compile-time error because we can’t have an Object class method overridden with the static keyword. The Object class has toString() method. You will get a compile-time error as “This static method cannot hide the instance method from Object”.

正确答案:B

我们将得到一个编译时错误,因为我们无法使用static关键字覆盖Object类方法。 Object类具有toString()方法。 您将收到一个编译时错误,因为“此静态方法无法从Object中隐藏实例方法”。

21.以下程序的输出是什么? (21. What will be the output of below program?)
public class Test {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		System.out.println("s1 == s2 is:" + s1 == s2);
	}
}

A. s1 == s2 is:true
B. false
C. s1 == s2 is:false
D. true

A. s1 == s2是:true

C. s1 == s2是:false
D.真实

点击以显示答案 (Click to Reveal Answer)

Correct Answer: B

The given statements output will be “false” because in java + operator precedence is more than == operator. So the given expression will be evaluated to “s1 == s2 is:abc” == “abc” i.e false.

正确答案:B

给定的语句输出将为“ false”,因为在Java +运算符中,优先级大于==运算符。 因此,给定的表达式将被评估为“ s1 == s2 is:abc” ==“ abc”,即false。

结论 (Conclusion)

I hope you liked the Core Java Quiz. If you think I have missed some important areas, let me know and I will add some more tricky quiz questions here.

Next Quiz: Java String Quiz

希望您喜欢Core Java Quiz。 如果您认为我错过了一些重要领域,请告诉我,我将在此处添加一些更棘手的测验问题。

下一个测验Java字符串测验

翻译自: https://www.journaldev.com/15161/core-java-quiz

java 测试用例核心代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值