Java Jumpstart
String Things
Truth or Bug?
Loop Troop
Classy Code
100

This primitive type stores whole numbers (no decimals).

What is int?

100

This String method returns the number of characters.

What is length()?

100

The Java operator that means “not equal to.”

What is !=?

100

The loop most commonly used when you know exactly how many times to repeat.

What is a for loop?

100

A variable defined in a class that belongs to each object (each instance).

What is an attribute / instance variable / field?

200

In Java, what is the value of 7 / 2 if both are int?

What is 3?

200

What does this return?

String s = "computer";

s.substring(1, 4);

What is "omp"?

200

What does this evaluate to?

What is true?

200

What is printed?

int sum = 0;

for (int i = 1; i <= 3; i++) sum += i;

System.out.println(sum);

What is 6?

200

The keyword that refers to the current object.

What is this?

300

Write an expression to get the last digit of a positive integer n.

What is n % 10?

300

Which method checks if two Strings have the same characters (case-sensitive)?

What is equals()?

300

Rewrite using De Morgan’s Law: !(x > 10 || y < 0)

What is (x <= 10 && y >= 0)?

300

In a countdown loop like for (int i = 10; i >= 1; i ___ ), what goes in the blank?

What is -- (or i--)?

300

Write a correct constructor header for Book with String title and int pages.

What is public Book(String title, int pages)?

400

What is the value of y?

double x = 5;

int y = (int)(x / 2);

What is 2?

400

What does this return?

"banana".indexOf("na")

What is 2?

400

What is n after this code runs?

int n = 7;

if (n % 2 == 0) n += 10;

else n += 1;

What is 8?

400

What is the final value of count?

int count = 0;

for (int i = 0; i < 3; i++) {

  for (int j = 0; j <= i; j++) {

    count++;

  }

}

What is 6?

400

If you have private int balance;, write a correct accessor method header.

What is public int getBalance()?

500

What is the value of c?

int a = 3, b = 4;

double c = (double) a / b;

What is 0.75?

500

Give one correct expression to get the last character of s as a String, using substring.

What is s.substring(s.length() - 1)?

500

Why is this condition safe from dividing by zero?

(a != 0 && b / a > 2)

What is “because of short-circuit evaluation”?

(the second part isn’t evaluated if a != 0 is false)

500

What is the bug in this code?

int i = 0;

while (i < 5) {

  System.out.print(i + " ");

}

What is “it’s an infinite loop because i never changes (missing i++)”?

500

If Counter starts at 0, what is c1’s count after this?

Counter c1 = new Counter();

Counter c2 = c1;

c2.add();

What is 1 (because both references point to the same object)?