Overriding And Overloading(Java Program ) Questions and Answers

Question 1. What will be printed after executing following program code?
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test{
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
  1.    10
  2.    20
  3.    30
  4.    40
  5.    None of these
Explanation:-
Answer: Option D. -> 40

Question 2. What will be the output?
interface A{
public void method1();
}
class One implements A{
public void method1(){
System.out.println("Class One method1");
}
}
class Two extends One{
public void method1(){
System.out.println("Class Two method1");
}
}
public class Test extends Two{
public static void main(String[] args){
A a = new Two();
a.method1();
}
}
  1.    Class One method1
  2.    Class Two method1
  3.    Nothing will be printed
  4.    Compilation Error
Explanation:-
Answer: Option B. -> Class Two method1

Question 3. What is the result of the following piece of code:
public class Person{
public void talk(){
System.out.print("I am a Person");
}
}
public class Student extends Person{
public void talk(){
System.out.print("I am a Student");
}
}
public class Test{
public static void main(String args[]){
Person p = new Student();
p.talk();
}
}
  1.    I am a Person
  2.    I am a Student
  3.    I am a Person I am a Student
  4.    I am a Student I am a Person
Explanation:-
Answer: Option B. -> I am a Student

Question 4. What will be the output of the following program code?
class Rectangle{
public int area(int length, int width){
return length*width;
}
}
class Square extends Rectangle{
public int area(long length, long width){
return (int) Math.pow(length, 2);
}
}
public class Test{
public static void main(String args[]){
Square r = new Square();
System.out.println(r.area(5 , 4));
}
}
  1.    Will not compile.
  2.    Will compile and run printing out 20
  3.    Runtime error
  4.    Will compile and run printing out 25
Explanation:-
Answer: Option B. -> Will compile and run printing out 20

Question 5. What is output of the program?
class Test{
public void display(int x, double y){
System.out.println(x+y);
}
public double display(int p, double q){
return (p+q);
}
public static void main(String args[]){
Test test = new Test();
test.display(4, 5.0);
System.out.println(test.display(4, 5.0));
}
}
  1.    9.0 9.0
  2.    9 9
  3.    Compilation Error
  4.    None of these
Explanation:-
Answer: Option C. -> Compilation Error

Question 6. ____________ method cannot be overridden.
  1.    super
  2.    static
  3.    final
  4.    private
  5.    None of these
Explanation:-
Answer: Option C. -> final

Question 7. What will be the output?
class A{
int i = 10;
public void printValue(){
System.out.print("Value-A");
}
}
class B extends A{
int i = 12;
public void printValue(){
System.out.print("Value-B");
}
}
public class Test{
public static void main(String args[]){
A a = new B();
a.printValue();
System.out.print(a.i);
}
}
  1.    Value-B 11
  2.    Value-B 10
  3.    Value-A 10
  4.    Value-A 11
  5.    None of these
Explanation:-
Answer: Option B. -> Value-B 10
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.

Question 8. Determine output:
class A{
public void printValue(){
System.out.println("Value-A");
}
}
class B extends A{
public void printNameB(){
System.out.println("Name-B");
}
}
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. }
  1.    Value-A Name-B
  2.    Value-A Value-A
  3.    Value-A Name-C
  4.    Name-B Name-C
  5.    None of these
Explanation:-
Answer: Option B. -> Value-A Value-A
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.

Question 9. What is the output for the below code?
public class Test{
public static void printValue(int i, int j, int k){
System.out.println("int");
}
public static void printValue(byte...b){
System.out.println("long");
}
public static void main(String... args){
byte b = 9;
printValue(b,b,b);
}
}
  1.    long
  2.    int
  3.    Compilation fails
  4.    Compilation clean but throws RuntimeException
  5.    None of these
Explanation:-
Answer: Option B. -> int
Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler choose method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than widen.

Question 10. What will be the output?
class A{
static void method(){
System.out.println("Class A method");
}
}
class B extends A{
static void method(){
System.out.println("Class B method");
}
}
public class Test{
public static void main(String args[]){
A a = new B();
a.method();
}
}
  1.    Class A method
  2.    Class B method
  3.    Compilation Error
  4.    Runtime Error
  5.    None of these
Explanation:-
Answer: Option A. -> Class A method
Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type. But in the above case the methods are static which means access to them is always resolved during compile time only using the compile time type information. Accessing them using object references is just an extra liberty given by the designers of Java.