OO Concepts QuizS2C Home « OO Concepts Quiz

The questions in this third quiz on Java5 are on the topics covered in the Java5 - OO Concepts section of the site. The table below lists the lessons, a description of the lesson content and the the quiz question number range.

Lesson Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that lesson.

OO Concepts Lesson Description Question Range
Lesson 1 - EncapsulationUp until now we haven't really worried about our data and how we expose it to the outside world. That is all about to change as we look at encapsulation.1 - 7
Lesson 2 - Inheritance BasicsThis lesson introduces us to the OO concept of Inheritance, what it means and how we start to use it.8 - 16
Lesson 3 - Inheritance ConceptsWe continue our investigation of inheritance and conceptualize where our objects fit into the hierarchy.17 - 24
Lesson 4 - AbstractionWe continue our studies of OO concepts by looking at abstraction.25 - 31
Lesson 5 - PolymorphismWe continue our investigation of OO concepts by learning about polymorphism.32 - 38
Lesson 6 - InterfacesThis lesson looks at interfaces and the contracts we provide when using them.39 - 46
Lesson 7 - Nested ClassesIn this lesson we learn about the various ways to code nested classes within our classes.47 - 54
Lesson 8 - The Object SuperclassWe finish the section by looking at the Object object, the daddy of all classes.55 - 61

Java5 Quiz

The quiz below tests your knowledge of the material learnt in the Java5 - OO Concepts section of the site.

Question 1 : What mechanism is used for encapsulation in Java?
- We use <em>access modifiers</em> as our mechanism for encapsulation in Java.
Question 2 : Which access modifiers can we explicitly apply to our top-level classes?
- We can only explicitly apply the <code>public</code> <em>access modifiers</em> to our top-level classes.
Question 3 : Which access modifiers can we explicitly apply to our members?
- We can explicitly apply the <code>private</code>, <code>protected</code> or <code>public</code> <em>access modifiers</em> to our members.
Question 4 : Which access modifier limits member visibility to the class they reside in?
- The <code>private</code> <em>access modifiers</em> limits member visibility to the class they reside in.
Question 5 : How do we gain access to the private instance variables of a class?
- We gain access to the <code>private</code> instance variables of a class using <code>public</code> methods of the called class.
Question 6 : Does the following code conform to the JavaBean standard for getting the value of an instance variable called bootWidth?

public String getbootWidth() { return type; }
- The name of the method should be <code>getBootWidth()</code> to conform, the rest is fine.
Question 7 : Does the following code conform to the JavaBean standard for setting the value of an instance variable called bootWidth?

public int setBootWidth(int width) { this.width = width; }
- Setter methods should always have a <code>void</code> return type to conform, the rest is fine.
Question 8 : How do we achieve a hierarchial connection in Java ?
- We achieve a hierarchial connection in Java using the <code>extends</code> keyword.
Question 9 : Which is an extended class known as?
- An extended class known as a <em>superclass</em>.
Question 10 : In which class do we put the extends keyword?
- We put the <code>extends</code> keyword in the <em>subclass</em>.
Question 11 : How many classes can you extend in a Java class definition?
- You can only extend one class in a Java class definition.
Question 12 : Method overrides can't be less restrictive?
- Less restrictive is fine, more restictive causes a compiler error.
Question 13 : Method overrides must have different argument lists?
- Method overrides must have the same argument lists or its an overloaded method.
Question 14 : Is the following a valid override?

int i; // instance variable in superclass
int i; // instance variable in subclass
- Instance variables are not overridden in subclasses but when present with the same name this is known as <em>hiding</em>. Think of it as a redefinition.
Question 15 : Is the following a valid override?

public class A {
public void a() {
}
}
public class B extends A {
public static void a() {
}
}
- We can't <em>override</em> an instance method with a static method, the compiler gets upset.
Question 16 : Is the following a valid override?

