Data Types and Casting
Objects, Classes, & Methods
Strings and Operators
Constructors
Class Creation
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?  What is usually contained in the code block of a constructor?

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

100

In a class, attributes are typically ___ and methods are typically ___.

private and public

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.  A constructor Rectangle(5) would assign 5 to both length and height, but a constructor Rectangle(6, 7) would assign 6 to length and 7 to height.  

200

___ refers to the attributes/methods belonging to the class itself, and ___ refers to the attributes/methods belonging to an instance of the class.

static and non-static

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 Student 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

The class Book has two instance variables; a String title, and an int pages.  Write a constructor for Book that takes in one parameter, String myTitle.   It will set the number of pages to a default of 100.

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

300

The name of a method you can include in your class in order to return a pre-defined string when an instance of the class is directly called (ex: print(object)).

toString

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

Used to return the flow of control to the point immediately following where the method or constructor was called

return

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, the division results in a double.

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 with a parameter int "newLevel", and sets the instance variable level to that value.

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


500

Type of variable declared in the body of constructors and methods  

local variable

M
e
n
u