Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
What are generics in Java?
A
A way to create multiple methods with the same name.
B
A mechanism to perform dynamic type casting at runtime.
C
A feature that allows classes and methods to operate on objects of various types while providing compile-time type safety.
D
A feature that enables multiple inheritance of classes.
Question 2
Which of the following is the correct way to declare a generic class?
A
public class Box { }
B
public class Box { }
C
public class Box<T,> { }
D
public class Box<?>{ }
Question 3
Which is the correct syntax to define a generic method that prints the elements of an array?
A
public void printArray(T[] array) { ... }
B
public void printArray(Object[] array) { ... }
C
public void printArray(T[] array) { ... }
D
public void printArray(T[] array) { ... }
Question 4
Consider the following code snippet:
public class Box<T> {
    private T content;
    
    public Box(T content) {
        this.content = content;
    }
    
    public T getContent() {
        return content;
    }
}
public class Test {
    public static void main(String[] args) {
        Box<Integer> intBox = new Box<Integer>(100);
        System.out.println("Content: " + intBox.getContent());
    }
}
What will be the output?
A
Content: 100
B
Content: null
C
A compile-time error occurs due to type mismatch.
D
A runtime error occurs because generics are erased.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next