1.
Given the following,
1. interface Base {2. boolean m1 ();3. byte m2(short s);4. }
Which code fragments will compile? (Choose all that apply.)
Correct Answer(s)
C. Abstract class Class2 implements Base { }
D. #
abstract class Class2. implements Base {
public boolean m1() { return (true); } }
Explanation
The code fragment "abstract class Class2 implements Base { }" will compile because it correctly implements the interface Base. The code fragment "abstract class Class2. implements Base { public boolean m1() { return (true); } }" will also compile because it correctly implements the interface Base and provides an implementation for the m1() method.
2.
Which declare a compilable abstract class? (Choose all that apply.)
Correct Answer
B. Public abstract class Canine { public Bark speak() { } }
Explanation
The correct answer is "public abstract class Canine { public Bark speak() { } }". This is the correct answer because it declares an abstract class "Canine" with a method "speak()" that returns a "Bark" object. The class is marked as abstract and the method is implemented with an empty body, indicating that it must be overridden by any concrete subclass.
3.
Which is true? (Choose all that apply. )
Correct Answer
C. "X extends Y" is correct if X and Y are either both classes or both interfaces.
Explanation
The statement "X extends Y" is correct if X and Y are either both classes or both interfaces. This means that if X is a class, then Y must also be a class. Similarly, if X is an interface, then Y must also be an interface. If X and Y are of different types (one class and one interface), the statement is not correct. However, if they are both of the same type, the statement holds true.
4.
Which are valid declarations? (Choose all that apply.)
Correct Answer(s)
A. Int $x;
C. Int _123;
E. Int central_sales_region_Summer_2005_gross_sales;
Explanation
The valid declarations in this question are int $x;, int _123;, and int central_sales_region_Summer_2005_gross_sales;. The first declaration, int $x;, is valid because it starts with a valid character, the dollar sign. The second declaration, int _123;, is valid because it starts with a valid character, the underscore. The last declaration, int central_sales_region_Summer_2005_gross_sales;, is valid because it starts with a valid character, a letter. The other declarations, int 123; and int #dim;, are not valid because they start with a number and a pound sign, respectively, which are not allowed in variable names.
5.
Which method names follow the JavaBeans standard? (Choose all that apply.)
Correct Answer(s)
B. GetCust
D. IsColorado
Explanation
The JavaBeans standard states that getter and setter methods should follow a specific naming convention. The methods should start with "get" or "set" followed by the name of the property with the first letter capitalized. In this case, the method names "getCust" and "isColorado" follow this convention and are therefore correct according to the JavaBeans standard.
6.
Given:1. class Voop {2. public static void main(String [] args) {3. doStuff(1);4. doStuff(1, 2);5. }6. // insert code here7. }
Which, inserted independently at line 6, will compile? (Choose all that apply.)
Correct Answer(s)
A. Static void doStuff(int... doArgs) { }
E. Static void doStuff(int x, int... doArgs) { }
Explanation
The correct answer is static void doStuff(int... doArgs) { }, static void doStuff(int x, int... doArgs) { }. These two options will compile because they both use the varargs syntax (int... doArgs) which allows for a variable number of arguments to be passed to the method. The first option does not have any additional parameters, while the second option has an additional parameter (int x) before the varargs parameter.
7.
Which statement(s) are true? (Choose all that apply.)
Correct Answer
B. Has-a relationships always rely on instance variables.
Explanation
Has-a relationships in object-oriented programming refer to the relationship between classes where one class has a reference to another class as an instance variable. This allows the class to access the methods and properties of the other class. Therefore, the statement "Has-a relationships always rely on instance variables" is true. It is important to note that has-a relationships do not necessarily rely on inheritance, require at least two class types, rely on polymorphism, or are always tightly coupled.
8.
Given:
class Clidders { public final void flipper() { System.out.println("Clidder"); }}public class Clidlets extends Clidders { public void flipper() { System.out.println("Flip a Clidlet"); super.flipper(); } public static void main(String [] args) { new Clidlets().flipper(); }}
What is the result?
Correct Answer
E. Compilation fails.
Explanation
The code fails to compile because the final keyword is used in the method declaration of the flipper() method in the Clidders class. This means that the method cannot be overridden in any subclass. However, in the Clidlets class, the flipper() method is attempting to override the flipper() method from the superclass, which is not allowed due to the final keyword. Therefore, the code fails to compile.
9.
Given:
public abstract interface Frobnicate { public void twiddle(String s) ; }
Which is a correct class? (Choose all that apply.)
Correct Answer
B. Public abstract class Frob implements Frobnicate { }
Explanation
The correct answer is "public abstract class Frob implements Frobnicate { }". This class correctly implements the Frobnicate interface by using the "implements" keyword and providing an empty implementation for the twiddle() method. Additionally, the class is declared as abstract, which is allowed since the interface is also declared as abstract.
10.
Given:
class Top { public Top(String s) { System.out.print("B"); }}public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); }}What is the result?
Correct Answer
E. Compilation fails.
Explanation
The code fails to compile because the constructor of the class Bottom2 does not explicitly call the constructor of its superclass, Top. Since the superclass, Top, does not have a default constructor, the code fails to compile.
11.
Select the two statements that best indicate a situation with low coupling. (Choose two.)
Correct Answer(s)
D. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods.
E. It is unlikely that changes made to one class will require any changes in another.
Explanation
A situation with low coupling is indicated by the reference variable being declared for an interface type, not a class, and the interface providing a small number of methods. This suggests that the class is not tightly coupled to specific implementations and can easily work with different objects that adhere to the same interface. Additionally, if changes made to one class do not require any changes in another class, it implies that the classes are loosely coupled and can be modified independently without affecting each other.
12.
Given:
class Clidder { private final void flipper() { System.out.println ("Clidder"); }}public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); }}
What is the result?
Correct Answer
A. Clidlet
Explanation
The result is "Clidlet" because the method flipper() in the Clidlet class overrides the method in the parent class Clidder. When the new Clidlet object is created and the flipper() method is called, it executes the override method in Clidlet, which prints "Clidlet" to the console.