Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Week 15

Reading

Lambdas:

Stream API:

Records

Slides

Download Slides

Assignment

The assignment for this week consists of a series of questions designed to emulate the style of the two-hour written exam. However, the scope, difficulty, and length of these questions may not reflect those of the actual exam.

While the assignment can be easily completed using online resources, lecture slides, and the textbook, I recommend attempting it under exam-like conditions for the most authentic experience: work alone, without any reference materials.

Question 1 Write seven Java keywords.
Question 2 What is a class file?
Question 3 What is double buffering?
Question 4 Write the Java comparison operators.
Question 5
  • Write an expression that causes an ArithmeticException.
  • Write an expression that evaluates to NaN.
Question 6 Write a Java program that causes the compiler error:
incompatible types: possible lossy conversion from long to int
Question 7 In the context of `StdDraw`, what do `setXscale` and `setYscale` do?
Question 8 Given:
static double compoundInterest(double principal, double rate, int years) {
    return principal * Math.pow(1 + rate, years);
}

Identify:

  • the method signature.
  • the method name.
  • the formal parameters.
  • the return type.
  • the method body.
Question 9 Given the program:
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello");
        System.out.println("World!");
    }
}

What is printed if you run:

$ javac Main.java
$ java Main.class
Question 10 Write a program that takes an integer `n` and prints the first `n` digits of pi:
$ java Pi 1
3
$ java Pi 3
3.14
  • Hint: You may use Math.PI
  • Hint: You may assume n is less than 10.
Question 11 Rewrite the code below using while-loop(s):
for (int i = 0, j = 10; i < j; i++, j--) {
    System.out.println(i + " " + j);
}
Question 12 Are all variables within scope?
int sum = 0;
int i = 0;
while (i < 10) {
int tmp = 0;
if (i % 2 == 0){
for (int j = 0; j < 10; j += 2){
tmp -= j;}} else {
    for (int j = 0; j < 10; j += 2){
tmp += j; }}
sum += tmp * j; i++; }