Object Oriented Programming 1
Object Oriented Programming 1
com
Object Oriented Programming
SET►1
Question 1
Which of the following statements is not true?
a. An interface that is declared within the body of a class or interface is known as a nested
interface.
b. A constant can be a member of an interface.
c. A class declaration can be a member of an interface.
d. If an interface is named in the implements clause of a class, then the class must implement all
of the methods declared within the interface.
e. None of the above.
Answer:
This question asks which answer option is not true. Some true
statements are as follows. An interface can be declared within
an enclosing class or interface. The members of an interface
If an interface is named in the can be constants, abstract method declarations, class
implements clause of a class, declarations or interface declarations. If an interface is named
d then the class must implement in the implements clause of a class, then the class must
all of the methods declared implement all of the methods declared within the interface or
within the interface. the class must be declared abstract. The untrue answer option
did not mention that an abstract class is not required to
implement any of the methods declared in an interface that is
named in the implements clause of the class declaration.
Question 2
Which of the following are true statements?
Question 3
Which of the following statements are true?
a. A constructor can invoke another constructor of the same class using the alternate constructor
invocation, "this(argumentListopt);".
b. A constructor can invoke itself using the alternate constructor invocation,
"this(argumentListopt);".
c. The alternate constructor invocation, "this(argumentListopt);", can legally appear anywhere in
the constructor body.
d. A constructor can invoke the constructor of the direct superclass using the superclass
constructor invocation, "super(argumentListopt);".
e. The number of constructor invocations that may appear in any constructor body can equal but
not exceed the number of alternate constructors declared in the same class.
f. A constructor is not permitted to throw an exception.
Answer:
If an alternate constructor invocation appears
A constructor can invoke another constructor of
in the body of the constructor, then it must be
the same class using the alternate constructor
the first statement. The same is true for a
a invocation, "this(argumentListopt);". A constructor can
superclass constructor invocation. A compile-
d invoke the constructor of the direct superclass
time error is generated if a constructor
using the superclass constructor invocation,
attempts to invoke itself either directly or
"super(argumentListopt);".
indirectly.
Question 4
Which of the following statements are true?
Question 5
Which of the following are true statements?
a.A top-level class can not be called "tightly encapsulated" unless it is declared private.
b.Encapsulation enhances the maintainability of the code.
c.A tightly encapsulated class allows fast public access to member fields.
d.A tightly encapsulated class allows access to data only through accessor and mutator methods.
e.Encapsulation usually reduces the size of the code.
f.A tightly encapsulated class might have mutator methods that validate data before it is loaded
into the internal data model.
Answer:
The data members of a tightly encapsulated class
are declared private; so changes to the data model
Encapsulation enhances the maintainability
are less likely to impact external code. Access to
of the code. A tightly encapsulated class
internal data can be provided by public accessor
b allows access to data only through accessor
(i.e. get) and mutator (i.e. set) methods. The
d and mutator methods. A tightly
mutator methods can be used to validate the data
f encapsulated class might have mutator
before it is loaded into the internal data model.
methods that validate data before it is loaded
The use of accessor and mutator methods is
into the internal data model.
likely to increase the size of the code and slow
execution speed.
Question 6
Suppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly
in a subclass constructor. If a compile-time error is to be avoided then the arguments for the
superclass constructor invocation, "super(argumentListopt);", can not refer to which of the
following?
Answer:
b Instance variables declared in If the superclass constructor invocation, "super(argumentListopt);",
d this class or any superclass. appears explicitly or implicitly, then it must be the first
e Instance methods declared in statement in the body of the constructor. Until the superclass
f this class or any superclass. The constructor invocation runs to completion, no other
keyword this. The keyword statements are processed within the body of the constructor.
super. The same is true of the constructors of any superclass. (Note:
The primordial class, Object, does not have a superclass,
so the constructors do not include a superclass constructor
invocation statement.) Suppose class B is a subclass of A.
The process of creating and initializing an instance of B
includes the creation and initialization of an instance of the
superclass A and an instance of the superclass Object. The
superclass constructor invocation statement appearing in a
constructor of B is invoked before the completion of the
constructors of the superclasses A and Object. A superclass
constructor invocation statement appearing in B can not refer
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
to the non-static members of the superclasses, because the
process of initializing those non-static superclass members is
not complete when the superclass constructor invocation
occurs in B. The same is true of the non-static members of B.
Question 7
Which of the following are modifiers that can be applied to an interface that is a member of a
directly enclosing interface?
Question 8
A class can not be called "tightly encapsulated" unless which of the following is true?
Answer:
If a class A has a method that returns a reference to an internal, mutable object; then
None of external code can use the reference to modify the internal state of class A. Therefore,
e the class A can not be considered tightly encapsulated. However, the methods of a tightly
above encapsulated class may return a reference to an immutable object or a reference to a
copy or clone of an internal object.
Question 9
Which of the following are modifiers that can be applied to an interface that is a member of a
directly enclosing class?
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
a. abstract c. final e. protected
b. extends d. private f. public
Answer:
All interfaces are implicitly abstract. The explicit application of the modifier,
abstract, to an interface is redundant and is strongly discouraged. The
declaration of an interface within the body of an enclosing class or interface is
abstract called a member type declaration. The private, protected and static
a
private
d modifiers are applicable to a member type declaration that appears in the body
protected
e f of a directly enclosing class. In contrast, the modifiers, private and
public
protected, are not applicable to a member type declaration appearing
within the body of a directly enclosing interface. The modifier, final, is
never applicable to an interface. The keyword, extends, is not a modifier.
Question 10
A class can not be called "tightly encapsulated" unless which of the following is true?
Question 11
Which of the following is a modifier that can be applied to an interface that is a member of a
directly enclosing class or interface?
a. static d. volatile
b. synchronized e. implements
c. transient f. None of the above.
Answer:
A member interface is always implicitly static. The modifier, static, can not be
applied to an interface that is not a member interface. The modifier, synchronized, is
a static applicable to a concrete implementation of a method, but is not applicable to any
interface. The modifiers, volatile and transient, are only applicable to variables
that are members of a class. The keyword, implements, is not a modifier.
Question 12
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
A class can not be called "tightly encapsulated" unless which of the following are true?
Question 13
class A {String s1 = "A.s1"; String s2 = "A.s2";}
class B extends A {
String s1 = "B.s1";
public static void main(String args[]) {
B x = new B(); A y = (A)x;
System.out.println(x.s1+" "+x.s2+" "+y.s1+" "+y.s2);
}}
Question 14
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D extends C {
void m1(D d) {System.out.print("D");}
public static void main(String[] args) {
A a1 = new A(); B b1 = new B(); C c1 = new C(); D d1 = new D();
d1.m1(a1); d1.m1(b1); d1.m1(c1);
}}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
What is the result of attempting to compile and run the program?
Question 15
class C {
void printS1() {System.out.print("C.printS1 ");}
static void printS2() {System.out.print("C.printS2 ");}
}
class D extends C {
void printS1(){System.out.print("D.printS1 ");}
void printS2() {System.out.print("D.printS2 ");}
public static void main (String args[]) {
C c = new D(); c.printS1(); c.printS2();
}}
Question 16
class A {} class B extends A {} class C extends B {}
class D {
void m1(A a) {System.out.print("A");}
void m1(B b) {System.out.print("B");}
void m1(C c) {System.out.print("C");}
public static void main(String[] args) {
A c1 = new C(); B c2 = new C(); C c3 = new C(); D d1 = new D();
d1.m1(c1); d1.m1(c2); d1.m1(c3);
}}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
What is the result of attempting to compile and run the program?
Question 17
class E {
void printS1(){System.out.print("E.printS1 ");}
static void printS2() {System.out.print("E.printS2");}
}
class F extends E {
void printS1(){System.out.print("F.printS1 ");}
static void printS2() {System.out.print("F.printS2");}
public static void main (String args[]) {
E x = new F(); x.printS1(); x.printS2();
}}
Answer:
A static method is selected based on the compile-time type of the reference--not
the run-time type of the object. A non-static method is selected based on the run-
time type of the object--not the compile-time type of the reference. Both method
Prints:
invocation expressions, x.printS1() and x.printS2(), use a reference of
c F.printS1
the superclass type, E, but the object is of the subclass type, F. The first of the
E.printS2
two expressions invokes an instance method on an object of the subclass type; so
the overriding subclass method is selected. The second invokes a static method
using a reference of the superclass type; so the superclass method is selected.
Question 18
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A c1 = new C(); B c2 = new C(); C c3 = new C(); C c4 = new C();
c4.m1(c1); c4.m1(c2); c4.m1(c3);
}}
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above
Answer:
b Prints: Three methods overload the method name m1. Each has a single parameter of type A or
ABC
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
B or C. For any method invocation expression of the form
m1(referenceArgument), the method is selected based on the declared type of
the variable referenceArgument--not the run-time type of the referenced object.
The method invocation expression c4.m1(c1) uses reference c4 of type C to invoke
method m1 on an instance of type C. The argument, c1, is a reference of type A and
the run-time type of the referenced object is C. The argument type is determined by the
declared type of the reference variable c1--not the run-time type of the object
referenced by c1. The declared type of c1 is type A; so the method A.m1(A a) is
selected. The declared type of c2 is type B; so the method invocation expression
c4.m1(c2) invokes method B.m1(B b). The declared type of c3 is type C; so the
method invocation expression c4.m1(c3) invokes method C.m1(C c).
Question 19
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}}
Question 20
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
a. Prints: A
b. Prints: B
c. Prints: C
d. Compile-time error
e. Run-time error
f. None of the above
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
Answer:
The reference c1 is of the superclass type, A; so it can be used to invoke only the
method m1 declared in class A. The methods that overload the method name m1 in the
subclasses, B and C, can not be invoked using the reference c1. A method invocation
conversion promotes the argument referenced by c2 from type C to type A, and the
method declared in class A is executed. Class A declares only one method, m1. The
single parameter is of type A. Class B inherits the method declared in class A and
overloads the method name with a new method that has a single parameter of type B.
Both methods sharing the overloaded name, m1, can be invoked using a reference of
Prints: type B; however, a reference of type A can be used to invoke only the method declared
a
A in class A. Class C inherits the methods declared in classes A and B and overloads the
method name with a new method that has a single parameter of type C. All three
methods sharing the overloaded name, m1, can be invoked using a reference of type C;
however, a reference of type B can be used to invoke only the method declared in class
B and the method declared in the superclass A. The method invocation expression
c1.m1(c2) uses reference c1 of type A to invoke method m1. Since the reference c1
is of type A, the search for an applicable implementation of m1 is limited to class A.
The subclasses, B and C, will not be searched; so the overloading methods declared in
the subclasses can not be invoked using a reference of the superclass type.
Question 21
class R {
private void printS1(){System.out.print("R.printS1 ");}
protected void printS2() {System.out.print("R.printS2 ");}
protected void printS1S2(){printS1();printS2();}
}
class S extends R {
private void printS1(){System.out.print("S.printS1 ");}
protected void printS2(){System.out.print("S.printS2 ");}
public static void main(String[] args) {
new S().printS1S2();
}}
Question 22
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A a1 = new A(); A b1 = new B(); A c1 = new C(); C c4 = new C();
a1.m1(c4); b1.m1(c4); c1.m1(c4);
}}
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above
Rahul Rastogi: www.faqworldofrastogi.wetpaint.com
Answer:
The declared type of the reference variables, a1, b1 and c1, is the superclass type, A;
so the three reference variables can be used to invoke only the method m1(A a) that
Prints: is declared in the superclass, A. The methods that overload the method name m1 in the
a
AAA subclasses, B and C, can not be invoked using a reference variable of the superclass
type, A. A method invocation conversion promotes the argument referenced by c4
from type C to type A, and the method declared in class A is executed.