OCP Certification – Advanced Java Class Design – OCP认证 – 高级Java类设计

最后修改: 2019年 9月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll discuss the advanced Java class design objective of the OCP certification.

在本教程中,我们将讨论OCP认证中的高级Java类设计目标。

2. OCP Java Certification

2.OCP Java认证

The OCP certification is an upgrade on the OCA certification but follows the same format of multiple-choice questions. It, however, includes advanced topics such as concurrency, generics, and NIO.

OCP认证OCA认证的升级版,但采用的是相同的选择题格式。然而,它包括高级主题,如并发、泛型和NIO。

In this tutorial, we’ll focus on the advanced Java class design objective of the exam. In reality, some of the topics we’ll discuss are overlapping with the Java class design objective of the OCA exam. But, at the same time, OCP also contains questions on advanced topics like inner classes, enum types, and lambdas.

在本教程中,我们将重点讨论考试中的高级Java类设计目标。实际上,我们要讨论的一些主题与OCA考试的Java类设计目标是重叠的。但是,与此同时,OCP也包含了一些高级主题的问题,如内部类、枚举类型和lambdas

Each of the following sections is dedicated to an objective from the exam.

下面的每一节都是专门针对考试中的一个目标。

3. Develop Code That Uses Abstract Classes and Methods

3.开发使用抽象类和方法的代码

The first exam objective is the use of abstract classes and methods. In Java, we use abstract classes to share variables and methods between the concrete child classes.

第一个考试目标是使用抽象类和方法。在Java中,我们使用抽象类来在具体的子类之间共享变量和方法。

Exam Tip 3.1: Incorrect Access Modifier with abstract Classes

考试技巧3.1:使用抽象类的不正确访问修饰语

We must always look for an access modifier in questions about abstract classes and methods.

在关于抽象类和方法的问题中,我们必须始终寻找一个访问修饰符。

For example, try and solve the following:

例如,尝试解决以下问题。

package animal;
public abstract class Animal {
    
    abstract boolean canFly();
}
    
package horse;
import animal.Animal;

public class Horse extends Animal {
    
    @Override
    boolean canFly() {
        return false;
    }
    
    public static void main(String[] args) {
    
        System.out.println(new Horse().canFly());
    }    
}
Which of the following is true?
A. The output is false
B. Compilation fails on Line 10
C. Compilation fails on Line 12
D. None of the above

Notably, the abstract method has a default access modifier and since both classes are in different packages, we cannot access it in the Horse class. Therefore,  the correct answer is (B).

值得注意的是,抽象方法有一个默认的访问修改器,由于两个类都在不同的包中,我们不能在中访问它。因此,正确答案是(B)。

Exam Tip 3.2: Syntax Errors in abstract Class or Method

考试技巧3.2:抽象类或方法的语法错误

Some questions require us to check for incorrect syntax in the given code. With abstract classes, we can easily miss such errors.

有些问题要求我们在给定的代码中检查出不正确的语法。有了抽象类,我们可以很容易地错过这种错误。

For example, try to solve the following:

例如,尝试解决以下问题。

public abstract class Animal {
  
    protected abstract boolean canFly() {
    }
  
    public abstract void eat() {
        System.out.println("Eat...");
    }
}
  
public class Amphibian extends Animal {
    @Override
    protected boolean canFly() {
        return false;
    }
  
    @Override
    public void eat() {
  
    }
  
    public abstract boolean swim();
}
  
public class Frog extends Amphibian {
}
Which are true? (Choose all that apply.)
A. Compilation error on line 3
B. Compilation error on line 6
C. Compilation error on line 11
D. Compilation error on line 13
E. Compilation error on line 22

It’s important to remember here that abstract methods can’t have a method body. Also, an abstract method cannot exist in a non-abstract class. Therefore, (A), (B), and (C)  are correct answers.

这里需要记住的是,抽象的方法不能有方法体。另外,一个抽象方法不能存在于一个非抽象类中。因此,(A)、(B)和(C)是正确答案。

Exam Tip 3.3: Missing Implementation for abstract Methods

考试技巧3.3:缺少对抽象方法的实现

Look for non-abstract child classes without the concrete implementation for an abstract method.

寻找没有具体实现抽象方法的非抽象子类。

For example, try to solve the following:

例如,尝试解决以下问题。

public abstract class Animal {
  
    protected abstract boolean canFly();
  
    public abstract void eat();
}
 
public abstract class Amphibian extends Animal {
  
