Java Classes and Objects Flashcards
36 Questions
100 Views

Java Classes and Objects Flashcards

Created by
@EasygoingAgate6318

Questions and Answers

What does OOP stand for?

  • Object-Orienting Programming
  • Object-Oriented Programming (correct)
  • Object-Ordered Programming
  • Object-Operations Programming
  • What are attributes in object-oriented programming?

    Characteristics in an object.

    What does behavior refer to in the context of objects?

    Dependent on the object type.

    What are the three dimensions in object-oriented programming?

    <p>Identity, Attributes, Behavior.</p> Signup and view all the answers

    What is a class?

    <p>It describes what an object will be.</p> Signup and view all the answers

    What is the basic class syntax in Java?

    <p>public class (name class here) {</p> Signup and view all the answers

    What is a method in programming?

    <p>Synonymous for behavior.</p> Signup and view all the answers

    How do you declare a method in Java?

    <p>class Myclass { static void sayHello() { //sayHello is the method System.out.println(&quot;Hello World!&quot;) }</p> Signup and view all the answers

    What is code reuse?

    <p>Write a method once and use it multiple times.</p> Signup and view all the answers

    What is return value syntax in Java?

    <p>class MyClass { static int sum(int val1, int val2) { return val1 + val2; }</p> Signup and view all the answers

    Public access modifier means the class is accessible by any other class.

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

    What is a package in Java?

    <p>A group of similar types of classes.</p> Signup and view all the answers

    Default variables or methods are available to any other class in different packages.

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

    Protected methods and variables can be accessed by subclasses.

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

    Private variables are accessible only within the declared class.

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

    What is a getter method?

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

    What is the syntax for a getter method in Java?

    <p>public String getVariable()</p> Signup and view all the answers

    What is a setter method?

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

    What is the syntax for a setter method in Java?

    <p>public void setVariable(String something)</p> Signup and view all the answers

    What is encapsulation in programming?

    <p>Wrapping data and code as a single unit.</p> Signup and view all the answers

    What are constructors in Java?

    <p>Special methods invoked when an object is created.</p> Signup and view all the answers

    What are the two expectations for constructors?

    <ul> <li>Name must be the same as class name</li> <li>Must have no explicit return type</li> </ul> Signup and view all the answers

    Match the following types to their descriptions:

    <p>byte = 8-bit integer short = 16-bit integer int = 32-bit integer long = 64-bit integer float = Single-precision floating point double = Double-precision floating point boolean = True or false value char = Single 16-bit Unicode character</p> Signup and view all the answers

    What is a boolean type?

    <p>Returns true or false.</p> Signup and view all the answers

    What is a reference variable?

    <p>When you create an object using the constructor.</p> Signup and view all the answers

    What is the Math class in Java?

    <p>Provides predefined methods for mathematical operations.</p> Signup and view all the answers

    What does Math.abs() do?

    <p>Returns the absolute value of its parameter.</p> Signup and view all the answers

    What does Math.ceil() do?

    <p>Rounds to the nearest integer up when using double.</p> Signup and view all the answers

    What does Math.max() do?

    <p>Rounds to the nearest integer down when using double.</p> Signup and view all the answers

    What does Math.min() do?

    <p>Returns the smallest parameter.</p> Signup and view all the answers

    What does Math.pow() do?

    <p>Takes two parameters and returns the first raised to the power of the second.</p> Signup and view all the answers

    What does sqrt() calculate?

    <p>Square root.</p> Signup and view all the answers

    What does sin() calculate?

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

    What does cos() calculate?

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

    What does the final keyword do?

    <p>Keeps a variable constant.</p> Signup and view all the answers

    What are packages in Java?

    <p>A group made up of similar types of classes.</p> Signup and view all the answers

    Study Notes

    Object-Oriented Programming (OOP)

    • Emphasizes the use of objects which contain data and methods.
    • Key components include attributes, behaviors, and class structures.

    Attributes and Behavior

    • Attributes are characteristics of an object, such as color.
    • Behavior refers to actions defined by the object type.

    Class Fundamentals

    • A class is a blueprint for creating objects.
    • Class definition begins with public class ClassName {.

    Methods

    • Methods represent behaviors; they encapsulate operations for an object.
    • Example method syntax:
      static void sayHello() {
          System.out.println("Hello World!");
      }
      

    Code Reuse

    • Methods can be written once and reused multiple times, enhancing efficiency.

    Return Values

    • Methods can return values using syntax like:
      static int sum(int a, int b) {
          return a + b;
      }
      
    • Return type must match the data type of the returned value.

    Access Modifiers

    • Public allows class accessibility from any class.
    • Default (no modifier) grants access within the same package.
    • Protected extends access to subclasses.
    • Private restricts access to the defining class only.

    Getters and Setters

    • Getters retrieve values, syntax example:
      public String getVariable() {
          return variable;
      }
      
    • Setters assign values, syntax example:
      public void setVariable(String value) {
          this.variable = value;
      }
      

    Encapsulation

    • Combines data (variables) and methods into a single unit to improve data security.

    Constructors

    • Special methods for initializing objects.
    • Constructors must have the same name as the class and no return type.

    Primitive Data Types

    • Includes byte, short, int, long, float, double, boolean, char.

    Boolean Data Type

    • Represents true or false values.

    Reference Variables

    • Created when instantiating an object from a class.

    Math Class Operations

    • Predefined methods for mathematical calculations, such as:
      • Math.abs(): Absolute value
      • Math.ceil(): Rounds up to the nearest integer
      • Math.max() and Math.min(): Returns the maximum or minimum of parameters
      • Math.pow(): Raises a number to a specified power

    Special Mathematical Functions

    • sqrt(): Computes the square root.
    • sin() and cos(): Compute sine and cosine, respectively.

    Final Keyword

    • Declares constants that cannot be modified after initial assignment. Example:
      public static final double PI = 3.14;
      

    Packages

    • A package groups related classes and can contain sub-packages for organizing code.

    Studying That Suits You

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

    Quiz Team

    Description

    Test your knowledge on Java's Object-Oriented Programming concepts with these flashcards. Cover essential terms like attributes, behaviors, and class characteristics. Perfect for beginners looking to solidify their understanding of OOP in Java.

    Use Quizgecko on...
    Browser
    Browser