void a() { ... } // a() method in superclass
public void a() { ... } // a() method in subclass
- The method override is less restrictive so it's fine.
Question 17 : When using inheritance we inherit all members of the superclass?
- We inherit the members that are allowed by the <em>access modifiers</em> of the <em>superclass</em>.<br />As an example <code>private</code> members of the <em>superclass</em> will never be inherited.
Question 18 : We can override the instance variables of a superclass?
- We can only <em>override</em> the methods of a <em>superclass</em>.
Question 19 : What can we check for using the HAS-A test?
- We can check for instance variables using the <code>HAS-A</code> test.
Question 20 : What do we use the IS-A test for?
- We use the <code>IS-A</code> to check for valid inheritance.
Question 21 : The IS-A test works down the inheritance tree?
- The <code>IS-A</code> works up the inheritance tree.
Question 22 : How do we access members of the superclass above the subclass?
- We access members of the <code>superclass</code> above the <code>subclass</code> using the <code>super</code> keyword.
Question 23 : We can explicitly code super() to access what?
- We can explicitly code <code>super()</code> to access <code>superclass</code> constructors.
Question 24 : When we override a method in a subclass and we invoke that method using an instance of the subclass, what gets called at runtime?
- The overridden method in the <em>subclass</em> will be called.
Question 25 : What are non-abstract classes called?
- non-abstract classes are called <em>concrete</em> classes.
Question 26 : Abstract methods must be implemented in the first subclass?
- Abstract methods must be implemented in the first <em>concrete subclass</em>.
Question 27 : How do we instantiate an abstract class?
- We can't instantiate an <em>abstract</em> class.
Question 28 : What is unusual about an abstract method?
- An <em>abstract</em> method has no body.
Question 29 : What are the implications of putting an abstract method in a class?
- Putting an <em>abstract</em> method in a class makes the class <em>abstract</em>, which means the class must be declared as <em>abstract</em>.
Question 30 : Is the following code snippet valid?

public abstract class A { public abstract void load(); }

public abstract class B extends A { public abstract void load(String payload); }
- The code will compile fine but both of these methods would have to be implememnted in the first concrete subclass.
Question 31 : Is the following code snippet valid?

public abstract class A { public abstract void load(); }

public class B extends A { public abstract void load(); }
- The code will not compile as class B is not abstract and doesn't implememnt the abstract method from A.
Question 32 : What is required to use polymorphism?
- We need to have an inheritance hierarchy to use polymorphism, although abstracting out generic methods to force implementation in subclasses helps to enforce it.
Question 33 : What do we use in the declaration of an object to enable polymorphism?
- We use the supertype of the object we are creating
Question 34 : What are used for polymorphism?
- <em>overridden</em> instance methods are used in polymorphism.
Question 35 : We use overloaded methods in polymorphism?
- We use <em>overridden</em> methods in polymorphism.
Question 36 : What is the following code snippet an example of?

public class A {
public static void aClassMethod() {
}
}
public class B extends A {
public static void aClassMethod() {
}
}
- This is an example of <em>hiding</em>.
Question 37 : Assuming the following are in different files is the following code snippet valid?

public class A { public void a() {} }
public class B extends A { public void a() {} }
public class TestAB {
public static void main(String[] args) {
B b = new A();
b.a();
}
}
- Class <code>TestAB</code> doesn't compile because of incompatible types. We can't use the <em>subclass</em> as the refernce type to a <em>superclass</em>.
Question 38 : What is the dynamic selection of overridden methods at runtime based on the actual object type, rather than the reference type known as?
- This concept is known as <em>virtual method invocation</em>.
Question 39 : How do we use an interface in our classes?
- We use an interface in our classes by implementing it with the <code>implements</code> keyword in the class definition.
Question 40 : Two modifiers are implicitly applied to methods within an interface, what are they?
- The <code>public</code> and <code>abstract</code> modifiers are implicitly applied to methods within an interface.
Question 41 : Is the following code snippet valid?

public class B implements C extends A { }
- This code snippet is invalid as the <code>implements</code> keyword must follow the <code>extends</code> keyword when used together.
Question 42 : What is the main benefit of interfaces?
- Interfaces allow us to use method arguments and return types polymorphically.
Question 43 : Assuming C, D and E are interfaces is the following code snippet valid?

public class B extends A implements C, D, E { }
- This snippet is perfectly valid as we can implement any number of interfaces.
Question 44 : Assuming the following are in different files is the following code snippet valid?

