Basic Review of Java

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.
 

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.
 

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

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.
 

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
 

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(
} }

A. 0
B. 1
C. 2
D. 3
E. 4
F. Does not compile
 

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.
 

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
 

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(
} }
 

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
 

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)

---

Answers:

1. 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. 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. 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. C, D. 

The Tank class is there to throw you off since it isn’t used by AquariumVisitor. 

Option C is correct because it imports Jelly by class name. 

Option D is correct because it imports all the classes in the jellies package, which includes Jelly. 

Option A is incorrect because it only imports classes in the aquarium package—Tank in this case —and not those in lowerlevel packages. 

Option B is incorrect because you cannot use wildcards anywhere other than the end of an import statement. 

Option E is incorrect because you cannot import parts of a class with a regular import statement. 

Option F is incorrect because options C and D do make the code compile.

5. A, C, D, E. 

Eclipse is an integrated development environment (IDE). It is not included in the Java Development Kit (JDK), making option B incorrect. The JDK comes with a number of command-line tools including a compiler, packager, and documentation, making options A, D, and E correct. The JDK also includes the
Java Virtual Machine (JVM), making option C correct.

6. E. 

The first two imports can be removed because java.lang is automatically imported. The following two imports can be removed because Tank and Water are in the same package, making the correct option E. If Tank and Water were in different packages, exactly one of these two imports could be removed. In that case, the answer would be option D.

7. A, B, C. 

Option A is correct because it imports all the classes in the aquarium package including aquarium.Water. 

Options B and C are correct because they import Water by class name. Since importing by class name takes precedence over wildcards, these compile. 

Option D is incorrect because Java doesn’t know which of the two wildcard Water classes to use. 

Option E is incorrect because you cannot specify the same class name in two imports. 

8. A, B. 

The wildcard is configured for files ending in .java, making options E and F incorrect. Additionally, wildcards aren’t recursive, making options C and D incorrect. Therefore, options A and B are correct.

9. B. 

Option B is correct because arrays start counting from zero and strings with spaces must be in quotes. 

Option A is incorrect because it outputs Blue. 

C is incorrect because it outputs Jay. 

Option D is incorrect because it outputs Sparrow. 

Options E and F are incorrect because they output java.lang.ClassNotFoundException:
BirdDisplay .class.

10. E. 

Option E is the canonical main() method signature. You need to memorize it. 

Option A is incorrect because the main() method must be public. 

Options B and F are incorrect because the main() method must have a void return type. 

Option C is incorrect because the main() method must be static. 

Option D is incorrect because the main() method must be named main.

Java Tips

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Do you have a Java Problem?
Ask It in The Java Forum

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.