    @Override
    public void eat() {
        System.out.println("Eat...");
    }
  
    public abstract boolean swim();
}
  
public class Frog extends Amphibian {
  
    @Override
    protected boolean swim() {
        return false;
    }
  
}
Which are true? (Choose all that apply)
A. Compilation error on line 8
B. Compilation error on line 11
C. Compilation error on line 18
D. Compilation error on line 21
E. No compilation error

The Frog class doesn’t implement the canFly() method and also reduces the visibility of the swim() method. Therefore, (C) and (D) are correct.

Frog没有实现canFly()方法,也减少了swim()方法的可见性。因此,(C)和(D)是正确的。

Even though Amphibian doesn’t implement canFly(), it’s declared as an abstract class, which is why (A) is incorrect.

尽管Amphibian 没有实现canFly(),它被声明为一个抽象类,这就是为什么(A)不正确。

Exam Tip 3.4: Use of privatefinal, or static With abstract Keyword

考试技巧3.4 使用privatefinal,staticabstract关键字的关系

The abstract keyword cannot be combined with static, private, or final keyword. As a result, any of the following statements are not allowed:

抽象关键字不能与静态私有最终关键字相结合。因此,以下任何语句都是不允许的。

public final abstract class Animal {
}

public abstract class Animal {

    public final abstract void eat();
}

public abstract class Animal {

    private abstract void eat();
}

Any such declaration will result in a compilation error.

任何这样的声明都会导致一个编译错误。

4. Develop Code That Uses the final Keyword

4.开发使用final关键字的代码

The final keyword in Java allows us to declare variables with a constant value. Moreover, it also allows us to declare classes and methods that we can’t extend or override.

Java中的final关键字允许我们声明具有常量值的变量。此外,它还允许我们声明不能扩展或覆盖的类和方法。

Exam Tip 4.1: Overridden final Classes or Methods

考试技巧4.1 覆盖的final 类或方法

Look for methods that are declared as final, and overridden in the child class.

寻找那些被声明为final, 并在子类中被重写的方法。

For example, try to solve the following:

例如,尝试解决以下问题。

public abstract class Animal {
  
    public final void eat() {
        System.out.println("Eat...");
    }
}
  
public class Horse extends Animal {
  
    public void eat() {
        System.out.println("Eat Grass");
    }
  
    public static void main(String[] args) {
        Animal animal = new Horse();
        animal.eat();
    }
}
What is the output?
A. Eat...
B. Eat Grass
C. The code will not compile because of line 3
D. The code will not compile because of line 8
E. The code will not compile because of line 10

Since eat() is declared as final in the Animal class, we cannot override it in the Horse class. Hence, (E) is the correct answer.

由于eat() Animal 类中被声明为final,我们不能在Horse 类中覆盖它。因此,(E)是正确的答案。

Also, look for final variables in an argument of a method. If a new value is assigned to such variables, it will result in a compilation error.

另外,在一个方法的参数中寻找final variables。如果给这样的变量分配一个新值,将导致编译错误。

5. Inner Classes

5.内在类目

Questions on inner classes are usually not as straightforward as other topics. There are many questions in the exam on topics like generics, collections, and concurrency that use the inner class syntax, thereby making it difficult for us to understand the intent of the question.

关于内层类的问题通常不像其他题目那样简单明了。在考试中,有许多关于泛型、集合和并发等主题的问题都使用了内类语法,从而使我们难以理解问题的意图。

Exam Tip 5.1: Incorrect Instantiation of Non-static Inner Classes

考试技巧5.1:非静态内类的不正确实例化

The only way to instantiate a non-static inner class is through an instance of the outer class.

实例化一个非静态内类的唯一方法是通过外类的实例。

For example, try to solve the following:

例如,尝试解决以下问题。

public class Animal {

    class EatingHabbits {
    }

    private EatingHabbits eatingHabbits() {
        return new EatingHabbits();
    }
}

public class Zookeeper {

    public static void main(String[] args) {
        Zookeeper zookeeper = new Zookeeper();
        zookeeper.feed();
    }

    private void feed() {
        EatingHabbits habbits = new EatingHabbits();
        Animal animal = new Animal();
        Animal.EatingHabbits habbits1 = animal.eatingHabbits();
    }
}
What is the result? (Choose all that apply.)
A. Compilation error on line 7
B. Compilation error on line 19
C. Compilation error on line 21
D. No compilation error

Since on line 19, we try to instantiate the inner class without the object of the outer class, (B) is the correct answer.

