Class Structure
Static, This, New &
Other Keywords
Substrings
Coding Terminology
Bonus Questions (& Ethical Impacts of Computing)
100
What is the return type of a Mutator method?

Void

100

When this keyword is placed in a method's signature, it can only use variables and other methods that use this modifier. 

What is "Static"?

If a method is static, it can ONLY use other static methods or static variables in its code. 

100

Determine output.  If an error, explain why:

String thing = "apple";
int a = 1;
int b = 4;
System.out.print( thing.substring(a, b) );

Output is "ppl"

100

A variable that defined in a class, for which each object of the class has a separate copy. 

What is an instance variable?

100

What famous cartoon character (specifically) entered public domain in 2024?

Steamboat Willie (Mickey Mouse)

200

Given the Pizza class, write a setter method for topping.

public class Pizza{
      private String topping;

      //constructor not shown

      //insert setter here

}

public void setTopping( String newTopping) {
      topping = newTopping;
}

OR

public void setTopping( String topping) {
      this.topping = topping;
}

200

This modifier allows an instance variable to be changed directly outside the class without the need for a Mutator / Setter method.

What is "public"?

200

Determine output.  If an error, explain why:

String thing = "apple";
int a = 2;
int b = 2;
System.out.print( thing.substring(a, b) );

No output. 

200

A keyword that makes a variable or method only visible and accessible within its own class.  

What is "private"?

200

One breaks but never falls.  It's opposite falls but never breaks.  What are we? 

Day and Night

300

Given the Pizza class, write an accessor method for topping.

public class Pizza{
      private String topping;

      //constructor not shown

      //insert accessor here

}

public String getTopping(){
      return topping;             //or this.topping
}


300

Which of these is a valid way to create a new object of the "Sport" class that has one written constructor that takes in a single string?

  1. Sport s1 = Sport("Baseball");

  2. Sport s2;
    new s2= Sport();

  3. Sport s3 = new Sport("Baseball", "America");

  4. Sport s4;
    s4 = new Sport();

Sport s4;
s4 = new Sport();

300

Determine output.  If an error, explain why:

String thing = "apple";
int a = 5;
int b = a+1;
System.out.print( thing.substring(a, b) );

An error is thrown; numbers taken in from substring must be between 0 and the length of the word.

300

A modifier that designates that a method or variable can be accessed without creating an object of a class.

What is "static"?

300

Someone throws a ball as hard as they can.  It comes back to her, even though nothing and nobody touches it.  How? 

They threw the ball straight up

400

What happens if two constructors exist with the same signature (number, type, and order of variables)?

  1. The code will not compile

  2. The code will compile, but will cause a run time error if an object of that type is created

  3. The code will compile and will use the first constructor that matches the object

  4. The code will compile and use the default constructor rather than either written constructors. 

1. The code will not compile

400

What is the value of numDucks after running Duck.addNephews();?

public class Duck{
    private static int numDucks = 1;

    public Duck(){
          numDucks = numDucks + 1;
    }

    public static void addNephews() {
          Duck Huey = new Duck();
          Duck Dewey = new Duck();
          Duck Louie = new Duck();
    }
}

numDucks = 4

400

Determine output.  If an error, explain why:

String thing = "apple";
int a = 1;
int b = 10;
System.out.print( thing.substring(a, b) );

An error is thrown; numbers taken in from substring must be between 0 and the length of the word.

400

Something that must be true before your method will work

What is a precondition?

400

What won the oscar for best picture in 2023?

Everything Everywhere All at Once

500

Identify the 4 different mistakes below (some errors, some conventions that we have learned): 

private class dragon
{
      public String name
      public int power
      //constructor and methods not shown
}

1. Class should be public 

2. Class name should be capitalized (Dragon)

3. Instance variables should be private

4. Semicolons missing after instance variables

500

What is wrong with this class definition? 

public class Superhero {
     private String name;
     private int strength;

     public Superhero (String name, int strength){
          name = name;
          strength = strength;
     }
}

Must use "this" in the constructor when the parameters have the same name as instance variables.  

500

Determine output.  If an error, explain why:

String thing = "apple";
int a = 4;
int b = 1;
System.out.print( thing.substring(a, b) );

An error is thrown; substring will only work if the first number is lower than the second number.

500

A way of referring to the current object in a method or constructor, often used to eliminate confusion between class variables and parameters with the same name.

What is the "this" keyword?

500

Which of the following is one of the 7 ACM Code of Ethics and Professional Conduct's General Ethical Principles?

Be fair

  1. Credit original creator when using other's work

  2. Be fair and take action not to discriminate

  3. Use energy efficient devices

  4. Install security protection and backup your computer every year

Be fair and take action not to discriminate

M
e
n
u