Java Programming Concepts and OOP Principles
74 Questions
100 Views

Java Programming Concepts and OOP Principles

Created by
@GlisteningRadon

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));

  • 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 (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?

    <p>extends</p> Signup and view all the answers

    Which of the following statements is true?

    <p>Abstract classes can contain final methods.</p> 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; } }

    <p>95</p> Signup and view all the answers

    To include a class in a package, you

    <p>code a package statement as the first statement in the class file</p> 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?

    <p>Only abstract methods</p> 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?

    <p>Automobile and Motorcycle</p> 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; }

    <p>public WaterCraft(String id, String type, double length){...}</p> Signup and view all the answers

    The attributes of an object are often coded as?

    <p>instance variables</p> Signup and view all the answers

    What happens if the user enters 'K' at the console prompt in the provided code example?

    <p>The setTotal method in the Rental class is called.</p> Signup and view all the answers

    Which of the following classes should not inherit a class that defines an employee?

    <p>Person</p> Signup and view all the answers

    Which of the following is not a reason to declare a class as final?

    <p>To give a class more functionality</p> Signup and view all the answers

    In what type of file is javadoc documentation stored?

    <p>html file</p> Signup and view all the answers

    You can use NetBeans to generate the Java documentation for?

    <p>all packages and classes in the project</p> 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;

    <p>address = new Address();</p> 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.

    <p>True</p> Signup and view all the answers

    What happens when the following code is executed? Rental rental = new JetSkiRental(); rental.setTotal();

    <p>The setTotal method in the JetSkiRental is called.</p> Signup and view all the answers

    An enumeration inherits methods from which of the following classes?

    <p>Both A and B</p> Signup and view all the answers

    The method printDeposit is a static method.

    <p>False</p> 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?

    <p>a method that changes the annual interest rate for all bank accounts</p> Signup and view all the answers

    Which of the following is not an advantage of using a three-tiered architecture for your applications?

    <p>It makes it easier to document the tasks performed by an application.</p> Signup and view all the answers

    What class or classes is a method that is declared protected available to?

    <p>All classes in the same package and to subclasses</p> Signup and view all the answers

    If the user enters 'C' at the console prompt, which class contains the setTotal method that is called?

    <p>CanoeRental</p> 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?

    <p>the New -&gt; Java Class command</p> Signup and view all the answers

    What happens when these statements are compiled? Loan loan = new Loan(amount, months); Object obj = loan; obj.calculatePayments();

    <p>A compile-time error occurs because the Object object can't invoke the calculatePayments method.</p> 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();

    <p>Automobile</p> Signup and view all the answers

    Generated Java documentation includes the code that implements each documented method.

    <p>False</p> 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?

    <p>encapsulation</p> Signup and view all the answers

    Which of the following can you not code in a subclass?

    <p>a call to a private method of the superclass</p> Signup and view all the answers

    Which of the following can you not do with an enumeration?

    <p>Use an integer in place of a constant defined by an enumeration</p> Signup and view all the answers

    Which of the following is an advantage of coding two or more classes in the same file?

    <p>You have fewer Java files to manage.</p> Signup and view all the answers

    Which method is an example of overloading the method that follows public int parseNumber(String numberString){...}?

    <p>public int parseNumber(String numberString, String entry){...}</p> Signup and view all the answers

    To determine an object's type, you can use?

    <p>Both A and B</p> Signup and view all the answers

    Which of the following methods can always be called from a Product object?

    <p>toString</p> 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){...}

    <p>AlarmClock</p> 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; }

    <p>The setAverage method can't access the average instance variable.</p> Signup and view all the answers

    You must use the this keyword?

    <p>to refer to an instance variable from a method when it has the same name as a method parameter.</p> Signup and view all the answers

    If a method accepts an Object object, what other types of objects can it accept?

    <p>All objects</p> Signup and view all the answers

    In the directory structure provided, what is the name of the package that contains the Invoice.java class?

    <p>murach.business</p> Signup and view all the answers

    Which of the following is a valid javadoc comment?

    <p>/** Comment */</p> Signup and view all the answers

    A class in the Java API can be?

    <p>both a superclass and a subclass</p> Signup and view all the answers

    How many public classes can exist in a file?

    <p>1</p> 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){...}

    <p>none</p> Signup and view all the answers

    What happens when the statement Automobile car = new Automobile(1); is executed?

    <p>An object of the Automobile class is created.</p> 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();

    <p>Calls the calculatePayment method in the Loan class</p> Signup and view all the answers

    Where must you code the javadoc comment for a method?

    <p>Immediately before the method</p> Signup and view all the answers

    What is the difference between these two constructors? public Transaction(){...} public Transaction(int x, int y){...}

    <p>They have different signatures</p> 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?

    <p>polymorphism</p> Signup and view all the answers

    It can access instance variables and methods of the outer class.

    <p>False</p> Signup and view all the answers

    Which of the following is NOT a reason to create a package?

    <p>False</p> Signup and view all the answers

    A subclass inherits?

    <p>public and protected data</p> Signup and view all the answers

    Which of the following is a benefit of using javadoc comments?

    <p>They make it easy for other programmers to learn about your class.</p> Signup and view all the answers

    Which of the following is NOT true about static imports?

    <p>They often make your code easier to read.</p> Signup and view all the answers

    Which of the following method declarations overrides this method? double calculateMilesPerGallon(double speed){...}

    <p>double calculateMilesPerGallon(double s){...}</p> 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);

    <p>Account account = new Account(111, &quot;Checking&quot;, 0.0);</p> Signup and view all the answers

    The values held by the attributes of an object describe the object's?

    <p>state</p> Signup and view all the answers

    You can use the final keyword for?

    <p>All of the above</p> 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; } }

    <p>Print 'true' to the console</p> Signup and view all the answers

    When using NetBeans, what must you do to use a class that's stored in a custom library?

    <p>Both b and c (b. Code an import statement for the class and c. Add the library to your project.)</p> Signup and view all the answers

    A static initialization block?

    <p>Is used to initialize a static variable that can't be initialized in the declaration</p> 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");

    <p>AbstractButton</p> 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++; } }

    <p>6</p> 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.

    <p>True</p> Signup and view all the answers

    Which of the following is NOT true about an inner class?

    <p>False</p> Signup and view all the answers

    Explicit casting is always required to cast a superclass object to a subclass object.

    <p>True</p> 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; } }

    <p>Remove the final keyword from the Trajectory class</p> Signup and view all the answers

    What class or classes is a variable available to if it is declared without an access modifier?

    <p>All classes in the same package</p> Signup and view all the answers

    Each constant within an enumeration is assigned?

    <p>an integer value beginning with zero</p> Signup and view all the answers

    If the statement that follows is executed, which constructor will NOT be called? SavingsAccount account = new SavingsAccount();

    <p>public Account(int number, double balance){...}</p> Signup and view all the answers

    Which of the following is true about a method that has this declaration? public Employee getDepartmentManager(){...}

    <p>It returns an object of the Employee class</p> Signup and view all the answers

    To compare the instance variables of two objects, you can?

    <p>override the equals method of the Object class</p> 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); }

    <p>False</p> Signup and view all the answers

    Study Notes

    Programming Concepts and Java Language Features

    • Code snippet displaying the GradeCurveApp, demonstrates that the variable grade remains 95 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.

    Quiz Team

    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.

    Use Quizgecko on...
    Browser
    Browser