LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Exercise 1 of 4

Object Class and Wrapper Classes Object and Wrapper Classes MCQ

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on java.lang.Object and the wrapper classes: toString, equals, hashCode, clone, autoboxing, unboxing, the Integer cache, parsing and BigDecimal. Pick an option to check your answer instantly.

Question 11 Medium

For which values does Integer.valueOf always return cached objects?

A 0 to 255
B −128 to 127
C −32768 to 32767
D All values
Question page & discussion
Question 12 Easy

What is the correct way to check whether two Integer variables hold the same number?

A a == b
B a.equals(b) (or Objects.equals(a, b) if either may be null)
C a = b
D a.compareTo(b) == 1
Question page & discussion
Question 13 Medium

What happens at int n = map.get("missing"); when the key is absent (map is a Map<String, Integer>)?

A n becomes 0
B NullPointerException – get returns null, which cannot be unboxed
C Compile error
D n becomes -1
Question page & discussion
Question 14 Easy

What is the difference between Integer.parseInt(s) and Integer.valueOf(s)?

A None
B parseInt returns a primitive int; valueOf returns an Integer object
C valueOf throws no exceptions
D parseInt works only for hex
Question page & discussion
Question 15 Easy

What does Integer.parseInt("12.5") do?

A Returns 12
B Throws NumberFormatException
C Returns 13
D Returns 0
Question page & discussion
Question 16 Medium

What does count++ do when count is an Integer?

A Changes the Integer object in place
B Unboxes, adds 1, and boxes a new (or cached) Integer that count now refers to
C Compile error
D Throws an exception
Question page & discussion
Question 17 Hard

What does Long.valueOf(5).equals(5) return?

A true
B false – 5 is boxed to Integer, and Long.equals only accepts Long
C Compile error
D NullPointerException
Question page & discussion
Question 18 Medium

Which creates exactly 0.1?

A new BigDecimal(0.1)
B new BigDecimal("0.1")
C BigDecimal.valueOf(0.1f)
D new BigDecimal(1 / 10)
Question page & discussion
Question 19 Hard

Why does new BigDecimal("2.0").equals(new BigDecimal("2.00")) return false?

A A bug
B BigDecimal.equals compares both value and scale; use compareTo for numeric equality
C Strings are compared
D They are different numbers
Question page & discussion
Question 20 Easy

What is Objects.hash(a, b, c) typically used for?

A Encrypting passwords
B Implementing hashCode() from several fields
C Comparing objects
D Creating unique IDs
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links