whole pdf
42 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the + sign indicate in the context of the TV class?

  • The method is private
  • The method has a return type
  • The method is public (correct)
  • The method is static
  • What is the maximum channel number that can be set for the TV object?

  • 100
  • 120 (correct)
  • 150
  • 200
  • Which method would you call to increase the current volume level of a TV object by one?

  • volumeUp() (correct)
  • adjustVolume()
  • setVolume()
  • increaseVolume()
  • If you wanted to turn off a TV object, which method would you use?

    <p>turnOff()</p> Signup and view all the answers

    In the context of the TV class, how would you denote the current state of the TV being on?

    <p>on: boolean</p> Signup and view all the answers

    What does the notation 'radius: double' represent in a UML class diagram?

    <p>A data field with its type specified</p> Signup and view all the answers

    Which method in the Circle class is responsible for calculating the area?

    <p>getArea(): double</p> Signup and view all the answers

    How are circle objects represented in the UML notation?

    <p>With the class name followed by their properties</p> Signup and view all the answers

    What is demonstrated by the constructor 'Circle(newRadius: double)' in the Circle class?

    <p>It initializes a new Circle object with a specified radius</p> Signup and view all the answers

    What is the purpose of the 'setRadius(newRadius: double): void' method in the Circle class?

    <p>To change the radius of the circle to a new value</p> Signup and view all the answers

    How many Circle objects were created in the example above?

    <p>Three Circle objects</p> Signup and view all the answers

    Which notation indicates that a method has a return type in the UML class diagram?

    <p>methodName(parameterName: parameterType): returnType</p> Signup and view all the answers

    What happens when the radius of circle2 is set to 100?

    <p>The area of circle2 is calculated and displayed</p> Signup and view all the answers

    Which method should be defined as static according to the design guide?

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

    What does the method m2(int i, int j) return?

    <p>i raised to the power of j</p> Signup and view all the answers

    Why is the radius variable in the Circle class an instance variable?

    <p>Because each instance of Circle has its own radius</p> Signup and view all the answers

    Which statement is true regarding instance methods?

    <p>Instance methods require an instance to be invoked.</p> Signup and view all the answers

    What is the result of invoking m1() given appropriate values for i and k?

    <p>The method updates i with a complex expression</p> Signup and view all the answers

    What is the main reason line 6 in the first code example is incorrect?

    <p>Instance variables must be accessed through an object.</p> Signup and view all the answers

    Which statement describes a valid way to access instance fields in a static context?

    <p>Instantiate an object first to access the instance fields.</p> Signup and view all the answers

    What can be inferred about instance methods when called from a static method?

    <p>Instance methods need to be invoked through an object.</p> Signup and view all the answers

    What would happen if line 7 in the first example was modified to 'A.m1();'?

    <p>An error would occur since 'm1()' is an instance method.</p> Signup and view all the answers

    In the corrected version of the code, what is the role of the line 'A a = new A();'?

    <p>It creates an object of class A to access instance members.</p> Signup and view all the answers

    How does the line 'int j = a.i;' access the instance variable 'i' correctly?

    <p>It uses an instance of class A to refer to 'i'.</p> Signup and view all the answers

    What is the significance of the method 'm2' in the class A?

    <p>It does not interact with any instance variables.</p> Signup and view all the answers

    Which statement about static fields in the provided code is true?

    <p>They can be accessed without creating an instance of the class.</p> Signup and view all the answers

    What will the JVM do if an object is no longer referenced by any reference variable?

    <p>It will automatically collect the space occupied by the object.</p> Signup and view all the answers

    What is the purpose of assigning null to a reference variable?

    <p>To explicitly indicate that the object is no longer needed.</p> Signup and view all the answers

    Which of the following statements about arrays is accurate?

    <p>An array can contain elements of object types.</p> Signup and view all the answers

    What type of exception is a NullPointerException?

    <p>An exception thrown when trying to perform a method on a null object.</p> Signup and view all the answers

    What is an anonymous object?

    <p>An object that is instantiated without being assigned to a variable.</p> Signup and view all the answers

    What can be inferred from the line 'ShowErrors t = new ShowErrors(5);' in program (a)?

    <p>The constructor for ShowErrors does not accept any parameters.</p> Signup and view all the answers

    What is the likely issue in the line 'System.out.println(c.value);' from program (d)?

    <p>Variable 'c' is not initialized.</p> Signup and view all the answers

    In program (b), which statement has an error?

    <p>t.x();</p> Signup and view all the answers

    What does the method distance(x: double, y: double) return?

    <p>The distance between this point and the specified coordinates.</p> Signup and view all the answers

    Which method would you invoke to get the y-coordinate of a Point2D object?

    <p>getY()</p> Signup and view all the answers

    In the provided program, what is printed when invoking toString() on the Point2D object p1 with coordinates (1.5, 5.5)?

    <p>Point2D [x = 1.5, y = 5.5]</p> Signup and view all the answers

    How many Point2D objects are created in the program TestPoint2D?

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

    Which line of code correctly initializes a Point2D object?

    <p>Point2D p = new Point2D(0, 0);</p> Signup and view all the answers

    What is the output of the program if the user inputs point1 coordinates as 1.5 and 5.5 and point2 coordinates as -5.3 and -4.4?

    <p>Distance is 12.010412149464313</p> Signup and view all the answers

    What is the purpose of the main method in the program TestPoint2D?

    <p>To perform user input and demonstrate the Point2D functionality.</p> Signup and view all the answers

    Which one of the following methods is NOT a part of the Point2D class?

    <p>calculateDistance(): double</p> Signup and view all the answers

    Study Notes

    Introduction

    • Object-oriented programming (OOP) allows you to develop large-scale software and GUIs effectively.
    • Java features like selections, loops, methods, and arrays are insufficient for GUI and large-scale software.

    Defining Classes for Objects

    • A class defines the properties (attributes) and behaviors (actions) for objects.
    • Objects represent real-world entities (student, desk, circle, button, loan).
    • Object state is represented by data fields (variables) with current values (e.g., a circle's radius, a rectangle's width and height).
    • Object behavior is defined by methods (actions). Methods can be used to get information from an object, change its data fields, or perform another action.

    Example: Circle Class

    • A class template defines an object's data fields and methods.
    • A class is a blueprint or contract. An object is an instance of a class.
    • Data fields define the characteristics of an object (eg a circle’s radius),
    • Methods define the actions an object can perform.

    Defining Classes and Creating Objects

    • Classes define objects.
    • Objects are created from classes.
    • Example code creates three circles with different radii and shows their area and perimeter.
    • The code changes the radius of one circle and shows the new area and perimeter.

    Constructing Objects Using Constructors

    • A constructor is a special kind of method used to create objects.
    • It has the same name as the class.
    • It does not have a return type.
    • It is invoked using the new operator.

    Accessing Objects via Reference Variables

    • Objects are accessed via reference variables.
    • Reference variables hold references to objects.
    • To access object data and methods use the dot operator with the reference variable (e.g. objectRefVar.dataField).
    • Reference variables that appear to hold objects actually hold references to them.

    Reference Data Fields and the null Value

    • Data fields can use reference types (e.g. String).
    • The null value represents the absence of reference to an object of a reference type.
    • Data fields have default values (null for reference types, 0 for numeric types, false for booleans).

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Java Text - Liang (1) PDF

    Description

    question in between every topic in the given pdf book including chapter 9,10,11,12 and13

    More Like This

    TV Show Trivia Quiz
    22 questions

    TV Show Trivia Quiz

    ProfoundPearTree avatar
    ProfoundPearTree
    TV Genres Overview Quiz
    16 questions

    TV Genres Overview Quiz

    RevolutionaryDulcimer avatar
    RevolutionaryDulcimer
    Use Quizgecko on...
    Browser
    Browser