DSA EXAM 1 QUESTIONS WITH
CORRECT ANSWERS
1. What differentiates an array from anArrayList?
A. Arrays can hold primitives,ArrayLists cannot.
B. Array elements are accessedby index, ArrayList elementsare not.
C. Arrays can hold objects,ArrayLists cannot.
D. Arrays are a set size, whereasArrayLists expand.
E. A & D - Correct Answers -E
2. When we talk about Big O notation and worst case complexity, we often refer to 'n'.
What is n?
A. A generic data type
B. A random number
C. The size (amount)of the data
D. One data item - Correct Answers -C
3. Adding a new item to the end of a 500-element ArrayList requires to programmer to
shift how many elements?
A. 1
B. 0
C. 499
D. 500 - Correct Answers -B
4. Which of the following are valid assertionstatements (in terms of syntax)? Assume
allvariables are of type int.
A. AssertTrue(instance.isEmpty());
B. AssertFalse(expectedResult, instance.size());
C. AssertEquals(result, expectedResult);
D. A & C - Correct Answers -D
5. What is the complexity of the following method?
public void myMethod(int x) {
int sum = 0;
for (int i = 0; i < x; i++) {
System.out.println(i);
, sum+= i;
}
System.out.println(sum);
}
A. O(1)
B. O(n)
C. O(n2)
D. O(2n) - Correct Answers -B
6. You can determine complexity of a method thatoperates on a data structure by
looking at the datastructure's interface (ADT).
A. True
B. False - Correct Answers -B
7. Which of the following operations is not O(1)From the perspective of a programmer
usingarrays?
A. Returning the capacity of the array
B. Searching for a specific element in the array
C. Creating a new array
D. Inserting an element at the beginning of an array without capacity
E. B & D - Correct Answers -E
8. Given the following code, what outputwill the last line produce?
ArrayList<Integer> myNums = new ArrayList<>():
myNums.add(5);
myNums.add(2);
myNums.add(1, 7);
System.out.println(myNums);
A. [5, 7, 2]
B. [5, 2, 1, 7]
C. [5, 2, 1]
D. [2, 7, 5] - Correct Answers -A
9. After executing the following line of code, what best describes myClass?
Student[] myClass = new Student[25];
A. A pointer to an array of 25 Student objects
B. A pointer to an array of 25 nulls
C. A null pointer
D. None of the above - Correct Answers -B
17. How many Employee objects are created by the following?
CORRECT ANSWERS
1. What differentiates an array from anArrayList?
A. Arrays can hold primitives,ArrayLists cannot.
B. Array elements are accessedby index, ArrayList elementsare not.
C. Arrays can hold objects,ArrayLists cannot.
D. Arrays are a set size, whereasArrayLists expand.
E. A & D - Correct Answers -E
2. When we talk about Big O notation and worst case complexity, we often refer to 'n'.
What is n?
A. A generic data type
B. A random number
C. The size (amount)of the data
D. One data item - Correct Answers -C
3. Adding a new item to the end of a 500-element ArrayList requires to programmer to
shift how many elements?
A. 1
B. 0
C. 499
D. 500 - Correct Answers -B
4. Which of the following are valid assertionstatements (in terms of syntax)? Assume
allvariables are of type int.
A. AssertTrue(instance.isEmpty());
B. AssertFalse(expectedResult, instance.size());
C. AssertEquals(result, expectedResult);
D. A & C - Correct Answers -D
5. What is the complexity of the following method?
public void myMethod(int x) {
int sum = 0;
for (int i = 0; i < x; i++) {
System.out.println(i);
, sum+= i;
}
System.out.println(sum);
}
A. O(1)
B. O(n)
C. O(n2)
D. O(2n) - Correct Answers -B
6. You can determine complexity of a method thatoperates on a data structure by
looking at the datastructure's interface (ADT).
A. True
B. False - Correct Answers -B
7. Which of the following operations is not O(1)From the perspective of a programmer
usingarrays?
A. Returning the capacity of the array
B. Searching for a specific element in the array
C. Creating a new array
D. Inserting an element at the beginning of an array without capacity
E. B & D - Correct Answers -E
8. Given the following code, what outputwill the last line produce?
ArrayList<Integer> myNums = new ArrayList<>():
myNums.add(5);
myNums.add(2);
myNums.add(1, 7);
System.out.println(myNums);
A. [5, 7, 2]
B. [5, 2, 1, 7]
C. [5, 2, 1]
D. [2, 7, 5] - Correct Answers -A
9. After executing the following line of code, what best describes myClass?
Student[] myClass = new Student[25];
A. A pointer to an array of 25 Student objects
B. A pointer to an array of 25 nulls
C. A null pointer
D. None of the above - Correct Answers -B
17. How many Employee objects are created by the following?