WGU C212 MARKETING STUDY EXAMS GUIDE
QUESTIONS AND ANSWERS SURE A+
✔✔Why does the call System.out.println(System.out); produce a result such as
java.io.PrintStream@7a84e4? - ✔✔Because the implementor of the PrintStream class
did not supply a toString method.
✔✔Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Hello";
System.out.println(obj.length()); - ✔✔The second line will not compile. The class Object
does not have a method length.
✔✔Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Who was the inventor of Java?";
Question q = (Question) obj;
q.display(); - ✔✔The code will compile, but the second line will throw a class cast
exception because Question is not a superclass of String.
✔✔Assuming that x is an object reference, what is the value of x instanceof Object?
(Answer). - ✔✔The value is false if x is null and true otherwise
, ✔✔When (and why) do you use the reserved word super? - ✔✔You can also use the
reserved word super to invoke a constructor of the superclass (as part of the constructor
chaining rules).
Constructor chaining: unless specified otherwise, the subclass constructor calls the
superclass constructor with no arguments. To
call a superclass constructor, use the super reserved word in the first statement of the
subclass constructor. The constructor of
a subclass can pass arguments to a superclass constructor, using the reserved word
super.
✔✔Assuming SavingsAccount is a subclass of BankAccount, which of the following
code fragments are valid in Java?
(a) BankAccount account = new SavingsAccount();
(b) SavingsAccount account2 = new BankAccount();
(c) BankAccount account = null;
(d) SavingsAccount account2 = account; - ✔✔(a) BankAccount account = new
SavingsAccount();
✔✔What is the purpose of the method toString in class java.lang.Object? - ✔✔Override
the toString method (and suppply your own version) to yield a string that describes the
object's state.
✔✔What is the purpose of the method equals in class java.lang.Object? - ✔✔The
equals method checks whether two objects have the same contents.
✔✔How do you check that two objects belong to the same class (even if you don't know
the name of the class)? - ✔✔Use getClass().
✔✔What is a simple rule of thumb for finding classes? - ✔✔Look for nouns in the
problem description.
✔✔What is a criterion of cohesion for a class's public interface? - ✔✔The public
interface of a class is cohesive if all of its features are related to the concept
that the class represents.
✔✔What is an immutable class? - ✔✔An immutable class has no mutator methods.
✔✔Why is it a good idea to minimize dependencies between classes? - ✔✔If a class
does not depend on another it is not affected by interface changes in the other class.
✔✔Is the substring method of the String class an accessor or a mutator? - ✔✔It's an
accessor -- calling substring doesn't modify the string on which the method is invoked.
In fact all
instance methods of the String class are accessors.
QUESTIONS AND ANSWERS SURE A+
✔✔Why does the call System.out.println(System.out); produce a result such as
java.io.PrintStream@7a84e4? - ✔✔Because the implementor of the PrintStream class
did not supply a toString method.
✔✔Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Hello";
System.out.println(obj.length()); - ✔✔The second line will not compile. The class Object
does not have a method length.
✔✔Will the following code fragment compile? Will it run? If not, what error is reported?
Object obj = "Who was the inventor of Java?";
Question q = (Question) obj;
q.display(); - ✔✔The code will compile, but the second line will throw a class cast
exception because Question is not a superclass of String.
✔✔Assuming that x is an object reference, what is the value of x instanceof Object?
(Answer). - ✔✔The value is false if x is null and true otherwise
, ✔✔When (and why) do you use the reserved word super? - ✔✔You can also use the
reserved word super to invoke a constructor of the superclass (as part of the constructor
chaining rules).
Constructor chaining: unless specified otherwise, the subclass constructor calls the
superclass constructor with no arguments. To
call a superclass constructor, use the super reserved word in the first statement of the
subclass constructor. The constructor of
a subclass can pass arguments to a superclass constructor, using the reserved word
super.
✔✔Assuming SavingsAccount is a subclass of BankAccount, which of the following
code fragments are valid in Java?
(a) BankAccount account = new SavingsAccount();
(b) SavingsAccount account2 = new BankAccount();
(c) BankAccount account = null;
(d) SavingsAccount account2 = account; - ✔✔(a) BankAccount account = new
SavingsAccount();
✔✔What is the purpose of the method toString in class java.lang.Object? - ✔✔Override
the toString method (and suppply your own version) to yield a string that describes the
object's state.
✔✔What is the purpose of the method equals in class java.lang.Object? - ✔✔The
equals method checks whether two objects have the same contents.
✔✔How do you check that two objects belong to the same class (even if you don't know
the name of the class)? - ✔✔Use getClass().
✔✔What is a simple rule of thumb for finding classes? - ✔✔Look for nouns in the
problem description.
✔✔What is a criterion of cohesion for a class's public interface? - ✔✔The public
interface of a class is cohesive if all of its features are related to the concept
that the class represents.
✔✔What is an immutable class? - ✔✔An immutable class has no mutator methods.
✔✔Why is it a good idea to minimize dependencies between classes? - ✔✔If a class
does not depend on another it is not affected by interface changes in the other class.
✔✔Is the substring method of the String class an accessor or a mutator? - ✔✔It's an
accessor -- calling substring doesn't modify the string on which the method is invoked.
In fact all
instance methods of the String class are accessors.