Shared Flashcard Set

Details

SCJP 6 Chapter 2
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 2 - Object Orientation
41
Software
Professional
02/13/2011

Additional Software Flashcards

 


 

Cards

Term
T/F: The constructor of a class must not have a return type.
Definition
T
(Ref: Question 7 in Mock Exam 1 for SCJP 6 at http://www.javaprepare.com/quests/test.html)
Term
If you inherit a method and decide to overload it what may you optionally change?
Definition
The return type. (p. 126)
Term
If you inherit a method and decide to overload it what must you change?
Definition
The parameter list. (p. 126-127)
Term
What's the difference between overloading and overriding?
Definition
An overloaded method is a completely different method (just reuse of the method name). It must have a different parameter

list than the method it is overloading and it may or may not have a different return type.

Overriding's goal is to change the method implementation and overriding methods must have the same definition; that is, the

parameter list must be exactly the same and the return type must be of the same type or a covariant.

(p. 127 & 128)
Term
Is this legal?

public class Foo {
void go() { }
}
public class Bar extends Foo {
String go() { return null; }
}
Definition
No. You can't change only the return type. To fix this, the 2nd go() method would have to be changed to either have:

a) The same return type of void (i.e. match 1st go() method). This would make the code above an example of overriding.

b) A different parameter list (note: no change to return type needed although one may be made). This would make the code

above an example of overloading.

(p. 127)
Term
Is this legal:

public Button doStuff() {
return null;
}
Definition
Yes, you may return null as an object reference return type. (p. 128)
Term
Is this legal:

public String{} go() {
return new String{} {"Fred", "Barney", "Wilma"};
}
Definition
Yes, arrays are legal return types. (p. 128)
Term
Is this legal:

public int foo() {
char c = 'c';
return c;
}
Definition
Yes, you may return any value or variable to a primitive return type that may be implicitly converted to the primitive return

type. (p. 128)
Term
T/F: A constructor can be called by its name from another method
Definition
False - constructors are invoked only by the keyword new, or from another constructor using this() or super()
(Ref: p. 131)
Term
T/F: An abstract class has a constructor
Definition
T: Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
(Ref: p. 134)
Term
What is constructor chaining?
Definition
Every constructor invokes the constructor if its superclass with an implicit or explicit call to super(). Even if a constructor invokes another constructor of its own class, that constructor (or another that it calls) will eventually call the super() constructor of the parent class.
(Ref: p. 132)
Term
Can a constructor be private?
Definition
Yes, but then only code within the class itself can instantiate an object of that type.
(Ref: p. 133)
Term
When will a default constructor be inserted by the compiler?
Definition
When there are no typed constructors at all within the class.
(Ref: p. 133)
Term
T/F: The default constructor can have arguments.
Definition
F
(Ref: p. 133)
Term
What must be present as the first call (implicitly or explicitly) in a constructor?
Definition
this() or super()
(Ref: p. 134)
Term
T/F: Instance methods and variables are accessible from the constructor
Definition
T, but only after the super constructor has run.
(Ref: p. 134)
Term
T/F: Static variables and methods can be accessed as part of the call to super() or this().
Definition
T
(Ref: p. 134)
Term
T/F: Interfaces have constructors
Definition
F: Interfaces do not have constructors. They are not part of an object's inheritance tree.
(Ref: p. 134)
Term

Define coupling

 

Definition
The degree to which one class knows about another class. (Ref: p. 152)
Term
Define cohesion
Definition
The degree to which a class has a single, well-focused purpose. (Ref: p. 153)
Term
With respect to coupling and cohesion levels, what is the desired state of a class?
Definition
Loosely coupled and highly cohesive.
(Ref: p. 161)
Term
public class SunRoof{}
public class SexAppeal{}
public class ManualTransmission{}


public class Vehicle{}
public class Car extends Vehicle{
private SunRoof sr;
}
public class Mustang extends Car{
private SexAppeal sa;
private ManualTransmission mt;
}



True or False? Vehicle IS-A Car.
Definition
False
Term
public class SunRoof{}
public class SexAppeal{}
public class ManualTransmission{}


public class Vehicle{}
public class Car extends Vehicle{
private SunRoof sr;
}
public class Mustang extends Car{
private SexAppeal sa;
private ManualTransmission mt;
}


True or False? Car HAS-A Mustang.
Definition
False
Term
public class SunRoof{}
public class SexAppeal{}
public class ManualTransmission{}


public class Vehicle{}
public class Car extends Vehicle{
private SunRoof sr;
}
public class Mustang extends Car{
private SexAppeal sa;
private ManualTransmission mt;
}


