Java Data Types Questions and Answers

1. Which of the following declarations does not compile?

1. double num1, int num2 = 0;
2. int num1, num2;
3. int num1, num2 = 0;
4. int num1 = 0, num2 = 0;
 

2. What is the output of the following?

public static void main(String... args) {
      String chair, table = "metal";
     chair = chair + table;
     System.out.println(chair);
}

1. metal
2. metalmetal
3. nullmetal
4. The code does not compile.
 

3. Which is correct about an instance variable of type String?

1. It defaults to an empty string.
2. It defaults to null.
3. It does not have a default value.
4. It will not compile without initializing on the declaration line.
 

4. Which of the following is not a valid variable name?

1. _blue
2. 2blue
3. blue$
4. Blue
 

5. Which of these class names best follows standard Java naming conventions?

1. fooBar
2. FooBar
3. FOO_BAR
4. F_o_o_B_a_r
 

6. How many of the following methods compile?

public String convert(int value) {
     return value.toString();
}
public String convert(Integer value) {
     return value.toString();
}
public String convert(Object value) {
     return value.toString();
}

1. None
2. One
3. Two
4. Three
 

7. Which of the following does not compile?

1. int num = 999;
2. int num = 999;
3. int num = 999;
4. None of the above; they all compile.
 

8. Which of the following is a wrapper class?

1. int
2. Int
3. Integer
4. Object
 

9. What is the result of running this code?

public class Values {
     integer a = Integer.valueOf("1");
     public static void main(String[] nums) {
          integer a = Integer.valueOf("2");
          integer b = Integer.valueOf("3");
          System.out.println(a + b);
     }
}

1. 4
2. 5
3. The code does not compile.
4. The code compiles but throws an exception at runtime.
 

10. Which best describes what the new keyword does?

1. Creates a copy of an existing object and treats it as a new one
2. Creates a new primitive
3. Instantiates a new object
4. Switches an object reference to 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
various legal combinations of combining multiple variables in the same declarations with optional default values.

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