Iteration
Conditionals
Objects / Classes
Arrays / ArrayLists
Inheritance / Recursion
100

Consider the following code segment:

int x = 10;

int y = 2; 

int count = 1; 

while(x > y) {

    x /= y;

    count++;

}

What will the value of count be after executing the code segment?

3
100

What will this code return given checkMethod(false, true)?

public int checkMethod(boolean x, boolean y) {

    if(!x) {

        if(y) {

            return -1;

        } else {

            return 1;

        }

    } else {

        return 0;

    } 

}

-1

100

Give an example of the valid way to create an instance of Athlete in a different class given the following Athlete class definition:

public class Athlete {

  private String first_name;

  private String last_name;

  private int jersey;

   public Athlete(String first_name, String last_name, int jersey) {

    this.first_name = first_name;

    this.last_name = last_name;

    this.jersey = jersey;

  } 

}

Athlete athlete = new Athlete("Dirk", "Nowitzki", 41);

100

Which of the following are valid arrays?

I.   int[] coolArray = {1, 2, 3}; 

II.  int[] threeThings = {1.0, 2.0, 3.0}; 

III. int[] = {"1", "2", "3"};

I only

100

Given the following two classes

public class Parent {    public void printName(String name){

        System.out.println(name + " is a parent");

    }

}

public class Child extends Parent {

    public void printName(String childName){

        System.out.println(childName + " is a child");

    } 

}


Does the Child printName method successfully override the Parent method? Explain your answer.

Yes, they method signature is the same (different parameter names does not change anything).

200

What kind of error would the method call myMethod("Frog") cause?

public void myMethod(String x) {

    for(int i = 0; i <= x.length(); i++)    {

        System.out.println(x.substring(i, i+1));    

    } 

}

Runtime Error: String index out of range

200

Given the following call, what value would be returned for findTheMiddle(2110890125)?

public static String findTheMiddle(int number) {

    String stringNum = "" + number;

    int mid = stringNum.length()/2;

     if(stringNum.length() % 2 == 1)    {

        return stringNum.substring(mid,mid+1);

    } else {

        return stringNum.substring(mid-1,mid+1);

    } 

}

89

200

Given the code snippet,

public class Pokemon {

    private String name;

    private int health;

    public Pokemon(String name, int health)    {

        name = name;

        health = health;

    } 

}

What is wrong with the class definition?

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

this.name = name; this.health = health;

200

What will the following code print?

ArrayList<String> list = new ArrayList<String>();

list.add("Hello"); 

list.add("World"); 

System.out.println(list[0]);

Nothing. This will be a compiler error

200

Consider the following class declarations

public class Car {
/* Implementation Not Shown */ 

}
 public class ElectricCar extends Car {
/* Implementation Not Shown */ 

}

The following line of code appears in a test class:

ArrayList<Car> cars = new ArrayList<Car>();

Which of the following objects can be added to the ArrayList?

I. Car leaf = new ElectricCar();
II. Car mustang = new Car();
III. ElectricCar model3 = new ElectricCar();

I, II and III

300

What will the call to the method funWithNumbers(314159) print to the screen?

public void funWithNumbers(double myDouble) {

    int myInt = (int) myDouble;

    String myString = "";

    while(myInt != 0)    {

        myString = myInt % 10 + myString;

        myInt /= 10;

    }

    System.out.println(myString); }

314159

300

A company uses the following table to determine pay rate based on hours worked:

Hours Worked  Rate

1-40               $10

41-50             $15

50+                $20

The following method is intended to represent this table:

public int calculateRate(int hours) {

    if (hours < 40) {

        return 10;

    } else if (hours < 50) {

        return 15;

    } else {

        return 20;

    } 

}


Give an example of when the code does not work as intended.

40 or 50

300

Which methods of class Foo can be called without an actual instance of the class Foo?

public class Foo {

    public static void foo()    {        ...    }

     public void bar()    {        ...    }

     public void baz()    {        ...    } 

}

foo(), because it is declared static, so it can be invoked on the class, without an instance.

300

What is the output of the following program?

public class SayHello {

    public void run()    {

       String stringArray[] = {"h", "e", "l", "l", "o", "w"};

       for(int i=0; i <= stringArray.length; i++)       {

             System.out.print(stringArray[i]);

       }

    } 

}

Error

300

Based on this code snippet

public class Shape {

   public String getShapeName()   {

       return "Shape";

   }

 }
 public class Rectangle extends Shape {

   public String getShapeName()   {

       return "Rectangle";

   } 

}

public class Square extends Rectangle {}


public class Oval extends Shape {

    public String getShapeName() {

        return "Oval";

    } 

}
 

