Java Questions and Answers Part-14

1. A class member declared protected becomes a member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member

Answer: b
Explanation: A class member declared protected becomes a private member of subclass.

2. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}

Answer: c
Explanation: class B extends A {}

3. What will be the output of the following Java program?
class A {
int i;
void display() {
System.out.println(i);
} }
class B extends A {
int j;
void display()
{
System.out.println(j);
}
}
class inheritance_demo {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
} }
a) 0
b) 1
c) 2
d) Compilation Error

Answer: c
Explanation: Class A & class B both contain display() method, class B inherits class A, when display() method is called by object of class B, display() method of class B is executed rather than that of Class A.
output:
2

4. What will be the output of the following Java program?
class A
{
int i;
}
class B extends A
{
int j;
void display()
{
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2

Answer: c
Explanation: output:
2 3

5. What will be the output of the following Java program?
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

Answer: a
Explanation: Keyword super is used to call constructor of class A by constructor of class B. Constructor of a initializes i & j to 1 & 2 respectively.
output:
1 2

6. What is not type of inheritance?
a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance

Answer: b
Explanation: Inheritance is way of acquiring attributes and methods of parent class. Java supports hierarchical inheritance directly.

7. Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods

Answer: a
Explanation: Multiple inheritance in java is implemented using interfaces. Multiple interfaces can be implemented by a class.

8. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object

Answer: d
Explanation: All classes in java are inherited from Object class. Interfaces are not inherited from Object Class.

9. In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?
a) Protected
b) Private
c) Public
d) Static

Answer: b
Explanation: By declaring variable private, the variable will not be available in inherited to subclass.

10. If super class and subclass have same variable name, which keyword should be used to use super class?
a) super
b) this
c) upper
d) classname

Answer: a
Explanation: Super keyword is used to access hidden super class variable in subclass.