60% found this document useful (5 votes)
6K views9 pages

Test Inheritance PDF

The document discusses a test on inheritance in Java that the user Vivek Kumar completed. The test had 6 multiple choice questions covering topics like inheritance relationships between classes, accessing variables and methods of parent classes, and constructor invocation order. Vivek answered all questions correctly and received full marks of 12 out of 12 with a grade of 100%.

Uploaded by

Ayushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
60% found this document useful (5 votes)
6K views9 pages

Test Inheritance PDF

The document discusses a test on inheritance in Java that the user Vivek Kumar completed. The test had 6 multiple choice questions covering topics like inheritance relationships between classes, accessing variables and methods of parent classes, and constructor invocation order. Vivek answered all questions correctly and received full marks of 12 out of 12 with a grade of 100%.

Uploaded by

Ayushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

 VIVEK KUMAR .

 Dashboard / Java / Inheritance, Polymorphism, Abstract class, Interface / Test Your Understanding - Inheritance

Started on Wednesday, 15 April 2020, 6:03 PM


State Finished
Completed on Wednesday, 15 April 2020, 6:06 PM
Time taken 3 mins 18 secs
Marks 12.00/12.00
Grade 100.00 out of 100.00
Feedback Congratulations!! You have passed by securing more than 80%
Question  VIVEK
The class Employee is declared in a package mainpack and the Derived classes PermanentEmployee and KUMAR .
1 TemporaryEmployee are declared in another package subordpack. The basicPay attribute should be accessed only by
Correct means of a derived class object.
Mark 1.00 out of How to ensure that the basicPay attribute is not accessed directly by the other classes in the subordpack?
1.00
Employee.java

package mainpack  ; 
public class Employee{

protected  int basicPay;


}
/*PermanentEmployee.java*/

package subordpack  ; 
public class PermanentEmployee extends Employee{
.....

//TemporaryEmployee.java

package subordpack  ; 
public class TemporaryEmployee extends Employee{
.....
}

For a child class that is residing in a package to access a variable of its parent that is residing in a different package,
the variable in the parent has to be declared "protected" so that it will be visible to all its children across packages. The
"protected" basicPay attribute is not accessed directly by the other classes in the subordpack, in our case.

The correct answer is:


The class Employee is declared in a package mainpack and the Derived classes PermanentEmployee and
TemporaryEmployee are declared in another package subordpack. The basicPay attribute should be accessed only by
means of a derived class object.
How to ensure that the basicPay attribute is not accessed directly by the other classes in the subordpack?

Employee.java
package [mainpack]; 
public class Employee{
[protected] int basicPay;
}
/*PermanentEmployee.java*/
package [subordpack]; 
public class PermanentEmployee extends Employee{
.....

//TemporaryEmployee.java
package [subordpack]; 
public class TemporaryEmployee extends Employee{
.....
}
Question  VIVEK KUMAR .
2 Constructor of the superclass can   be invoked by from its subclass.

Correct
A default member of a super class in one package cannot  be accessed in its own subclass which is in a
Mark 1.00 out of
1.00 different package.

A protected member of a super class in one package can  be accessed by its own subclass which is
in a different package.

Your answer is correct.

The correct answer is:

Constructor of the superclass [can] be invoked by from its subclass.

A default member of a super class in one package [cannot] be accessed in its own subclass which is in a different
package.

A protected member of a super class in one package [can] be accessed by its own subclass which is in a different
package.

Question
State True or False
3 Child class objects can be instantiated when the parent class constructor is protected
Correct

Mark 1.00 out of


1.00
Select one:
True 

False

The correct answer is 'True'.

Question Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships
4 between these classes 

Correct

Mark 1.00 out of


1.00 Select one:
a. IBMComputer is the superclass, AppleComputer and Computer are subclasses of IBMComputer.

b. Computer is the super class, AppleComputer and IBMComputer are subclasses of computer 

c. Computer is a superclass, AppleComputer is a subclasses of Computer, and IBMComputer is a sublclas of


AppleComputer

d. Computer, AppleComputer and IBMComputer are sibling classes.

Your answer is correct.


The correct answer is: Computer is the super class, AppleComputer and IBMComputer are subclasses of computer
Question
Interpret which of the following statements are correct with respect to inheritance relationship in VIVEK
java?     KUMAR .
5
Correct

Mark 1.00 out of


1.00
Select one or more:
 a. object of subclass referenced by super class type can access super class variables 

 b. object of subclass referenced by super class type can invoke overridden sub class methods 