public class Circle extends Oval {

    public String getShapeName()    {

        return "Circle";

    } 

}


What does this program output?

Shape shape1 = new Shape(); Shape shape2 = new Rectangle();

Shape shape3 = new Square(); Shape shape4 = new Circle();

System.out.println(shape1.getShapeName());

System.out.println(shape2.getShapeName());

System.out.println(shape3.getShapeName());

System.out.println(shape4.getShapeName());

Shape
Rectangle
Rectangle
Circle

400

Consider the following code segment:

int p = 5; 

while(p > 0) {

    for(int j = p; j < p*2; j++)    {

        System.out.print(" * ");

    }

    System.out.println();

    p--;

}

What will the final result be when run in the console?

 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 *

400

What will the following code output when executed?

String word = "elementary";
if (word.length() > 8) {

  System.out.println("Long Word"); 

}
if (word.length() > 5) {

  System.out.println("Medium Word"); 

} else if (word.length() > 0) {

  System.out.println("Short Word"); 

}
else {  System.out.println("No Word"); 

}

Long Word
Medium Word

400

Given the following code:

public class TvShow {

    private String name;

    private int channel;

    public TvShow (String name, int channel)    {

        this.name = name;

        this.channel = channel;

    }

     public void getName()    {

        return name;

    }

     public void setName(String name)    {

        this.name = name;

    }

}

Which of the following explains why this code will not compile?

The return type for the getName method should be set to String.

400

Consider the following code segment:

ArrayList<Integer> nums = new ArrayList<Integer>();

nums.add(10); 

nums.add(20); 

nums.add(30); 

nums.add(40); 

nums.add(50); 

int x = nums.remove(3); 

int y = x + nums.remove(0); 

int z = x + y; 

nums.add(2, z);

What are the value of nums after the code segment has been executed?

[20, 30, 90, 50]

400

Consider the following recursive function:

public int mystery(int n) {

    if(n == 0)    {

        return 2;

    }    else    {

        return 2 * mystery(n - 1);

    } 

}

How many times will the function mystery be called if we call mystery(5)

6

500

Consider the following method:

public static void mix(String word, String letter) {

    int counter = 0;

    while(word.indexOf(letter) > 0)    {

       if(word.substring(counter,counter + 1).equals(letter))        {

            word = word.substring(0, counter) + "0" + word.substring(counter +2, word.length());

        }

        counter++;

    }

   System.out.println(word); 

}

What value word would be printed as a result of the call mix("Hippopotamus", "p")?

Hi0o0tamus

500

The following code is intended to return only even numbers. If an even number is passed to the method, it should return that number. If an odd number or zero is passed, it should return the next highest even number.

1:  public int returnEven(int number) 

2:  { 

3:      if (number % 2 == 0) 

4:      { 

5:          return number; 

6:      } 

7:      else if (number == 0) 

8:      { 

9:          return number + 2; 

10:     } 

11:     else 

12:     { 

13:         return number + 1; 

14:     } 

15:  }

Does the code work as intended?

No. Zero will get returned on line 5 and not make it to line 7.

500

Given the following code

public class Storm {

    private int lighteningCount;

    private int precipTotal;

    private String precipType;

     public Storm(String precipType)    {

        this.precipType = precipType;

        precipTotal = 0;

        lighteningCount = 0;

    }

     public Storm(String precipType, int precipTotal)    {

        this.precipType = precipType;

        this.precipTotal = precipTotal;

        lighteningCount = 0;

    }

     public Storm(String precipType, int lighteningCount)    {

        this.precipType = precipType;

        this.lighteningCount = lighteningCount;

        precipTotal = 0;

    }

}


Why will this code not compile?

The code will not compile because there are two constructors with the same signature.

500

The following method is a search method intended to return the index at which a String value occurs within an ArrayList:

public int search(ArrayList<String> list, String target) 

//Line 1 {

    int counter = 0;

    while(counter < list.size())    {

        if(list.get(counter).equals(target))  {

            return list.get(counter);

         }

        counter++;

     }

    return -1; 

}

However, there is currently an error preventing the method to work as intended. Which of the following Lines needs to be modified in order for the method to work as intended?

Line 8 - The return should be the counter, not list.get(counter).

500

Given the following code:

public class Calculator {

     /* Constructor and methods omitted */

     public double multiply(double num1, double num2){

        return num1 * num2;

    } 

}

public class ScientificCalculator extends Calculator {

     /* Constructor and methods omitted */

     public double square(double number) {

        /* Missing Code */

    } 

}

What goes in the missing code if the desired return is a square of the number?

return super.multiply(number, number);