Java Basics Programmer Practice Test

1) Which of the following method signatures is a valid declaration of an entry point in a Java application?

1. public void main(String[] args)
2. public static void main()
3. private static void start(String[] mydata)
4. public static final void main(String[] mydata)
 

2) Given that a Date class exists in both the java.util and java.sql packages, what is the result of compiling the following class?

1: import java.util.*;
2: import java.sql.*;
3: public class BirthdayManager {
4: private Date rob = new Date();
5: private java.util.Date sharon = new 
java.util.Date();
6: }

1. The code does not compile because of lines 1 and 2.
2. The code does not compile because of line 4.
3. The code does not compile because of line 5.
4. The code compiles without issue.
 

3) Which of the following is not a facet of traditional object-oriented programming languages?

1. Objects are grouped as procedures, separate from the data they act on.
2. An object can take many forms via casting.
3. An object can hold data, referred to as attributes.
4. An object can perform actions, via methods.
 

4) Which variables have a scope limited to a method?

1. Interface variables
2. Class variables
3. Instance variables
4. Local variables
 

5) Which package is imported into every Java class by default?

1. java.util
2. java.lang
3. system.lang
4. java.system
 

6) Which of the following is not a valid code comment in Java?

1. // Add 5 to the result
2. /*** TODO: Fix bug 12312 ***/
3. # Add configuration value
4. /* Read file from system ****/
 

7) Which statement about a valid .java file is true?

1. It can only contain one class declaration.
2. It can contain one public class declaration and one public interface
definition.
3. It must define at least one public class.
4. It may define at most one public class.
 

8) Which statement about import statements is true?

1. The class will not compile if it contains unused import statements.
2. Unused import statements can be removed from the class without causing a class to become unable to be compiled. 
3. The class will not compile if a duplicate import statement is present.
4. If a class contains an import statement for a class used in the program that cannot be found, it can still compile.
 

9) What is the result of compiling and executing the following class?

1: public class ParkRanger {
2: int birds = 10;
3: public static void main(String[] data) {
4: int trees = 5;
5: System.out.print(trees+birds);
6: }
7: }

1. It does not compile.
2. It compiles but throws an exception at runtime.
3. It compiles and outputs 5.
4. It compiles and outputs 15.
 

9) Which statements about Java are true?
The java command can execute .java and .class files.
Java is not object oriented.
The javac command compiles directly into native machine code.
1. I only
2. III only
3. II and III
4. None are true.
 

11) Which of the following lines of code is not allowed as the first line of a Java class file?

1. import widget.*;
2. // Widget Manager
3. package sprockets;
4. int facilityNumber;

--- 

Answer:

1) D. 

An entry point in a Java application consists of a main() method with a single String[] argument, return type of void, and modifiers public and static. The name of the variable in the input argument does not matter. 

Option A is missing the static modifier, Option B is missing the String[] argument, and Option C has the wrong access modifier and method name.

Only Option D fulfills these requirements. Note that the modifier final is optional and may be added to an entry point method.

2) B. 

The fact that the Date class exists in both packages does not impact the ability to import both packages, so lines 1 and 2 compile without issue, and
Option A is incorrect. Line 4 will not compile because the Date class used is ambiguous, making Option B correct and Option D incorrect. 

Finally, Option C is incorrect because line 5 does compile, as the fully qualified name of the class is used.

3). A. 

Options B, C, and D are each attributes of traditional object-oriented programming. 

Option A is incorrect as an object-oriented project tends to group data and the actions related to that data into a single object. 

4) D. 

Only local variables have such a small scope, making Option D the correct answer.

5) B. 

The package java.lang is imported into every Java class, so Option B is correct. The other options must be explicitly imported. Option A exists but must be explicitly imported. Options C and D do not exist in the standard Java runtime.
 

6) C. 

Java accepts Options A, B, and D as valid comments. Note that the /* */ syntax can have additional (and uneven) star (*) characters as shown in B and D. 

Option C is incorrect as hashtag (#) is not a valid comment character in Java.
 

7) D. 

A valid .java file may define any number of classes or interfaces but have at most one public class. It can also not define any public classes. 

For these reasons, Option A, B, and C are incorrect, leaving Option D as the only correct answer.

8) B. 

A class will compile if it has unused or redundant import statements, making Option A and C incorrect. 

Option D is also incorrect as the compiler must be able to locate the class of the import statement. The correct answer is Option B. Removing unused import statements does not cause a class to become unable to be compiled.

9) A. 

The code does not compile because of line 5, making Option A the correct answer. For this question, it helps to understand variable scope. The main() method is static and does not have access to any class instance variables. The birds variable is not static and requires a class instance variable to access. Therefore, the code does not compile when the static method attempts to access a non-static variable without an instance of the class.

10) D. 

The java command can only execute compiled .class files, so I is false. Java is most certainly object oriented, one of the key design principles, so II is also false. The javac command compiles into bytecode, which must be run in a Java virtual machine (JVM), and is not native machine code, so III is false as well. Since none of the statements are true, Option D is the correct answer.

11) D. 

A class can start with a comment, an optional package statement, or an import statement if there is no package statement. It cannot start with a variable definition, making Option D the correct answer.

Java Tips

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.