 c. object of subclass referenced by super class type can invoke super class methods 

d. object of subclass referenced by super class type can access newly defined sub class variables

e. object of subclass referenced by super class type can invoke newly defined sub class methods

Your answer is correct.

The correct answers are: object of subclass referenced by super class type can invoke super class methods, object of
subclass referenced by super class type can invoke overridden sub class methods, object of subclass referenced by
super class type can access super class variables

Question
public class ArithmeticOperation{
6   private void add(int operand1, int operand2)
Correct  {
Mark 1.00 out of      System.out.println(operand1 + operand2);
1.00  }
}
public class Addition extends ArithmeticOperation
{
  public void show()
 {
     add(10, 12);
 }
   public static void main(String args[])
   {
     Addition ob = new Addition();
      ob.show();
   }
}

What will be the output of above code when compiled and executed?

Select one:
a. Runtime error as add method is not defined in MethodOverriding class

b. Compile time error 

c. Will compile and display 1012

d. Will compile and display 32

e. Will print false

The method add cannot be invoked from anywhere since it's declared private

The correct answer is: Compile time error


Question  VIVEK KUMAR .
Given:
7 class FourWheeler 
Correct { 
Mark 1.00 out of public FourWheeler () 
1.00 { 
System.out.print(1); 


class Car extends FourWheeler

public Car() 

System.out.print(2); 


class Audi extends Car

public Audi() 
{
System.out.print(3); 


public class Driver

public static void main( String[] argv ) 
{
new Audi(); 


What is the result when this code is executed?

Select one:
a. 123 

b. 321

c. 3

d. The code runs with no output

The first statement that always gets executed from within any constructor is super() which means the invocation of 
super class no-parameterized constructor.

FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens
from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized
constructor call of FourWheeler. Hence, the output "123".
The correct answer is: 123
Question  VIVEK KUMAR .
10. class FourWheeler {
8
11. private  void display() { }
Correct

Mark 1.00 out of 12. }


1.00
13. class Car extends FourWheeler {

14.  protected   void display() { }

15. }

Which method at line 14, will correctly complete class Car?

The visibility of a method can only be increased as we traverse down the hierarchy.

The correct answer is:

10. class FourWheeler {


11. [private] void display() { }
12. }

13. class Car extends FourWheeler {

14.  [protected ] void display() { }

15. }
Which method at line 14, will correctly complete class Car?
Question  VIVEK KUMAR .
What will be the output of the following program ?
9 class FourWheeler
Correct {
Mark 1.00 out of     public FourWheeler()
1.00     {
        System.out.println("Class FourWheeler");
    }
}
class Car extends FourWheeler
{
    public Car() 
    {
        System.out.println("Class Car");    
    }
}
class Audi extends Car
{
   public Audi()
    {
        super();
        System.out.println("Class Audi");
    }
}
class Driver
{
    public static void main(String args[])
    {
        Audi cc=new Audi();
    }
}

Select one:
a.
Class FourWheeler
Class Car
Class Audi 

b.
Class Audi
Class Car
Class FourWheeler

c.
Compile Time Error

d.
Exception occurs

The first statement that always gets executed from within any constructor is super() which means the invocation of 
super class no-parameterized constructor.
FourWheeler is the parent of Car and Car is the parent of Audi. The no-parameterized constructor call of Audi happens
from the driver class. Followed by the no-parameterized constructor call of Car. Followed by the no-parameterized
constructor call of FourWheeler. Hence, the output "Class Four WheelerClass Car Class Audi".

The correct answer is:


Class FourWheeler
Class Car
Class Audi
Question class FourWheeler{  VIVEK KUMAR .
10 protected FourWheeler getObject()
{
Correct
   //logic with return statement
Mark 1.00 out of
}
1.00
}
class Car extends FourWheeler
{
protected Car getObject() {}  
}
class Driver
{
public static void main(String args[])
{
    FourWheeler object = new Car();
    object.getObject();
}
}

 private FourWheeler getObject() {} Car getObject() {}

protected void getObject() {}

Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return
type should be sub-type of parent’s return type. The visibility of a method can only be increased as we traverse down
the hierarchy.

The correct answer is: class FourWheeler{


protected FourWheeler getObject()
{
   //logic with return statement
}
}
class Car extends FourWheeler
{
    [protected Car getObject() {} ]
}
class Driver
{
public static void main(String args[])
{
    FourWheeler object = new Car();
    object.getObject();
}
}
Question  VIVEK KUMAR .
Default Access  is the most restrictive access modifier that will allow members of one class to have access to
11 members of another class in the same package.
Correct

Mark 1.00 out of


1.00

Public Synchronized Protected Abstract

Your answer is correct.

The correct answer is:

[Default Access] is the most restrictive access modifier that will allow members of one class to have access to
members of another class in the same package.

Question
Given a method in a public class, private  access modifier must be used to restrict access to that
12 method to only the other members of the same class.
Correct

Mark 1.00 out of


1.00

Your answer is correct.


The correct answer is:
Given a method in a public class, [private] access modifier must be used to restrict access to that method to only the
other members of the same class.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy