Data Types and Casting
Objects, Classes, & Methods
Strings and Operators
Constructors
Math and Random
100

What is the result of 7 / 2 in Java?

3. This is integer division, and the decimal is truncated.

100

What is the difference between a class and an object?

A class defines the blueprint (structure/behavior); an object is a specific instance of that class.

100

What is the result of "AP" + 5 + 3?

"AP53". Java evaluates left to right, so it concatenates "AP" with 5, then with 3.

100

What is the purpose of a constructor in a class?

To initialize new objects of that class—usually by assigning values to instance variables.

100

What does Math.sqrt(25) return?

5.0 (a double).

200

What is the result of (double) 11 / 2 and why?

5.5. Casting 11 to a double causes the division to be floating-point.

200

What keyword is used to define a method that belongs to a class rather than an instance?

static

200

What does "Computer".substring(1, 4) return?

"omp". Characters at index 1 to 3 (4 is exclusive).

200

What is constructor overloading? Give an example.

Having multiple constructors with the same name but different parameter lists.

200

How do you generate a random integer from 1 to 10 (inclusive)?

(int)(Math.random() * 10) + 1

300

Why must you cast a double to an int before assigning it to an int variable?

Because Java does not automatically convert from double to int due to potential loss of data. You must cast it explicitly using (int).

300

Given a class Student with a constructor Student(String name, int grade), write the code to create a new object named s1.

Student s1 = new Student("Alice", 11);

300

What is the output of the following code?System.out.println("The\tEnd");

The    End

The \t inserts a tab space.

300

Write a constructor for a class Book that takes in a String title and sets a default number of pages to 100.

public Book(String myTitle) {
    title = myTitle;
    pages = 100;
}

300

What is the output of Math.pow(2, 3) and what type is it?

8.0 — the result is a double.

400

Is this declaration valid: int x = 3.14;? Explain why or why not.

Invalid. 3.14 is a double, and assigning it directly to an int without casting causes a type mismatch error.

400

What's the difference between an instance method and a static method in terms of how you call them?

Instance methods are called using an object: obj.method().

Static methods are called using the class name: ClassName.method().

400

Given a String s = "Jeopardy", what does s.indexOf("p") return?

3. Index starts at 0, and "p" is the 4th character.

400

Why will these two methods NOT overload?

void doSomething(int x) 

void doSomething(int y)

They have the same parameter type and order. The parameter names do not matter, so they are considered duplicates.

400

Write an expression to generate a random integer from 5 to 15.

(int)(Math.random() * 11) + 5

500

What is printed by the following code?

int a = 5;
double b = 2.0;
System.out.println(a / b);

2.5. Since b is a double, a is promoted, and floating-point division occurs.

500

Write a class method named getAv that takes in two int values: score and total, and returns the percentage score as a double.

public static double getAv(int score, int total) {
    return (double) score / total * 100;
}

500

Write a class method named comb that takes in two Strings, returns a new String consisting of the first 2 letters of the first String and the last 2 of the second. Use substring.

public static String comb(String a, String b) {
   return a.substring(0,2)+b.substring(b.length()-2);
}


500

Assume a class contains an instance variable "level".  Write an instance method called changeLevel that takes an int and sets the instance variable level to that value.

public void changeLevel(int newLevel) {
    level = newLevel;
}


500

You are writing a method that simulates rolling two dice. Show how to generate the two values using Math.random().

int die1 = (int)(Math.random() * 6) + 1;

int die2 = (int)(Math.random() * 6) + 1;