Unit 2 Computer Programming Flashcards

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards


What is an object in Java?


An object is something that contains both state and behavior

2
New cards

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

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

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

  3. A class specifies the attributes and behaviors for exactly one object.

  4. Every object can choose which attributes and behavior it wants to keep from the class definition.

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

3
New cards

Every class definition has each of the following EXCEPT

  1. A name

  2. Defined attributes

  3. Defined behaviors to manipulate the state of the objects

  4. Defined objects as copies of the class

  1. Defined objects as copies of the class

4
New cards

Consider this class definition of a Pineapple.

public class Pineapple
{
    private boolean isRipe;
    private String color;
    private double weight;

    // Rest of class goes here
}

Java

When we use this class to create Pineapple objects, which of the following is guaranteed to be true?

  1. Every Pineapple object will be yellow

  2. Every Pineapple object must choose which attributes it wants.

  3. Every Pineapple object will have the same attributes.

  4. It is impossible to know what attributes the Pineapple objects will have since the attributes are not listed here.

  1. Every Pineapple object will have the same attributes.

5
New cards

What is a constructor in Java?

  1. A constructor is something that holds the private state of an instance.

  2. A constructor allows us to create a new instance of a class, usually initializing instance variables.

  3. A constructor is a method with instructions on how to use a class.

  4. A constructor is a syntax rule in Java for placing curly brackets.

  1. A constructor allows us to create a new instance of a class, usually initializing instance variables.

6
New cards

Refer to the Card class shown below.

public class Card
{
     private String suit;
     private int value; //13 values for each suit in deck (0 to 12)

     public Card (String cardSuit, int cardValue)
     { 
         /* implementation */
     }

      // Rest of the class goes here
 }

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

  1. cardSuit = suit; cardValue = value;

  2. Card = new Card (cardSuit, cardValue);
  3. Card = new Card (suit, value);

  4. suit = cardSuit; 
    value = cardValue; 

    Java

  5. suit = "Hearts"; value = 3;

  1. suit = cardSuit; 
    value = cardValue; 

7
New cards

/** 
 * The Shark class describes a shark.
 * 
 * Every shark has a region where it lives and an age.
 * 
 */ 

public class Shark
{
    // Attributes
    private String habitat;
    private int age;

   public Shark(String region, int sharkAge)
   {
          habitat = region;
          age = sharkAge;
   }
}

Java

Which of the following choices is a formal parameter of the constructor?

  1. habitat

  2. String

  3. Shark

  4. sharkAge

4. sharkAge

8
New cards

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

  1. Which instance variables are initialized

  2. The parameter types

  3. The order of the parameters

  4. The name of the constructor

  1. Which instance variables are initialized

9
New cards

Which of the following is NOT a valid way to overload this constructor? For brevity, only the signature is given.

Pineapple(String color)

Java

  1. Pineapple()

  2. Pineapple(String color, int age)

  3. Pineapple(int age, String species)

  4. FancyPineapple(String color, int age)

  1. FancyPineapple(String color, int age)

10
New cards

What is the importance of the null value?

null allows a reference variable to be empty and not hold any memory address.

11
New cards

A reference variable holds a special value. What is this special value?


The memory address of an object

12
New cards

Consider this code snippet that uses a class called Rectangle.

int roomHeight = 40;
int roomWidth = roomHeight * 3;

Rectangle room = new Rectangle(roomHeight, roomWidth);

Java

Which of the following is a reference variable?

  1. room

  2. roomHeight

  3. roomWidth

  4. Rectangle

  1. room

13
New cards

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


Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.

14
New cards

You are using a class as a client. What would you need to know in order to create an object of the class you intend to use?

You need to know the formal parameters in order to pass in actual parameters.

15
New cards

What is the purpose of overloading a class’ constructor?

It allows the user to call different constructors for the same object to initialize different instance variables.

16
New cards

What is an instance method?

An instance method is a piece of code called on a specific instance (an object) of the class.

17
New cards

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

public class Timer 
{
    private int startMin;
    private int length;

    public Timer(int minute, int duration)
    {
        startMin = minute;
        length = duration;
    }

    public Timer(int duration)
    {
        startMin = 0;
        length = duration;
    }
}
  1. public void addFiveMinutes()
    {
        length = length + 5;
    }

    Java

  2. public addFiveMinutes() { length = length + 5; }

    Java

  3. addFiveMinutes() { length = length + 5; }

    Java

  4. public void addFiveMinutes { length = length + 5; }

  1. public void addFiveMinutes()
    {
        length = length + 5;
    }

18
New cards

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

kitchenTimer.addFiveMinutes();

19
New cards

Suppose the class Timer has a method called startTime that prints out the starting time of the Timer.
Which of the following correctly uses this method to print out the start time of a Timer object called laundry?

  1. System.out.println(laundry.startTime());

  2. System.out.println(laundry.startTime);

  3. int start = laundry.startTime();

  4. laundry.startTime();
  5. laundry.startTime;

  1. laundry.startTime();


20
New cards

A Timer class is a class that represents a minute timer. A partial definition of the class is given below.

public class Timer 
{
    private int length;

    public Timer(int duration)
    {
        length = duration;
    }

    public void endTime()
    {
        System.out.print("The timer will end in " );
        System.out.print(length);
        System.out.println(" minutes");
    }

    public void addFiveMinutes()
    {
        length = length + 5;
    }
}

Java

What is the output of the following main method?

public static void main(String[] args){
    Timer muffins = new Timer(30);

    muffins.endTime();
    muffins.addFiveMinutes();
    muffins.endTime();

}

  1. 
    The timer will end in 30 minutes
    The timer will end in 35 minutes

21
New cards

What are parameters?

The formal names given to the data that gets passed into a method.

22
New cards

Consider the Circle class below.

public class Circle
{

    private double radius;

    public Circle(double circleRadius)
    {
        radius = circleRadius;
    }

    public void setRadius(double newRadius)
    {
        radius = newRadius;
    }

    public void printDiameter()
    {
        double diameter = 2 * radius;
        System.out.println(diameter);
    }
}

What is the output of the main method below?

public class MyProgram 
{
    public static void main(String[] args)
    {
       Circle pizza = new Circle(12);
       pizza.printDiameter();
       pizza.setRadius(10);
       pizza.printDiameter();

    }
}

24.0
20.0

23
New cards

Suppose there is a class called Student. One of the methods is given below. It sets the instance variable isHonors to the parameter value.

public void setHonorStatus(boolean status)
{
    isHonors = status;
}

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


karel.setHonorStatus(true);