True or False? Mustang HAS-A SexAppeal.
Definition
True
Term
public class SunRoof{}
public class SexAppeal{}
public class ManualTransmission{}


public class Vehicle{}
public class Car extends Vehicle{
private SunRoof sr;
}
public class Mustang extends Car{
private SexAppeal sa;
private ManualTransmission mt;
}


True or False? Mustang IS-A Vehicle.
Definition
True
Term
public class SunRoof{}
public class SexAppeal{}
public class ManualTransmission{}


public class Vehicle{}
public class Car extends Vehicle{
private SunRoof sr;
}
public class Mustang extends Car{
private SexAppeal sa;
private ManualTransmission mt;
}


True or False? Mustang IS-A Car.
Definition
True
Term
True or False? Any Java object that can pass more than one IS-A test can be considered polymorphic.
Definition
True
Term

Does an overriding method need to declare exceptions if the overridden method declares them?

Definition

No!  An Overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

 

pg(106-107)

Term
public abstract class Fruit {

public String taste() throws Exception {
throw new Exception();
}
}


public class Nana extends Fruit{

public String taste() {
return "Yummy";
}
Public static void main(String args []){
Fruit nana = new Nana();
System.out.println(nana.taste());
}
}

What is the output?

1) Code throws an excpetion when run.
2) Code prints “Yummy”
3) Code does not compile.
Definition

3 - the code does not compile.  The reference type throws an exception, so event hought he method that executes will never throw an exception, the exception must be handled.

pg (108)

Term
Which Non access modifiers create a class that cannot be overriden?
Definition
final, static
Term
public abstract class Fruit {

public String getCategory(){
return "Fruit";
}

}

public class Nana extends Fruit{

public String getCategory(){
return "Nana";
}
Public static void main(String args []){
Nana nana = new Nana();
System.out.println(nana.super.getCategory());
}
}

Whats it the output?
Definition

This is not the correct way to use the super modifier. 

Term
Which of these lend towards maintainability, flexibility and extensibility in design using encapsulation? A. Keep Instance Variables visible via public declaration. B. Make Public Accessor Methods, and force calling code to use those methods. C. For methods, use the JavaBeans naming convention of set and get.
Definition
A, C
Term
class Frog {
Frog(String name) { }
}

T/F - The compiler will put in a default constructor for class Frog.
Definition
False - Default constructor will be created only if you don't write any constructors in your class.  (pg 135)
Term
T / F - The default constructor has the same access modifiers as the class.
Definition
True.  (pg 136)
Term
class Clothing {
Clothing(String s) { }
}

class TShirt extends Clothing { }

Choose one of the following:
a. This code will run without errors
b. This code will have a runtime error.
c. This code will have a compiling error.
Definition
c.  The superclass Clothing does not have a no-arg constructor.  You must type a constructor in the subclass or you will get a compiling error (pg 138)
Term
T/F - Contructors can be overloaded and overridden but are never inherited.
Definition
False - Constructors are not methods.  They cannot be overridden.
Term
What is a downcast of a reference variable? upcast? Which one requires explicit casting?
Definition
Downcast is when you assign a reference variable to a subtype. This requires an explicit cast.
ex. Assume Dog extends Animal
Dog dog = (Dog) animal;

Upcasting is when you assign a reference variable to a supertype. No explicit cast required.
Term
What is the limit for number of interfaces a class can implement?
Definition
Infinite! pg 122
Term
When does a class that implements an interface not have to implement all methods from the interface?
Definition
When the class is abstract. pg 121
Term
Which are valid?
1.) class Foo { }
2.) class Bar implements Foo { }
3.) interface Baz { }
4.) interface Fi { }
5.) interface Fee implements Baz { }
6.) interface Zee implements Foo { }
7.) interface Zoo extends Foo { }
8.) interface Boo extends Fi { }
9.) class Toon extends Foo, Button { }
10.) class Zoom implements Fi, Baz { }
11.) interface Vroom extends Fi, Baz { }
12.) class Yow extends Foo implements Fi { }
Definition
1,3,4,8,10,11,12 are Valid
2.) cant implement a class
5.) Interface cant implement an interface
6.) Interface cant implement a class
7.) Interface cant extend a class
9.) Class cant extend multiple classes
Term
class Frog {
int count=0;

public Frog() { count++; }

public static void main(String[] args)
{
new Frog();
new Frog();
new Frog();

System.out.println("count=" + count);
}
}
Definition
3
Supporting users have an ad free experience!