因为在第19行,我们试图在没有外类对象的情况下实例化内类,所以(B)是正确答案。

Exam Tip 5.2: Incorrect Use of this Keyword in Inner Classes

考试技巧5.2:在内部类中不正确地使用this关键词

Look for incorrect use of this keyword inside inner classes:

寻找内部类中this关键字的不正确使用。

public class Animal {
    private int age = 10;

    public class EatingHabbits {
        private int numOfTimes = 5;

        public void print() {
            System.out.println("The value of numOfTimes " + this.numOfTimes);
            System.out.println("The value of age " + this.age);
            System.out.println("The value of age " + Animal.this.age);
        }
    }

    public static void main(String[] args) {
        Animal.EatingHabbits habbits = new Animal().new EatingHabbits();
        habbits.print();
    }
}

Since this can only be used to access the currently executing object, line 9 would result in a compilation error. For this reason, we must closely observe the use of this inside inner classes.

由于this 只能用于访问当前执行的对象,第9行会导致编译错误。出于这个原因,我们必须密切观察this 在内部类中的使用情况。

Exam Tip 5.3: Non-final Variables Inside Local Inner Classes

考试技巧5.3 本地内类中的非最终变量

Method local classes cannot access a local variable unless it’s declared as final or its value remains unchanged inside the inner class.

方法局部类不能访问局部变量,除非它被声明为final 或者它的值在内部类中保持不变。

For example, try to solve the following:

例如,尝试解决以下问题。

public class Animal {
    private int age = 10;

    public void printAge() {
        String message = "The age is ";
        class PrintUtility {
            void print() {
                System.out.println(message + age);
            }
        }

        PrintUtility utility = new PrintUtility();
        utility.print();
    }

    public static void main(String[] args) {
        new Animal().printAge();
    }
}
What is the result of the following code?
 
A. The age is 0
B. The age is 10
C. Line 8 generates a compiler error
D. Line 12 generates a compiler error
E. An exception is thrown

Since we never updated the message field, it’s effectively final. Hence, (B) is the correct answer.

由于我们从未更新过message字段,它实际上是最终的。因此,(B)是正确的答案。

Exam Tip 5.4: Local Inner Class Cannot Be Marked as private, public, protected, or static

考试技巧5.4 Local内类不能被标记为private, public, protected, static

The same rules apply to local inner classes as to local variables. Therefore, we must look out for any question that violates such constraints.

同样的规则也适用于本地内类和本地变量。因此,我们必须注意任何违反这种约束的问题。

Additionally, any local class declared in a static method has access to only static members of the enclosing class.

此外,任何在static方法中声明的局部类只能访问包围类的static成员。

Exam Tip 5.5: Non-static Member Variables in a static Inner Class

考试技巧5.5:非静态成员变量在静态内类中的应用

static nested classes don’t have access to the instance variables or non-static methods of the outer class.

嵌套类静态不能访问外层类的实例变量或非静态方法。

It’s, therefore, important to look out for questions that involve static nested classes but behave as non-static nested classes:

因此,要注意那些涉及静态嵌套类但表现为非静态嵌套类的问题。

public class Animal {

    private int age = 10;

    static class EatingHabits {

        private int numOfTimes = 5;

        public void print() {
            System.out.println("The value of x " + age);
            System.out.println("The value of x " + Animal.this.age);
            System.out.println("The value of numOfTimes " + numOfTimes);
        }
    }
}

Even though line 10 and 11 were valid for non-static nested classes, it results in a compilation error here.

尽管第10行和第11行对非静态嵌套类是有效的,但它在这里导致了编译错误。

Exam Tip 5.6: Incorrect Declaration for Anonymous Inner Classes

考试技巧5.6:匿名内类的不正确声明

Anonymous classes are scattered across the OCP exam in the same way as the nested classes. There are a lot of questions around collections, threads, and concurrency that use an anonymous inner class, mostly with a confusing syntax.

匿名类与嵌套类一样,散见于OCP考试中。有很多围绕集合、线程和并发的问题都使用了匿名内类,大多是语法混乱的问题。

For example, try to solve the following:

例如,尝试解决以下问题。

public class Animal {

    public void feed() {
        System.out.println("Eating Grass");
    }
}

public class Zookeeper {

    public static void main(String[] args) {
        Animal animal = new Animal(){
            public void feed(){
                System.out.println("Eating Fish");
            }
        }
        animal.feed();
    }
}
What is the result?
 
