|
1. Which of the following declarations does not compile?
1. double num1, int num2 = 0;
2. What is the output of the following? public static void main(String... args) {
1. metal
3. Which is correct about an instance variable of type String? 1. It defaults to an empty string.
4. Which of the following is not a valid variable name? 1. _blue
5. Which of these class names best follows standard Java naming conventions? 1. fooBar
6. How many of the following methods compile? public String convert(int value) {
1. None
7. Which of the following does not compile? 1. int num = 999;
8. Which of the following is a wrapper class? 1. int
9. What is the result of running this code? public class Values {
1. 4
10. Which best describes what the new keyword does? 1. Creates a copy of an existing object and treats it
as a new one
--- Answers: 1. A. Option A does not compile because Java does not allow
declaring different types as part of the same declaration. The other three
options show
2. D. The table variable is initialized to "metal". However, chair is not initialized. In Java, initialization is per variable and not for all the variables in a single declaration. Therefore, the second line tries to reference an uninitialized local variable and does not compile, which makes Option D correct. 3. B. Instance variables have a default value based on the type. For any nonprimitive, including String, that type is a reference to null. Therefore Option B is correct. If the variable was a local variable, Option C would be correct. 4. B. An identifier name must begin with a letter, $, or _. Numbers are only permitted for subsequent characters. Therefore, Option B is not a valid variable name. 5. B. In Java, class names begin with an uppercase letter by convention. Then they use lowercase with the exception of new words. Option B follows this convention and is correct. Option A follows the convention for variable names. Option C follows the convention for constants. Option D doesn’t follow any Java conventions. 6. C. Objects have instance methods while primitives do not. Since int is a primitive, you cannot call instance methods on it. Integer and String are both objects and have instance methods. Therefore, Option C is correct. 7. C. Underscores are allowed between any two digits in a numeric literal. Underscores are not allowed at the beginning or end of the literal, making Option C the correct answer. 8. C. Option A is incorrect because int is a primitive. Option B is incorrect because it is not the name of a class in Java. While Option D is a class in Java, it is not a wrapper class because it does not map to a primitive. Therefore, Option C is correct. 9. C. There is no class named integer. There is a primitive int and a class Integer. Therefore, the code does not compile, and Option C is correct. If the type was changed to Integer, Option B would be correct. 10. C. The new keyword is used to call the constructor for a class and instantiate an instance of the class. A primitive cannot be created using the new keyword. Dealing with references happens after the object created by new is returned. |
|
Java Books
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.
|