Given the Rectangle Class with a Constructor that takes in two ints, write a line of code that would create an instance of the Rectangle class named rec1 with ints 3 and 7.
Rectangle rec1 = new Rectangle(3, 7);
What is the common name of the method you use to get the value of a private instance variable?
The getter method
The "static" keyword in front of a variable means that it is a general variable that applies to the whole class, and not any object made from that class.
What is the "static" keyword used for most often (or one common use for it)?
To create a variable that keeps track of the number of objects created.
Where do we use the "extends" keyword?
What country did the first Starbucks open outside of North America?
Japan
What is missing from this class definition?
public class Student {
public Student(int age) {
this.age = age;
}
}
private int age;
needs to be declared in the class definition before the Constructor.
Given an instance of the Pizza class with private instance variable String topping, write a setter method for topping.
public String getTopping()
{
return topping;
}
What is the problem with this code?
Must use "this" in constructor (this.height = height) when the parameters have the same name as instance variables.
OR
Must change parameters so they are not the same as instance variables (height = myHeight;)
What does the "super" keyword do in the Constructor or a subclass?
The "super" keyword in a Constructor of a subclass calls the superclass's Constructor. The parameters will decide which Constructor will be called if it is overloaded.
What was the first Disney animated feature movie that was not based on an already existing story?
The Lion King (though it has some loose connection to Hamlet, it is NOT an adaptation of it)
Given a Book class with private instance variables String title and int pageCount, write a Constructor that would create an instance of the Book class.
public Book(String title, int pageCount)
{
this.title = title;
this.pageCount = pageCount;
}
What would a blank default constructor for the Rectangle class look like?
public Rectangle() {}
Which of these variables exist only from lines 3 to 5?
1. public class Example{
2. private int a = 0;
3. public int mystery1(int b) {
4. int c = 5 + a;
5. return c;
6. }
7. }
b
B starts on line 3 where it is declared, and ends when the box where it was created is closed by line 6.
Write an example of polymorphism for a superclass "Animal" and subclass "Dog".
Creating an object named "spot" built with the default Constructor.
The object "spot" should then call the "talk()" method.
Animal spot = new Dog();
spot.talk();
What is the human body's heaviest organ?
The skin!
Write the shortest code that will change the height of Rectangle r1 to 5.
r1.height = 5;
Given an instance of the Movie class called "Shrek", what is the proper way to set the value of "length" to 92 after it has been instantiated?
It is impossible to set "length", since it is private and there is no setter method.
What methods of class Mystery can be called without an instance of the class?
public class Foo {
public static void sum() {
...
}
public static void average() {
...
}
public void count()
{
...
}
}
Only "sum" and "average" can be called without creating an instance of the class because they are static.
Which of the following will be inherited from a superclass? (Choose all that apply)
A. the public instance variables of the superclass
B. the private instance variables of the superclass
C. the Constructors of the superclass
D. any getter or setter methods from the superclass
E. non-getter and non-setter instance methods from the superclass
F. the toString method from the superclass
A, D, E, and F will all be inherited.
Everything is inherited from a superclass EXCEPT for Constructors and private instance variables.
What is the best selling book of all time (after the Bible)? - as of Feb 2025
A Tale of Two Cities with over 200 million!
Other books that have sold more than 100 million copies include:
The Little Prince, The Alchemist, Harry Potter and the Philosopher's Stone, And Then There Were None, Dream of the Red Chamber, The Hobbit, and Alice's Adventures in Wonderland
[Choose all that are correct] Which of the following are correct ways to create an instance of Rectangle:
A. Rectangle rec1 = new Rectangle(3, 7);
B. Rectangle rec1 = new Rectangle(4);
C. Rectangle rec1;
rec1 = new Rectangle(2, 5);
D. Rectangle rec1 = new Rectangle("Square", 8);
E. Rectangle rec1;
new rec1 = Rectangle(6, 10);
A, B, and C
What happens when the default constructor is called for an objects (assume there is no code in the default constructor and it has been properly written in the class)?
All of the instance variables of the class will get their default values (ints will be 0, Strings will be null, etc.)
What is the output of this program?
3
Based on this code, what will be printed?
Cookie
Chocolate
Chocolate
What are the only two mammals that lay eggs?
(If you get both right, you get the points! If you only get one right, others can steal!)
Echidna and Platypus