WorksheetsOOP - session 2.1 - JVM, Array, Stack
Total questions: 21
Worksheet time: 13mins
Which of the following is the correct way to declare and allocate an array of 10 integers in Java?
int arr = new int[10];
int arr[] = new int[10];
int arr = [10];
int[] arr = int[10];
Which statement best describes arrays in Java?
Arrays are primitive types and their size can be changed at runtime.
Arrays are objects stored on the heap and have a fixed size once created.
Arrays are automatically resized like Python lists when elements are added.
Arrays are stored in the stack and allow dynamic memory allocation.
int[] numbers = new int[5];
What are the default values of the elements in the numbers array?
They are undefined and accessing them will cause a runtime error.
All elements are set to 0.
All elements are set to null.
They contain random garbage values.
Given an array declared as
String[] names = new String[3];
what happens if you try to access names[3]?
The compiler automatically resizes the array to include the index.
It returns null because the element has not been assigned a value.
It throws an ArrayIndexOutOfBoundsException at runtime.
It returns an empty string.
Which of the following statements about multi-dimensional arrays in Java is true?
Multi-dimensional arrays in Java are arrays of arrays, meaning the subarrays (rows) can be of different lengths (jagged arrays).
Java does not support multi-dimensional arrays.
Multi-dimensional arrays in Java are implemented as linked lists under the hood.
Multi-dimensional arrays in Java are truly multi-dimensional like in C, and their rows must all be of equal length.
Which of the following best describes a stack data structure in Java?
A data structure that follows the Last In, First Out (LIFO) principle.
A structure that supports random access to its elements.
A non-linear data structure optimized for search operations.
A data structure that follows the First In, First Out (FIFO) principle.
Which Java class provides a direct implementation of a stack?
java.util.ArrayList
java.util.Stack
java.util.LinkedList
java.util.Queue
Which method is used in the java.util.Stack class to add an element to the top of the stack?
add()
insert()
push()
append()
What exception is thrown when attempting to remove (pop) an element from an empty Java stack?
ArrayIndexOutOfBoundsException
EmptyStackException
NullPointerException
NoSuchElementException
Python lists can be used to implement stack behavior. Which of the following correctly compares the two approaches?
Java’s Stack does not allow duplicate elements, unlike Python lists.
Python lists require manual resizing, whereas Java’s Stack automatically resizes.
Both Python lists (using append() and pop()) and Java’s Stack (using push() and pop()) support dynamic resizing and similar LIFO operations.
Python lists and Java stacks both use the same method names for stack operations.
Why are Java arrays considered static in terms of their size?
Because their size is determined at runtime and can change dynamically.
Because they automatically adjust their size as elements are added or removed.
ecause their size must be specified at the time of creation and cannot be altered later.
Because they are implemented as linked lists.
Which limitation of static arrays is directly related to memory utilization?
They require non-contiguous memory, leading to memory fragmentation.
They allow dynamic resizing, which can lead to unpredictable memory usage.
They automatically shrink when not in use, wasting little memory.
They allocate a contiguous block of memory based on the declared size, possibly wasting space if not fully utilized.
What must a programmer do if they need an array with a larger size than originally declared?
Use an in-built method to automatically resize the array.
Convert the array to a Python list to gain dynamic resizing features.
Create a new array with the required size and copy the existing elements to the new array.
Modify the existing array’s size using reflection.
Which statement best describes the type limitation of Java arrays?
Arrays enforce type homogeneity, meaning all elements must be of the same declared type.
Arrays can store elements of multiple data types in a single array.
Arrays allow mixing primitive types with object types without restrictions.
Arrays automatically convert elements to the most general type.
Which statement best describes the way a stack manages its elements?
Elements are removed in the same order they were added.
Elements are removed based on their priority value.
Elements are removed in the reverse order of their insertion
Elements are randomly removed regardless of their insertion order.
Which of the following best describes the primary function of the Java Virtual Machine (JVM) in the execution of Java applications?
It converts Java source code into machine-specific binary code at compile time.
It provides an abstraction layer that executes Java bytecode while managing system resources and memory.
It directly manages network connections and system I/O without involving memory management.
It serves as a static code analyzer that optimizes source code before runtime.
In the context of the JVM, which memory area is primarily used for the dynamic allocation of objects?
Stack
Heap
Method Area
Code Cache
What is the primary role of the stack memory within the JVM?
Storing objects and arrays created during program execution.
Holding class-level data such as static variables and runtime constants.
Caching compiled code for faster execution.
Maintaining local variables, method call information, and execution context for each thread.
What is the primary purpose of garbage collection in the JVM?
To manually free memory by requiring explicit calls from the programmer.
To move objects between the stack and heap to optimize runtime performance.
To automatically identify and deallocate objects that are no longer reachable, thereby preventing memory leaks.
To pre-allocate memory for all potential objects during program startup.
Which memory area in the JVM is responsible for storing class-level information, such as static variables, runtime constants, and metadata?
Method Area (Metaspace in modern JVMs)
Heap
Stack
Code Cache
What do you think about our class today?