A. An exception occurs at runtime
B. Eating Fish
C. Eating Grass
D. Compilation fails because of an error on line 11
E. Compilation fails because of an error on line 12
F. Compilation fails because of an error on line 15

Since the anonymous class of Animal is not closed with a semicolon, there is a compilation error on line 15, which is why (F) is the correct answer.

由于匿名类的Animal 没有用分号封闭,在第15行有一个编译错误,这就是为什么(F)是正确答案。

Exam Tip 5.7: Instantiating an Interface

考试技巧5.7:实例化一个接口

Look out for questions attempting to instantiate an interface rather than implementing it:

注意那些试图实例化一个接口而不是实现它的问题:

Runnable r = new Runnable(); // compilation error

Runnable r = new Runnable() { // legal statement
    @Override
    public void run() {
    
    }
};

6. Enums

6.枚举

Enums are a way to represent an enumerated list of constants in Java. They behave like regular Java classes and can, therefore, contain variables, methods, and constructors.

枚举是一种表示Java中常量枚举列表的方式。它们的行为与普通的Java类相似,因此可以包含变量、方法和构造函数

Albeit similar, enums do have a rather complex syntax than regular classes. The OCP exams focus on such syntax uncertainties with questions containing enums.

尽管相似,但枚举确实比普通类有相当复杂的语法。OCP考试通过包含枚举的问题来关注这种语法上的不确定性。

Exam Tip 6.1: Syntax Errors in enum Declaration

考试技巧6.1 enum声明中的语法错误

Look out for enum declarations with incorrect syntax errors.

注意有不正确语法错误的enum声明。

For example, try to solve the following:

例如,尝试解决以下问题。

public enum AnimalSpecies {
    MAMMAL(false), FISH(true), BIRD(false),
    REPTILE(false), AMPHIBIAN(true)

    boolean hasFins;

    public AnimalSpecies(boolean hasFins) {
        this.hasFins = hasFins;
    }

    public boolean hasFins() {
        return hasFins;
    }
}
What is the result of the following code? (Choose all that apply.)
 
A. Compiler error on line 2
B. Compiler error on line 3
C. Compiler error on line 7
D. Compiler error on line 11
E. The code compiles successfully

There are two problems with this question:

这个问题有两个问题。

  • On line 3, there is a missing semicolon (;). Remember that if an enum contains variables or methods, a semicolon is mandatory
  • There is a public constructor in this enum

Therefore, (B) and (C) are correct answers.

因此,(B)和(C)是正确答案。

Exam Tip 6.2: enum with abstract Methods

考试技巧6.2:enumabstractMethods

Look out for enum questions that implement an interface or contain an abstract method.

注意那些实现了一个接口或包含一个抽象方法的enum问题。

For example, try to solve the following:

例如,尝试解决以下问题。

public enum AnimalSpecies {
    MAMMAL(false), FISH(true){
        @Override
        boolean canFly() {
            return false;
        }
    }, BIRD(false),
    REPTILE(false), AMPHIBIAN(true);

    boolean hasFins;

    AnimalSpecies(boolean hasFins) {
        this.hasFins = hasFins;
    }

    public boolean hasFins() {
        return hasFins;
    }

    abstract boolean canFly();
}

public class Zookeeper {

    public static void main(String[] args) {
        AnimalSpecies.MAMMAL.canFly();
    }
}
What is the result of the following code? (Choose all that apply.)
  
A. Compilation error on line 2
B. Compilation error on line 4
C. Compilation error on line 20
D. Compilation error on line 26
E. No compilation error

Since there is an abstract method, we must provide its implementation for every enum constant. And because the above code only implements it for FISH, we’ll get a compilation error. Hence, (A) is the correct answer.

因为有一个抽象方法,我们必须为每个enum常量提供它的实现。而由于上面的代码只为FISH实现了它,我们会得到一个编译错误。因此,(A)是正确的答案。

Likewise, if the enum implements an interface, every constant must provide implementations for all methods of that interface.

同样,如果enum 实现了一个接口每个常量必须为该接口的所有方法提供实现

Exam Tip 6.3: Iterating Over enum Values

考试技巧6.3:在enum值上进行迭代

Java provides static methods for iterating over the enum values. We must expect questions that ask us to calculate the output of one such iteration.

Java为迭代enum值提供了静态方法。我们必须期待那些要求我们计算一个这样的迭代的输出的问题。

For example, try to solve the following:

例如,尝试解决以下问题。

