Java Programming OOPs
Questions 111 to 120
111.
|
Which of the
following statements is true?
|
||||||||||
112.
|
Which of the
following statements is true?
|
||||||||||
113.
|
Which is the
first line that will cause compilation to fail in the following program?
class
MyClass
{
public static void main(String[] args)
{
MyClass a;
MySubclass b;
a = new MyClass(); //(1)
b = new MySubclass(); //(2)
a = b; //(3)
b = a; //(4)
a = new MySubclass(); //(5)
}
}
class
MySubclass extends MyClass
{
}
|
||||||||||
114.
|
Given the
following definitions and reference declarations, which one of the following
assignments is legal?
//Definitions:
interface I1{}
interface I2{}
class C1 implements I1 {}
class C2 implements I2 {}
class C3 extends C1 implements I2 {}
//Reference declarations:
//. . . .
C1
obj1;
C2 obj2;
C3 obj3;
//.
. . .
|
||||||||||
115.
|
Given the
following class definitions and the reference declarations, what can be said
about the statement y = (Sub) x?
// Class definitions:
class Super { }
class Sub extends Super { }
//Reference declarations
// . . .
Super
x;
Sub
y;
// . . .
|
||||||||||
116.
|
Given three
classes A,B,C, where B is a subclass of A and C is a subclass of B, which one
of these Boolean expressions is true
when an object denoted by reference has actually been instantiated from class
B as opposed to from A or C?
|
||||||||||
117.
|
What will be
the result of attempting to compile and run the following program?
public class Polymorphism
{
public
static void main(String[] args)
{
A
ref1 = new C();
B
ref2 = (B) ref1;
System.out.println(ref2.f());
}
}
class A
{
int
f() { return 0; }
}
class B extends A
{
int
f() { return 1; }
}
class C extends B
{
int
f() { return 2; }
}
|
||||||||||
118.
|
Which of the
following statements is true?
|
||||||||||
119.
|
Which
statement is true?
|
||||||||||
120.
|
Which
statement is true?
|
Answers
111.
|
Answer : (b)
Reason: Inheritance defines an is-a relation. Aggregation
defines a has-a relation. The Object class has a public method named equals,
but it does not have any method named length. Since all classes are
subclasses of the Object class, they all inherit the equals() method. Thus,
all Java objects have a public method named equals. In Java, a class can only
extend a single superclass.
|
112.
|
Answer : (a)
Reason: The keyword implements is used when a class
inherits from an interface. The keyword extends is used when an interface
inherits from another interface or a class inherits from another class.
|
113.
|
Answer : (d)
Reason: Line (4) will cause a compile-time error
since it attempts to assign a reference value of a supertype object to a
reference of a subtype. The type of the source reference value is MyClass and
the type of the destination reference is MySubclass. Lines (1) and (2) will
compile since the reference is assigned a reference value of the same type.
Line (3) will also compile since the reference is assigned a reference value
of a subtype.
|
114.
|
Answer : (e
Reason: Only the assignment I1 b = obj3 is valid.
The assignment is allowed since C3 extends C1, which implements I1.
Assignment obj2 = obj1 is not legal since C1 is not a subclass of C2.
Assignments obj3 = obj1 and obj3 = obj2 are not legal since neither C1 nor C2
is a subclass of C3. Assignment I1 a = obj2 is not legal since C2 does not
implement I1.
|
115.
|
Answer : (b)
Reason: The statement would be legal at compile
time, since the reference x might actually refer to an object of the type Sub.
The cast tells the compiler to go ahead and allow the assignment. At runtime,
the reference x may turn out to denote an object of the type Super instead.
If this happens, the assignment will be aborted and a ClassCastException will
be thrown.
|
116.
|
Answer : (b)
Reason: The expression (o instanceof B) will return
true if the object referred to by o is of type B or a subtype of B. the
expression (!(o instanceof C)) will return true unless the object referred to
by o is of type C or a subtype of C. thus, the expression (o instanceof B)
&& (!(o instanceof C)) will only return true if the object is of type
B or a subtype of B that is nto C or a subtype of C. Given objects of classes
A, B, and C, this expression will only return true for objects of class B.
|
117.
|
Answer : (e)
Reason: The program will print 2 when
System.out.println(ref2.f()) is executed. The object referenced by ref2 is of
class C, but the reference is of type B. Since B contains a method f(), the
method call will be allowed at compile time. During execution it is
determined that the object is of class C, and dynamic method lookup will
cause the overridden method in C to be executed.
|
118.
|
Answer : (e)
Reason: Non-static member classes, unlike top-level
classes, can have any accessibility modifier. Static member classes can only
be declared in top-level or nested static member classes and interfaces. Only
static member classes can be declared static. Declaring a class static only
means that instances of the class are created without having an outer
instance. This has no bearing on whether the members of the class can be
static or not.
|
119.
|
Answer : (e)
Reason: An object is only eligible for garbage
collection if all remaining references to the object are from other objects
that are also eligible for garbage collection. An object will no necessarily
be garbage collected immediately after it becomes unreachable. However, the
object will be eligible for garbage collection.
|
120.
|
Answer : (b)
Reason: The object class defines a protected
finalize() method. All classes inherit from Object, thus, all objects have a
finalize() method. The finalize() method of an eligible object is called by
the garbage collector to allow the object to do any cleaning up, before the
object is destroyed.
|
Why in Q: 116 option d is not true ????
ReplyDeleteIt is not supported for concreate class. However, you imply multiple inheritance concept in java for interfaces (you cant ssay its a complete multiple inheritance as we are implementing our own method details).
ReplyDeletejava training in chennai
Helpful post.. This interview question collections are really helpful to all for cracking the interviews.. thanks a lot for sharing this post..
ReplyDeletehadoop training institute in chennai velachery | big data training institute in chennai velachery
thank you.
ReplyDeletejava tutorial
java tutorial
Fantastic article post.Really thank you! Awesome.dot net online course
ReplyDelete. net online training