|
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) 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.*;
1. The code does not compile because of lines 1 and 2.
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.
4) Which variables have a scope limited to a method? 1. Interface variables
5) Which package is imported into every Java class by default? 1. java.util
6) Which of the following is not a valid code comment in Java? 1. // Add 5 to the result
7) Which statement about a valid .java file is true? 1. It can only contain one class declaration.
8) Which statement about import statements is true? 1. The class will not compile if it contains unused import
statements.
9) What is the result of compiling and executing the following class? 1: public class ParkRanger {
1. It does not compile.
9) Which statements about Java 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.*;
--- 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
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. |
|
Do you have a Java Problem?
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|