Exams.RecruitmentIndia.in

A Learning Portal from Recruitment India

  • Home
  • Questions
    • Aptitude Questions
    • English Questions
    • Technical Questions
    • HR Interview Questions
    • Reasoning Questions
    • GK Questions
    • Current Affairs
    • Entrance Exams
  • IT Jobs By Location
    • Ahmedabad
    • Bangalore
    • Chandigarh
    • Chennai
    • Coimbatore
    • Delhi
    • Gurgaon
    • Hyderabad
    • Kolkata
    • Lucknow
    • Mumbai
    • Noida
    • Pune
    • Vizag
    • Vijaywada
  • Govt Jobs Portal
  • Online Tests Portal
  • Contact Us
New
Latest Private Jobs 2022 || Today Walkins || Latest Central Govt Jobs
New Recruitment India Job Notifications & Preparation Portal App || RecruitmentIndia Online Mock tests app
You are here: Home / Question and Answers

What will be the result of compiling and running the given code?

class A{
      int b=10;
      private A(){
            this.b=7;
      }
      int f(){
            return b;
      }
}
class B extends A{
      int b;
}
public class Test{
      public static void main(String[] args){
            A a = new B();
            System.out.println(a.f());
      }
}

A. 

Prints 7

B. 

Prints 10

C. 

Prints 0

D. 

Compilation fails

Answer
Workspace
Report
Discuss

Answer with explanation

Answer: Option D

Explanation

The code does not compile because the constructor of class A is declared as private. This creates a problem when the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated.

Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the output would have been 7.

Workspace

Discuss about the question Cancel reply

Search

State Wise Govt Jobs

  • Andhra Pradesh Govt Jobs
  • Arunachal Pradesh Govt Jobs
  • Assam Govt Jobs
  • Bihar Govt Jobs
  • Chattishgarh Govt Jobs
  • Goa Govt Jobs
  • Gujarat Govt Jobs
  • Haryana Govt Jobs
  • Himachal Pradesh Govt Jobs
  • Jammu and Kashmir Govt Jobs
  • Jharkhand Govt Jobs
  • Karnataka Govt Jobs
  • Kerala Govt Jobs
  • Madhya Pradesh Govt Jobs
  • Maharashtra Govt Jobs
  • Tripura Govt Jobs
  • Uttar Pradesh Govt Jobs
  • Uttarakhand Govt Jobs
  • Manipur Govt Jobs
  • Meghalaya Govt Jobs
  • Mizoram Govt Jobs
  • Nagaland Govt Jobs
  • Odisha Govt Jobs
  • Punjab Govt Jobs
  • Rajasthan Govt Jobs
  • Sikkim Govt Jobs
  • Tamilnadu Govt Jobs
  • Telangana Govt Jobs
  • West Bengal Govt Jobs

Advertisement

Useful Links

  • About Us
  • Privacy Policy
  • Disclaimer Policy
  • Contact Us

Our Android Applications

  • Preparation & Job Updates App
  • Download Online Mock Tests App

Copyright © 2016 -2022 Exams Recruitment India || Product from Recruitment India Made with ❤ in India.