What must a subclass do to modify a private superclass

Assignment Help JAVA Programming
Reference no: EM13940050

QUESTION 1

public abstract class Account{
public abstract boolean open(double);
}
public class Checking extends Account{
public boolean open (double bal){
}
}

In the above code, what happens if open() is not declared in each subclass of Account?

Question 2

public class A{
private int i;
public void setI(int j){
i = j;
}

public class B{
private A a;
public A getA(){ return a; }
}

How do you access the value of i from the B class?

Question 3

Given the following code:

public class Exam {
private int [] list;
private int num;
Exam () { list = new int[100];}
public void addItem (int i)
{ list[num++] = i; }

// insert method here
}

Which of the following may be a violation of information hiding if inserted for the comment above?

Answer

A public int[] getList(){ return list; }

B None of the above

C public int[] getList(){int[] copy=new int[list.length]; for(int i = 0;i<num; i++) copy[i]=list[i]; return copy;}

D public int getAListValue (int index){ return list[index]; }

Question 4

Which of the following is true regarding subclasses?

Answer

A A subclass that inherits methods from its superclass may not override the methods.

B A subclass that inherits instance variables from its superclass may not declare additional instance variables.

C A subclass may inherit methods or instance variables from its superclass but not both.

D A subclass inherits methods and instance variables from its superclass, and may also implement its own methods and declare its own instance variables.

Question 5

Which of the following statements about an interface is true?

Answer

A An interface has instance variables but no methods.

B An interface has methods but no instance variables.

C An interface has neither methods nor instance variables.

D An interface has methods and instance variables.

Question 6

Which statement is true about a class that is marked with the keyword final:

Answer

A A class marked final will not compile unless at least one method is marked final

B You can instantiate only a subclass of a final class, not the final class itself

C You can extend a final class

D You can instantiate a final class

Question 7

What must a subclass do to modify a private superclass instance variable?

Answer

A The subclass must simply use the name of the superclass instance variable.

B The subclass must declare its own instance variable with the same name as the superclass instance variable.

C The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable.

D The subclass must have its own public method to update the superclass's private instance variable.

Question 8

Consider the following class hierarchy:

public class Vehicle{
private String type;
public Vehicle(String type)
{
this.type = type; }
public String getType()
{
return type;
}
}
public class LandVehicle extends Vehicle
{
public LandVehicle(String type)
{
. . .

}

}
public class Auto extends LandVehicle
{
public Auto(String type)
{
. . .
}
}

Which of the following code fragments is NOT valid in Java?

Answer

A LandVehicle myAuto = new Auto("sedan");

B LandVehicle myAuto = new Vehicle("sedan");

C Auto myAuto = new Auto("sedan");

D Vehicle myAuto = new Auto("sedan");

Question 9

Consider the following code snippet:

Employee anEmployee = new Programmer();
anEmployee.increaseSalary(2500);

Assume that the Programmer class inherits from the Employee class, and both classes have an implementation of the increaseSalary method with the same set of parameters and the same return type. Which class's increaseSalary method is to be executed is determined by ____.

Answer

A the variable's type.

B it is not possible to determine which method is executed.

C the hierarchy of the classes.

D the actual object type.

Question 10

Consider the following code snippet:

if (anObject instanceof Auto)

{

Auto anAuto = (Auto) anObject;
. . .

}

What does this code do?

Answer

A This code tests whether anObject was created from a superclass of Auto.

B This code creates a subclass type object from a superclass type object.

C This code safely converts an object of type Auto or a subclass of Auto to an object of type Auto.

D This class safely converts an object of any type to an object of type Auto.

Question 11

Consider the following code snippet:

public void deposit(double amount)

{
transactionCount ++;

super.deposit(amount);

}

Which of the following statements is true?

Answer

A This method will call itself.

B This method calls a public method in its superclass.

C This method calls a public method in its subclass.

D This method calls a private method in its superclass

Question 12

If a super class named Pet and a sub class named Cat both have an instance method named methodOne() with the same method declarations, the Cat class would call methodOne() in the Pet class as follows:

Answer

A this.methodOne();

B Pet.methodOne();

C Cat.methodOne();

D super.methodOne();

E methodOne();

Question 13

Consider the following code snippet:

public class Motorcycle extends Vehicle
{
private String model;
. . .

public Motorcycle(int numberAxles, String modelName)
{
model = modelName;
super(numberAxles);

}
}

What does this code do?

Answer

A It invokes a private method of the Vehicle class from within a method of the Motorcycle class.

B It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class.

C This code will not compile.

D It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.

Question 14

Select any statement in below that will generate an error if it replaces the comment

public class Person {
public String toString() { return "Person"; }
}

public class Student extends Person {
public String toString() { return "Student"; } }

public class GraduateStudent extends Student { }

public class Test {
public static void m(Student x) {
System.out.println(x.toString()); }

public static void main(String[ ] args) {

// Replace this comment with given statement
}
}

Answer

A m(new GraduateStudent());

B m(new Object());

C m(new Person());

D m(new Student());

Question 15

Fill in the blank to indicate either the type of error (be specific!) or the output produced by the code : var.m();

public class A {
public void m() {System.out.println ("m in class A");}

}
public class B extends A {
public void m() {System.out.println ("m in class B");}
}
public class Main {

public static void main (String [] args)

{

Object var = new B();

var.m();

// ____________________________

}
}

Answer

Question 16

Fill in the blank to indicate either the type of error (be specific!) or the output produced by the code: ((A)var).m();

public class A {
public void m() {System.out.println ("m in class A");}
}
public class B extends A {
public void m() {System.out.println ("m in class B");}
}
public class Main { public static void main (String [] args)
{ Object var = new B();

((A)var).m(); // ____________________________

}
}

Question 17

If a method in a subclass has the same signature as a method in the superclass, does the subclass method overload or override the superclass method?

Question 18

Given the following 3 data definition classes that will provide structure for a application to manage inventory at a store that sells Produce and Grocery items:

public abstract class Item {
String name; double cost;
public static final double TAX = .05;
public String getName() {return name;}
public double getCost() {return cost;}
public void setName(String n) { name = n;}
public boolean setCost (double c){
cost = c;
return (c > 0 && c < 100) ? true : false;

}

abstract public double amount() ;
public double total() {return amount() * (1+TAX); }
abstract public boolean sellItem (double amt) ;

}
public class Grocery extends Item {
private int quantity;
private static int numItems = 0;
public static int getNumItems() { return numItems; }
public boolean setQty (int q)

{

quantity = q;
return (q > 0 && q < 100) ? true : false;

}

public double amount () {return quantity * getCost();}
public boolean sellItem (double amt)

{

if (amt > quantity)

return false;

quantity -= (int) (amt+ .001);

return true;
}

Grocery () {numItems++;}

}

public class Produce extends Item {
private double weight;
private static int numItems = 0;
public static int getNumItems() { return numItems; }
public boolean setWeight(double w)

{

weight = w;
return (w > 0 && w < 10) ? true : false;

}

public double amount () {return weight * getCost(); }
public boolean sellItem (double amt)

{

if (amt > weight) return false;

weight -= amt;

return true;

} Produce () {numItems++;}

}

Give an example (by a short code segment and explanation) of how polymorphism would be implemented.

Question 19

A Java method doing Input/Output or other tasks may encounter errors that generate exceptions. In these cases, the Java method may wish to handle the exception itself.

If a Java method wants to handle an exception, how does it do this? Augment your answer with some Java code segments, to illustrate your explanation.

Solution Preview

Question 1

When a class extends an abstract class, it must provide definition of all the methods that are declared in the abstract class. Otherwise, it gives a compile time error.

Question 2

Private members can only be accessed inside the class, they are defined. So, in this case, you cannot access variable "i" from class B.

Question 3

C

Question 4

D

Question 5

B

Question 6

C

Question 7

C

Question 8

B

Question 9

D

Question 10

C

Question 11

B

Question 12

D

Question 13

D

Question 14

B and C

Question 15

The method m() is undefined for the type Object

Question 16

The output produced by the code is : m in class B

Question 17

If a method in a subclass has the same signature as a method in the superclass,the subclass method overrides the superclass method.
To overload a method with a new method in subclass, the new method in subclass should have different signature.

Question 18

From ...

Reference no: EM13940050

Questions Cloud

Create a number of security threats : "Footprinting is considered to be the most challenging but possibly the most important step during an attempt to carry out an intrusion". Critically discuss the above statement.
Clean zone of the treatment room : Explain why dental materials and medicament bottles should be stored in the clean zone of the treatment room and not left on the bench top of the working area.
How does the reduction in fixed cost affect break-even point : Compute the break-even point in units. How does the reduction in fixed costs affect the break-even point? Operating income? The margin of safety?
What information does ratio analysis provide for meeting : Referencing the list of ratios from your assigned readings for this week, respond to the following: What information does ratio analysis provide for meeting the requirements of the scenario? Which ratios are the most important, and which ones are of ..
What must a subclass do to modify a private superclass : When a class extends an abstract class, it must provide definition of all the methods that are declared in the abstract class. Otherwise, it gives a compile time error.
Awareness of the importance of decision making : Has what you have learned in this subject created an increased awareness of the importance of decision making as a management activity? Why or why not?
What are the implications of managing quality at delta : Would statistical process control tools such as control charts or acceptance sampling be enough to resolve quality shortcomings along all of those dimensions?
Calculate the contribution margin ratio : Compute the contribution margin per unit, and calculate the break-even point in units (round to the nearest unit). Calculate the contribution margin ratio and the break-even sales revenue.
Regulatory provisions and corporate approval requirements : What advice will you give Mike Chalkley about the takeover defence strategies that he can implement to protect Small Bank PLC? Please include in your answer a discussion of the various takeover defence mechanisms and how they are implemented. You ..

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a program that will calculate monthly pay

Write a program that will calculate monthly pay and expenses for a commercial utility sales person. Sales employees in this company are paid monthly. Each employee is paid a base pay of $1,000 and 9½ % commission on sales. The company deducts 18% of..

