Podcast
Questions and Answers
What modification would you have to make to this code for it to compile?
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 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));
- Throw a NullPointerException
- Print 'false' to the console (correct)
- Print 'true' to the console
- Compile error
If the Professor class extends the Teacher class, which of the following if statements will compile? Professor professor = new Professor(); Teacher teacher = new Teacher();
If the Professor class extends the Teacher class, which of the following if statements will compile? Professor professor = new Professor(); Teacher teacher = new Teacher();
- if (teacher instanceof Professor) {...}
- if (professor instanceof Object) {...} (correct)
- if (teacher instanceof Teacher) {...} (correct)
- if (professor instanceof Teacher) {...} (correct)
What keyword do you use in a class declaration to create a subclass?
What keyword do you use in a class declaration to create a subclass?
Which of the following statements is true?
Which of the following statements is true?
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; } }
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; } }
To include a class in a package, you
To include a class in a package, you
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?
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?
If the MotorVehicle class contains a protected method named getEngineType, what other class or classes can access this method?
If the MotorVehicle class contains a protected method named getEngineType, what other class or classes can access this method?
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; }
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; }
The attributes of an object are often coded as?
The attributes of an object are often coded as?
What happens if the user enters 'K' at the console prompt in the provided code example?
What happens if the user enters 'K' at the console prompt in the provided code example?
Which of the following classes should not inherit a class that defines an employee?
Which of the following classes should not inherit a class that defines an employee?
Which of the following is not a reason to declare a class as final?
Which of the following is not a reason to declare a class as final?
In what type of file is javadoc documentation stored?
In what type of file is javadoc documentation stored?
You can use NetBeans to generate the Java documentation for?
You can use NetBeans to generate the Java documentation for?
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;
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;
When a subclass inherits an abstract class, it must override all of the abstract methods in the abstract class.
When a subclass inherits an abstract class, it must override all of the abstract methods in the abstract class.
What happens when the following code is executed? Rental rental = new JetSkiRental(); rental.setTotal();
What happens when the following code is executed? Rental rental = new JetSkiRental(); rental.setTotal();
An enumeration inherits methods from which of the following classes?
An enumeration inherits methods from which of the following classes?
The method printDeposit
is a static method.
The method printDeposit
is a static method.
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?
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?
Which of the following is not an advantage of using a three-tiered architecture for your applications?
Which of the following is not an advantage of using a three-tiered architecture for your applications?
What class or classes is a method that is declared protected available to?
What class or classes is a method that is declared protected available to?
If the user enters 'C' at the console prompt, which class contains the setTotal method that is called?
If the user enters 'C' at the console prompt, which class contains the setTotal method that is called?
To use NetBeans to create a new class, you can right-click on the package for the class and select?
To use NetBeans to create a new class, you can right-click on the package for the class and select?
What happens when these statements are compiled? Loan loan = new Loan(amount, months); Object obj = loan; obj.calculatePayments();
What happens when these statements are compiled? Loan loan = new Loan(amount, months); Object obj = loan; obj.calculatePayments();
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();
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();
Generated Java documentation includes the code that implements each documented method.
Generated Java documentation includes the code that implements each documented method.
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?
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?
Which of the following can you not code in a subclass?
Which of the following can you not code in a subclass?
Which of the following can you not do with an enumeration?
Which of the following can you not do with an enumeration?
Which of the following is an advantage of coding two or more classes in the same file?
Which of the following is an advantage of coding two or more classes in the same file?
Which method is an example of overloading the method that follows public int parseNumber(String numberString){...}
?
Which method is an example of overloading the method that follows public int parseNumber(String numberString){...}
?
To determine an object's type, you can use?
To determine an object's type, you can use?
Which of the following methods can always be called from a Product object?
Which of the following methods can always be called from a Product object?
What is the name of the class that contains the following code? private Clock clock; private Alarm alarm; public AlarmClock(Date time){...}
What is the name of the class that contains the following code? private Clock clock; private Alarm alarm; public AlarmClock(Date time){...}
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; }
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; }
You must use the this keyword?
You must use the this keyword?
If a method accepts an Object object, what other types of objects can it accept?
If a method accepts an Object object, what other types of objects can it accept?
In the directory structure provided, what is the name of the package that contains the Invoice.java class?
In the directory structure provided, what is the name of the package that contains the Invoice.java class?
Which of the following is a valid javadoc comment?
Which of the following is a valid javadoc comment?
A class in the Java API can be?
A class in the Java API can be?
How many public classes can exist in a file?
How many public classes can exist in a file?
If the constructor that follows is in the Employee class, what other constructor, if any, is implicitly provided? public Employee(String name, int salary){...}
If the constructor that follows is in the Employee class, what other constructor, if any, is implicitly provided? public Employee(String name, int salary){...}
What happens when the statement Automobile car = new Automobile(1);
is executed?
What happens when the statement Automobile car = new Automobile(1);
is executed?
If the Loan class contains a final method named calculatePayment, what does the following code do? HomeLoan loan = new HomeLoan(amount, months); loan.calculatePayment();
If the Loan class contains a final method named calculatePayment, what does the following code do? HomeLoan loan = new HomeLoan(amount, months); loan.calculatePayment();
Where must you code the javadoc comment for a method?
Where must you code the javadoc comment for a method?
What is the difference between these two constructors? public Transaction(){...} public Transaction(int x, int y){...}
What is the difference between these two constructors? public Transaction(){...} public Transaction(int x, int y){...}
What feature allows you to treat an Admin object as though it were an Account object in this inheritance hierarchy?
What feature allows you to treat an Admin object as though it were an Account object in this inheritance hierarchy?
It can access instance variables and methods of the outer class.
It can access instance variables and methods of the outer class.
Which of the following is NOT a reason to create a package?
Which of the following is NOT a reason to create a package?
A subclass inherits?
A subclass inherits?
Which of the following is a benefit of using javadoc comments?
Which of the following is a benefit of using javadoc comments?
Which of the following is NOT true about static imports?
Which of the following is NOT true about static imports?
Which of the following method declarations overrides this method? double calculateMilesPerGallon(double speed){...}
Which of the following method declarations overrides this method? double calculateMilesPerGallon(double speed){...}
Which of the following statements creates an object of the Account class? Account account = new Account(111, "Checking", 0.0);
Which of the following statements creates an object of the Account class? Account account = new Account(111, "Checking", 0.0);
The values held by the attributes of an object describe the object's?
The values held by the attributes of an object describe the object's?
You can use the final keyword for?
You can use the final keyword for?
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; } }
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; } }
When using NetBeans, what must you do to use a class that's stored in a custom library?
When using NetBeans, what must you do to use a class that's stored in a custom library?
A static initialization block?
A static initialization block?
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");
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");
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++; } }
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++; } }
A method can operate directly on the data in a reference type that's passed to it and change that data.
A method can operate directly on the data in a reference type that's passed to it and change that data.
Which of the following is NOT true about an inner class?
Which of the following is NOT true about an inner class?
Explicit casting is always required to cast a superclass object to a subclass object.
Explicit casting is always required to cast a superclass object to a subclass object.
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; } }
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; } }
What class or classes is a variable available to if it is declared without an access modifier?
What class or classes is a variable available to if it is declared without an access modifier?
Each constant within an enumeration is assigned?
Each constant within an enumeration is assigned?
If the statement that follows is executed, which constructor will NOT be called? SavingsAccount account = new SavingsAccount();
If the statement that follows is executed, which constructor will NOT be called? SavingsAccount account = new SavingsAccount();
Which of the following is true about a method that has this declaration? public Employee getDepartmentManager(){...}
Which of the following is true about a method that has this declaration? public Employee getDepartmentManager(){...}
To compare the instance variables of two objects, you can?
To compare the instance variables of two objects, you can?
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); }
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); }
Flashcards are hidden until you start studying
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.