java 8 jshell mock questions

I gave a presentation showing how to make mock exam questions. We used Java 9 JShell as the example. The idea was to pick a feature nobody knew much about. We started by picking key points from this blog. Then we made questions. This post is the results. (Edited for formatting, to alphabetize answers, after confirming, etc). Good job all!

Question 1

What does REPL stand for?

  1. Random Enterprise Process Logic
  2. Rapid Enterprise Process Line
  3. Rapid Execution of Programming Logic
  4. Read Evaluate Print Loop
  5. Read Evaluate Print Line
  6. Read Execute Print Loop

Question 2

When typing into JShell interactively, it uses ______.

  1. Nashorn
  2. JRE
  3. JS
  4. JVM
  5. REPL
  6. RPEL
  7. Streams

Question 3

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. Class.forName(“String.class”)
  2. Class.forName(“String.class”);
  3. Thread.sleep(1000)
  4. Thread.sleep(1000);
  5. System.out.println(“hello”)
  6. System.out.println(“hello”);

Question 4

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. void method() { throw new Exception(); }
  2. void method() { throw new RuntimeException(); }
  3. void method() throw Exception { throw new Exception(); }
  4. void method() throw Exception { throws new Exception(); }
  5. void method() throws Exception { throw new Exception(); }
  6. void method() throw Exception { throws new Exception(); }

Question 5

Which of the following compiles when typed into JShell? (Choose all that apply)

  1. System.out.println(“Hello Java”)
  2. System.out.println(“Hello Java”);
  3. public void method() { throw new Exception(); }
  4. public void method() { throw new Exception(); };
  5. public void method() {System.out.println(“Hello Java”)}
  6. public void method() {System.out.println(“Hello Java”);}
  7. public void method() {System.out.println(“Hello Java”);};

Question 6

What are valid statement terminators in JShell? (Choose all that apply)

  1. ;
  2. :
  3. \n
  4. None (just press enter)
  5. None of the above

Question 7

Which of the following lines will compile and display a value if it’s the first line you type after launching JShell? (Choose all that apply)

  1. 5 + 6
  2. a + b
  3. System.out.println(“Hello”);
  4. System.out.println(“Hello”)
  5. out.println(“Hello”)
  6. out.println(“Hello”);
  7. { System.out.println(7) }
  8. { System.out.println(7); }
  9. 6 + 6; 10
  10. None of the above

Question 8

Which of the following is valid code in JShell? (Choose all that apply)

  1. system.out.println(“hello world”);
  2. system.out.println(hello world);
  3. Println(“hello world”);
  4. “hello word”
  5. hello world
  6. Answer: 5+3
  7. 5f+3
  8. new String()

Question 9

What is the output of the code below if executed in JShell?(Check all that apply)

Integer a = 5;

Integer c = a + 10;

Integer a = 7

a

  1. Compiler Error
  2. Runtime error
  3. 7
  4. Error: Duplicate variable declaration
  5. None of the above

Question 10

Which of the following commands invokes the help command in Jshell?  Choose all that apply.

  1. /h
  2. /help
  3. /?
  4. ?
  5. f1
  6. ctrl-f1
  7. help

Question 11

How would you invoke help on a particular Jshell command?  Choose all that apply.

  1. /help=command
  2. /help:command
  3. /help command
  4. /he command
  5. /h command

Question 12

double area (double radius) {

return 3.14 * square(radius);

}

area(2)

What is the correct output?

  1. 6.28
  2. 6
  3. 7
  4. Attempted to call area(double) which cannot be invoked
  5. Attempted to call square which cannot be invoked

Answers

  1. Choice 4 – Read Evaluate Print Loop
  2. Choice 5 – REPL
  3. All 6 choices are correct. The first two throw an exception, but do compile.  (This question had public in the user group lab. I wrote this question and removed it after)
  4. Choices 2 and 5. The semicolon is only optional at the end of the statement; not within a block
  5. Choices 1, 2, 6 and 7. The last two give a warning about public not being allowed, but work. The others fail because of an exception or missing semicolon within a method.
  6. Choices 1 and 4. Semicolon terminates a statement or you can omit the terminator
  7. Choices 1, 3, 4, 8 and 9. The last one is tricky. It gets executed as two statements.
  8. Choices 4, 7 and 8. JShell is case sensitive; just like Java
  9. Choice 3. (In the user group lab, there was another choice about a warning for a duplicate variable. I couldn’t reproduce the warning so removed that choice to avoid ambiguity.)
  10. Choices 2 and 3. Isn’t memorization fun?
  11. Choices 3 and 4. More memorizing
  12. Choice 4. Creating the method works, but gives a warning that square(double) must be declared. Then calling area gives “attempted to call method area(double) which cannot be invoked until method square(double) is declared”

 

 

 

using docker on mac to run java 9

Last week, I was trying to install Java 9 when I messed up my Mac and had to recover using Time Machine. I didn’t have difficulty using the Java 8 preview. Anyway, I realized I should be using a VM for Java 9 so this doesn’t happen again.

Conveniently, I was reading a book about Docker on the plane when I was traveling right after that. Which meant I had a use case for installing Docker on my home machine. It was easy and I went from not having Docker installed to being able to use it for Java 9 in under 30 minutes.

Installing

Since Mac isn’t Linux, the first step is to install the Docker Toolbox for OS X. This uses half a gig of disk space. Which is fair since it is running a VM.  Overall, the Docker getting started docs for Mac are excellent. It took a few minutes the first time I ran the Docker Quickstart Client as I saw:

