Welcome Guest | Sign in | Register

Home > Java Programming > Inheritance > Questions and Answers

01. What is the output for the below code ?

public class A {
public void printValue(){
System.out.println("A");
}
}
public class B extends A {
public void printValue(){
System.out.println("B");
}
}
1. public class Test {
2. public static void main(String... args) {
3. A b = new B();
4. newValue(b);
5. }
6. public static void newValue(A a){
7. if(a instanceof B){
8. ((B)a).printValue();
9. }
10. }
11. }
A. A B. B
C. Compilation fails with an error at line 4 D. Compilation fails with an error at line 8

Answer and Explanation

Answer: B

Explanation:
instanceof operator is used for object reference variables to check whether an object is of a particular type. In newValue(b); b is instance of B So works properly.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. class A{
int I;
A(){
System.out.println(1);
} }

Class B extends A{
B(int i){
System.out.println(2);
This.i=I;
}}

Class Manager{
Public static void main(String args[]){
B b1=new B(20);
System.out.println(b1.i);} }
A. 1,2,20 B. 20,2,1
C. compile time error D. run time error

Answer and Explanation

Answer: 1,2,20

Explanation:
in main() method we are creating an object of class B by calling B class constructor, class B constructor will call super class constructor because in each constructor body super() keyword will be there by default, so first class A constructor will execute then class B constructor finally main() method. So ans a)is correct

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class A{
A(int i){
System.out.println(1);
} }

Class B extends A{
B(int i){
System.out.println(2);
This.i=I;
}}

Class Manager{
Public static void main(String args[]){
A a=new A(10);
B b1=new B(20);
} }
A. 1,2,20 B. 20,2,1
C. compile time error D. run time error

Answer and Explanation

Answer: compile time error

Explanation:
in main() method we are creating an object of class A as well as class B by calling A and B class constructor, class B constructor will call super class constructor because in each constructor body super() keyword will be there by default with no parameter ,but in class A there is no constructor with no parameter ,so it will throw an compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. class A{
A(){
System.out.println(1);
} }
Class B extends A{
B(){
System.out.println(2);
}}
Class C extends B {
C(){
System.out.println(3);
Super();
} }

Class Manager{
Public static void main(String args[]){
C c=new C();
} }
A. 1,2,20 B. 20,2,1
C. compile time error D. run time error

Answer and Explanation

Answer: compile time error

Explanation:
in main() method we are creating an object of class C by using C class constructor, in class C constructor super() keyword is there in second statement, but according to the rule super () must be in the first statement in the constructor body , so it will throw compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. class A{
A(){
This(10);
System.out.println(1);
}
A(int i){
This();
System.out.println(2); }
}
Class Manager{
Public static void main(String args[]){
System.out.println(“done”);
} }
A. 1,2 B. 2,1
C. compile time error D. run time error

Answer and Explanation

Answer: compile time error

Explanation:
here in class A there are two constructor one with no parameter, other with one parameter, in each constructor we are using this() keyword , from no parameter constructor we are calling parameter constructor using this() keyword, and from parameter constructor we are calling no parameter constructor using this() keyword, so it will become cyclic calling so it will throw an compile time error

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. class A{
A(){
System.out.println(1);
} }

Class B extends A{
B(){
Super();
This(10);
System.out.println(2); }
B(int x){
System.out.println(2); }}
Class C extends B {
C(){
System.out.println(3);
Super();
} }

Class Manager{
Public static void main(String args[]){
B c=new B();
} }
A. 1,2 B. 2,1
C. compile time error D. run time error

Answer and Explanation

Answer: compile time error

Explanation:
in a single constructor we can’t use both super() and this(), if you try to use then it will throw an compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class v{
v(){
System.out.println(1);
}
void test(){
this();
System.out.println(2); }
}
class manager
{
public static void main(String args[]){
v v1=new v();
}
}
A. 1 B. 2
C. compile time error D. runtime error

Answer and Explanation

Answer: compile time error

Explanation:
call to this must be first statement in constructor not in method

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. class v{
v(){
System.out.println(1);
} }
class manager
{
public static void main(String args[]){
v v1=new v();
v1.v();
}
}
A. 1 B. 2
C. compile time error D. runtime error

Answer and Explanation

Answer: 1

Explanation:
Normal execution

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. class v{
private v(){
System.out.println(1); } }

class manager {
public static void main(String args[]){
v v1=new v();
v1.v(); } }
A. 1 B. 2
C. compile time error D. runtime error

Answer and Explanation

Answer: compile time error

Explanation:
if constructor is private you can not create an object from other class if you try to create then it will throw an compile time error , if you want to create then create from same class.  

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class manager {
private manager() {
System.out.println(1); }
Private void manager(){
System.out.println(2); }
public static void main(String args[]){
new manager(); } }
A. 1 B. 2
C. compile time error D. runtime error

Answer and Explanation

Answer: 1

Explanation:
if constructor is private you can not create an object from other class if you try to create then it will throw an compile time error , if you want to create then create from same class. Here we are creating from same class so it will display 1.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.