Sail E0 Webinar

MCQs

Total Questions : 30 | Page 3 of 3 pages
Question 21.


What is the output of this program?


class A {
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
  1.    1 2
  2.    2 1
  3.    1 3
  4.    3 1
 Discuss Question
Answer: Option A. -> 1 2

Both class A & B have member with same name that is j, member of class B will be called by 

default if no specifier is used. I contains 1 & j contains 2, printing 1 2.
output:
$ javac Output.java
$ java Output
1 2


Question 22.


What is the output of this 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)
}
}
  1.    1 2
  2.    2 1
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option A. -> 1 2

Keyword super is used to call constructor of class A by constructor of class B. Constructor of 

Question 23.


What is the output of this program?


class A {
public int i;
private int j;
}
class B extends A {
void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
  1.    2 2
  2.    3 3
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option D. -> Compilation Error

class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:
$ javac inheritance.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field A.j is not visible


Question 24.


What is the output of this 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();
}
}
  1.    2 2
  2.    3 3
  3.    2 3
  4.    3 2
 Discuss Question
Answer: Option C. -> 2 3

 None
output:
$ javac inheritance.java
$ java inheritance
2 3


Question 25.


What is the output of this 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();
}
}
  1.    0
  2.    1
  3.    2
  4.    Compilation Error
 Discuss Question
Answer: Option C. -> 2

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


Question 26.

Which of the following statements are incorrect?


  1.    public members of class can be accessed by any code in the program.
  2.    private members of class can only be accessed by other members of the class.
  3.    private members of class can be inherited by a sub class, and become protected members in sub class.
  4.    protected members of a class can be inherited by a sub class, and become private members of the sub class.
 Discuss Question
Answer: Option C. -> private members of class can be inherited by a sub class, and become protected members in sub class.

private members of a class cannot be inherited by a sub class.

Question 27.

Which of these is correct way of inheriting class A by class B?


  1.    class B + class A {}
  2.    class B inherits class A {}
  3.    class B extends A {}
  4.    class B extends class A {}
 Discuss Question
Answer: Option C. -> class B extends A {}

None.


Question 28.

A class member declared protected becomes member of subclass of which type?


  1.    public member
  2.    private member
  3.    protected member
  4.    static member
 Discuss Question
Answer: Option B. -> private member

A class member declared protected becomes private member of subclass.


Question 29.

Which of these keywords is used to refer to member of base class from a sub class?


  1.    upper
  2.    super
  3.    this
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> super

whenever a subclass needs to refer to its immediate superclass, it can do so by use of the 

keyword super.


Question 30.

Which of these keyword must be used to inherit a class?


  1.    super
  2.    this
  3.    extent
  4.    extends
 Discuss Question
Answer: Option D. -> extends

None.


Latest Videos

Latest Test Papers