Welcome Guest | Sign in | Register

Home > Java Programming > Inheritance > Questions and Answers

01. What is the output for the below code ?
package com;
class Animal {
public void printName(){
System.out.println("Animal");
}
}
package exam;
import com.Animal;
public class Cat extends Animal {
public void printName(){
System.out.println("Cat");
}
}
package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
Animal a = new Cat();
a.printName();
}
}
A. Animal B. Cat
C. Animal Cat D. Compile Error

Answer and Explanation

Answer: Compile Error

Explanation:
Cat class won't compile because its superclass, Animal, has default access and is in a different package. Only public superclass can be accessible for different package.



Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What is the output for the below code ?

public class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
};
}
public class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B");
}
}
public class Test{
public static void main(String argv[]){
A a = new B();
a.printValue();
System.out.println(a.i);
}
}
A. Value-B 11 B. Value-B 10
C. Value-A 10 D. Value-A 11

Answer and Explanation

Answer: Value-B 10

Explanation:
If you create object of subclass with reference of super class like ( A a = new B();) then subclass method and super class variable will be executed.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the result of compiling the following code:

public class SuperClass {
public int doIt(String str, Integer... data)throws Exception{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class SubClass extends SuperClass{
public int doIt(String str, Integer... data)
{
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " +
signature);
return 0;
}
public static void main(String... args)
{
SuperClass sb = new SubClass();
sb.doIt("hello", 3);
}
}
A. Overridden: hello (String, Integer[]) B. hello (String, Integer[])
C. Complilation fails D. None of the above

Answer and Explanation

Answer: Complilation fails

Explanation:
Unhandled exception type Exception.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What is the output for the below code ?

public class A {
public void printValue(){
System.out.println("Value-A");
}
}
public class B extends A{
public void printNameB(){
System.out.println("Name-B");
}
}
public class C extends A{
public void printNameC(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args) {
3. B b = new B();
4. C c = new C();
5. newPrint(b);
6. newPrint(c);
7. }
8. public static void newPrint(A a){
9. a.printValue();
10. }
11. }
A. Value-A Name-B B. Value-A Value-A
C. Value-A Name-C D. Name-B Name-C

Answer and Explanation

Answer: Value-A Value-A

Explanation:
Class B extended Class A therefore all methods of Class A will be available to class B except private methods. Class C extended Class A therefore all methods of Class A will be available to class C except private methods.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the output for the below code ?

public class A {
public void printName(){
System.out.println("Value-A");
}
}
public class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
public class C extends A{
public void printName(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args) {
3. B b = new B();
4. C c = new C();
5. b = c;
6. newPrint(b);
7. }
8. public static void newPrint(A a){
9. a.printName();
10. }
11. }
A. Name-B B. Name-C
C. Compilation fails due to an error on lines 5 D. Compilation fails due to an error on lines 9

Answer and Explanation

Answer: Compilation fails due to an error on lines 5

Explanation:
Reference variable can refer to any object of the same type as the declared reference OR can refer to any subtype of the declared type. Reference variable "b" is type of class Band reference variable "c" is a type of class C. So Compilation fails.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. What is the output for the below code ?

public class C {
}
public class D extends C{
}
public class A {
public C getOBJ(){
System.out.println("class A - return C");
return new C();
}
}
public class B extends A{
public D getOBJ(){
System.out.println("class B - return D");
return new D();
}
}
public class Test {
public static void main(String... args) {
A a = new B();
a.getOBJ();
}
}
A. class A - return C B. class B - return D

C. Compilation fails D. Compilation succeed but no output

Answer and Explanation

Answer: class B - return D

Explanation:
From J2SE 5.0 onwards. return type in the overriding method can be same or subtype of the declared return type of the overridden (superclass) method.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What is the output for the below code ?

public class A {
private void printName(){
System.out.println("Value-A");
}
}
public class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) {
B b = new B();
b.printName();
}
}
A. Value-A B. Name-B
C. Value-A Name-B D. Compilation fails - private methods can't be override

Answer and Explanation

Answer: Name-B

Explanation:
You can not override private method , private method is not availabe in subclass . In this case printName() method a class A is not overriding by printName() method of class B. printName() method of class B different method. So you can call printName() method of class B.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What is the output for the below code ?

import java.io.FileNotFoundException;
public class A {
public void printName() throws FileNotFoundException {
System.out.println("Value-A");
}
}
public class B extends A{
public void printName() throws NullPointerException{
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) throws Exception{
A a = new B();
a.printName();
}
}
A. Value-A B. Compilation fails-Exception NullPointerException is not compatible with throws
clause in A.printName()
C. Name-B D. Compilation succeed but no output

Answer and Explanation

Answer: Name-B

Explanation:
The overriding method can throw any unchecked (runtime) exception, regardless of
exception thrown by overridden method. NullPointerException is RuntimeException so compiler not complain

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. What is the output for the below code ?

public class A {
public A(){
System.out.println("A");
}
public A(int i){
this();
System.out.println(i);
}
}
public class B extends A{
public B (){
System.out.println("B");
}
public B (int i){
this();
System.out.println(i+3);
}
}
public class Test{
public static void main (String[] args){
new B(5);
}
}
A. A B 8 B. A 5 B 8
C. A B 5 D. B 8 A 5

Answer and Explanation

Answer: A B 8

Explanation:
Constructor of class B call their superclass constructor of class A (public A()) , which
execute first, and that constructors can be overloaded. Then come to constructor of class B (public B (int i)).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. public class A {
public void test1(){
System.out.println("test1");
}
}
public class B extends A{
public void test2(){
System.out.println("test2");
}
}
1. public class Test{
2. public static void main (String[] args){
3. A a = new A();
4. A b = new B();
5. B b1 = new B();
6. // insert code here
7. }
8. }

Which of the following , inserted at line 6, will compile and print
test2?
A. ((B)b).test2(); B. (B)b.test2();
C. b.test2(); D. a.test2();

Answer and Explanation

Answer: ((B)b).test2();

Explanation:
((B)b).test2(); is proper cast. test2() method is in class B so need to cast b then only
test2() is accessible. (B)b.test2(); is not proper cast without the second set of parentheses,the compiler thinks it is an incomplete statement.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



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