public enum AnimalSpecies {
    MAMMAL, FISH, BIRD, REPTILE, AMPHIBIAN
}

public class Zookeeper {

    public static void main(String[] args) {
        AnimalSpecies[] animals = AnimalSpecies.values();
        System.out.println(animals[2]);
    }
}
What is the result? (Choose all that apply.)
 
A. FISH
B. BIRD
C. Compilation fails due to an error on line 2
D. Compilation fails due to an error on line 8
E. Compilation fails due to an error on line 10

The output is BIRD, therefore, (B) is correct.

输出是BIRD,因此,(B)是正确的。

7. Interfaces and @Override in Java

7.Java中的接口和@Override

In Java, interfaces are abstract types that define a contract for a class. OCP exam has various questions that test a candidate on inheritance, method overriding, and multiple inheritance problems.

在Java中,interfaces是为一个类定义契约的抽象类型。OCP考试有各种问题来测试考生的继承、方法覆盖和多重继承问题。

Exam Tip 7.1: abstract Method Implementation in Non-abstract Classes

考试技巧7.1 抽象的方法在非抽象的类中的实现

Look out for concrete implementations that don’t implement all abstract methods of an interface.

注意那些没有实现接口的所有抽象方法的具体实现。

For example, try to solve the following:

例如,尝试解决以下问题。

class Bird implements Flyable {
    public void fly() {
    }
}
  
abstract class Catbirds extends Bird {
  
}
  
abstract class Flamingos extends Bird {
    public abstract String color();
}
  
class GreaterFlamingo extends Flamingos {
    public String color() {
        System.out.println("The color is pink");
    }    
}
  
interface Flyable {
    void fly();
}
What is the result? (Choose all that apply.)
 
A. Compilation succeeds
B. Compilation fails with an error on line 6
C. Compilation fails with an error on line 10
D. Compilation fails with an error on line 11
E. Compilation fails with an error on line 14

Since all these are valid statements, (A) is the correct answer.

由于所有这些都是有效的陈述,(A)是正确的答案。

With the level of inheritance, such questions can be tricky at times. Therefore, we must look out for any compilation errors before trying to calculate the output by following a trace of overridden methods.

随着继承水平的提高,这种问题有时会很棘手。因此,在试图通过跟踪重载方法来计算输出之前,我们必须注意任何编译错误。

Another such compilation error arises from the use of implements and extends:

另一个这样的编译错误来自于对implements extends的使用:

interface Bird extends Flyable, Wings {}
 
public class GreaterFlamingo extends Flamingos implements Bird, Vegetarian {}
 
public class GreaterFlamingo extends Flamingos, Bird {}

Here, line 1 & 3 are valid statements while 5 is not allowed in Java. The GreaterFlamingo class on line 3 must now provide concrete implementations of all abstract methods.

这里,第1行和第3行是有效的语句,而第5行在Java中是不允许的。第3行的GreaterFlamingo类现在必须提供所有抽象方法的具体实现。

Exam Tip 7.2: default Methods With Identical Method Signatures

考试技巧7.2 默认具有相同方法签名的方法

Starting with JDK 8, interfaces can now have static and default methods. This might lead to a situation where multiple interfaces contain a default method with the same signature. We’ll find questions in the exam with such interfaces.

从 JDK 8 开始,接口现在可以有 staticdefault methods。这可能会导致一种情况,即多个接口包含一个具有相同签名的default方法。我们会在考试中发现有这种接口的问题。

For example, try to solve the following:

例如,尝试解决以下问题。

public interface Vegetarian {

    default void eat() {
        System.out.println("Eat Veg");
    }
}

public interface NonVegetarian {

    default void eat() {
        System.out.println("Eat NonVeg");
    }
}

public class Racoon implements Vegetarian, NonVegetarian {

    @Override
    void eat() {
        System.out.println("Eat Something")
    }

    public static void main(String[] args) {
        Racoon racoon = new Racoon();
        racoon.eat();
    }
}
What is the result?
 
A. Eat Veg
B. Eat NonVeg
C. Eat Something
D. The output is unpredictable
E. Compilation fails
F. An exception is thrown at runtime

This question is related to multiple inheritance. Notably, the rule says that we must provide the implementation of default methods if it’s overridden from multiple interfaces.

这个问题与多重继承有关。值得注意的是,该规则说我们必须提供默认方法的实现,如果它被多个接口重载的话