  Create a sales tracking program named salestracking.java

create a sales tracking program named SalesTracking.java. This program must track monthly sales as well as compute average yearly sales, total sales for the year, and which month had the highest sales and which month had the lowest sales.

  Write a general package for reading in arithmetic expression

Write a general package for reading in arithmetic expressions and simplifying them. Write a program for a simple calculating language with two forms of statement.

  Loops and files

Convert an algorithm using control structures into Java and write a while loop

  Display a table of values

Using Netbeans, use repetition to display a table of values showing x, the square of x and the cube of x. X is to go up to 5.

  Write java program to accept two words as input

Write a Java program that accepts two words as input and determines if one of them is resulting from changing the order of the others' letters.

  Specify, design, and implement a class

Specify, design, and implement a class that can be used in a program that simulates a combination lock. The lock has a circular knob with the numbers 0 through 39 marked on the edge, and it has a three-number combination, which we will call x,y,z.

  Compare string values

Write a program that reads two strings from the keyboard and compares them and prints whether they are equal or not.

  What would be an incorrect way of writing this equation

Jim develops 5 Java applications a year. Joe develops 10 Java applications a year. Jim gets paid $5000.00 per application, but Joe gets paid $10000.00 per application.

  What is the full path the to location of the web application

suppose that you are creating a java web application named "cset-test" consisting of a JSP file named "main.jsp" , and a java Servlet in a file named "InfoServlet.java". The user's home directory is /home/jdoe.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  How can i stop tomcat from spilling off its own detail file

I've setup Tomcat 3.1 with Apache on my web server to serve JSP pages. How can I stop tomcat from spilling off its own detail file path on the server.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd