Questions and Answers
What modification would you have to make to this code for it to compile?
Declare the Person class as abstract
If the Point class doesn't override the equals method in the Object class, what will the following code do? Point pointOne = new Point(3, 4); int x = 3; int y = 4; Point pointTwo = new Point(x, y); System.out.println(pointOne.equals(pointTwo));
If the Professor class extends the Teacher class, which of the following if statements will compile? Professor professor = new Professor(); Teacher teacher = new Teacher();
What keyword do you use in a class declaration to create a subclass?
Signup and view all the answers
Which of the following statements is true?
Signup and view all the answers
What does the following code display? public class GradeCurveApp { public static void main(String[] args) { int grade = 95; GradeCurve curve = new GradeCurve(); curve.lowerGrade(grade); System.out.println(grade); } } public class GradeCurve { public void lowerGrade(int g) { g -= 5; } }
Signup and view all the answers
To include a class in a package, you
Signup and view all the answers
If the AbstractButton class is abstract and the JButton class inherits it, which of the following data from the AbstractButton class must be defined in the JButton class?
Signup and view all the answers
If the MotorVehicle class contains a protected method named getEngineType, what other class or classes can access this method?
Signup and view all the answers
What other constructor is also called when the following constructor is invoked: public Sailboat(String id, double length, int sails) { super(id, "sailboat", length); this.sails = sails; }
Signup and view all the answers
The attributes of an object are often coded as?
Signup and view all the answers
What happens if the user enters 'K' at the console prompt in the provided code example?
Signup and view all the answers
Which of the following classes should not inherit a class that defines an employee?
Signup and view all the answers
Which of the following is not a reason to declare a class as final?
Signup and view all the answers
In what type of file is javadoc documentation stored?
Signup and view all the answers
You can use NetBeans to generate the Java documentation for?
Signup and view all the answers
If the instance variables of the Employee class are declared as follows, which statement is most likely to be in the constructor? private String name; private Address address; private long employeeNumber;
Signup and view all the answers
When a subclass inherits an abstract class, it must override all of the abstract methods in the abstract class.
Signup and view all the answers
What happens when the following code is executed? Rental rental = new JetSkiRental(); rental.setTotal();
Signup and view all the answers
An enumeration inherits methods from which of the following classes?
Signup and view all the answers
The method printDeposit
is a static method.
Signup and view all the answers
In which of the following situations would it make the most sense to code a static method rather than a regular method in a class that defines a bank account?
Signup and view all the answers
Which of the following is not an advantage of using a three-tiered architecture for your applications?
Signup and view all the answers
What class or classes is a method that is declared protected available to?
Signup and view all the answers
If the user enters 'C' at the console prompt, which class contains the setTotal method that is called?
Signup and view all the answers
To use NetBeans to create a new class, you can right-click on the package for the class and select?
Signup and view all the answers
What happens when these statements are compiled? Loan loan = new Loan(amount, months); Object obj = loan; obj.calculatePayments();
Signup and view all the answers
Assume that all three classes in this inheritance hierarchy contain a calculateMaxSpeed method. Which class defines the method that is called when the statement is executed? MotorVehicle car = new Automobile(); car.calculateMaxSpeed();
Signup and view all the answers
Generated Java documentation includes the code that implements each documented method.
Signup and view all the answers
When you call the equals method in the String class without knowing how it's coded, you're taking advantage of what object-oriented concept?
Signup and view all the answers
Which of the following can you not code in a subclass?
Signup and view all the answers
Which of the following can you not do with an enumeration?
Signup and view all the answers
Which of the following is an advantage of coding two or more classes in the same file?
Signup and view all the answers
Which method is an example of overloading the method that follows public int parseNumber(String numberString){...}
?
Signup and view all the answers
To determine an object's type, you can use?
Signup and view all the answers
Which of the following methods can always be called from a Product object?
Signup and view all the answers
What is the name of the class that contains the following code? private Clock clock; private Alarm alarm; public AlarmClock(Date time){...}
Signup and view all the answers
If name
is a String instance variable, average
is a double instance variable, and numOfStudents
is a static int variable, why won't the following code compile? public Student(String s) { name = s; average = getAverage(name); numOfStudents++; } public double getAverage(String x) { numOfStudents++; double ave = StudentDB.getAverage(x); return ave; } public static void setAverage(double g) { average = g; }
Signup and view all the answers
You must use the this keyword?
Signup and view all the answers
If a method accepts an Object object, what other types of objects can it accept?
Signup and view all the answers
In the directory structure provided, what is the name of the package that contains the Invoice.java class?
Signup and view all the answers
Which of the following is a valid javadoc comment?
Signup and view all the answers
A class in the Java API can be?
Signup and view all the answers
How many public classes can exist in a file?
Signup and view all the answers
If the constructor that follows is in the Employee class, what other constructor, if any, is implicitly provided? public Employee(String name, int salary){...}
Signup and view all the answers
What happens when the statement Automobile car = new Automobile(1);
is executed?
Signup and view all the answers
If the Loan class contains a final method named calculatePayment, what does the following code do? HomeLoan loan = new HomeLoan(amount, months); loan.calculatePayment();
Signup and view all the answers
Where must you code the javadoc comment for a method?
Signup and view all the answers
What is the difference between these two constructors? public Transaction(){...} public Transaction(int x, int y){...}
Signup and view all the answers
What feature allows you to treat an Admin object as though it were an Account object in this inheritance hierarchy?
Signup and view all the answers
It can access instance variables and methods of the outer class.
Signup and view all the answers
Which of the following is NOT a reason to create a package?
Signup and view all the answers
A subclass inherits?
Signup and view all the answers
Which of the following is a benefit of using javadoc comments?
Signup and view all the answers
Which of the following is NOT true about static imports?
Signup and view all the answers
Which of the following method declarations overrides this method? double calculateMilesPerGallon(double speed){...}
Signup and view all the answers
Which of the following statements creates an object of the Account class? Account account = new Account(111, "Checking", 0.0);
Signup and view all the answers
The values held by the attributes of an object describe the object's?
Signup and view all the answers
You can use the final keyword for?
Signup and view all the answers
What will the following code do? public class Person { private String name; public Person(String n) { name = n; } public String getName() { return name; } public boolean equals(Object obj) { if (obj instanceof Person) { Person person2 = (Person)obj; if (name.equals(person2.getName())) return true; } return false; } public static void main(String[] args) { String name = "Alex Thomas"; Customer custOne = new Customer(name, "123456"); Customer custTwo = new Customer(name, "333"); System.out.println(custOne.equals(custTwo)); } } public class Customer extends Person { String number; public Customer(String name, String num) { super(name); number = num; } }
Signup and view all the answers
When using NetBeans, what must you do to use a class that's stored in a custom library?
Signup and view all the answers
A static initialization block?
Signup and view all the answers
Assume that only the AbstractButton class contains a setText method. Which class defines the method that is called when the statement is executed? JButton button = new JButton(); button.setText("Exit");
Signup and view all the answers
What does the following code display? public class RentalApp { public static void main(String[] args) { Rental r = new Rental(); r.setNumOfPersons(5); r.addPerson(r); System.out.println(r.getNumOfPersons()); } } public class Rental { private int numOfPersons; public int getNumOfPersons() { return numOfPersons; } public void setNumOfPersons(int numOfPersons) { this.numOfPersons = numOfPersons; } public void addPerson(Rental rental) { rental.numOfPersons++; } }
Signup and view all the answers
A method can operate directly on the data in a reference type that's passed to it and change that data.
Signup and view all the answers
Which of the following is NOT true about an inner class?
Signup and view all the answers
Explicit casting is always required to cast a superclass object to a subclass object.
Signup and view all the answers
What change do you have to make to the code that follows to get it to compile? public final class Trajectory { double velocity; void convertVelocityUnits() { velocity *= 1.2; } } public class FlightPath extends Trajectory { final Trajectory trajectory = new Trajectory(); final void computeFlightPath() { System.out.println("Computing Flight Path"); trajectory.velocity /= 1.2; } }
Signup and view all the answers
What class or classes is a variable available to if it is declared without an access modifier?
Signup and view all the answers
Each constant within an enumeration is assigned?
Signup and view all the answers
If the statement that follows is executed, which constructor will NOT be called? SavingsAccount account = new SavingsAccount();
Signup and view all the answers
Which of the following is true about a method that has this declaration? public Employee getDepartmentManager(){...}
Signup and view all the answers
To compare the instance variables of two objects, you can?
Signup and view all the answers
If the duesOwed instance variable in the code that follows is set in the setDuesOwed method, which of the following is NOT true about the setDuesOwed method? private String name; //instance variable private double duesOwed; //instance variable public Member(String s) { name = s; setDuesOwed(name); }
Signup and view all the answers
Study Notes
Programming Concepts and Java Language Features
- Code snippet displaying the
GradeCurveApp
, demonstrates that the variablegrade
remains95
since primitives are passed by value. - To include a class in a package, the first statement in the class file must be a package declaration.
- Only abstract methods from an abstract class must be defined in any inheriting subclass.
- Protected methods in a parent class can be accessed by subclasses and any class within the same package.
- Constructor chaining is evident; if a specific constructor is called, the superclass constructor is also invoked.
Object-Oriented Programming Principles
- Object attributes are generally represented as instance variables in a class.
- When an input of "K" is provided in a switch case scenario, the parent class method is invoked as default behavior.
- "Person" should not inherit from an Employee class because it does not represent an employee.
Java Development and Documentation
- Java documentation generated by Javadoc is stored in an HTML file format.
- NetBeans allows the generation of Java documentation for all packages and classes within a project.
- In a class constructor, an instance of another class (like Address) is likely initialized if it’s an attribute.
Abstract and Inheritance in Java
- All abstract methods in a superclass must be implemented in derived classes.
- A method declared protected can be accessed by classes within the same package and subclasses, enhancing encapsulation.
- Subclasses inherit public and protected members from parent classes, enabling code reuse.
Code Behavior and Error Handling
- If a method accepts an object type, it will work with any derived object type.
- Using the
instanceof
keyword can effectively check the object type during runtime. - If a
static method
is called from a subclass, it cannot access non-static instance variables from the parent class.
Java Class Structure and Accessibility
- A static inner class cannot directly access instance variables of the outer class.
- Classes that lack access modifiers are available to other classes within the same package.
- Constructors with the same name but different parameters present unique signatures, allowing method overloading.
Miscellaneous Java Facts
- Enums assign integer values starting from zero, automatically incrementing for subsequent constants.
- Utilizing the
this
keyword is essential when instance variables are shadowed by method parameters. - The
equals
method can be overridden to compare custom object instances based on specific attributes.
Development Practices
- Javadoc comments should be placed immediately before method declarations to enhance readability and accessibility of documentation.
- The final keyword can secure classes and methods from being altered or extended, safeguarding the intended functionality.
- Only one public class is allowed per Java file, ensuring consistent access and control over class visibility.
Summary of Key Points
- Understanding inheritance, encapsulation, polymorphism, and abstraction is vital in Java programming.
- Recognizing compile-time and runtime distinctions aids in effective error handling and debugging.
- Documenting code clearly using Javadoc can significantly help in conveying class functionalities to other developers.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers essential concepts in Java programming, including variable passing, package declarations, and object-oriented principles like inheritance and encapsulation. Test your understanding of Java's unique features and how they relate to object-oriented design.