chapter 3 sample exam questions Flashcards
OCA Java SE 8 Programmer I Certification Guide methods and encapsulation
Which option defines a well-encapsulated class?
a class Template { public String font; } b class Template2 { public String font; public void setFont(String font) { this.font = font; } public String getFont() { return font; } } c class Template3 { private String font; public String author; public void setFont(String font) { this.font = font; } public String getFont() { return font; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return author; } } d None of the above
Answer: d
Explanation: Options (a), (b), and (c) are incorrect because they all define a public instance variable. A well-encapsulated class should be like a capsule, hiding its instance variables from the outside world. The only way you should access and modify instance variables is through the public methods of a class to ensure that the outside world can access only the variables the class allows it to. By defining methods to assign val-ues to its instance variables, a class can control the range of values that can be assigned to them.
Examine the following code and select the correct option(s): public class Person { public int height; public void setHeight(int newHeight) { if (newHeight <= 300) height = newHeight; } } a The height of a Person can never be set to more than 300. b The preceding code is an example of a well-encapsulated class. c The class would be better encapsulated if the height validation weren’t set to 300. d Even though the class isn’t well encapsulated, it can be inherited by other classes.
Answer: d
Explanation: This class isn’t well encapsulated because its instance variable height is defined as a public member. Because the instance variable can be directly accessed by other classes, the variable doesn’t always use the method setHeight to set its height. The class Person can’t control the values that can be assigned to its public variable height.
Which of the following methods correctly accepts three integers as method
arguments and returns their sum as a floating-point number?
a public void addNumbers(byte arg1, int arg2, int arg3) {
double sum = arg1 + arg2 + arg3;
}
b public double subtractNumbers(byte arg1, int arg2, int arg3) {
double sum = arg1 + arg2 + arg3;
return sum;
}
c public double numbers(long arg1, byte arg2, double arg3) {
return arg1 + arg2 + arg3;
}
d public float wakaWakaAfrica(long a1, long a2, short a977) {
double sum = a1 + a2 + a977;
return (float)sum;
}
Answer: b, d
Explanation: Option (a) is incorrect. The question specifies the method should return a
decimal number (type double or float), but this method doesn’t return any value.
Option (b) is correct. This method accepts three integer values that can be auto-matically converted to an integer: byte, int, and int. It computes the sum of these
integer values and returns it as a decimal number (data type double). Note that the
name of the method is subtractNumbers, which doesn’t make it an invalid option.
Practically, you wouldn’t name a method subtractNumbers if it’s adding them. But
syntactically and technically, this option meets the question’s requirements and is a
correct option.
Option (c) is incorrect. This method doesn’t accept integers as the method argu-ments. The type of the method argument arg3 is double, which isn’t an integer.
Option (d) is correct. Even though the name of the method seems weird, it
accepts the correct argument list (all integers) and returns the result in the correct
data type (float).
Which of the following statements are true?
a If the return type of a method is int, the method can return a value of type
byte.
b A method may or may not return a value.
c If the return type of a method is void, it can define a return statement without
a value, as follows:
return;
d A method may or may not accept any method arguments.
e A method must accept at least one method argument or define its return type.
f A method whose return type is String can’t return null.
Answer: a, b, c, d
Explanation: Option (e) is incorrect. There’s no constraint on the number of argu-ments that can be passed to a method, regardless of whether the method returns a
value.
Option (f) is incorrect. You can’t return the value null for methods that return
primitive data types. You can return null for methods that return objects (String is a
class and not a primitive data type).
Given the following definition of class Person, class Person { public String name; public int height; } what is the output of the following code? class EJavaGuruPassObjects1 { public static void main(String args[]) { Person p = new Person(); p.name = "EJava"; anotherMethod(p); System.out.println(p.name); someMethod(p); System.out.println(p.name); } static void someMethod(Person p) { p.name = "someMethod"; System.out.println(p.name); } static void anotherMethod(Person p) { p = new Person(); p.name = "anotherMethod"; System.out.println(p.name); } } a anotherMethod anotherMethod someMethod someMethod b anotherMethod EJava someMethod someMethod c anotherMethod EJava someMethod EJava d Compilation error
Answer: b
Explanation: The class EJavaGuruPassObject1 defines two methods, someMethod and
anotherMethod. The method someMethod modifies the value of the object parameter
passed to it. Hence, the changes are visible within this method and in the calling
method (method main). But the method anotherMethod reassigns the reference vari-able passed to it. Changes to any of the values of this object are limited to this method.
They aren’t reflected in the calling method (the main method).
What is the output of the following code? class EJavaGuruPassPrim { public static void main(String args[]) { int ejg = 10; anotherMethod(ejg); System.out.println(ejg); someMethod(ejg); System.out.println(ejg); } static void someMethod(int val) { \++val; System.out.println(val); } static void anotherMethod(int val) { val = 20; System.out.println(val); } } a 20 10 11 11 b 20 20 11 10 c 20 10 11 10 d Compilation error
Answer: c
Explanation: When primitive data types are passed to a method, the values of the vari-ables in the calling method remain the same. This behavior doesn’t depend on
whether the primitive values are reassigned other values or modified by addition, sub-traction, or multiplication—or any other operation.
Given the following signature of method eJava, choose the options that cor-rectly overload this method:
public String eJava(int age, String name, double duration)
a private String eJava(int val, String firstName, double dur)
b public void eJava(int val1, String val2, double val3)
c String eJava(String name, int age, double duration)
d float eJava(double name, String age, byte duration)
e ArrayList eJava()
f char[] eJava(double numbers)
g String eJava()
Answer: c, d, e, f, g
Explanation: Option (a) is incorrect. Overloaded methods can change the access modi-fiers, but changing the access modifier alone won’t make it an overloaded method. This option also changes the names of the method parameters, but that doesn’t make any
difference to a method signature.
Option (b) is incorrect. Overloaded methods can change the return type of the
method, but changing the return type won’t make it an overloaded method.
Option (c) is correct. Changing the placement of the types of the method parame-ters overloads it.
Option (d) is correct. Changing the return type of a method and the placement of
the types of the method parameters overloads it.
Option (e) is correct. Changing the return type of a method and making a change
in the parameter list overloads it.
Option (f) is correct. Changing the return type of a method and making a change
in the parameter list overloads it.
Option (g) is correct. Changing the parameter list also overloads a method.
Given the following code, class Course { void enroll(long duration) { System.out.println("long"); } void enroll(int duration) { System.out.println("int"); } void enroll(String s) { System.out.println("String"); } void enroll(Object o) { System.out.println("Object"); } } what is the output of the following code? class EJavaGuru { public static void main(String args[]) { Course course = new Course(); char c = 10; course.enroll(c); course.enroll("Object"); } } a Compilation error b Runtime exception c int String d long Object
Answer: c Explanation: No compilation issues exist with the code. You can overload methods by changing the type of the method arguments in the list. Using method arguments with data types having a base-derived class relationship (Object and String classes) is acceptable. Using method arguments with data types for which one can be automati-cally converted to the other (int and long) is also acceptable. When the code executes course.enroll(c), char can be passed to two overloaded enroll methods that accept int and long. The char gets expanded to its nearest type—int—so course.enroll(c) calls the overloaded method that accepts int, print-ing int. The code course.enroll("Object") is passed a String value. Although String is also an Object, this method calls the specific (not general) type of the argu-ment passed to it. So course.enroll("Object") calls the overloaded method that accepts String, printing String.
Examine the following code and select the correct options: class EJava { public EJava() { this(7); System.out.println("public"); } private EJava(int val) { this("Sunday"); System.out.println("private"); } protected EJava(String val) { System.out.println("protected"); } } class TestEJava { public static void main(String[] args) { EJava eJava = new EJava(); } } a The class EJava defines three overloaded constructors. b The class EJava defines two overloaded constructors. The private constructor isn’t counted as an overloaded constructor. c Constructors with different access modifiers can’t call each other. d The code prints the following: protected private public e The code prints the following: public private protected
Answer: a, d Explanation: You can define overloaded constructors with different access modifiers in the same way that you define overloaded methods with different access modifiers. But a change in only the access modifier can’t be used to define overloaded methods or constructors. private methods and constructors are also counted as overloaded methods. The following line of code calls EJava’s constructor, which doesn’t accept any method argument: EJava eJava = new EJava(); The no-argument constructor of this class calls the constructor that accepts an int argument, which in turn calls the constructor with the String argument. Because the constructor with the String constructor doesn’t call any other methods, it prints protected and returns control to the constructor that accepts an int argument. This constructor prints private and returns control to the constructor that doesn’t accept any method argument. This constructor prints public and returns control to the main method.
Select the incorrect options: a If a user defines a private constructor for a public class, Java creates a public default constructor for the class. b A class that gets a default constructor doesn’t have overloaded constructors. c A user can overload the default constructor of a class. d The following class is eligible for a default constructor: class EJava {} The following class is also eligible for a default constructor: class EJava { void EJava() {}
Answer: a, c Explanation: Option (a) is incorrect. If a user defines a constructor for a class with any access modifier, it’s no longer an eligible candidate to be provided with a default constructor. Option (b) is correct. A class gets a default constructor only when it doesn’t have any constructor. A default or an automatic constructor can’t exist with other constructors. Option (c) is incorrect. A default constructor can’t coexist with other constructors. A default constructor is automatically created by the Java compiler if the user doesn’t define any constructor in a class. If the user reopens the source code file and adds a constructor to the class, upon recompilation no default constructor will be created for the class. Option (d) is correct. Because this class doesn’t have a constructor, Java will create a default constructor for it. Option (e) is also correct. This class also doesn’t have a constructor, so it’s eligible for the creation of a default constructor. The following isn’t a constructor because the return type of a constructor isn’t void: void EJava() {} It’s a regular and valid method, with the same name as it's class.