Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Quiz About Java Inheritance

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Quiz About Java Inheritance

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Inheritance Trending in News View More

105 Funny Things to Do to Make


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top Picks for
Expanding Your Storage
Which of the following is true about inheritance in Java?
Best Nintendo Switch Controllers in
2024
1) Private methods are final. #geekstreak2024 – 21 Days POTD
2) Protected members are accessible within a package and Challenge Powered By Deutsche Bank
inherited classes outside the package. Full Stack Developer Roadmap [2024
3) Protected methods are final. Updated]
4) We cannot override private methods.

1, 2 and 4

Only 1 and 2

1, 2 and 3

2, 3 and 4

Discuss it

Question 1 ‒ Explanation
See http://www.geeksforgeeks.org/can-override-private-methods-java/ and htt
p://www.geeksforgeeks.org/comparison-of-inheritance-in-c-and-java/

Question 2

Output of following Java program?

Java

class Base {
public void Print() {
System.out.println("Base");
}
}

class Derived extends Base {


public void Print() {
System.out.println("Derived");
}
}

class Main{
public static void DoPrint( Base o ) {
o.Print();
}
public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}

Base
Derived
Derived

Base
Base
Derived

Base
Derived
Base

Compiler Error

Discuss it

Question 2 ‒ Explanation
See question 1 of
http://www.geeksforgeeks.org/output-of-java-program-set-2/

Question 3

Predict the output of following program. Note that fun() is public in base and
private in derived.

Java

class Base {
public void foo() { System.out.println("Base"); }
}

class Derived extends Base {


private void foo() { System.out.println("Derived"); }
}

public class Main {


public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}

Base

Derived

Compiler Error

Runtime Error

Discuss it
Question 3 ‒ Explanation
It is compiler error to give more restrictive access to a derived class function w
hich overrides a base class function.

Question 4

Predict the output of following Java Program

Java

// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}

class Parent extends Grandparent {


public void Print() {
System.out.println("Parent\'s Print()");
}
}

class Child extends Parent {


public void Print() {
super.super.Print();
System.out.println("Child\'s Print()");
}
}

public class Main {


public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

Compiler Error in super.super.Print()

Grandparent\'s Print()
Parent\'s Print()
Child\'s Print()

Runtime Error

Discuss it

Question 4 ‒ Explanation
In Java, it is not allowed to do super.super. We can only access Grandparent\'s
members using Parent. For example, the following program works fine.

// Guess the output


// filename Main.java
class Grandparent {
public void Print() {
System.out.println("Grandparent\'s Print()");
}
}
class Parent extends Grandparent {
public void Print() {
super.Print();
System.out.println("Parent\'s Print()");
}
}

class Child extends Parent {


public void Print() {
super.Print();
System.out.println("Child\'s Print()");
}
}

class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

Question 5

Output of following Java Program?

Java

class Base {
public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

public class Main {


public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

Derived::show() called

Base::show() called

Discuss it

Question 5 ‒ Explanation
In the above program, b is a reference of Base type and refers to an abject of De
rived class. In Java, functions are virtual by default. So the run time polymorph
ism happens and derived fun() is called.

Question 6

Which of the following is true about inheritance in Java. 1) In Java all classes
inherit from the Object class directly or indirectly. The Object class is root of all
classes. 2) Multiple inheritance is not allowed in Java. 3) Unlike C++, there is
nothing like type of inheritance in Java where we can specify whether the
inheritance is protected, public or private.

1, 2 and 3

1 and 2

2 and 3

1 and 3

Discuss it

Question 6 ‒ Explanation
See Comparison of Inheritance in C++ and Java

Question 7

final class Complex {

private final double re;


private final double im;

public Complex(double re, double im) {


this.re = re;
this.im = im;
}

public String toString() {


return "(" + re + " + " + im + "i)";
}
}

class Main {
public static void main(String args[])
{
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}

Complex number is (10.0 + 15.0i)


Compiler Error

Complex number is SOME_GARBAGE

Complex number is Complex@8e2fb5


Here 8e2fb5 is hash code of c

Discuss it

Question 7 ‒ Explanation
See http://www.geeksforgeeks.org/overriding-tostring-method-in-java/

Question 8

Java

class Base {
final public void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}

Base::show() called

Derived::show() called

Compiler Error

Runtime Error

Discuss it

Question 8 ‒ Explanation
Final methods cannot be overridden. See the compiler error
here
.

Question 9
\Java\

class Base {
public static void show() {
System.out.println("Base::show() called");
}
}

class Derived extends Base {


public static void show() {
System.out.println("Derived::show() called");
}
}

class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

Base::show() called

Derived::show() called

Compiler Error

Discuss it

Question 9 ‒ Explanation
Like C++, when a function is static, runtime polymorphism doesn\'t happen.

You have completed 9/9 questions .


Your accuracy is 55%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion


Company Languages DSA Data Web Python
About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Contact Us PHP DSA for JavaScript Examples
With Python
Pradesh (201305) | Registered Advertise with GoLang Beginners TypeScript Python Projects
Address:- K 061, Tower K, Data Science
us SQL Basic DSA ReactJS Python Tkinter
Gulshan Vivante Apartment, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Sector 137, Noida, Gautam Solution
Machine
Buddh Nagar, Uttar Pradesh, Android DSA Bootstrap OpenCV Tutorial
Learning
201305 Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like