A.1 Chapter 1: Java basics

Chapter 1 includes four Twist in the Tale exercises.

A.1.1 Twist in the Tale 1.1

Purpose: This exercise encourages you to practice code with a combination of the correct contents (classes and interfaces) of a Java source code file.

Answer: c, d

Explanation: Options (a) and (b) are incorrect.

Option (c) is correct because a Java source code file can define multiple interfaces and classes.

Option (d) is correct because a public interface or class can be defined in a Java source code file with a matching name. The public interface Printable can’t be defined in the Java source code file, Multiple.java. It must be defined in Printable.java.

A.1.2 Twist in the Tale 1.2

Purpose: Though similar to Twist in the Tale 1.1, this question is different in terms of its wording and intent. It asks you to select the options that are correct individually. Selecting an option that’s correct individually means that an option should be correct on its own and not in combination with any other option. You may get to answer similar questions in the real exam.

Answer: a, c, d

Explanation: Option (a) is correct and (b) is incorrect because Multiple2.java won’t compile. Multiple2.java can’t define a public class Car.

Option (c) is correct because removal of the definition of the public class Car from Multiple2.java will leave only one public interface in Multiple2.java—Multiple2. Because the names of the public interface Multiple2 and the source code file match, Multiple2.java will compile successfully.

Option (d) is correct. Changing the public class Car to a non-public class will leave only one public interface in Multiple2.java—Multiple2. Because the names of the public interface Multiple2 and source code file match, Multiple2.java will compile successfully.

Option (e) is incorrect. If you change the access modifier of the public interface Multiple2 to non-public, Multiple2.java will contain a definition of a public class Car, which isn’t allowed.

A.1.3 Twist in the Tale 1.3

Purpose: This exercise encourages you to execute the code in the options to understand the correct method signature of the method main together with the method parameters that are passed to it.

Answer: a, b

Explanation: All the options in this question are supposed to execute using the command javaEJava java one one. The purpose of each of these terms is as follows:

  • Term 1, java—Used to execute a Java class
  • Term 2, EJava—Name of class to execute
  • Term 3, java—Passed as the first argument to the method main
  • Term 4, one—Passed as the second argument to main
  • Term 5, one—Passed as the third argument to main

To output java one, the main method should output the first and either the second or third method parameters passed to it.

Options (a) and (b) are correct because they use the correct method signature of the method main. The name of the method parameter need not be args. It can be any other valid identifier. Option (a) outputs the values of the first and third terms passed to it. Option (b) outputs the values of the first and second terms passed to it.

Option (c) is incorrect because this main method accepts a two-dimensional array. Hence, it won’t be treated as the main method.

Option (d) is incorrect because this code won’t compile. The access modifier of a method (public) should be placed before its return type (void); otherwise, the code won’t compile.

A.1.4 Twist in the Tale 1.4

Purpose: Apart from determining the right access modifier that can limit the visibility of a class within a package, this exercise wants you to try out different access modifiers that can be used to declare a class.

Answer: The code submitted by Harry.

Explanation: The code submitted by Paul is incorrect because when the class Curtain is defined with the public access modifier, it will be accessible outside the package building.

The code submitted by Shreya and Selvan is incorrect because the class Curtain is a top-level class (it’s not defined within another class), so it can’t be defined using the access modifiers protected and private.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.218