Creating CA: /Users/nyjeanne/.docker/machine/certs/ca.pem
Creating client certificate: /Users/nyjeanne/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Copying /Users/nyjeanne/.docker/machine/cache/boot2docker.iso to /Users/nyjeanne/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Found a new host-only adapter: "vboxnet1"
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: /usr/local/bin/docker-machine env default
##         .
## ## ##        ==
## ## ## ## ##    ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
\______ o           __/
\    \         __/
\____\_______/
docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Initial test

I ran:

docker run hello-world

It worked as expected. Pretty easy.

Java 9 beta container

Finding the relevant library on DockerHub was pretty easy. There is an official Java library.

First I accidentally ran docker with only the tag:

docker run openjdk-9

It probably isn’t a surprise that I got an error. It just wasn’t the error I expected:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

Luckily, a github issue helped. I needed to set the environment variables. The issue said to run:

eval $(docker-machine env default)

I ran the docker-machine command first so I knew what I was evaluating. It was harmless

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/nyjeanne/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env default)

Then I got the message I was expecting:

Unable to find image 'openjdk-9:latest' locally
Pulling repository docker.io/library/openjdk-9
docker: Error: image library/openjdk-9 not found.
See 'docker run --help'.

Running the command correctly was far more productive:

docker run java:openjdk-9

It started downloading the container. While it was downloading, it looked like:

52e20300f877: Downloading 20.54 MB/52.71 MB
a3ed95caeb02: Download complete
fb4c7723b752: Download complete
45c1fd7a2514: Download complete
d12ad3d1f060: Download complete
80f5aeb42fe5: Download complete
a6fac263fca7: Download complete
8cb87a5d8c56: Downloading 15.12 MB/215.8 MB

And when it was done, I had:

Unable to find image 'java:openjdk-9' locally
openjdk-9: Pulling from library/java
52e20300f877: Pull complete
a3ed95caeb02: Pull complete
fb4c7723b752: Pull complete
45c1fd7a2514: Pull complete
d12ad3d1f060: Pull complete
80f5aeb42fe5: Pull complete
a6fac263fca7: Pull complete
8cb87a5d8c56: Pull complete
Digest: sha256:49a691b3e64950a45a45dbac6fde08636bded81acf4a36807d12208d3d3af293
Status: Downloaded newer image for java:openjdk-9

Then I ran it interactive mode so I could test jshell:

docker run -i java:openjdk-9

By default, there is no prompt, but you can type unix commands. I typed

jshell

To get out of jshell, you type /exit and to get of the container, you type exit.

End to end success

This is pretty easy when you aren’t doing it wrong:

nyjeanne$ docker run -i java:openjdk-9
javac -version
javac 9-internal
jshell
May 02, 2016 12:17:02 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 9-internal
|  For an introduction type: /help intro
jshell> 1+2
1+2
$1 ==> 3
jshell> new File("abc")
new File("abc")
$2 ==> abc
 
jshell> java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
java.util.stream.Stream.of(11, 2, 16).sorted().findFirst()
$3 ==> Optional[2]
 
jshell> /exit
/exit
|  Goodbye
exit

Initial impressions of JShell

Granted it isn’t released yet, but I’m not more impressed with JShell than I was with Nashorn’s jjs. There’s still no tab autocompleted or up arrow support. And while there are a couple imports included by default, NIO and Streams packages are not in that list.

recovering postgres from a time machine backup

Last night, I needed to do a full Time Machine restore on my Mac. I messed up installing things and made the problem far worse in my attempt to “fix” it. Then I tried to rollback a fraction of the disk. The laptop predicted 11 hours to rollback the files. It was easier to rollback to the state a few hours earlier. The only time I did a full Time Machine restore was when I got a new computer. That was going forward in calendar time though. This is the first time I went backwards.

For the most part, it was easy. The Mac prompted me on whether it was ok to erase the disk and restore. That took about two hours. My computer was pretty much set up on restore. It remembered lots down to my internet connection and github key. I got re-prompted from my Dropbox credentials (and for two factor.) No big deal.

Then there was Postgres. Every few seconds, I got the message “Do you want the application “postgres” to accept incoming network connections?” The message only appeared for about a second. Even if I was fast enough to click on it, it was back a few seconds later.

I tried signing the app based on some internet posts. No luck.

Temporary Relief

I got temporarily relief while I looked into the problem by blocking all connections:

  • System Preferences
  • Security & Preferences
  • Click the lock to make changes
  • My password
  • Click unlock
  • Firewall Options
  • Click “Block all incoming connections”
  • Ok

This is not helpful in the long run, but it let me look at things without going crazy.

The problem

I checked the database logs in /Library/PostgreSQL/8.4/datapg_log and found a file every few seconds with:

2016-04-25 20:39:15 EDT LOG:  database system was interrupted; last known up at 2016-04-24 09:18:51 EDT
2016-04-25 20:39:15 EDT LOG:  record with zero length at 0/5C42472C
2016-04-25 20:39:15 EDT LOG:  invalid primary checkpoint record
2016-04-25 20:39:15 EDT LOG:  record with zero length at 0/5C4246E8
2016-04-25 20:39:15 EDT LOG:  invalid secondary checkpoint record
2016-04-25 20:39:15 EDT PANIC:  could not locate a valid checkpoint record
2016-04-25 20:39:15 EDT LOG:  startup process (PID 2326) was terminated by signal 6: Abort trap
2016-04-25 20:39:15 EDT LOG:  aborting startup due to startup process failure

Ah ha! That makes perfect sense. I jumped around in time which would confuse a transaction log. Now,t hat’s something I know how to fix. I did a forced reset of the transaction log:

  1. sudo su postgres
  2. cd /Library/PostgreSQL/8.4/bin
  3. ./pg_resetxlog -f /Library/PostgreSQL/8.4/data

And all better! I turned the firewall connections back to the way they were before I started and good as new.