Java Objects and Classes Flashcards
41 Questions
100 Views

Java Objects and Classes Flashcards

Created by
@MatchlessAltoSaxophone

Questions and Answers

What is an object in Java?

An object is something that contains both state and behavior.

Which of the following best describes the relationship between a class and an object?

A class definition specifies the attributes and behavior of every object that will be made.

Every class definition has each of the following EXCEPT?

  • Defined objects as copies of the class (correct)
  • A name
  • Defined attributes
  • Defined behaviors to manipulate the state of the objects
  • Consider this class definition of a Pineapple. When we use this class to create Pineapple objects, which of the following is guaranteed to be true?

    <p>Every Pineapple object will have the same attributes.</p> Signup and view all the answers

    What is a constructor in Java?

    <p>A constructor allows us to create a new instance of a class, usually initializing instance variables.</p> Signup and view all the answers

    Which of the following is the correct code for the constructor in the Card class?

    <p>suit = cardSuit; value = cardValue;</p> Signup and view all the answers

    Which of the following choices is a formal parameter of the constructor for the Shark class?

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

    What is the purpose of overloading a class' constructor?

    <p>It allows the user to set the values of different combinations of the instance variables when the object is created.</p> Signup and view all the answers

    Which of the following is NOT part of the constructor signature?

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

    Which of the following is NOT a valid way to overload this constructor?

    <p>FancyPineapple(String color, int age)</p> Signup and view all the answers

    What is the importance of the null value?

    <p>null allows a reference variable to be empty and not hold any memory address.</p> Signup and view all the answers

    What is the special value that a reference variable holds?

    <p>The memory address of an object.</p> Signup and view all the answers

    In the given code snippet, which of the following is a reference variable?

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

    What does it mean to be a client of a class?

    <p>Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.</p> Signup and view all the answers

    Where would you find the formal parameters of a constructor?

    <p>In the documentation.</p> Signup and view all the answers

    What do you need to know in order to create an object of the class you intend to use?

    <p>You need to know the formal parameters in order to pass in actual parameters.</p> Signup and view all the answers

    What is an instance method?

    <p>An instance method is a piece of code called on a specific instance (an object) of the class.</p> Signup and view all the answers

    Which of the following is a correctly written method for the Timer class?

    <p>public void addFiveMinutes() { length = length + 5; }</p> Signup and view all the answers

    Which of the following correctly calls the method addFiveMinutes on an object of the Timer class?

    <p>kitchenTimer.addFiveMinutes();</p> Signup and view all the answers

    Which of the following correctly uses the method startTime to print out the start time of a Timer object called laundry?

    <p>laundry.startTime();</p> Signup and view all the answers

    What is the output of the given main method for the Timer class?

    <p>The timer will end in 30 minutes The timer will end in 35 minutes.</p> Signup and view all the answers

    What are parameters?

    <p>The formal names given to the data that gets passed into a method.</p> Signup and view all the answers

    What is the output of the main method for the Circle class?

    <p>24.0 20.0.</p> Signup and view all the answers

    Using the Student object called karel, which of the following is the correct way to set karel's honor status to true?

    <p>karel.setHonorStatus(true);</p> Signup and view all the answers

    The value that a method outputs is called?

    <p>a return value.</p> Signup and view all the answers

    What is the correct syntax for retrieving the area of Rectangle shape?

    <p>int area = shape.getArea();</p> Signup and view all the answers

    Which of the following statements correctly stores the return value of goingUp when it is called on the Elevator object called hotel?

    <p>boolean up = hotel.goingUp();</p> Signup and view all the answers

    Which of the following methods is implemented correctly with respect to the method's return type?

    <p>public String getColor() { return &quot;Red&quot;; }</p> Signup and view all the answers

    Strings are immutable. This means that?

    <p>Once a String variable has been assigned a value, the value cannot be modified but the variable can be assigned to a different value.</p> Signup and view all the answers

    What will be printed by the following code snippet: System.out.println(language + opinion);

    <p>Java is fun!</p> Signup and view all the answers

    Which of the following would properly print this quote by Edsger W. Dijkstra?

    <p>System.out.println(&quot;&quot;Testing shows the presence, not the absence of bugs&quot;&quot;);</p> Signup and view all the answers

    Which of the following statements will not compile?

    <p>All of these statements will compile.</p> Signup and view all the answers

    What are the values of s and num at the end of the myTest() method?

    <p>s is the string &quot;Golden&quot;; num = 7.</p> Signup and view all the answers

    What method must a class implement in order to concatenate an object of the class with a String object?

    <p>toString.</p> Signup and view all the answers

    What is the output of the following code snippet? String forest = "Amazon Rainforest"; System.out.println(forest.indexOf('a'));

    <p>2 -1 5.</p> Signup and view all the answers

    What would be printed by the following code snippet comparing lastName and otherLastName?

    <p>A positive number because &quot;Vu&quot; comes after &quot;Lopez&quot; in lexicographical order.</p> Signup and view all the answers

    The purpose of a wrapper class is to?

    <p>&quot;Wrap&quot; a primitive value to convert it to an object.</p> Signup and view all the answers

    What happens when a Double is unboxed?

    <p>A Double is unboxed when it is converted to a primitive value.</p> Signup and view all the answers

    What happens when an int is autoboxed?

    <p>A int is autoboxed when it is converted to an Integer.</p> Signup and view all the answers

    Which of these is not a difference between primitives and objects?

    <p>A primitive has data and methods associated with it while an object only stores data.</p> Signup and view all the answers

    Which of these is an example of calling a static method?

    <p>Not provided.</p> Signup and view all the answers

    Study Notes

    Objects and Classes

    • An object in Java contains both state (attributes) and behavior (methods).
    • A class serves as a blueprint for creating objects, defining their attributes and behaviors.

    Class Definitions

    • Class definitions include a name, defined attributes, and behaviors to manipulate the state.
    • Objects are not defined as copies of the class themselves.

    Constructors

    • A constructor allows for the creation of a new instance of a class, initializing instance variables.
    • Constructors can be overloaded, enabling the creation of objects with different configurations of instance variables.

    Reference Variables

    • A reference variable holds a memory address of an object, not the actual object itself.
    • null indicates that a reference variable isn’t pointing to any object.

    Instance Methods

    • An instance method operates on a specific object, providing functionality tied to that instance.
    • Correctly implementing methods involves matching the method’s return type with the expected data type.

    String Data Type

    • Strings in Java are immutable, meaning their value cannot be changed after assignment.
    • String concatenation combines multiple string values or variables, producing a new string.

    Parameters and Return Values

    • Parameters are formal names for data passed into methods, while a return value is the output produced by a method.
    • Calling a method may change instance variables' values but does not alter the original variables passed as arguments unless they refer to mutable objects.

    Random Number Generation

    • To generate a random integer within a range, use Math.random() scaled to the desired range.

    Static vs. Instance Methods

    • Static methods can be called on a class level without needing an object, while instance methods require an object to be called.

    Wrapper Classes

    • Wrapper classes convert primitive data types into objects, allowing primitives to be used where objects are required.

    Array and List Management

    • Correct syntax and method calls are crucial when working with arrays, lists, and classes, ensuring expected outputs and functionality.

    Miscellaneous

    • Use toString to concatenate an object with a String.
    • compareTo can assess the lexicographical order of strings, returning a positive, negative, or zero value based on their comparative order.
    • Math functions like sqrt, pow, and abs perform mathematical calculations and return double values.

    Studying That Suits You

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

    Quiz Team

    Description

    This quiz focuses on understanding objects and classes in Java programming, covering key concepts and definitions. Through a series of flashcards, you'll enhance your comprehension of how objects function and their relationship with classes.

    More Quizzes Like This

    Use Quizgecko on...
    Browser
    Browser