Java Programming Questions and Answers Set 12

Java Programming OOPs

Questions 111 to 120


111.
Which of the following statements is true?
(a)
Inheritance defines a has-a relationship between a superclass and its subclasses
(b)
Every java object has a public method named equals
(c)
Every java object has a public method named length
(d)
A class can extend any number of other classes
(e)
All of the above.
112.
Which of the following statements is true?
(a)
The keyword extends is used to specify that an interface inherits from another interface
(b)
The keyword extends is used to specify that a class inherits from an interface
(c)
The keyword implements is used to specify that an interface inherits from another interface
(d)
The keyword implements is used to specify that a class inherits from another class
(e)
None of the above.
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
{
}
(a)
Line labeled (1)
(b)
Line labeled (2)
(c)
Line labeled (3)
(d)
Line labeled (4)
(e)
Line labeled (5).
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;
          //. . . .
(a)
obj2 = obj1;
(b)
obj3 = obj1;
(c)
obj3 = obj2;
(d)
I1 a = obj2;
(e)
I1 b = 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;
// . . .
(a)
Illegal at compile time
(b)
Legal at compile time, but might be illegal at runtime
(c)
Definitely legal at runtime, but the (Sub) cast is not strictly needed
(d)
Definitely legal at runtime, and the (Sub) cast is needed
(e)
None of the above.
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?
(a)
(o instanceof B) && (!(o instanceof A))
(b)
(o instanceof B) && (!(o instanceof C))
(c)
!((o instanceof A) || (o instanceof B))
(d)
(o instanceof B)
(e)
(o instanceof B) && !((o instanceof A) || (o instanceof 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; }
}
(a)
The program will fail to compile
(b)
The program will compile without error, but will throw a ClassCastException when run
(c)
The program will compile without error and print 0 when run
(d)
The program will compile without error and print 1 when run
(e)
The program will compile without error and print 2 when run.
118.
Which of the following statements is true?
(a)
Non-static member classes must have either default or public accessibility
(b)
All nested classes can declare static member classes
(c)
Methods in all nested classes can be declared static
(d)
All nested classes can be declared static
(e)
Static member classes can contain non-static methods.
119.
Which statement is true?
(a)
Objects can be explicitly destroyed using the keyword delete
(b)
An object will be garbage collected immediately after it becomes unreachable
(c)
If object obj1 is accessible from object obj2, and object obj2 is accessible from obj1, then obj1 and obj2 are not eligible for garbage collection
(d)
Once an object has become eligible for garbage collection, it will remain eligible until it is destroyed
(e)
If object obj1 can access object obj2 that is eligible for garbage collection, then obj1 is also eligible for garbage collection.
120.
Which statement is true?
(a)
If an exception is thrown during the execution of the finalize() method of an eligible object, then the exception is ignored and the object is destroyed
(b)
All objects have a finalize() method
(c)
Objects can be destroyed by explicitly calling the finalize() method
(d)
The finalize() method can be declared with any accessibility
(e)
The compiler will fail to compile code that defines an overriding finalize() method that does not explicitly call the overridden finalize() method from the superclass.

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.



<< Prev   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22  

 23   24   25   26   27   28   29   30  Next >>


5 comments :

  1. Why in Q: 116 option d is not true ????

    ReplyDelete
  2. It 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).

    java training in chennai

    ReplyDelete
  3. Helpful post.. This interview question collections are really helpful to all for cracking the interviews.. thanks a lot for sharing this post..

    hadoop training institute in chennai velachery | big data training institute in chennai velachery

    ReplyDelete