Now, since this code does provide an implementation of eat() method, it may seem a valid code at first. However, if we look closely, we’ll see that the overridden eat() method is not public. Therefore, the correct answer is (E).

现在,由于这段代码确实提供了eat()方法的实现,所以一开始它可能看起来是一段有效的代码。然而,如果我们仔细观察,我们会发现被重载的eat()方法不是public。</因此,正确的答案是(E)。

Exam Tip 7.3: The Use of @Override 

考试技巧7.3:@Override的使用

@Override is used to denote an overridden method in Java. Although optional, it improves readability and helps the compiler in reporting incorrect syntaxes. Look for misuse of this annotation in the exam.

@Override用于表示Java中的覆盖方法。虽然是可选的,但它提高了可读性,有助于编译器报告错误的语法。在考试中要注意这个注释的误用。

For example, try to solve the following:

例如,尝试解决以下问题。

public abstract class Flamingo {

    public abstract String color();

    public abstract void fly();
}

public class GreaterFlamingo extends Flamingo {
    @Override
    public String color() {
        return "Pink";
    }

    @Override
    public void fly() {
        System.out.println("Flying");
    }

    @Override
    public void eat() {
        System.out.println("Eating");
    }
    
    public static void main(String[] args) {
        GreaterFlamingo flamingo = new GreaterFlamingo();
        System.out.println(flamingo.color());
    }
}
What is the result? (Choose all that apply.)
 
A. Pink
B. Compilation error on line 8
C. Compilation error on line 19
D. Compilation error on line 20

Please note that we used the @Override on the eat() method. However, since there is no such abstract method in the Flamingo class, this is not an overridden method. Hence, (C) is the correct answer.

请注意,我们在eat()方法上使用了@Override。然而,由于在Flamingo类中没有这样的抽象方法,这不是一个被重载的方法。因此,(C)是正确的答案。

8. Create and Use Lambda Expressions

8.创建和使用Lambda表达式

The last exam objective in advanced Java class design is about lambdas. It must be remembered that lambda expressions can be used as a substitute for anonymous inner classes implementing a functional interface. As a result, we’ll see a lot of questions in the exam using both of them alternatively.

高级Java类设计的最后一个考试目标是关于lambdas。必须记住,lambda表达式可以用来替代实现functional interface的匿名内类。因此,我们在考试中会看到很多问题都是交替使用这两者。

The syntax for lambda expression is a bit tricky. To spot syntax errors in the exam, it’s important to understand some rules around lambdas.

lambda表达式的语法有点棘手。为了在考试中发现语法错误,了解一些围绕lambdas的规则很重要。

Exam Tip 8.1: Non-final Variables Inside Lambda Declarations

考试技巧8.1 Lambda声明中的非最终变量

Similar to method local classes, we can only use final or effectively final variables inside a lambda function. Exam questions may not honor such constraints.

与方法局部类类似,我们只能在lambda函数内使用final 或有效final 变量。考题可能不尊重这种限制。

For example, try to solve the following:

例如,尝试解决以下问题。

List<String> birds = Arrays.asList("eagle", "seagull", "albatross", "buzzard", "goose");
int longest = 0;
birds.forEach(b -> {
    if (b.length() > longest){
        longest = b.length();
    }
});
 
System.out.println("Longest bird name is length: " + longest);
What is the result?

A. "Longest bird name is length: 9"
B. Compilation fails because of an error on line 3
C. Compilation fails because of an error on line 5
D. A runtime exception occurs on line 5

This will result in a compilation error because we tried to assign a value to a variable inside the lambda expression. Hence, (C) is the correct answer.

这将导致编译错误,因为我们试图在lambda表达式内给变量赋值。因此,(C)是正确答案。

9. Conclusion

9.结语

Generally speaking, it’s important to read and understand the syntax of questions in the exam. Most coding questions try and confuse the candidates with compilation errors. It’s therefore important to rule out any such errors before calculating the output.

一般来说,阅读和理解考试中问题的语法是很重要的。大多数编码问题试图用编译错误来迷惑考生。因此,在计算输出之前,排除任何此类错误是很重要的。

In this article, we discussed a few tips that appear frequently in the exam along with some sample questions. These are just sample questions to demonstrate what we can expect in the exam.

在这篇文章中,我们讨论了一些在考试中经常出现的提示,以及一些样本问题。这些只是样本问题,以证明我们在考试中可以期待什么。

And of course, the best way to crack the exam is by practicing such mock questions beforehand!

当然,破解考试的最好方法是事先练习这种模拟题!”。