332). Which of this keyword must be used to inherit a class?
[A]super
[B]this
[C]extent
[D]extends
Show Answer
Correct Answer: extends
Notes:
Answer: d
Explanation: None.
333). 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
Show Answer
Correct Answer: private member
Notes:
Answer: b
Explanation: A class member declared protected becomes a private member of subclass.
334). 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 {}
Show Answer
Correct Answer: class B extends A {}
Notes:
Answer: c
Explanation: None.
335). Which two classes use the Shape class correctly?
A. public class Circle implements Shape
{
private int radius;
}
B. public abstract class Circle extends Shape
{
private int radius;
}
C. public class Circle extends Shape
{
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape
{
private int radius;
public void draw();
}
E. public class Circle extends Shape
{
private int radius;
public void draw()
{
/* code here */
}
}
F. public abstract class Circle implements Shape
{
private int radius;
public void draw()
{
/* code here */
}
}
[A] B,E
[B]A,C
[C]C,E
[D]T,H
Show Answer
Correct Answer: B,E
Notes:
Answer: a
Explanation: If one is extending any class, then they should use extends keyword not implements.
336). 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
Show Answer
Correct Answer: 2
Notes:
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: $ javac inheritance_demo.java
$ java inheritance_demo
2
337). 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
Show Answer
Correct Answer: 2 3
Notes:
Answer: c
Explanation: None
output:
$ javac inheritance.java
$ java inheritance
2 3
338). 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
Show Answer
Correct Answer: 1 2
Notes:
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:
$ javac super_use.java
$ java super_use
1 2