public interface A { void b(int b); }
public class B implements A { void b(int b) {} }
- Class <code>B</code> won't compile as the <code>b()</code> method is less restrictive. <br />. The complier implicitly applies the <code>public</code> and <code>abstract</code> modifiers to interface methods and <em>overrides</em> methods can't be less restrictive.
Question 45 : What would be the output of the following code snippet?

public interface A { void b(); }
public class B implements A { public void b() {} }
public class C extends B {
public static void main(String[] args) {
B b = new B();
C c = new C();
if (b instanceof A) { System.out.print("true,"); }
if (c instanceof B) { System.out.print("true,"); }
if (c instanceof A) { System.out.print("true"); }
}
}
- The output will be 'true,true,true' as class <code>C</code> inherits the implementation of interface <code>A</code> through class <code>B</code>
Question 46 : What must we ensure when implementing an interface?
- We must honour the interface contract by ensuring we <em>override</em> all methods in the implementation class or first <em>concrete subclass</em> thereof.
Question 47 : What are classes that are attached to an outer instance of the enclosing class collectively known as?
- classes that are attached to an outer instance of the enclosing class are collectively known as <em>inner classes</em>
Question 48 : Where are local inner classes declared?
- <em>local inner classes</em> are declared within a method.
Question 49 : What is class Inner in the following code snippet an example of?

public class OuterQuiz {
private String a;
public void createInner() {
Inner inner = new Inner(); // Create inner class instance
inner.changeInstanceVariables(); // Call inner class method
}
class Inner {
int b;
void changeInstanceVariables() {
a = "Updated";
b = 1234;
}
}
}
- class <code>Inner</code> is an example of a <em>non-static member class</em>.
Question 50 : Where can you use an anonymous inner class?
- You use an <em>anonymous inner class</em> anywhere where an expression is legal.
Question 51 : Which of the following statements is true for inner classes?
- We can never have just an inner instance, is the only true statement.
Question 52 : Assuming class A compiles, what is the result of trying to compile and run class ATest below?

public class A {
public void aMethod() { System.out.println("Outer method."); }
public void anonSubclass() {
A a = new A() {
public void aMethod() { System.out.println("Anonymous method."); }};
a.aMethod();
}
}
public class ATest {
public static void main (String[] args) {
A a = new A();
a.aMethod();
a.anonSubclass();
}
}
- class <code> ATest</code> compiles and runs fine and outputs "Outer method.Anonymous method.".
Question 53 : What is class Inner in the following code snippet an example of?

public class A { public void modifyState() { class Inner { int b; } } }
- class <code>Inner</code> is an example of a <em>local inner class</em>.
Question 54 : What local variables can a local inner class use?
- A <em>local inner class</em> can only use local variables that are marked as <code>final</code>.
Question 55 : What would be output from the following code snippet?

public class TestEquals {
public static void main(String[] args) {
Carrier a = new Carrier(10, 20);
Carrier b = new Carrier(10, 20);
System.out.println(a.equals(b));
}
}
- The output would be <em>false</em>. Although the two instances have the same <em>object state</em> the <code>equals()</code> method only returns <em>true</em> if the reference variables refer to the same object.
Question 56 : What is returned from the getClass() method of the Object class?
- The <code>getClass()</code> method returns the runtime class of an object
Question 57 : Which of the following statements is true?
- The <code>finalize()</code> method is only ever invoked once, for any given object.
Question 58 : We can override the getClass() method of the Object class?
- The <code>getClass()</code> method is <code>final</code> so we cannot <em>override</em> it.
Question 59 : If we override the equals() of the Object class what other method of this class should we also override?

- When we <em>override</em> the <code>equals()</code> method we should also <em>override</em> the <code>hashCode()</code> method.
Question 60 : if x.equals(y) returns true what else must be assured to keep the equals() / hashCode() contract?
- If <code>x.equals(y)</code> then <code>x.hashCode() == y.hashCode()</code> must be assured.
Question 61 : if x.hashCode() == y.hashCode() then x.equals(y) must return true?
- if <code>x.hashCode() == y.hashCode()</code> then <code>x.equals(y)</code> may return true but this isn't mandatory
Quiz Progress Bar Please select an answer

What's Next?

The next quiz on Java5 is all about Flow Control.

go to home page Homepage go to top of page Top