Chapter 1 Flashcards

1
Q

What are the key (identifying) benefits of Java

A
  • Object Oriented
  • Encapsulation
  • Platform independent
  • Robust
  • Simple
  • Secure
  • Multithreaded
  • Backward Compatibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What key pieces does the Java Development Kit contain?

A
  • The compiler (javac)
  • The launcher (java)
  • The Java Virtual Machine (JVM)
  • javadoc
  • jar
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an object?

A

A runtime instance of a class in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a reference?

A

A variable that points to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the two primary elements of a Java class?

A

Methods and fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the simplest valid Java class we can write?

A

public class Animal { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The method signature consists of .. ?

A

The method name and parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does Java require a class to be public?

A

Java does not require a class to be public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given a file with two or more classes, how many are allowed to be public? How many must be public?

A

Only one is allowed to be public (must match file name) & none are required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where does Java begin execution?

A

By calling the main() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What command do we use to check the version of java?

A

java -version

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using the terminal, how can we compile and then run the file Zoo.java?

A
  1. javac Zoo.java

2. java Zoo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Using the terminal with single-file source-code, how can we compile and then run the file Zoo.java?

A
  1. java Zoo.java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the constraint of single-file source-code?

A

It can only be used if your program contains one file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the differences between using the commands javac + java and single-file source-code to compile and run a program?

A

Full command

  • need to use two commands
  • Produces a class file
  • For any program
  • Can import code in any available Java library

Single-file source-code

  • one command
  • Fully in memory (no class files)
  • For programs with one file
  • Can only import code that came with the JDK
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Is using java.util.Random more efficient than using java.util.* to import the Random class?

A

Java automatically imports only the classes that are needed so in the end only Random will be importer in both cases.

17
Q

There is one special package in Java that is implicitely imported by Java. Which one is it?

A

java.lang

18
Q

What package do the Files and Paths class belong to?

A

java.nio.file

19
Q

How many wildcards (*) are allowed in a package declaration?

A

One, and it must be at the end.

20
Q

What does java do with “ties” for precedence in import declarations?

example:

import java.util.Date;
import java.sql.Date;

A

It throws an error:

- error: reference to Date is ambiquous

21
Q

Compile and run the following two classes using the java terminal commands:

  • packagea/ClassA
  • packageb/ClassB (contains the main method)
A
  • javac packagea/ClassA.java packageb/ClassB.java

- java packageb.ClassB

22
Q

Where does the javac command place the compiled classes by default?

A

In the same directory as the source code.

23
Q

How can we use the javac command to compile ‘packagea/ClassA’ in the directory ‘classes’

A

javac -d classes packagea/ClassA.java

24
Q

Compile and run the two classes below using the java terminal commands. Compile them into a directory called ‘classes’.

  • packagea/ClassA
  • packageb/ClassB (contains the main method)
A
  1. javac -d classes packagea/ClassA.java packageb/ClassB.java
  2. java -cp classes packageb.ClassB
25
Q

What options are there to specify the classpath to the java or javac command?

A
  • ‘-cp’
  • ‘-classpath’
  • ’–class-path’
26
Q

Create a JAR file: ‘myNewFile.jar’ based on the current directory.

A

jar -cvf myNewFile.jar .
– or –
jar –create –verbose –file myNewFile.jar .

27
Q

Create a JAR file: ‘myNewFile.jar’ based on the directory ‘dir’

A

jar -cvf myNewFile.jar -C dir .

28
Q

Is the following entry point method allowed?

  • static public void main(String [ ]args) { }
A

yes