Courseiva
Working with Arrays and CollectionshardMultiple ChoiceObjective-mapped

1Z0-829 Working with Arrays and Collections Practice Question

Exhibit

Refer to the exhibit.

Error log:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
    at com.example.Cache.get(Cache.java:12)
    at com.example.Main.main(Main.java:10)

Source code:
public class Cache {
    private Map<String, Object> store = new HashMap<>();
    public <T> T get(String key, Class<T> type) {
        Object value = store.get(key);
        return type.cast(value);
    }
}

public class Main {
    public static void main(String[] args) {
        Cache cache = new Cache();
        cache.store.put("age", 30);
        String age = cache.get("age", String.class);
        System.out.println(age);
    }
}

What is the cause of the ClassCastException?

⚠ Common exam trap

Candidates often confuse the lack of type safety (Option A) with the direct cause of the exception, which is the actual type mismatch at retrieval time.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

The value stored is Integer but retrieved as String.

The ClassCastException occurs because the code retrieves a value from the store map and attempts to cast it to String, but the actual stored object is an Integer. Since the map is not using generics (raw type), the compiler does not enforce type safety, allowing the mismatch to be caught only at runtime.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The store map is not type-safe.

    Why it's wrong here

    It's not the direct cause; the cast is.

  • The value stored is Integer but retrieved as String.

    Why this is correct

    Correct cause.

  • The get method should use (T) cast instead of type.cast().

    Why it's wrong here

    Still would throw ClassCastException.

  • The type parameter T is not used correctly.

    Why it's wrong here

    It is used correctly; the mismatch is the issue.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.