Chapter 1 Review Questions Flashcards
1.Which of the following are true statements? (Choose all that apply.)
A. Java allows operator overloading.
B. Java code compiled on Windows can run on Linux.
C. Java has pointers to specific locations in memory.
D. Java is a procedural language.
E. Java is an object-oriented language.
F. Java is a functional programming language.
B, E.
C++ has operator overloading and pointers. Java made a point of not having either. Java does have references to objects, but these are pointing to an object that can move around in memory. Option B is correct because Java is platform independent. Option E is correct because Java is object-oriented. While it does support some parts of functional programming, these occur within a class.
2.Which of the following are true? (Choose all that apply.)
A. javac compiles a .class file into a .java file.
B. javac compiles a .java file into a .bytecode file.
C. javac compiles a .java file into a .class file.
D. java accepts the name of the class as a parameter.
E. java accepts the filename of the .bytecode file as a parameter.
F. java accepts the filename of the .class file as a parameter.
C, D.
Java puts source code in .java files and bytecode in .class files. It does not use a .bytecode file. When running a Java program, you pass just the name of the class without the .class extension.
3.Which of the following are true if this command completes successfully assuming the CLASSPATH is not set? (Choose all that apply.)
java MyProgram.java
A .class file is created.
B. MyProgram can reference classes in the package com.sybex.book.
C. MyProgram can reference classes in the package java.lang.
D. MyProgram can reference classes in the package java.util.
E. None of the above. The program needs to be run as java MyProgram.
C, D.
This example is using the single-file source-code launcher. It compiles in memory rather than creating a .class file, making option A incorrect. To use this launcher, programs can only reference classes built into the JDK. Therefore, option B is incorrect, and options C and D are correct.
4.Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile? (Choose all that apply.)
package aquarium; public class Tank { } package aquarium.jellies; public class Jelly { } package visitor; INSERT IMPORTS HERE public class AquariumVisitor { public void admire(Jelly jelly) { } }
A. import aquarium.;
B. import aquarium..Jelly;
C. import aquarium.jellies.Jelly;
D. import aquarium.jellies.;
E. import aquarium.jellies.Jelly.;
F. None of these can make the code compile.
C, D
5.Which are included in the JDK? (Choose all that apply.)
A.javac
B.Eclipse
C.JVM
D.javadoc
E.jar
F.None of the above
A, C, D, E
6.Given the following classes, what is the maximum number of imports that can be removed and have the code still compile?
package aquarium; public class Water { } package aquarium; import java.lang.*; import java.lang.System; import aquarium.Water; import aquarium.*; public class Tank { public void print(Water water) { System.out.println(water); } }
A. 0
B. 1
C. 2
D. 3
E. 4
F. Does not compile
E
7.Given the following classes, which of the following snippets can independently be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply.)
package aquarium; public class Water { boolean salty = false; } package aquarium.jellies; public class Water { boolean salty = true; } package employee; INSERT IMPORTS HERE public class WaterFiller { Water water; }
A. import aquarium.;
B. import aquarium.Water;
import aquarium.jellies.;
C. import aquarium.;
import aquarium.jellies.Water;
D. import aquarium.;
import aquarium.jellies.*;
E. import aquarium.Water;
import aquarium.jellies.Water;
F.None of these imports can make the code compile.
A, B, C
8.Given the following command, which of the following classes would be included for compilation? (Choose all that apply.)
javac *.java
A. Hyena.java
B. Warthog.java
C. land/Hyena.java
D. land/Warthog.java
E. Hyena.groovy
F. Warthog.groovy
A, B
9.Given the following class, which of the following calls print out Blue Jay? (Choose all that apply.)
public class BirdDisplay { public static void main(String[] name) { System.out.println(name[1]); } }
A. java BirdDisplay Sparrow Blue Jay
B. java BirdDisplay Sparrow “Blue Jay”
C. java BirdDisplay Blue Jay Sparrow
D. java BirdDisplay “Blue Jay” Sparrow
E. java BirdDisplay.class Sparrow “Blue Jay”
F. java BirdDisplay.class “Blue Jay” Sparrow
B
10.Which of the following are legal entry point methods that can be run from the command line? (Choose all that apply.)
A. private static void main(String[] args)
B. public static final main(String[] args)
C. public void main(String[] args)
D. public static void test(String[] args)
E. public static void main(String[] args)
F. public static main(String[] args)
E
11.Which of the following are true statements about Java? (Choose all that apply.)
A. Bug-free code is guaranteed.
B. Deprecated features are never removed.
C. Multithreaded code is allowed.
D. Security is a design goal.
E. Sideways compatibility is a design goal.
C, D
12.Which options are valid on the javac command without considering module options? (Choose all that apply.)
A. -c
B. -C
C. -cp
D. -CP
E. -d
F. -f
G. -p
C, E
13.Which options are valid on the java command without considering module options? (Choose all that apply.)
A. -c
B. -C
C. -cp
D. -d
E. -f
F. -p
C
14.Which options are valid on the jar command without considering module options? (Choose all that apply.)
A. -c
B. -C
C. -cp
D. -d
E. -f
F. -p
A, B, C, E
15.What does the following code output when run as java Duck Duck Goose?
public class Duck { public void main(String[] args) { for (int i = 1; i <= args.length; i++) System.out.println(args[i]); } }
A. Duck Goose
B. Duck ArrayIndexOutOfBoundsException
C. Goose
D. Goose ArrayIndexOutOfBoundsException
E. None of the above
D